Convert test utilities to kotlin
This commit is contained in:
parent
28f2351654
commit
c2e4d76700
|
@ -10,6 +10,7 @@ import org.junit.jupiter.api.TestInstance
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
||||||
import scanner.LispScanner.UnterminatedStringException
|
import scanner.LispScanner.UnterminatedStringException
|
||||||
import stream.UncheckedIOException
|
import stream.UncheckedIOException
|
||||||
|
import testutil.TestUtilities.assertIsErrorWithMessage
|
||||||
import testutil.TestUtilities.createIOExceptionThrowingInputStream
|
import testutil.TestUtilities.createIOExceptionThrowingInputStream
|
||||||
import testutil.TestUtilities.createInputStreamFromString
|
import testutil.TestUtilities.createInputStreamFromString
|
||||||
import testutil.TypeAssertions.assertAtSignExpression
|
import testutil.TypeAssertions.assertAtSignExpression
|
||||||
|
@ -191,10 +192,7 @@ class LispParserTest {
|
||||||
try {
|
try {
|
||||||
parser.nextSExpression()
|
parser.nextSExpression()
|
||||||
} catch (e: BadCharacterException) {
|
} catch (e: BadCharacterException) {
|
||||||
val message = e.message
|
assertIsErrorWithMessage(e)
|
||||||
|
|
||||||
assertThat(message).isNotEmpty()
|
|
||||||
assertThat(e.severity).isEqualTo(ERROR)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,10 +220,7 @@ class LispParserTest {
|
||||||
try {
|
try {
|
||||||
parser.nextSExpression()
|
parser.nextSExpression()
|
||||||
} catch (e: EofEncounteredException) {
|
} catch (e: EofEncounteredException) {
|
||||||
val message = e.message
|
assertIsErrorWithMessage(e)
|
||||||
|
|
||||||
assertThat(message).isNotEmpty()
|
|
||||||
assertThat(e.severity).isEqualTo(ERROR)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,10 +240,7 @@ class LispParserTest {
|
||||||
try {
|
try {
|
||||||
parser.nextSExpression()
|
parser.nextSExpression()
|
||||||
} catch (e: StartsWithRightParenthesisException) {
|
} catch (e: StartsWithRightParenthesisException) {
|
||||||
val message = e.message
|
assertIsErrorWithMessage(e)
|
||||||
|
|
||||||
assertThat(message).isNotEmpty()
|
|
||||||
assertThat(e.severity).isEqualTo(ERROR)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -119,7 +119,7 @@ class FunctionTableTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `lispFunctionInstantiationException is cool`() {
|
fun `LispFunctionInstantiationException is cool`() {
|
||||||
val e = LispFunctionInstantiationException("Bad")
|
val e = LispFunctionInstantiationException("Bad")
|
||||||
|
|
||||||
assertThat(e.message).isNotEmpty()
|
assertThat(e.message).isNotEmpty()
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class ControlSequenceHandlerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private SafeInputStream createSafeInputStream(String data) {
|
private SafeInputStream createSafeInputStream(String data) {
|
||||||
return new SafeInputStream(TestUtilities.createInputStreamFromString(data));
|
return new SafeInputStream(TestUtilities.INSTANCE.createInputStreamFromString(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
package testutil;
|
|
||||||
|
|
||||||
import error.LispException;
|
|
||||||
import parser.LispParser;
|
|
||||||
import sexpression.Cons;
|
|
||||||
import sexpression.SExpression;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import static error.Severity.ERROR;
|
|
||||||
import static function.builtin.EVAL.eval;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.hamcrest.Matchers.isEmptyOrNullString;
|
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static sexpression.Nil.NIL;
|
|
||||||
|
|
||||||
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) {
|
|
||||||
return eval(parseString(input));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SExpression parseString(String input) {
|
|
||||||
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
|
|
||||||
|
|
||||||
return new LispParser(stringInputStream, "testFile").nextSExpression();
|
|
||||||
}
|
|
||||||
|
|
||||||
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 expected, SExpression actual) {
|
|
||||||
assertThat(actual.toString(), is(expected.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void assertSExpressionsDoNotMatch(SExpression unexpected, SExpression actual) {
|
|
||||||
assertThat(actual.toString(), not(unexpected.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void assertIsErrorWithMessage(LispException e) {
|
|
||||||
assertThat(e.getSeverity(), is(ERROR));
|
|
||||||
assertThat(e.getMessage(), not(isEmptyOrNullString()));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
package testutil
|
||||||
|
|
||||||
|
import error.LispException
|
||||||
|
import error.Severity.ERROR
|
||||||
|
import function.builtin.EVAL.eval
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import parser.LispParser
|
||||||
|
import sexpression.Cons
|
||||||
|
import sexpression.Nil.NIL
|
||||||
|
import sexpression.SExpression
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.io.OutputStream
|
||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
object TestUtilities {
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun createInputStreamFromString(string: String) = ByteArrayInputStream(string.toByteArray())
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun createIOExceptionThrowingInputStream() = object : InputStream() {
|
||||||
|
|
||||||
|
override fun read(): Int {
|
||||||
|
throw IOException("read()")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun close() {
|
||||||
|
throw IOException("close()")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun createIOExceptionThrowingOutputStream() = object : OutputStream() {
|
||||||
|
|
||||||
|
override fun write(b: ByteArray) {
|
||||||
|
throw IOException("write(byte[])")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun flush() {
|
||||||
|
throw IOException("flush()")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun close() {
|
||||||
|
throw IOException("close()")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun write(arg0: Int) {
|
||||||
|
throw IOException("write(int)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun evaluateString(input: String): SExpression = eval(parseString(input))
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun parseString(input: String): SExpression {
|
||||||
|
val stringInputStream = TestUtilities.createInputStreamFromString(input)
|
||||||
|
|
||||||
|
return LispParser(stringInputStream, "testFile").nextSExpression()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun makeList(vararg expressionList: SExpression): Cons {
|
||||||
|
if (expressionList.isEmpty())
|
||||||
|
return NIL
|
||||||
|
|
||||||
|
val rest = makeList(*Arrays.copyOfRange(expressionList, 1, expressionList.size))
|
||||||
|
|
||||||
|
return Cons(expressionList[0], rest)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun assertSExpressionsMatch(expected: SExpression, actual: SExpression) {
|
||||||
|
assertThat(actual.toString()).isEqualTo(expected.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun assertSExpressionsDoNotMatch(unexpected: SExpression, actual: SExpression) {
|
||||||
|
assertThat(actual.toString()).isNotEqualTo(unexpected.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic()
|
||||||
|
fun assertIsErrorWithMessage(e: LispException) {
|
||||||
|
assertThat(e.severity).isEqualTo(ERROR)
|
||||||
|
assertThat(e.message).isNotEmpty()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue