2017-01-16 13:38:49 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
import static function.builtin.EVAL.lookupSymbol;
|
2017-03-12 12:19:45 -04:00
|
|
|
import static org.junit.Assert.assertNull;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
2017-01-16 13:38:49 -05:00
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import function.ArgumentValidator.*;
|
2017-03-12 12:19:45 -04:00
|
|
|
import function.builtin.BackquoteEvaluator.AtSignNotInCommaException;
|
2017-01-16 13:38:49 -05:00
|
|
|
import function.builtin.EVAL.*;
|
|
|
|
|
|
|
|
public class EVALTester {
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalNumber() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String input = "(eval 9)";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("9"), evaluateString(input));
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalNil() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String input = "(eval ())";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("()"), evaluateString(input));
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lookupKeywordSymbol() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String symbol = ":symbol";
|
2017-03-02 09:54:23 -05:00
|
|
|
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lookupT() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String symbol = "T";
|
2017-03-02 09:54:23 -05:00
|
|
|
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
2017-02-04 13:51:10 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lookupNil() {
|
2017-02-04 13:51:10 -05:00
|
|
|
String symbol = "NIL";
|
2017-03-02 09:54:23 -05:00
|
|
|
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
|
2017-02-04 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
2017-01-16 13:38:49 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void lookupUndefinedSymbol() {
|
2017-01-16 13:38:49 -05:00
|
|
|
assertNull(EVAL.lookupSymbol("undefined"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = UndefinedFunctionException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalUndefinedFunction() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String input = "(funcall 'eval '(undefined))";
|
|
|
|
|
|
|
|
evaluateString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = UndefinedSymbolException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalUndefinedSymbol() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String input = "(eval undefined)";
|
|
|
|
|
|
|
|
evaluateString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalWithDottedLambdaList() {
|
2017-01-16 13:38:49 -05:00
|
|
|
String input = "(funcall 'eval (cons '+ 1))";
|
|
|
|
|
|
|
|
evaluateString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalWithTooManyArguments() {
|
2017-01-16 13:38:49 -05:00
|
|
|
evaluateString("(eval '1 '2 '3)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void evalWithTooFewArguments() {
|
2017-01-16 13:38:49 -05:00
|
|
|
evaluateString("(eval)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-03 12:07:23 -05:00
|
|
|
public void undefinedFunctionException_HasCorrectAttributes() {
|
2017-03-12 12:19:45 -04:00
|
|
|
assertIsErrorWithMessage(new UndefinedFunctionException(NIL));
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-03 12:07:23 -05:00
|
|
|
public void undefinedSymbolException_HasCorrectAttributes() {
|
2017-03-12 12:19:45 -04:00
|
|
|
assertIsErrorWithMessage(new UndefinedSymbolException(NIL));
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
@Test(expected = UnmatchedCommaException.class)
|
|
|
|
public void evalComma() {
|
|
|
|
String input = ",a";
|
|
|
|
|
|
|
|
evaluateString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = UnmatchedAtSignException.class)
|
|
|
|
public void evalAtSign() {
|
|
|
|
String input = "@a";
|
|
|
|
|
|
|
|
evaluateString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evalBackTick() {
|
|
|
|
String input = "`(a b c)";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("(a b c)"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evalBackTickWithCommasAndAtSigns() {
|
|
|
|
String input = "(let ((x '(1 2 3)) (y '(4 5 6)) (z 'apple)) `(start ,x ,@y ,z end))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("(start (1 2 3) 4 5 6 apple end)"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evalBackTickOnComma() {
|
|
|
|
String input = "`,9";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("9"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = AtSignNotInCommaException.class)
|
|
|
|
public void evalBackTickOnAtSign() {
|
2017-03-12 12:19:45 -04:00
|
|
|
evaluateString("`@9");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evalNestedBackquotes() {
|
|
|
|
String input = "`,`,`,`,9";
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("9"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
@Test
|
|
|
|
public void unmatchedCommaException_HasCorrectAttributes() {
|
|
|
|
assertIsErrorWithMessage(new UnmatchedCommaException());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void unmatchedAtSignException_HasCorrectAttributes() {
|
|
|
|
assertIsErrorWithMessage(new UnmatchedAtSignException());
|
|
|
|
}
|
|
|
|
|
2017-03-12 14:18:37 -04:00
|
|
|
@Test
|
|
|
|
public void evalQuoteInNestedList() {
|
|
|
|
String input = "(let ((g 27)) `((,g)))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("((27))"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evalAtSignInNestedList() {
|
|
|
|
String input = "(let ((g '(1 2 3))) `((,@g)))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("((1 2 3))"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|