2017-02-28 15:01:05 -05:00
|
|
|
package function.builtin.special;
|
|
|
|
|
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
|
|
|
|
import function.ArgumentValidator.*;
|
|
|
|
import table.ExecutionContext;
|
|
|
|
|
|
|
|
public class CASETester {
|
|
|
|
|
|
|
|
private ExecutionContext executionContext;
|
|
|
|
|
|
|
|
public CASETester() {
|
|
|
|
this.executionContext = ExecutionContext.getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
executionContext.clearContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
executionContext.clearContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-28 16:16:43 -05:00
|
|
|
public void caseWithKeyOnly() {
|
2017-02-28 15:01:05 -05:00
|
|
|
String input = "(case t)";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithEmptyConsequent() {
|
|
|
|
String input = "(case :a ((:a)))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithOneClause_Match() {
|
|
|
|
String input = "(case :a ((:a) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("banana"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithOneClause_NoMatch() {
|
|
|
|
String input = "(case :a ((:b) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithSeveralClauses_Match() {
|
|
|
|
String input = "(case :a ((:b) 'orange) ((:a) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("banana"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithSeveralClauses_NoMatch() {
|
|
|
|
String input = "(case :a ((:b) 'orange) ((:c) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-02-28 15:07:37 -05:00
|
|
|
@Test
|
|
|
|
public void caseWithSeveralItemsInKeyList_Match() {
|
|
|
|
String input = "(case :a ((:b :a) 'orange) ((:c :d) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithSeveralItemsInKeyList_NoMatch() {
|
|
|
|
String input = "(case :a ((:b :f) 'orange) ((:c :d) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-02-28 15:01:05 -05:00
|
|
|
@Test
|
|
|
|
public void caseWithSymbolicKeyList_Match() {
|
|
|
|
String input = "(case :a (:a 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithSymbolicKeyList_NoMatch() {
|
|
|
|
String input = "(case :a (:b 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:16:43 -05:00
|
|
|
@Test
|
|
|
|
public void caseDoesNotEvaluateKeyList() {
|
|
|
|
String input = "(case 'x ((x) t))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("t"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-02-28 15:01:05 -05:00
|
|
|
@Test
|
|
|
|
public void caseWithEmptyKeyList() {
|
|
|
|
String input = "(case nil (() 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithNil() {
|
|
|
|
String input = "(case nil ((nil) 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:33:06 -05:00
|
|
|
@Test
|
|
|
|
public void caseWithEmptyList() {
|
|
|
|
String input = "(case () ((()) 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithList() {
|
|
|
|
String input = "(case '(5 4 3) (((1 2) (5 4 3)) 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2017-02-28 15:01:05 -05:00
|
|
|
@Test
|
|
|
|
public void caseWithDefaultClause() {
|
|
|
|
String input = "(case nil (() 'banana) (t 'orange))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithOutOfOrderDefaultClause() {
|
|
|
|
String input = "(case :a (t 'orange) (:a 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("orange"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithKeyListContainingT() {
|
|
|
|
String input = "(case t ((t) 'banana))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("banana"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseWithMultipleMatches_ReturnsFirst() {
|
|
|
|
String input = "(case 2 ((0) 'banana) ((1) 'apple) ((2) 'avocado) ((2) 'greenbean))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("avocado"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-28 16:16:43 -05:00
|
|
|
public void caseEvaluatesMultipleConsequents() {
|
2017-03-07 16:41:26 -05:00
|
|
|
String input = "(case 2 (1 1) (2 (setq x 'x) (setq y 'y)) (3 3)))";
|
2017-02-28 16:16:43 -05:00
|
|
|
|
|
|
|
evaluateString(input);
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("x"), evaluateString("x"));
|
|
|
|
assertSExpressionsMatch(parseString("y"), evaluateString("y"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseReturnsValueOfLastConsequent() {
|
|
|
|
String input = "(case 2 (1 1) (2 3 4 5) (3 3))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("5"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void caseOnlyEvaluatesConsequentInFirstMatchingClause() {
|
2017-03-07 16:41:26 -05:00
|
|
|
String input = "(case 2 ((0) (setq zero 0)) ((1) (setq one 1)) ((2) (setq two '2)) ((2) (setq two 'two)))";
|
2017-02-28 15:01:05 -05:00
|
|
|
|
2017-03-07 16:41:26 -05:00
|
|
|
evaluateString("(setq zero nil)");
|
|
|
|
evaluateString("(setq one nil)");
|
2017-02-28 15:01:05 -05:00
|
|
|
evaluateString(input);
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString("zero"));
|
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString("one"));
|
|
|
|
assertSExpressionsMatch(parseString("2"), evaluateString("two"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
|
|
public void caseWithTooFewArguments() {
|
|
|
|
evaluateString("(case)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void caseWithNonListClause() {
|
|
|
|
evaluateString("(case :a t)");
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:16:43 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
2017-03-01 16:33:06 -05:00
|
|
|
public void caseWithEmptyClause() {
|
2017-02-28 16:16:43 -05:00
|
|
|
evaluateString("(case :a ())");
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:33:06 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void caseWithNilClause() {
|
|
|
|
evaluateString("(case :a nil)");
|
|
|
|
}
|
|
|
|
|
2017-02-28 15:01:05 -05:00
|
|
|
}
|