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

105 lines
2.8 KiB
Java
Raw Normal View History

package function.builtin.special;
2017-02-11 10:42:07 -05:00
import static org.junit.Assert.assertTrue;
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.RuntimeEnvironment;
2017-02-11 10:42:07 -05:00
import error.ErrorManager;
import function.ArgumentValidator.*;
import table.FunctionTable;
public class DEFUNTester {
2017-01-27 12:12:27 -05:00
private ByteArrayOutputStream outputStream;
2017-02-11 10:51:37 -05:00
private RuntimeEnvironment environment;
public DEFUNTester() {
this.environment = RuntimeEnvironment.getInstance();
}
2017-01-27 12:12:27 -05:00
2017-02-11 10:42:07 -05:00
private void assertSomethingPrinted() {
assertTrue(outputStream.toByteArray().length > 0);
2017-01-27 12:12:27 -05:00
}
@Before
public void setUp() {
2017-02-11 10:51:37 -05:00
outputStream = new ByteArrayOutputStream();
environment.setOutput(new PrintStream(outputStream));
environment.setErrorManager(new ErrorManager());
2017-02-11 13:33:34 -05:00
environment.setWarningOutputDecorator(s -> s);
2017-02-11 10:51:37 -05:00
FunctionTable.reset();
}
@After
public void tearDown() {
FunctionTable.reset();
2017-01-27 12:12:27 -05:00
}
@Test
public void testDefun() {
String input = "(defun f () t)";
assertSExpressionsMatch(parseString("f"), evaluateString(input));
assertSExpressionsMatch(parseString("t"), evaluateString("(f)"));
}
@Test
public void testDefunWithEmptyBody() {
String input = "(defun f ())";
assertSExpressionsMatch(parseString("f"), evaluateString(input));
assertSExpressionsMatch(parseString("()"), evaluateString("(f)"));
}
@Test
public void testDefunEvaluatesArguments() {
evaluateString("(defun f (x) (car x))");
assertSExpressionsMatch(parseString("1"), evaluateString("(f '(1 2 3))"));
}
2017-01-27 12:12:27 -05:00
@Test
public void redefineFunction_DisplaysWarning() {
String input = "(defun myFunction () nil)";
evaluateString(input);
evaluateString(input);
2017-02-11 10:42:07 -05:00
assertSomethingPrinted();
2017-01-27 12:12:27 -05:00
}
@Test
public void redefineFunction_ActuallyRedefinesFunction() {
evaluateString("(defun myFunction2 () nil)");
evaluateString("(defun myFunction2 () T)");
2017-02-11 10:42:07 -05:00
assertSomethingPrinted();
2017-01-27 12:12:27 -05:00
assertSExpressionsMatch(parseString("t"), evaluateString("(myFunction2)"));
}
@Test(expected = DottedArgumentListException.class)
public void testDefunWithDottedLambdaList() {
evaluateString("(funcall 'defun 'x (cons 'a 'b) ())");
}
@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 testDefunWithTooFewArguments() {
evaluateString("(defun x)");
}
}