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
|
|
|
|
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-16 11:26:53 -05:00
|
|
|
import error.ErrorManager;
|
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() {
|
2016-12-29 13:32:45 -05:00
|
|
|
assertEquals(Nil.getInstance().getCar(), Nil.getInstance());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCdrOfNilIsNil() {
|
2016-12-29 13:32:45 -05:00
|
|
|
assertEquals(Nil.getInstance().getCdr(), Nil.getInstance());
|
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
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
assertEquals(nil.getCar(), Nil.getInstance());
|
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
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
assertEquals(nil.getCdr(), Nil.getInstance());
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:05:33 -05:00
|
|
|
@Test
|
|
|
|
public void testLispNumberConstants() {
|
|
|
|
assertEquals(LispNumber.ONE.getValue(), new BigInteger("1"));
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|