146 lines
3.8 KiB
Java
146 lines
3.8 KiB
Java
package function.builtin;
|
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
|
import static function.builtin.EVAL.lookupSymbol;
|
|
import static org.junit.Assert.*;
|
|
import static sexpression.Nil.NIL;
|
|
import static testutil.TestUtilities.*;
|
|
|
|
import org.junit.Test;
|
|
|
|
import function.ArgumentValidator.*;
|
|
import function.builtin.BackTickEvaluator.AtSignNotInCommaException;
|
|
import function.builtin.EVAL.*;
|
|
|
|
public class EVALTester {
|
|
|
|
@Test
|
|
public void evalNumber() {
|
|
String input = "(eval 9)";
|
|
|
|
assertSExpressionsMatch(parseString("9"), evaluateString(input));
|
|
}
|
|
|
|
@Test
|
|
public void evalNil() {
|
|
String input = "(eval ())";
|
|
|
|
assertSExpressionsMatch(parseString("()"), evaluateString(input));
|
|
}
|
|
|
|
@Test
|
|
public void lookupKeywordSymbol() {
|
|
String symbol = ":symbol";
|
|
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
|
|
}
|
|
|
|
@Test
|
|
public void lookupT() {
|
|
String symbol = "T";
|
|
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
|
|
}
|
|
|
|
@Test
|
|
public void lookupNil() {
|
|
String symbol = "NIL";
|
|
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
|
|
}
|
|
|
|
@Test
|
|
public void lookupUndefinedSymbol() {
|
|
assertNull(EVAL.lookupSymbol("undefined"));
|
|
}
|
|
|
|
@Test(expected = UndefinedFunctionException.class)
|
|
public void evalUndefinedFunction() {
|
|
String input = "(funcall 'eval '(undefined))";
|
|
|
|
evaluateString(input);
|
|
}
|
|
|
|
@Test(expected = UndefinedSymbolException.class)
|
|
public void evalUndefinedSymbol() {
|
|
String input = "(eval undefined)";
|
|
|
|
evaluateString(input);
|
|
}
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
public void evalWithDottedLambdaList() {
|
|
String input = "(funcall 'eval (cons '+ 1))";
|
|
|
|
evaluateString(input);
|
|
}
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
|
public void evalWithTooManyArguments() {
|
|
evaluateString("(eval '1 '2 '3)");
|
|
}
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
public void evalWithTooFewArguments() {
|
|
evaluateString("(eval)");
|
|
}
|
|
|
|
@Test
|
|
public void undefinedFunctionException_HasCorrectAttributes() {
|
|
UndefinedFunctionException e = new UndefinedFunctionException(NIL);
|
|
|
|
assertEquals(ERROR, e.getSeverity());
|
|
assertNotNull(e.getMessage());
|
|
assertTrue(e.getMessage().length() > 0);
|
|
}
|
|
|
|
@Test
|
|
public void undefinedSymbolException_HasCorrectAttributes() {
|
|
UndefinedSymbolException e = new UndefinedSymbolException(NIL);
|
|
|
|
assertEquals(ERROR, e.getSeverity());
|
|
assertNotNull(e.getMessage());
|
|
assertTrue(e.getMessage().length() > 0);
|
|
}
|
|
|
|
@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() {
|
|
String input = "`@9";
|
|
|
|
assertSExpressionsMatch(parseString("9"), evaluateString(input));
|
|
}
|
|
|
|
}
|