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

76 lines
2.0 KiB
Java
Raw Normal View History

package function.builtin.special;
2017-01-27 12:12:27 -05:00
import static org.junit.Assert.assertEquals;
import static testutil.TestUtilities.*;
2017-01-27 12:12:27 -05:00
import java.io.*;
2017-01-27 12:12:27 -05:00
import org.junit.*;
import environment.Environment;
import function.ArgumentValidator.*;
public class DEFUNTester {
2017-01-27 12:12:27 -05:00
private ByteArrayOutputStream outputStream;
private void assertPrinted(String expected) {
assertEquals(expected, outputStream.toString());
}
@Before
public void setUp() {
this.outputStream = new ByteArrayOutputStream();
Environment.getInstance().setOutput(new PrintStream(outputStream));
}
@Test
public void testDefun() {
String input = "(defun f () nil)";
assertSExpressionsMatch(parseString("f"), evaluateString(input));
assertSExpressionsMatch(parseString("()"), evaluateString("(f)"));
}
2017-01-27 12:12:27 -05:00
@Test
public void redefineFunction_DisplaysWarning() {
String input = "(defun myFunction () nil)";
evaluateString(input);
evaluateString(input);
assertPrinted("WARNING: redefining function MYFUNCTION\n");
}
@Test
public void redefineFunction_ActuallyRedefinesFunction() {
evaluateString("(defun myFunction2 () nil)");
evaluateString("(defun myFunction2 () T)");
assertPrinted("WARNING: redefining function MYFUNCTION2\n");
assertSExpressionsMatch(parseString("t"), evaluateString("(myFunction2)"));
}
@Test(expected = DottedArgumentListException.class)
public void testDefunWithDottedLambdaList() {
String input = "(funcall 'defun 'x (cons 'a 'b) ())";
evaluateString(input);
}
@Test(expected = BadArgumentTypeException.class)
public void testDefunWithNonSymbolName() {
evaluateString("(defun 1 () ())");
}
@Test(expected = BadArgumentTypeException.class)
public void testDefunWithBadLambdaList() {
evaluateString("(defun x a ())");
}
@Test(expected = TooFewArgumentsException.class)
public void testApplyWithTooFewArguments() {
evaluateString("(defun x ())");
}
}