transcendental-lisp/test/function/builtin/BackTickEvaluatorTester.java

136 lines
5.5 KiB
Java

package function.builtin;
import static sexpression.Nil.NIL;
import static testutil.TestUtilities.*;
import org.junit.Test;
import function.builtin.BackTickEvaluator.*;
import sexpression.*;
public class BackTickEvaluatorTester {
private BackTickEvaluator createBackTickEvaluator(SExpression expression) {
return new BackTickEvaluator(new BackTickExpression(expression));
}
@Test
public void evaluateNil() {
BackTickEvaluator evaluator = createBackTickEvaluator(NIL);
assertSExpressionsMatch(NIL, evaluator.evaluate());
}
@Test
public void evaluateNumber() {
SExpression input = new LispNumber("99");
BackTickEvaluator evaluator = createBackTickEvaluator(input);
assertSExpressionsMatch(input, evaluator.evaluate());
}
@Test
public void evaluateList() {
SExpression input = makeList(new LispNumber("1"), new LispNumber("99"));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
assertSExpressionsMatch(input, evaluator.evaluate());
}
@Test
public void evaluateComma() {
SExpression input = new CommaExpression(makeList(new Symbol("+"), new LispNumber("1"), new LispNumber("9")));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
assertSExpressionsMatch(new LispNumber("10"), evaluator.evaluate());
}
@Test
public void evaluateListWithComma() {
SExpression input = makeList(new CommaExpression(makeList(new Symbol("+"), new LispNumber("1"),
new LispNumber("9"))));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
assertSExpressionsMatch(makeList(new LispNumber("10")), evaluator.evaluate());
}
@Test(expected = NestedCommaException.class)
public void evaluateListWithNestedComma() {
SExpression input = makeList(new CommaExpression(new CommaExpression(new Symbol("+"))));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
evaluator.evaluate();
}
@Test(expected = AtSignNotInCommaException.class)
public void evaluateListWithNoCommaPrecedingAtSign() {
SExpression input = makeList(new AtSignExpression(makeList(new Symbol("+"))));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
evaluator.evaluate();
}
@Test(expected = AtSignNotInCommaException.class)
public void evaluateAtSign() {
SExpression input = new AtSignExpression(makeList(new Symbol("+")));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
evaluator.evaluate();
}
@Test(expected = NestedAtSignException.class)
public void evaluateListWithNestedAtSigns() {
SExpression input = makeList(new CommaExpression(new AtSignExpression(new AtSignExpression(makeList(new Symbol("+"))))));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
evaluator.evaluate();
}
@Test(expected = NestedCommaException.class)
public void evaluateListWithCommaAfterAtSign() {
SExpression input = makeList(new CommaExpression(new AtSignExpression(new CommaExpression(makeList(new Symbol("+"))))));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
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"));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
assertSExpressionsMatch(expected, evaluator.evaluate());
}
@Test(expected = AtSignNotListException.class)
public void atSignDoesNotEvaluateToList() {
SExpression input = makeList(new CommaExpression(new AtSignExpression(new LispNumber("1"))));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
evaluator.evaluate();
}
@Test
public void evaluateListWithCommasAndAtSign() {
SExpression input = makeList(new LispNumber("78"),
new CommaExpression(new AtSignExpression(makeList(new Symbol("LIST"),
new LispNumber("1"),
new LispNumber("9")))),
new CommaExpression(makeList(new Symbol("+"), new LispNumber("20"),
new LispNumber("5"))),
new CommaExpression(makeList(new Symbol("LIST"), new LispNumber("7"),
new LispNumber("6"))),
new LispString("\"sky\""));
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\""));
BackTickEvaluator evaluator = createBackTickEvaluator(input);
assertSExpressionsMatch(expected, evaluator.evaluate());
}
}