2016-12-15 15:33:48 -05:00
|
|
|
package sexpression;
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
import static org.junit.Assert.*;
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
import error.ErrorManager;
|
|
|
|
import sexpression.LispNumber.InvalidNumberException;
|
|
|
|
|
2016-12-15 15:33:48 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|