167 lines
4.3 KiB
Java
167 lines
4.3 KiB
Java
package sexpression;
|
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
|
import static org.junit.Assert.*;
|
|
import static sexpression.Nil.NIL;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import org.junit.*;
|
|
|
|
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);
|
|
}
|
|
|
|
@Test
|
|
public void lambdaExpressionToString() {
|
|
String expected = "(LAMBDA)";
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
|
|
|
|
assertSExpressionMatchesString(expected, lambda);
|
|
}
|
|
|
|
@Test
|
|
public void lambdaExpressionGetLambdaExpression() {
|
|
String expected = "(LAMBDA)";
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
|
|
|
|
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
|
|
}
|
|
|
|
@Test
|
|
public void lambdaExpressionGetFunction() {
|
|
String expected = "(LAMBDA)";
|
|
UserDefinedFunction function = new UserDefinedFunction(expected, NIL, NIL);
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), function);
|
|
|
|
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());
|
|
}
|
|
|
|
}
|