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

66 lines
1.5 KiB
Java

package function.builtin;
import static java.text.MessageFormat.format;
import static org.junit.Assert.assertEquals;
import static testutil.TestUtilities.evaluateString;
import java.io.*;
import org.junit.*;
import environment.RuntimeEnvironment;
import function.ArgumentValidator.*;
public class PRINTTest {
private RuntimeEnvironment environment;
private ByteArrayOutputStream outputStream;
public PRINTTest() {
this.environment = RuntimeEnvironment.getInstance();
}
private void assertPrinted(String expected) {
assertEquals(expected, outputStream.toString());
}
@Before
public void setUp() {
outputStream = new ByteArrayOutputStream();
environment.reset();
environment.setOutput(new PrintStream(outputStream));
}
@After
public void tearDown() {
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)");
}
}