2016-12-15 15:33:48 -05:00
|
|
|
package sexpression;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static testutil.TestUtilities.assertSExpressionsMatch;
|
|
|
|
import static testutil.TestUtilities.makeList;
|
2016-12-15 15:33:48 -05:00
|
|
|
|
2016-12-25 13:56:24 -05:00
|
|
|
import java.math.BigInteger;
|
|
|
|
|
2017-03-24 09:36:44 -04:00
|
|
|
import org.junit.Test;
|
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;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class SExpressionTest {
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
private void assertSExpressionMatchesString(String expected, SExpression sExpression) {
|
|
|
|
assertEquals(expected, sExpression.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void nil_ToString() {
|
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-11 15:41:07 -05:00
|
|
|
public void number_ToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "12";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input, new LispNumber(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void numberValue_ToString() {
|
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-11 15:41:07 -05:00
|
|
|
public void string_ToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "\"hi\"";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input, new LispString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void symbol_ToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String input = "symbol";
|
|
|
|
|
|
|
|
assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void simpleCons_ToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
String expected = "(1)";
|
2017-03-11 15:41:07 -05:00
|
|
|
Cons cons = makeList(new LispNumber("1"));
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, cons);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void complexCons_ToString() {
|
2016-12-15 15:33:48 -05:00
|
|
|
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\""));
|
2016-12-15 15:33:48 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, list);
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void improperList_ToString() {
|
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-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)";
|
2017-03-02 09:54:23 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
@Test
|
|
|
|
public void backTickExpression_ToString() {
|
|
|
|
String expected = "`(TEST)";
|
2017-03-12 12:19:45 -04:00
|
|
|
SExpression backTick = new BackquoteExpression(makeList(new Symbol("TEST")));
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
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)";
|
2017-03-12 12:19:45 -04:00
|
|
|
SExpression backTick = new BackquoteExpression(makeList(new Symbol("LIST"),
|
|
|
|
new CommaExpression(new Symbol("A")),
|
|
|
|
new CommaExpression(new AtSignExpression(new Symbol("B")))));
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionMatchesString(expected, backTick);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void backTickExpression_GetExpression() {
|
|
|
|
SExpression expression = makeList(new Symbol("TEST"));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteExpression backTick = new BackquoteExpression(expression);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:33:48 -05:00
|
|
|
}
|