2017-01-26 15:58:15 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static testutil.TestUtilities.evaluateString;
|
|
|
|
import static testutil.TypeAssertions.*;
|
2017-01-26 15:58:15 -05:00
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
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;
|
2017-01-26 15:58:15 -05:00
|
|
|
import function.ArgumentValidator.*;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class LOADTest {
|
2017-01-26 15:58:15 -05:00
|
|
|
|
|
|
|
private ByteArrayOutputStream outputStream;
|
2017-02-11 10:42:07 -05:00
|
|
|
private ByteArrayOutputStream errorOutputStream;
|
2017-02-11 10:51:37 -05:00
|
|
|
private RuntimeEnvironment environment;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public LOADTest() {
|
2017-02-11 10:51:37 -05:00
|
|
|
this.environment = RuntimeEnvironment.getInstance();
|
|
|
|
}
|
2017-01-26 15:58:15 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
private void assertWarningMessagePrinted() {
|
2017-02-09 11:00:23 -05:00
|
|
|
assertTrue(outputStream.toByteArray().length > 0);
|
2017-02-11 10:42:07 -05:00
|
|
|
assertTrue(errorOutputStream.toByteArray().length == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertErrorMessagePrinted() {
|
|
|
|
assertTrue(errorOutputStream.toByteArray().length > 0);
|
|
|
|
assertTrue(outputStream.toByteArray().length == 0);
|
2017-01-26 15:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void assertNothingPrinted() {
|
2017-03-03 12:56:06 -05:00
|
|
|
assertTrue(errorOutputStream.toByteArray().length == 0);
|
2017-02-11 10:42:07 -05:00
|
|
|
assertTrue(outputStream.toByteArray().length == 0);
|
2017-01-26 15:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
2017-02-11 10:51:37 -05:00
|
|
|
outputStream = new ByteArrayOutputStream();
|
|
|
|
errorOutputStream = new ByteArrayOutputStream();
|
|
|
|
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.reset();
|
2017-02-11 10:51:37 -05:00
|
|
|
environment.setOutput(new PrintStream(outputStream));
|
|
|
|
environment.setErrorOutput(new PrintStream(errorOutputStream));
|
|
|
|
environment.setErrorManager(new ErrorManager());
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.setPath("");
|
2017-02-11 13:33:34 -05:00
|
|
|
environment.setWarningOutputDecorator(s -> s);
|
|
|
|
environment.setErrorOutputDecorator(s -> s);
|
2017-01-26 15:58:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-05 10:20:31 -05:00
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
environment.reset();
|
|
|
|
}
|
|
|
|
|
2017-03-05 10:41:49 -05:00
|
|
|
@Test
|
|
|
|
public void loadEmptyFileName_ReturnsNilAndPrintsWarning() {
|
|
|
|
String input = "(load \"\")";
|
|
|
|
|
|
|
|
assertNil(evaluateString(input));
|
|
|
|
assertWarningMessagePrinted();
|
|
|
|
}
|
|
|
|
|
2017-01-26 15:58:15 -05:00
|
|
|
@Test
|
|
|
|
public void loadGoodFile_ReturnsTAndPrintsNothing() {
|
|
|
|
String input = "(load \"test/function/builtin/test-files/load-good.lisp\")";
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertT(evaluateString(input));
|
2017-01-26 15:58:15 -05:00
|
|
|
assertNothingPrinted();
|
|
|
|
}
|
2017-01-26 16:00:47 -05:00
|
|
|
|
2017-01-26 15:58:15 -05:00
|
|
|
@Test
|
|
|
|
public void loadBadFile_ReturnsNilAndPrintsError() {
|
|
|
|
String input = "(load \"test/function/builtin/test-files/load-bad.lisp\")";
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertNil(evaluateString(input));
|
|
|
|
assertErrorMessagePrinted();
|
2017-01-26 15:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-05 10:41:49 -05:00
|
|
|
public void loadNonExistentFile_ReturnsNilAndPrintsWarning() {
|
2017-01-26 15:58:15 -05:00
|
|
|
String input = "(load \"doesNotExist.lisp\")";
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertNil(evaluateString(input));
|
|
|
|
assertWarningMessagePrinted();
|
2017-01-26 15:58:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:56:06 -05:00
|
|
|
@Test
|
|
|
|
public void nestedLoadsInTheSameDirectory() {
|
|
|
|
String input = "(load \"test/function/builtin/test-files/nested/nested.lisp\")";
|
|
|
|
|
|
|
|
assertT(evaluateString(input));
|
|
|
|
assertNothingPrinted();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void nestedLoadsInDifferentDirectories() {
|
|
|
|
String input = "(load \"test/function/builtin/test-files/nested/one/load-one.lisp\")";
|
|
|
|
|
|
|
|
assertT(evaluateString(input));
|
|
|
|
assertNothingPrinted();
|
|
|
|
}
|
|
|
|
|
2017-01-26 15:58:15 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void loadWithBadArgumentType() {
|
2017-01-26 15:58:15 -05:00
|
|
|
evaluateString("(load '1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void loadWithTooManyArguments() {
|
2017-01-26 15:58:15 -05:00
|
|
|
evaluateString("(load \"1\" \"2\")");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void loadWithTooFewArguments() {
|
2017-01-26 15:58:15 -05:00
|
|
|
evaluateString("(load)");
|
|
|
|
}
|
|
|
|
|
2017-03-05 10:20:31 -05:00
|
|
|
@Test
|
|
|
|
public void loadUsesRuntimePath() {
|
|
|
|
environment.setPath("test/function/builtin/test-files/nested/one/");
|
|
|
|
String input = "(load \"load-one.lisp\")";
|
|
|
|
|
|
|
|
assertT(evaluateString(input));
|
|
|
|
assertNothingPrinted();
|
|
|
|
}
|
|
|
|
|
2017-01-26 15:58:15 -05:00
|
|
|
}
|