transcendental-lisp/test/testutil/TestUtilities.java

95 lines
2.7 KiB
Java
Raw Normal View History

package testutil;
import static error.ErrorManager.Severity.ERROR;
2017-02-21 13:59:56 -05:00
import static function.builtin.EVAL.eval;
2017-03-10 13:14:16 -05:00
import static org.junit.Assert.*;
2017-03-11 15:41:07 -05:00
import static sexpression.Nil.NIL;
import java.io.*;
2017-03-11 15:41:07 -05:00
import java.util.Arrays;
import error.LispException;
import parser.LispParser;
2017-03-11 15:41:07 -05:00
import sexpression.*;
public final class TestUtilities {
public static InputStream createInputStreamFromString(String string) {
return new ByteArrayInputStream(string.getBytes());
}
public static InputStream createIOExceptionThrowingInputStream() {
return new InputStream() {
@Override
public int read() throws IOException {
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)");
}
};
}
public static SExpression evaluateString(String input) {
2017-02-21 13:59:56 -05:00
return eval(parseString(input));
}
public static SExpression parseString(String input) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
return new LispParser(stringInputStream, "testFile").getNextSExpression();
}
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);
}
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());
}
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
}
}