Mercurial > hg > graal-jvmci-8
annotate graal/com.oracle.truffle.ruby.test/src/com/oracle/truffle/ruby/test/RubyTests.java @ 14094:3f27e57439ed
Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Mon, 03 Feb 2014 20:58:23 -0800 |
parents | 497fada09efb |
children | cf16ff2dba69 |
rev | line source |
---|---|
13514 | 1 /* |
14094
3f27e57439ed
Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. This |
13514 | 3 * code is released under a tri EPL/GPL/LGPL license. You can use it, |
4 * redistribute it and/or modify it under the terms of the: | |
5 * | |
6 * Eclipse Public License version 1.0 | |
7 * GNU General Public License version 2 | |
8 * GNU Lesser General Public License version 2.1 | |
9 */ | |
10 package com.oracle.truffle.ruby.test; | |
11 | |
12 import static org.junit.Assert.*; | |
13 | |
14 import java.io.*; | |
15 import java.util.*; | |
16 | |
17 import org.junit.*; | |
18 | |
19 import com.oracle.truffle.api.*; | |
20 import com.oracle.truffle.ruby.nodes.core.*; | |
21 import com.oracle.truffle.ruby.parser.*; | |
22 import com.oracle.truffle.ruby.runtime.*; | |
23 import com.oracle.truffle.ruby.runtime.configuration.*; | |
24 import com.oracle.truffle.ruby.runtime.core.*; | |
25 | |
26 /** | |
27 * Base class for Ruby tests. | |
28 */ | |
29 public class RubyTests { | |
30 | |
31 @BeforeClass | |
32 public static void applyDefaultLocale() { | |
33 // Avoid printing comparison issues | |
34 Locale.setDefault(Locale.ENGLISH); | |
35 } | |
36 | |
37 /** | |
38 * Executes some Ruby code and asserts that it prints an expected string. Remember to include | |
39 * the newline characters. | |
40 */ | |
41 public static void assertPrints(String expectedOutput, String code, String... args) { | |
42 assertPrintsWithInput(expectedOutput, code, "", args); | |
43 } | |
44 | |
45 /** | |
46 * Executes some Ruby code and asserts that it prints an expected string. Remember to include | |
47 * the newline characters. Allows input for {@code Kernel#gets} to be passed in. | |
48 */ | |
49 public static void assertPrintsWithInput(String expectedOutput, String code, String input, String... args) { | |
50 assertPrints(null, expectedOutput, "(test)", code, input, args); | |
51 } | |
52 | |
53 /** | |
54 * Executes some Ruby code and asserts that it prints an expected string. Remember to include | |
55 * the newline characters. Also takes a string to simulate input. | |
56 */ | |
57 public static void assertPrints(Configuration configuration, String expectedOutput, String fileName, String code, String input, String... args) { | |
58 final ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); | |
59 final PrintStream printStream = new PrintStream(byteArray); | |
60 | |
61 ConfigurationBuilder configurationBuilder; | |
62 | |
63 if (configuration == null) { | |
64 configurationBuilder = new ConfigurationBuilder(); | |
65 } else { | |
66 configurationBuilder = new ConfigurationBuilder(configuration); | |
67 } | |
68 | |
69 configurationBuilder.setStandardOut(printStream); | |
70 | |
71 final BufferedReader inputReader = new BufferedReader(new StringReader(input)); | |
72 | |
73 configurationBuilder.setInputReader(new InputReader() { | |
74 | |
75 @Override | |
76 public String readLine(String prompt) throws IOException { | |
77 return inputReader.readLine(); | |
78 } | |
79 | |
80 }); | |
81 | |
82 final RubyContext context = new RubyContext(new Configuration(configurationBuilder), new JRubyParser()); | |
14094
3f27e57439ed
Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
83 context.setDebugContext(new MinimumRubyDebugContext(context)); |
13514 | 84 |
85 CoreMethodNodeManager.addMethods(context.getCoreLibrary().getObjectClass()); | |
86 context.getCoreLibrary().initializeAfterMethodsAdded(); | |
87 | |
88 for (String arg : args) { | |
89 context.getCoreLibrary().getArgv().push(new RubyString(context.getCoreLibrary().getStringClass(), arg)); | |
90 } | |
91 | |
92 final Source source = context.getSourceManager().getFakeFile(fileName, code); | |
93 | |
94 context.execute(context, source, RubyParser.ParserContext.TOP_LEVEL, context.getCoreLibrary().getMainObject(), null); | |
95 context.shutdown(); | |
96 | |
97 assertEquals(expectedOutput, byteArray.toString().replaceAll("\r\n", "\n")); | |
98 } | |
99 } |