From c2e4d76700ca5fc40b1faaf2e907270449fc607d Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 5 May 2018 17:01:49 -0400 Subject: [PATCH] Convert test utilities to kotlin --- src/test/kotlin/parser/LispParserTest.kt | 16 +-- src/test/kotlin/table/FunctionTableTest.kt | 2 +- .../terminal/ControlSequenceHandlerTest.java | 2 +- src/test/kotlin/testutil/TestUtilities.java | 99 ------------------- src/test/kotlin/testutil/TestUtilities.kt | 89 +++++++++++++++++ 5 files changed, 95 insertions(+), 113 deletions(-) delete mode 100644 src/test/kotlin/testutil/TestUtilities.java create mode 100644 src/test/kotlin/testutil/TestUtilities.kt diff --git a/src/test/kotlin/parser/LispParserTest.kt b/src/test/kotlin/parser/LispParserTest.kt index 34202c8..fb38768 100644 --- a/src/test/kotlin/parser/LispParserTest.kt +++ b/src/test/kotlin/parser/LispParserTest.kt @@ -10,6 +10,7 @@ import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS import scanner.LispScanner.UnterminatedStringException import stream.UncheckedIOException +import testutil.TestUtilities.assertIsErrorWithMessage import testutil.TestUtilities.createIOExceptionThrowingInputStream import testutil.TestUtilities.createInputStreamFromString import testutil.TypeAssertions.assertAtSignExpression @@ -191,10 +192,7 @@ class LispParserTest { try { parser.nextSExpression() } catch (e: BadCharacterException) { - val message = e.message - - assertThat(message).isNotEmpty() - assertThat(e.severity).isEqualTo(ERROR) + assertIsErrorWithMessage(e) } } @@ -222,10 +220,7 @@ class LispParserTest { try { parser.nextSExpression() } catch (e: EofEncounteredException) { - val message = e.message - - assertThat(message).isNotEmpty() - assertThat(e.severity).isEqualTo(ERROR) + assertIsErrorWithMessage(e) } } @@ -245,10 +240,7 @@ class LispParserTest { try { parser.nextSExpression() } catch (e: StartsWithRightParenthesisException) { - val message = e.message - - assertThat(message).isNotEmpty() - assertThat(e.severity).isEqualTo(ERROR) + assertIsErrorWithMessage(e) } } diff --git a/src/test/kotlin/table/FunctionTableTest.kt b/src/test/kotlin/table/FunctionTableTest.kt index acab1dd..3d510af 100644 --- a/src/test/kotlin/table/FunctionTableTest.kt +++ b/src/test/kotlin/table/FunctionTableTest.kt @@ -119,7 +119,7 @@ class FunctionTableTest { } @Test - fun `lispFunctionInstantiationException is cool`() { + fun `LispFunctionInstantiationException is cool`() { val e = LispFunctionInstantiationException("Bad") assertThat(e.message).isNotEmpty() diff --git a/src/test/kotlin/terminal/ControlSequenceHandlerTest.java b/src/test/kotlin/terminal/ControlSequenceHandlerTest.java index fc157d2..cd4dae0 100644 --- a/src/test/kotlin/terminal/ControlSequenceHandlerTest.java +++ b/src/test/kotlin/terminal/ControlSequenceHandlerTest.java @@ -31,7 +31,7 @@ public class ControlSequenceHandlerTest { } private SafeInputStream createSafeInputStream(String data) { - return new SafeInputStream(TestUtilities.createInputStreamFromString(data)); + return new SafeInputStream(TestUtilities.INSTANCE.createInputStreamFromString(data)); } @Before diff --git a/src/test/kotlin/testutil/TestUtilities.java b/src/test/kotlin/testutil/TestUtilities.java deleted file mode 100644 index e5062bb..0000000 --- a/src/test/kotlin/testutil/TestUtilities.java +++ /dev/null @@ -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())); - } -} diff --git a/src/test/kotlin/testutil/TestUtilities.kt b/src/test/kotlin/testutil/TestUtilities.kt new file mode 100644 index 0000000..b46a384 --- /dev/null +++ b/src/test/kotlin/testutil/TestUtilities.kt @@ -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() + } +}