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 public void caseWithKeyOnly() { 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)); } @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)); } @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)); } @Test public void caseDoesNotEvaluateKeyList() { String input = "(case 'x ((x) t))"; assertSExpressionsMatch(parseString("t"), evaluateString(input)); } @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)); } @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 public void caseEvaluatesMultipleConsequents() { String input = "(case 2 (1 1) (2 (setf x 'x) (setf y 'y)) (3 3)))"; 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() { String input = "(case 2 ((0) (setf zero 0)) ((1) (setf one 1)) ((2) (setf two '2)) ((2) (setf two 'two)))"; evaluateString("(setf zero nil)"); evaluateString("(setf one nil)"); 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)"); } @Test(expected = BadArgumentTypeException.class) public void caseWithNilClause() { evaluateString("(case :a ())"); } }