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 caseWithOnlySwitchExpression() { 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 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 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 caseOnlyEvaluatesFirstMatchingClause() { 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)"); } }