2017-01-18 16:25:09 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
2017-03-23 12:14:44 -04:00
|
|
|
import static java.text.MessageFormat.format;
|
2017-01-18 16:25:09 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static testutil.TestUtilities.evaluateString;
|
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.PrintStream;
|
2017-01-18 16:25:09 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
import org.junit.Test;
|
2017-01-18 16:25:09 -05:00
|
|
|
|
2017-02-06 13:39:05 -05:00
|
|
|
import environment.RuntimeEnvironment;
|
2017-11-12 09:42:25 -05:00
|
|
|
import function.ArgumentValidator.TooFewArgumentsException;
|
|
|
|
import function.ArgumentValidator.TooManyArgumentsException;
|
2017-07-19 15:23:15 -04:00
|
|
|
import testutil.SymbolAndFunctionCleaner;
|
2017-01-18 16:25:09 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
public class PRINTTest extends SymbolAndFunctionCleaner {
|
2017-01-18 16:25:09 -05:00
|
|
|
|
2017-03-05 10:20:31 -05:00
|
|
|
private RuntimeEnvironment environment;
|
2017-01-18 16:25:09 -05:00
|
|
|
private ByteArrayOutputStream outputStream;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public PRINTTest() {
|
2017-03-05 10:20:31 -05:00
|
|
|
this.environment = RuntimeEnvironment.getInstance();
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:25:09 -05:00
|
|
|
private void assertPrinted(String expected) {
|
|
|
|
assertEquals(expected, outputStream.toString());
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
@Override
|
|
|
|
public void additionalSetUp() {
|
2017-02-11 10:51:37 -05:00
|
|
|
outputStream = new ByteArrayOutputStream();
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.reset();
|
|
|
|
environment.setOutput(new PrintStream(outputStream));
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
@Override
|
|
|
|
public void additionalTearDown() {
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.reset();
|
2017-01-18 16:25:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void printStringWorks() {
|
|
|
|
String output = "\"Hello, world!\"";
|
|
|
|
|
2017-03-23 12:14:44 -04:00
|
|
|
evaluateString(format("(print {0})", output));
|
|
|
|
assertPrinted(format("{0}\n", output));
|
2017-01-18 16:25:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void printSymbolWorks() {
|
|
|
|
String output = "A";
|
|
|
|
|
2017-03-23 12:14:44 -04:00
|
|
|
evaluateString(format("(print ''{0})", output));
|
|
|
|
assertPrinted(format("{0}\n", output));
|
2017-01-18 16:25:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void printWithTooManyArguments() {
|
2017-01-18 16:25:09 -05:00
|
|
|
evaluateString("(print '1 '2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void printWithTooFewArguments() {
|
2017-01-18 16:25:09 -05:00
|
|
|
evaluateString("(print)");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|