Skip to content

Commit 29e3d99

Browse files
committed
allow unit tests to specify code as Source objects, add test for multiple executions of the same source object
1 parent 9f5f697 commit 29e3d99

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,15 @@ public static void assertPrints(String expected, Path scriptName) {
192192
public static void assertPrints(String expected, String code) {
193193
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
194194
final PrintStream printStream = new PrintStream(byteArray);
195-
String source = code;
196-
PythonTests.runScript(new String[0], source, printStream, System.err);
195+
PythonTests.runScript(new String[0], code, printStream, System.err);
196+
String result = byteArray.toString().replaceAll("\r\n", "\n");
197+
assertEquals(expected.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"), result.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"));
198+
}
199+
200+
public static void assertPrints(String expected, org.graalvm.polyglot.Source code) {
201+
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
202+
final PrintStream printStream = new PrintStream(byteArray);
203+
PythonTests.runScript(new String[0], code, printStream, System.err);
197204
String result = byteArray.toString().replaceAll("\r\n", "\n");
198205
assertEquals(expected.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"), result.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"));
199206
}
@@ -311,6 +318,15 @@ public static void runScript(String[] args, String source, OutputStream out, Out
311318
}
312319
}
313320

321+
public static void runScript(String[] args, org.graalvm.polyglot.Source source, OutputStream out, OutputStream err) {
322+
try {
323+
enterContext(args);
324+
context.eval(source);
325+
} finally {
326+
flush(out, err);
327+
}
328+
}
329+
314330
public static void runScript(String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
315331
try {
316332
enterContext(args);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/basic/HelloWorldTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ public class HelloWorldTests {
3434
public void helloworld() {
3535
assertPrints("hello world\n", "print(\"hello world\")");
3636
}
37+
38+
@Test
39+
public void helloworldAgain() {
40+
org.graalvm.polyglot.Source source = org.graalvm.polyglot.Source.create("python", "try: print(value)\nexcept:print('hello')\nvalue='world'");
41+
assertPrints("hello\n", source);
42+
assertPrints("hello\n", source);
43+
}
3744
}

0 commit comments

Comments
 (0)