2017-01-14 18:01:14 -05:00
|
|
|
package function.builtin.special;
|
2016-12-24 13:16:03 -05:00
|
|
|
|
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-25 12:49:18 -05:00
|
|
|
import function.ArgumentValidator.*;
|
2016-12-24 13:16:03 -05:00
|
|
|
|
|
|
|
public class CONDTester {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithNoArguments() {
|
|
|
|
String input = "(cond)";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithTrue() {
|
|
|
|
String input = "(cond (T))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("T"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithSingleExpression() {
|
|
|
|
String input = "(cond (T \"true\"))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("\"true\""), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithMultipleExpressions() {
|
|
|
|
String input = "(cond ((= 1 2) 2) ((= 1 2) 2) ((= 1 1) 3))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("3"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithMultipleConditionsMatching_ReturnFirstOne() {
|
|
|
|
String input = "(cond ((= 1 1) 2) ((= 1 1) 3))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("2"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithMultipleConditionsMatching_OnlyEvaluatesFirstOne() {
|
|
|
|
String input = "(cond ((= 1 1) 2) ((= 1 1) x))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("2"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithMultipleResultValues_OnlyReturnsLast() {
|
|
|
|
String input = "(cond ((= 1 1) 2 3 4))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("4"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCondWithNoConditionMatching_ReturnsNil() {
|
|
|
|
String input = "(cond ((= 1 2) T) ((= 1 3) T))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void testCondWithNilArgument_ThrowsException() {
|
|
|
|
evaluateString("(cond ())");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void testCondWithNonListArgument_ThrowsException() {
|
|
|
|
evaluateString("(cond o)");
|
|
|
|
}
|
|
|
|
|
2016-12-25 12:49:18 -05:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void testCondWithDottedArgumentList_ThrowsException() {
|
|
|
|
evaluateString("(apply 'cond (cons '(nil T) 'b))");
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|