2017-01-14 18:01:14 -05:00
|
|
|
package function.builtin.special;
|
2016-12-25 13:29:06 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static org.junit.Assert.assertTrue;
|
2016-12-25 13:29:06 -05:00
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
2017-01-27 12:12:27 -05:00
|
|
|
import java.io.*;
|
2016-12-25 13:29:06 -05:00
|
|
|
|
2017-01-27 12:12:27 -05:00
|
|
|
import org.junit.*;
|
|
|
|
|
2017-02-06 13:39:05 -05:00
|
|
|
import environment.RuntimeEnvironment;
|
2017-02-11 10:42:07 -05:00
|
|
|
import error.ErrorManager;
|
2016-12-25 13:29:06 -05:00
|
|
|
import function.ArgumentValidator.*;
|
2017-02-06 12:02:19 -05:00
|
|
|
import table.FunctionTable;
|
2016-12-25 13:29:06 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-02-06 12:02:19 -05:00
|
|
|
FunctionTable.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
FunctionTable.reset();
|
2017-01-27 12:12:27 -05:00
|
|
|
}
|
|
|
|
|
2016-12-25 13:29:06 -05:00
|
|
|
@Test
|
|
|
|
public void testDefun() {
|
2017-02-04 15:38:47 -05:00
|
|
|
String input = "(defun f () t)";
|
|
|
|
|
|
|
|
assertSExpressionsMatch(parseString("f"), evaluateString(input));
|
|
|
|
assertSExpressionsMatch(parseString("t"), evaluateString("(f)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testDefunWithEmptyBody() {
|
|
|
|
String input = "(defun f ())";
|
2016-12-25 13:29:06 -05:00
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("f"), evaluateString(input));
|
|
|
|
assertSExpressionsMatch(parseString("()"), evaluateString("(f)"));
|
2016-12-25 13:29:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-25 19:11:31 -05:00
|
|
|
@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)"));
|
|
|
|
}
|
|
|
|
|
2017-01-16 13:38:49 -05:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void testDefunWithDottedLambdaList() {
|
2017-02-25 19:11:31 -05:00
|
|
|
evaluateString("(funcall 'defun 'x (cons 'a 'b) ())");
|
2017-01-16 13:38:49 -05:00
|
|
|
}
|
|
|
|
|
2016-12-25 13:29:06 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void testDefunWithNonSymbolName() {
|
|
|
|
evaluateString("(defun 1 () ())");
|
|
|
|
}
|
|
|
|
|
2017-01-16 13:38:49 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void testDefunWithBadLambdaList() {
|
|
|
|
evaluateString("(defun x a ())");
|
|
|
|
}
|
|
|
|
|
2016-12-25 13:29:06 -05:00
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
2017-02-04 15:38:47 -05:00
|
|
|
public void testDefunWithTooFewArguments() {
|
|
|
|
evaluateString("(defun x)");
|
2016-12-25 13:29:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|