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.*;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void nilToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "NIL";
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertSExpressionMatchesString(input, NIL);
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void numberToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "12";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input, new LispNumber(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void numberValueToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void stringToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "\"hi\"";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input, new LispString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void symbolToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "symbol";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void simpleConsToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String expected = "(1)";
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons cons = new Cons(new LispNumber("1"), NIL);
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, cons);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void complexConsToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String expected = "(1 A \"string\")";
|
2016-12-25 13:56:24 -05:00
|
|
|
Cons list = new Cons(new LispNumber("1"),
|
2017-03-02 09:54:23 -05:00
|
|
|
new Cons(new Symbol("a"), new Cons(new LispString("\"string\""), NIL)));
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, list);
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void improperListToString() {
|
2016-12-16 11:26:53 -05:00
|
|
|
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-02 09:54:23 -05:00
|
|
|
public void lambdaExpressionToString() {
|
2016-12-19 13:29:31 -05:00
|
|
|
String expected = "(LAMBDA)";
|
2017-03-02 09:54:23 -05:00
|
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
|
2016-12-19 13:29:31 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, lambda);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lambdaExpressionGetLambdaExpression() {
|
2016-12-19 13:29:31 -05:00
|
|
|
String expected = "(LAMBDA)";
|
2017-03-02 09:54:23 -05:00
|
|
|
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
|
2016-12-19 13:29:31 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lambdaExpressionGetFunction() {
|
2016-12-19 13:29:31 -05:00
|
|
|
String expected = "(LAMBDA)";
|
2017-03-02 09:54:23 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void firstOfNilIsNil() {
|
|
|
|
assertEquals(NIL, NIL.getFirst());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void restOfNilIsNil() {
|
|
|
|
assertEquals(NIL, NIL.getRest());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-24 16:00:05 -05:00
|
|
|
public void afterSettingFirstOfNil_ShouldStillBeNil() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons nil = NIL;
|
2017-02-24 11:07:06 -05:00
|
|
|
nil.setFirst(new LispNumber("2"));
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(NIL, nil.getFirst());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-24 16:00:05 -05:00
|
|
|
public void afterSettingRestOfNil_ShouldStillBeNil() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons nil = NIL;
|
2017-02-24 11:07:06 -05:00
|
|
|
nil.setRest(new LispNumber("2"));
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(NIL, nil.getRest());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void numberValue() {
|
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)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void invalidNumberText_ThrowsException() {
|
2016-12-16 11:26:53 -05:00
|
|
|
new LispNumber("a");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-03 12:07:23 -05:00
|
|
|
public void invalidNumberException_HasCorrectAttributes() {
|
2016-12-16 11:26:53 -05:00
|
|
|
try {
|
|
|
|
new LispNumber("a");
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
|
2017-03-03 12:07:23 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-16 11:26:53 -05:00
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:05:33 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lispNumberConstants() {
|
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
|
|
|
}
|