2016-12-14 14:41:43 -05:00
|
|
|
package testutil;
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
2017-02-21 13:59:56 -05:00
|
|
|
import static function.builtin.EVAL.eval;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2017-03-11 15:41:07 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
2016-12-22 10:32:48 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2017-03-11 15:41:07 -05:00
|
|
|
import java.util.Arrays;
|
2016-12-14 14:41:43 -05:00
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
import error.LispException;
|
2016-12-22 10:32:48 -05:00
|
|
|
import parser.LispParser;
|
2017-11-12 09:42:25 -05:00
|
|
|
import sexpression.Cons;
|
|
|
|
import sexpression.SExpression;
|
2016-12-22 10:32:48 -05:00
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
public final class TestUtilities {
|
2016-12-14 14:41:43 -05:00
|
|
|
|
|
|
|
public static InputStream createInputStreamFromString(String string) {
|
|
|
|
return new ByteArrayInputStream(string.getBytes());
|
|
|
|
}
|
2016-12-22 10:32:48 -05:00
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
public static InputStream createIOExceptionThrowingInputStream() {
|
|
|
|
return new InputStream() {
|
|
|
|
|
2017-03-13 09:58:30 -04:00
|
|
|
@Override
|
2016-12-14 14:41:43 -05:00
|
|
|
public int read() throws IOException {
|
2017-03-23 12:14:44 -04:00
|
|
|
throw new IOException("read()");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
throw new IOException("close()");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static OutputStream createIOExceptionThrowingOutputStream() {
|
|
|
|
return new OutputStream() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(byte[] b) throws IOException {
|
|
|
|
throw new IOException("write(byte[])");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void flush() throws IOException {
|
|
|
|
throw new IOException("flush()");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
throw new IOException("close()");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(int arg0) throws IOException {
|
|
|
|
throw new IOException("write(int)");
|
2016-12-14 14:41:43 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
public static SExpression evaluateString(String input) {
|
2017-02-21 13:59:56 -05:00
|
|
|
return eval(parseString(input));
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static SExpression parseString(String input) {
|
|
|
|
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
|
|
|
|
|
|
|
|
return new LispParser(stringInputStream, "testFile").getNextSExpression();
|
|
|
|
}
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
public static Cons makeList(SExpression... expressionList) {
|
|
|
|
if (expressionList.length == 0)
|
|
|
|
return NIL;
|
|
|
|
|
|
|
|
Cons rest = makeList(Arrays.copyOfRange(expressionList, 1, expressionList.length));
|
|
|
|
|
|
|
|
return new Cons(expressionList[0], rest);
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
public static void assertSExpressionsMatch(SExpression one, SExpression two) {
|
|
|
|
assertEquals(one.toString(), two.toString());
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:14:16 -05:00
|
|
|
public static void assertSExpressionsDoNotMatch(SExpression one, SExpression two) {
|
|
|
|
assertNotEquals(one.toString(), two.toString());
|
|
|
|
}
|
|
|
|
|
2017-03-12 12:19:45 -04:00
|
|
|
public static void assertIsErrorWithMessage(LispException e) {
|
|
|
|
assertEquals(ERROR, e.getSeverity());
|
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
}
|