transcendental-lisp/test/function/builtin/special/CONDTest.java

95 lines
2.5 KiB
Java
Raw Normal View History

package function.builtin.special;
2016-12-24 13:16:03 -05:00
import static testutil.TestUtilities.*;
import org.junit.Test;
import function.ArgumentValidator.*;
2016-12-24 13:16:03 -05:00
2017-03-15 13:37:39 -04:00
public class CONDTest {
2016-12-24 13:16:03 -05:00
@Test
public void condWithNoArguments() {
2016-12-24 13:16:03 -05:00
String input = "(cond)";
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithTrue() {
2016-12-24 13:16:03 -05:00
String input = "(cond (T))";
assertSExpressionsMatch(parseString("T"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithNumber() {
String input = "(cond ((+ 1 2)))";
assertSExpressionsMatch(parseString("3"), evaluateString(input));
}
2016-12-24 13:16:03 -05:00
@Test
public void condWithSingleClause() {
2016-12-24 13:16:03 -05:00
String input = "(cond (T \"true\"))";
assertSExpressionsMatch(parseString("\"true\""), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithMultipleClauses() {
2016-12-24 13:16:03 -05:00
String input = "(cond ((= 1 2) 2) ((= 1 2) 2) ((= 1 1) 3))";
assertSExpressionsMatch(parseString("3"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithMultipleTrueTests_ReturnsFirstOne() {
2016-12-24 13:16:03 -05:00
String input = "(cond ((= 1 1) 2) ((= 1 1) 3))";
assertSExpressionsMatch(parseString("2"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithMultipleTrueTests_OnlyEvaluatesFirstOne() {
2016-12-24 13:16:03 -05:00
String input = "(cond ((= 1 1) 2) ((= 1 1) x))";
assertSExpressionsMatch(parseString("2"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithMultipleValuesInConsequent_OnlyReturnsLast() {
2016-12-24 13:16:03 -05:00
String input = "(cond ((= 1 1) 2 3 4))";
assertSExpressionsMatch(parseString("4"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test
public void condWithNoTrueTest_ReturnsNil() {
2016-12-24 13:16:03 -05:00
String input = "(cond ((= 1 2) T) ((= 1 3) T))";
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
2016-12-24 13:16:03 -05:00
}
@Test(expected = BadArgumentTypeException.class)
public void condWithEmptyListArgument_ThrowsException() {
2016-12-24 13:16:03 -05:00
evaluateString("(cond ())");
}
@Test(expected = BadArgumentTypeException.class)
public void condWithNilArgument_ThrowsException() {
evaluateString("(cond nil)");
}
2016-12-24 13:16:03 -05:00
@Test(expected = BadArgumentTypeException.class)
public void condWithNonListArgument_ThrowsException() {
2016-12-24 13:16:03 -05:00
evaluateString("(cond o)");
}
@Test(expected = DottedArgumentListException.class)
public void condWithDottedArgumentList_ThrowsException() {
evaluateString("(apply 'cond (cons '(nil T) 'b))");
}
2016-12-24 13:16:03 -05:00
}