transcendental-lisp/test/function/builtin/PRINTTest.java

69 lines
1.8 KiB
Java
Raw Normal View History

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