13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
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());
|
|
83
|
|
84 CoreMethodNodeManager.addMethods(context.getCoreLibrary().getObjectClass());
|
|
85 context.getCoreLibrary().initializeAfterMethodsAdded();
|
|
86
|
|
87 for (String arg : args) {
|
|
88 context.getCoreLibrary().getArgv().push(new RubyString(context.getCoreLibrary().getStringClass(), arg));
|
|
89 }
|
|
90
|
|
91 final Source source = context.getSourceManager().getFakeFile(fileName, code);
|
|
92
|
|
93 context.execute(context, source, RubyParser.ParserContext.TOP_LEVEL, context.getCoreLibrary().getMainObject(), null);
|
|
94 context.shutdown();
|
|
95
|
|
96 assertEquals(expectedOutput, byteArray.toString().replaceAll("\r\n", "\n"));
|
|
97 }
|
|
98 }
|