2017-01-14 18:01:14 -05:00
|
|
|
package function.builtin.special;
|
2016-12-24 13:16:03 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import static testutil.TestUtilities.assertSExpressionsMatch;
|
|
|
|
import static testutil.TestUtilities.evaluateString;
|
|
|
|
import static testutil.TestUtilities.parseString;
|
2016-12-24 13:16:03 -05:00
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import function.ArgumentValidator.BadArgumentTypeException;
|
|
|
|
import function.ArgumentValidator.DottedArgumentListException;
|
2017-07-19 15:23:15 -04:00
|
|
|
import testutil.SymbolAndFunctionCleaner;
|
2016-12-24 13:16:03 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
public class CONDTest extends SymbolAndFunctionCleaner {
|
2016-12-24 13:16:03 -05:00
|
|
|
|
|
|
|
@Test
|
2017-02-28 15:01:05 -05:00
|
|
|
public void condWithNoArguments() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
2017-02-28 15:01:05 -05:00
|
|
|
public void condWithTrue() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-21 12:35:47 -05:00
|
|
|
@Test
|
2017-02-28 15:01:05 -05:00
|
|
|
public void condWithNumber() {
|
2017-02-21 12:35:47 -05:00
|
|
|
String input = "(cond ((+ 1 2)))";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("3"), evaluateString(input));
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:16:03 -05:00
|
|
|
@Test
|
2017-02-28 16:16:43 -05:00
|
|
|
public void condWithSingleClause() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
2017-02-28 16:16:43 -05:00
|
|
|
public void condWithMultipleClauses() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
2017-02-28 16:16:43 -05:00
|
|
|
public void condWithMultipleTrueTests_ReturnsFirstOne() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
2017-02-28 16:16:43 -05:00
|
|
|
public void condWithMultipleTrueTests_OnlyEvaluatesFirstOne() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
2017-02-28 16:16:43 -05:00
|
|
|
public void condWithMultipleValuesInConsequent_OnlyReturnsLast() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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
|
2017-02-28 16:16:43 -05:00
|
|
|
public void condWithNoTrueTest_ReturnsNil() {
|
2016-12-24 13:16:03 -05:00
|
|
|
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)
|
2017-03-01 16:33:06 -05:00
|
|
|
public void condWithEmptyListArgument_ThrowsException() {
|
2016-12-24 13:16:03 -05:00
|
|
|
evaluateString("(cond ())");
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:33:06 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void condWithNilArgument_ThrowsException() {
|
|
|
|
evaluateString("(cond nil)");
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:16:03 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
2017-02-28 15:01:05 -05:00
|
|
|
public void condWithNonListArgument_ThrowsException() {
|
2016-12-24 13:16:03 -05:00
|
|
|
evaluateString("(cond o)");
|
|
|
|
}
|
|
|
|
|
2016-12-25 12:49:18 -05:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
2017-02-28 15:01:05 -05:00
|
|
|
public void condWithDottedArgumentList_ThrowsException() {
|
2016-12-25 12:49:18 -05:00
|
|
|
evaluateString("(apply 'cond (cons '(nil T) 'b))");
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|