package function.builtin; import static org.junit.Assert.assertEquals; import static testutil.TestUtilities.evaluateString; import java.io.*; import java.text.MessageFormat; import org.junit.*; import environment.RuntimeEnvironment; import function.ArgumentValidator.*; public class PRINTTester { private ByteArrayOutputStream outputStream; private void assertPrinted(String expected) { assertEquals(expected, outputStream.toString()); } @Before public void setUp() { outputStream = new ByteArrayOutputStream(); RuntimeEnvironment.getInstance().setOutput(new PrintStream(outputStream)); } @Test public void printStringWorks() { String output = "\"Hello, world!\""; evaluateString(MessageFormat.format("(print {0})", output)); assertPrinted(MessageFormat.format("{0}\n", output)); } @Test public void printSymbolWorks() { String output = "A"; evaluateString(MessageFormat.format("(print ''{0})", output)); assertPrinted(MessageFormat.format("{0}\n", output)); } @Test(expected = TooManyArgumentsException.class) public void testPrintWithTooManyArguments() { evaluateString("(print '1 '2)"); } @Test(expected = TooFewArgumentsException.class) public void testPrintWithTooFewArguments() { evaluateString("(print)"); } }