transcendental-lisp/test/sexpression/SExpressionTester.java

175 lines
4.5 KiB
Java
Raw Normal View History

package sexpression;
2017-02-11 10:42:07 -05:00
import static error.ErrorManager.Severity.ERROR;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import java.math.BigInteger;
2016-12-19 13:29:31 -05:00
import org.junit.*;
2016-12-19 13:29:31 -05:00
import function.UserDefinedFunction;
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 nilToString() {
String input = "NIL";
assertSExpressionMatchesString(input, NIL);
}
@Test
public void numberToString() {
String input = "12";
assertSExpressionMatchesString(input, new LispNumber(input));
}
@Test
public void numberValueToString() {
String expected = "12";
assertSExpressionMatchesString(expected, new LispNumber("12"));
}
@Test
public void stringToString() {
String input = "\"hi\"";
assertSExpressionMatchesString(input, new LispString(input));
}
@Test
public void symbolToString() {
String input = "symbol";
assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input));
}
@Test
public void simpleConsToString() {
String expected = "(1)";
Cons cons = new Cons(new LispNumber("1"), NIL);
assertSExpressionMatchesString(expected, cons);
}
@Test
public void complexConsToString() {
String expected = "(1 A \"string\")";
Cons list = new Cons(new LispNumber("1"),
new Cons(new Symbol("a"), new Cons(new LispString("\"string\""), NIL)));
assertSExpressionMatchesString(expected, list);
}
@Test
public void improperListToString() {
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 lambdaExpressionToString() {
2016-12-19 13:29:31 -05:00
String expected = "(LAMBDA)";
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
2016-12-19 13:29:31 -05:00
assertSExpressionMatchesString(expected, lambda);
}
@Test
public void lambdaExpressionGetLambdaExpression() {
2016-12-19 13:29:31 -05:00
String expected = "(LAMBDA)";
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
2016-12-19 13:29:31 -05:00
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
}
@Test
public void lambdaExpressionGetFunction() {
2016-12-19 13:29:31 -05:00
String expected = "(LAMBDA)";
UserDefinedFunction function = new UserDefinedFunction(expected, NIL, NIL);
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), function);
2016-12-19 13:29:31 -05:00
assertEquals(function, lambda.getFunction());
}
@Test
public void firstOfNilIsNil() {
assertEquals(NIL, NIL.getFirst());
}
@Test
public void restOfNilIsNil() {
assertEquals(NIL, NIL.getRest());
}
@Test
public void afterSettingFirstOfNil_ShouldStillBeNil() {
Cons nil = NIL;
nil.setFirst(new LispNumber("2"));
assertEquals(NIL, nil.getFirst());
}
@Test
public void afterSettingRestOfNil_ShouldStillBeNil() {
Cons nil = NIL;
nil.setRest(new LispNumber("2"));
assertEquals(NIL, nil.getRest());
}
@Test
public void numberValue() {
BigInteger value = new BigInteger("12");
LispNumber number = new LispNumber(value.toString());
assertEquals(value, number.getValue());
}
@Test(expected = InvalidNumberException.class)
public void invalidNumberText_ThrowsException() {
new LispNumber("a");
}
@Test
public void invalidNumberException_HasCorrectSeverity() {
try {
new LispNumber("a");
} catch (InvalidNumberException e) {
2017-02-11 10:42:07 -05:00
assertEquals(ERROR, e.getSeverity());
}
}
@Test
public void invalidNumberException_HasMessageText() {
try {
new LispNumber("a");
} catch (InvalidNumberException e) {
String message = e.getMessage();
assertNotNull(message);
assertTrue(message.length() > 0);
}
}
@Test
public void lispNumberConstants() {
assertEquals(BigInteger.ZERO, LispNumber.ZERO.getValue());
assertEquals(BigInteger.ONE, LispNumber.ONE.getValue());
}
}