package sexpression; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import error.ErrorManager; import sexpression.LispNumber.InvalidNumberException; public class SExpressionTester { private void assertSExpressionMatchesString(String expected, SExpression sExpression) { assertEquals(expected, sExpression.toString()); } @Before public void setUp() throws Exception {} @Test public void testNilToString() { String input = "NIL"; assertSExpressionMatchesString(input, Nil.getUniqueInstance()); } @Test public void testNumberToString() { String input = "12"; assertSExpressionMatchesString(input, new LispNumber(input)); } @Test public void testNumberValueToString() { String expected = "12"; assertSExpressionMatchesString(expected, new LispNumber(12)); } @Test public void testStringToString() { String input = "\"hi\""; assertSExpressionMatchesString(input, new LispString(input)); } @Test public void testSymbolToString() { String input = "symbol"; assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input)); } @Test public void testSimpleConsToString() { String expected = "(1)"; Cons cons = new Cons(new LispNumber(1), Nil.getUniqueInstance()); assertSExpressionMatchesString(expected, cons); } @Test public void testComplexConsToString() { String expected = "(1 A \"string\")"; Cons list = new Cons(new LispNumber(1), new Cons(new Symbol("a"), new Cons(new LispString("\"string\""), Nil.getUniqueInstance()))); assertSExpressionMatchesString(expected, list); } @Test public void testConsWithNonListCdrToString() { String expected = "(A . B)"; Cons list = new Cons(new Symbol("A"), new Symbol("B")); assertSExpressionMatchesString(expected, list); } @Test public void testCarOfNilIsNil() { assertEquals(Nil.getUniqueInstance().getCar(), Nil.getUniqueInstance()); } @Test public void testCdrOfNilIsNil() { assertEquals(Nil.getUniqueInstance().getCdr(), Nil.getUniqueInstance()); } @Test public void afterSettingCarOfNil_ShouldStillBeNil() { Cons nil = Nil.getUniqueInstance(); nil.setCar(new LispNumber(2)); assertEquals(nil.getCar(), Nil.getUniqueInstance()); } @Test public void afterSettingCdrOfNil_ShouldStillBeNil() { Cons nil = Nil.getUniqueInstance(); nil.setCdr(new LispNumber(2)); assertEquals(nil.getCdr(), Nil.getUniqueInstance()); } @Test public void testNumberValue() { int value = 12; LispNumber number = new LispNumber(String.valueOf(value)); assertEquals(number.getValue(), value); } @Test(expected = InvalidNumberException.class) public void testInvalidNumberText_ThrowsException() { new LispNumber("a"); } @Test public void testInvalidNumberException_HasCorrectSeverity() { try { new LispNumber("a"); } catch (InvalidNumberException e) { assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL); } } @Test public void testInvalidNumberException_HasMessageText() { try { new LispNumber("a"); } catch (InvalidNumberException e) { String message = e.getMessage(); assertNotNull(message); assertTrue(message.length() > 0); } } }