transcendental-lisp/test/sexpression/SExpressionTester.java

224 lines
6.1 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;
2017-03-11 15:41:07 -05:00
import static testutil.TestUtilities.*;
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
2017-03-11 15:41:07 -05:00
public void nil_ToString() {
String input = "NIL";
assertSExpressionMatchesString(input, NIL);
}
@Test
2017-03-11 15:41:07 -05:00
public void number_ToString() {
String input = "12";
assertSExpressionMatchesString(input, new LispNumber(input));
}
@Test
2017-03-11 15:41:07 -05:00
public void numberValue_ToString() {
String expected = "12";
assertSExpressionMatchesString(expected, new LispNumber("12"));
}
@Test
2017-03-11 15:41:07 -05:00
public void string_ToString() {
String input = "\"hi\"";
assertSExpressionMatchesString(input, new LispString(input));
}
@Test
2017-03-11 15:41:07 -05:00
public void symbol_ToString() {
String input = "symbol";
assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input));
}
@Test
2017-03-11 15:41:07 -05:00
public void simpleCons_ToString() {
String expected = "(1)";
2017-03-11 15:41:07 -05:00
Cons cons = makeList(new LispNumber("1"));
assertSExpressionMatchesString(expected, cons);
}
@Test
2017-03-11 15:41:07 -05:00
public void complexCons_ToString() {
String expected = "(1 A \"string\")";
2017-03-11 15:41:07 -05:00
Cons list = makeList(new LispNumber("1"), new Symbol("a"), new LispString("\"string\""));
assertSExpressionMatchesString(expected, list);
}
@Test
2017-03-11 15:41:07 -05:00
public void improperList_ToString() {
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
2017-03-11 15:41:07 -05:00
public void lambdaExpression_ToString() {
2016-12-19 13:29:31 -05:00
String expected = "(LAMBDA)";
2017-03-11 15:41:07 -05:00
LambdaExpression lambda = new LambdaExpression(makeList(new Symbol("lambda")), null);
2016-12-19 13:29:31 -05:00
assertSExpressionMatchesString(expected, lambda);
}
@Test
2017-03-11 15:41:07 -05:00
public void lambdaExpression_GetLambdaExpression() {
2016-12-19 13:29:31 -05:00
String expected = "(LAMBDA)";
2017-03-11 15:41:07 -05:00
LambdaExpression lambda = new LambdaExpression(makeList(new Symbol("lambda")), null);
2016-12-19 13:29:31 -05:00
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
}
@Test
2017-03-11 15:41:07 -05:00
public void lambdaExpression_GetFunction() {
2016-12-19 13:29:31 -05:00
String expected = "(LAMBDA)";
UserDefinedFunction function = new UserDefinedFunction(expected, NIL, NIL);
2017-03-11 15:41:07 -05:00
LambdaExpression lambda = new LambdaExpression(makeList(new Symbol("lambda")), 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_HasCorrectAttributes() {
try {
new LispNumber("a");
} catch (InvalidNumberException e) {
String message = e.getMessage();
assertEquals(ERROR, e.getSeverity());
assertNotNull(message);
assertTrue(message.length() > 0);
}
}
@Test
public void lispNumberConstants() {
assertEquals(BigInteger.ZERO, LispNumber.ZERO.getValue());
assertEquals(BigInteger.ONE, LispNumber.ONE.getValue());
}
2017-03-11 15:41:07 -05:00
@Test
public void backTickExpression_ToString() {
String expected = "`(TEST)";
SExpression backTick = new BackTickExpression(makeList(new Symbol("TEST")));
assertSExpressionMatchesString(expected, backTick);
}
@Test
public void commaExpression_ToString() {
String expected = ",A";
SExpression comma = new CommaExpression(new Symbol("A"));
assertSExpressionMatchesString(expected, comma);
}
@Test
public void atSignExpression_ToString() {
String expected = "@A";
SExpression atSign = new AtSignExpression(new Symbol("A"));
assertSExpressionMatchesString(expected, atSign);
}
@Test
public void complexBackTickExpression_ToString() {
String expected = "`(LIST ,A ,@B)";
SExpression backTick = new BackTickExpression(makeList(new Symbol("LIST"), new CommaExpression(new Symbol("A")),
new CommaExpression(new AtSignExpression(new Symbol("B")))));
assertSExpressionMatchesString(expected, backTick);
}
@Test
public void backTickExpression_GetExpression() {
SExpression expression = makeList(new Symbol("TEST"));
BackTickExpression backTick = new BackTickExpression(expression);
assertSExpressionsMatch(expression, backTick.getExpression());
}
@Test
public void commaExpression_GetExpression() {
SExpression expression = new Symbol("A");
CommaExpression comma = new CommaExpression(expression);
assertSExpressionsMatch(expression, comma.getExpression());
}
@Test
public void atSignExpression_GetExpression() {
SExpression expression = new Symbol("A");
AtSignExpression atSign = new AtSignExpression(expression);
assertSExpressionsMatch(expression, atSign.getExpression());
}
}