2017-03-11 15:41:07 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
|
|
|
import static sexpression.Nil.NIL;
|
2017-03-12 12:19:45 -04:00
|
|
|
import static sexpression.Symbol.T;
|
2017-03-11 15:41:07 -05:00
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
import function.ArgumentValidator.DottedArgumentListException;
|
|
|
|
import function.builtin.BackquoteEvaluator.*;
|
2017-03-11 15:41:07 -05:00
|
|
|
import sexpression.*;
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
public class BackquoteEvaluatorTester {
|
2017-03-11 15:41:07 -05:00
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
private BackquoteEvaluator createBackquoteEvaluator(SExpression expression) {
|
|
|
|
return new BackquoteEvaluator(new BackquoteExpression(expression));
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateNil() {
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(NIL);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(NIL, evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateNumber() {
|
|
|
|
SExpression input = new LispNumber("99");
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(input, evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateList() {
|
|
|
|
SExpression input = makeList(new LispNumber("1"), new LispNumber("99"));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(input, evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateComma() {
|
|
|
|
SExpression input = new CommaExpression(makeList(new Symbol("+"), new LispNumber("1"), new LispNumber("9")));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(new LispNumber("10"), evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateListWithComma() {
|
|
|
|
SExpression input = makeList(new CommaExpression(makeList(new Symbol("+"), new LispNumber("1"),
|
|
|
|
new LispNumber("9"))));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(makeList(new LispNumber("10")), evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = NestedCommaException.class)
|
|
|
|
public void evaluateListWithNestedComma() {
|
|
|
|
SExpression input = makeList(new CommaExpression(new CommaExpression(new Symbol("+"))));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = AtSignNotInCommaException.class)
|
|
|
|
public void evaluateListWithNoCommaPrecedingAtSign() {
|
|
|
|
SExpression input = makeList(new AtSignExpression(makeList(new Symbol("+"))));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = AtSignNotInCommaException.class)
|
|
|
|
public void evaluateAtSign() {
|
|
|
|
SExpression input = new AtSignExpression(makeList(new Symbol("+")));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = NestedAtSignException.class)
|
|
|
|
public void evaluateListWithNestedAtSigns() {
|
|
|
|
SExpression input = makeList(new CommaExpression(new AtSignExpression(new AtSignExpression(makeList(new Symbol("+"))))));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = NestedCommaException.class)
|
|
|
|
public void evaluateListWithCommaAfterAtSign() {
|
|
|
|
SExpression input = makeList(new CommaExpression(new AtSignExpression(new CommaExpression(makeList(new Symbol("+"))))));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateListWithAtSign() {
|
|
|
|
SExpression input = makeList(new CommaExpression(new AtSignExpression(makeList(new Symbol("LIST"),
|
|
|
|
new LispNumber("1"),
|
|
|
|
new LispNumber("9")))));
|
|
|
|
SExpression expected = makeList(new LispNumber("1"), new LispNumber("9"));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(expected, evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = AtSignNotListException.class)
|
|
|
|
public void atSignDoesNotEvaluateToList() {
|
|
|
|
SExpression input = makeList(new CommaExpression(new AtSignExpression(new LispNumber("1"))));
|
2017-03-12 12:19:45 -04:00
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void evaluateListWithCommasAndAtSign() {
|
2017-03-12 12:19:45 -04:00
|
|
|
Cons list1 = makeList(new Symbol("LIST"), new LispNumber("1"), new LispNumber("9"));
|
|
|
|
Cons list2 = makeList(new Symbol("+"), new LispNumber("20"), new LispNumber("5"));
|
|
|
|
Cons list3 = makeList(new Symbol("LIST"), new LispNumber("7"), new LispNumber("6"));
|
|
|
|
|
|
|
|
SExpression input = makeList(new LispNumber("78"), new CommaExpression(new AtSignExpression(list1)),
|
|
|
|
new CommaExpression(list2), new CommaExpression(list3), new LispString("\"sky\""));
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
SExpression expected = makeList(new LispNumber("78"), new LispNumber("1"), new LispNumber("9"),
|
|
|
|
new LispNumber("25"), makeList(new LispNumber("7"), new LispNumber("6")),
|
|
|
|
new LispString("\"sky\""));
|
2017-03-12 12:19:45 -04:00
|
|
|
|
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
2017-03-11 15:41:07 -05:00
|
|
|
|
|
|
|
assertSExpressionsMatch(expected, evaluator.evaluate());
|
|
|
|
}
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void evaluateDottedList() {
|
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(new Cons(T, T));
|
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void atSignWithDottedList() {
|
|
|
|
SExpression input = makeList(new CommaExpression(new AtSignExpression(makeList(new Symbol("CONS"), T, T))));
|
|
|
|
BackquoteEvaluator evaluator = createBackquoteEvaluator(input);
|
|
|
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void backquoteExceptionsHaveCorrectAttributes() {
|
|
|
|
assertIsErrorWithMessage(new NestedCommaException());
|
|
|
|
assertIsErrorWithMessage(new NestedAtSignException());
|
|
|
|
assertIsErrorWithMessage(new AtSignNotInCommaException());
|
|
|
|
assertIsErrorWithMessage(new AtSignNotListException());
|
|
|
|
}
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|