2016-12-15 15:33:48 -05:00
|
|
|
package sexpression;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
2016-12-16 11:26:53 -05:00
|
|
|
import static org.junit.Assert.*;
|
2016-12-15 15:33:48 -05:00
|
|
|
|
2016-12-25 13:56:24 -05:00
|
|
|
import java.math.BigInteger;
|
|
|
|
|
2016-12-19 13:29:31 -05:00
|
|
|
import org.junit.*;
|
2016-12-15 15:33:48 -05:00
|
|
|
|
2016-12-19 13:29:31 -05:00
|
|
|
import function.UserDefinedFunction;
|
2016-12-16 11:26:53 -05:00
|
|
|
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";
|
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
assertSExpressionMatchesString(input, Nil.getInstance());
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNumberToString() {
|
|
|
|
String input = "12";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input, new LispNumber(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNumberValueToString() {
|
|
|
|
String expected = "12";
|
|
|
|
|
2016-12-25 13:56:24 -05:00
|
|
|
assertSExpressionMatchesString(expected, new LispNumber("12"));
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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)";
|
2016-12-29 13:32:45 -05:00
|
|
|
Cons cons = new Cons(new LispNumber("1"), Nil.getInstance());
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, cons);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testComplexConsToString() {
|
|
|
|
String expected = "(1 A \"string\")";
|
2016-12-25 13:56:24 -05:00
|
|
|
Cons list = new Cons(new LispNumber("1"),
|
2017-01-13 14:05:33 -05:00
|
|
|
new Cons(new Symbol("a"), new Cons(new LispString("\"string\""), Nil.getInstance())));
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-19 13:29:31 -05:00
|
|
|
@Test
|
|
|
|
public void testLambdaExpressionToString() {
|
|
|
|
String expected = "(LAMBDA)";
|
2016-12-29 13:32:45 -05:00
|
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getInstance()), null);
|
2016-12-19 13:29:31 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, lambda);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLambdaExpressionGetLambdaExpression() {
|
|
|
|
String expected = "(LAMBDA)";
|
2016-12-29 13:32:45 -05:00
|
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getInstance()), null);
|
2016-12-19 13:29:31 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLambdaExpressionGetFunction() {
|
|
|
|
String expected = "(LAMBDA)";
|
2017-01-13 14:05:33 -05:00
|
|
|
UserDefinedFunction function = new UserDefinedFunction(expected, Nil.getInstance(), Nil.getInstance());
|
|
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getInstance()), function);
|
2016-12-19 13:29:31 -05:00
|
|
|
|
|
|
|
assertEquals(function, lambda.getFunction());
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
|
|
|
public void testCarOfNilIsNil() {
|
2017-01-18 16:25:09 -05:00
|
|
|
assertEquals(Nil.getInstance(), Nil.getInstance().getCar());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCdrOfNilIsNil() {
|
2017-01-18 16:25:09 -05:00
|
|
|
assertEquals(Nil.getInstance(), Nil.getInstance().getCdr());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void afterSettingCarOfNil_ShouldStillBeNil() {
|
2016-12-29 13:32:45 -05:00
|
|
|
Cons nil = Nil.getInstance();
|
2016-12-25 13:56:24 -05:00
|
|
|
nil.setCar(new LispNumber("2"));
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2017-01-18 16:25:09 -05:00
|
|
|
assertEquals(Nil.getInstance(), nil.getCar());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void afterSettingCdrOfNil_ShouldStillBeNil() {
|
2016-12-29 13:32:45 -05:00
|
|
|
Cons nil = Nil.getInstance();
|
2016-12-25 13:56:24 -05:00
|
|
|
nil.setCdr(new LispNumber("2"));
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2017-01-18 16:25:09 -05:00
|
|
|
assertEquals(Nil.getInstance(), nil.getCdr());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNumberValue() {
|
2016-12-25 13:56:24 -05:00
|
|
|
BigInteger value = new BigInteger("12");
|
|
|
|
LispNumber number = new LispNumber(value.toString());
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2017-01-18 16:25:09 -05:00
|
|
|
assertEquals(value, number.getValue());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = InvalidNumberException.class)
|
|
|
|
public void testInvalidNumberText_ThrowsException() {
|
|
|
|
new LispNumber("a");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testInvalidNumberException_HasCorrectSeverity() {
|
|
|
|
try {
|
|
|
|
new LispNumber("a");
|
|
|
|
} catch (InvalidNumberException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testInvalidNumberException_HasMessageText() {
|
|
|
|
try {
|
|
|
|
new LispNumber("a");
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:05:33 -05:00
|
|
|
@Test
|
|
|
|
public void testLispNumberConstants() {
|
2017-01-18 16:25:09 -05:00
|
|
|
assertEquals(BigInteger.ZERO, LispNumber.ZERO.getValue());
|
|
|
|
assertEquals(BigInteger.ONE, LispNumber.ONE.getValue());
|
2017-01-13 14:05:33 -05:00
|
|
|
}
|
|
|
|
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|