From b4cd5f2fe43e7acb4c4a91fe63f32a63cc065bf4 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 19 May 2018 11:59:51 -0400 Subject: [PATCH] Refactor code --- src/main/kotlin/error/ErrorManager.kt | 9 ++--- src/main/kotlin/error/LineColumnException.kt | 3 +- src/main/kotlin/token/TokenFactoryImpl.kt | 18 ++++------ src/test/kotlin/application/MainTest.kt | 4 +-- .../kotlin/interpreter/LispInterpreterTest.kt | 3 +- src/test/kotlin/token/TokenFactoryTest.kt | 36 +++++++++---------- 6 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/main/kotlin/error/ErrorManager.kt b/src/main/kotlin/error/ErrorManager.kt index 595a086..e788c93 100644 --- a/src/main/kotlin/error/ErrorManager.kt +++ b/src/main/kotlin/error/ErrorManager.kt @@ -3,8 +3,6 @@ package error import environment.RuntimeEnvironment import error.Severity.CRITICAL import error.Severity.WARNING -import java.io.PrintStream -import java.text.MessageFormat.format /** * Prints error messages and potentially terminates the application. @@ -23,14 +21,13 @@ class ErrorManager { output.println(formatMessage(lispException)) } - private fun selectOutputStream(severity: Severity): PrintStream { - return if (severity === WARNING) RuntimeEnvironment.output!! else RuntimeEnvironment.errorOutput!! - } + private fun selectOutputStream(severity: Severity) = + if (severity === WARNING) RuntimeEnvironment.output!! else RuntimeEnvironment.errorOutput!! private fun formatMessage(lispException: LispException): String { val severity = lispException.severity val prefix = severity.toDisplayString() - val message = format("[{0}] {1}", prefix, lispException.message) + val message = "[$prefix] ${lispException.message}" return severity.decorate(message, RuntimeEnvironment) } diff --git a/src/main/kotlin/error/LineColumnException.kt b/src/main/kotlin/error/LineColumnException.kt index f2eb7e0..bdcdc21 100644 --- a/src/main/kotlin/error/LineColumnException.kt +++ b/src/main/kotlin/error/LineColumnException.kt @@ -1,12 +1,11 @@ package error import file.FilePosition -import java.text.MessageFormat.format abstract class LineColumnException(private val position: FilePosition) : LispException() { abstract val messagePrefix: String override val message: String - get() = format("{0} - line {1}, column {2}", messagePrefix, position.lineNumber, position.columnNumber) + get() = "$messagePrefix - line ${position.lineNumber}, column ${position.columnNumber}" } \ No newline at end of file diff --git a/src/main/kotlin/token/TokenFactoryImpl.kt b/src/main/kotlin/token/TokenFactoryImpl.kt index 3d085bb..217bb96 100644 --- a/src/main/kotlin/token/TokenFactoryImpl.kt +++ b/src/main/kotlin/token/TokenFactoryImpl.kt @@ -38,19 +38,13 @@ class TokenFactoryImpl : TokenFactory { } } - override fun createEofToken(position: FilePosition): Token { - return Eof("EOF", position) - } + override fun createEofToken(position: FilePosition) = Eof("EOF", position) - private fun isNumeric(firstCharacter: Char, text: String): Boolean { - return isDigit(firstCharacter) || isPrefixedNumeric(firstCharacter, text) - } + private fun isNumeric(firstCharacter: Char, text: String) = + isDigit(firstCharacter) || isPrefixedNumeric(firstCharacter, text) - private fun isPrefixedNumeric(firstCharacter: Char, text: String): Boolean { - return isNumberPrefix(firstCharacter) && isNextCharacterDigit(text) - } + private fun isPrefixedNumeric(firstCharacter: Char, text: String) = + isNumberPrefix(firstCharacter) && isNextCharacterDigit(text) - private fun isNextCharacterDigit(text: String): Boolean { - return text.length > 1 && isDigit(text[1]) - } + private fun isNextCharacterDigit(text: String) = text.length > 1 && isDigit(text[1]) } diff --git a/src/test/kotlin/application/MainTest.kt b/src/test/kotlin/application/MainTest.kt index ecf75ed..24f1a93 100644 --- a/src/test/kotlin/application/MainTest.kt +++ b/src/test/kotlin/application/MainTest.kt @@ -94,7 +94,7 @@ class MainTest : SymbolAndFunctionCleaner() { exit.expectSystemExitWithStatus(1) exit.checkAssertionAfterwards { - assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), systemErrLog()) + assertEquals("$ANSI_PURPLE$expectedMessage$ANSI_RESET\n", systemErrLog()) assertEquals("", systemOutLog()) } @@ -106,7 +106,7 @@ class MainTest : SymbolAndFunctionCleaner() { runInterpreterWithFile(FILE) assertEquals("", systemErrLog()) - assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET), systemOutLog()) + assertEquals("${ANSI_GREEN}RADISH$ANSI_RESET\n\n", systemOutLog()) } @Test diff --git a/src/test/kotlin/interpreter/LispInterpreterTest.kt b/src/test/kotlin/interpreter/LispInterpreterTest.kt index 67ae1e2..e5c2972 100644 --- a/src/test/kotlin/interpreter/LispInterpreterTest.kt +++ b/src/test/kotlin/interpreter/LispInterpreterTest.kt @@ -11,7 +11,6 @@ import org.junit.Test import testutil.TestUtilities.createInputStreamFromString import java.io.ByteArrayOutputStream import java.io.PrintStream -import java.text.MessageFormat.format import java.util.HashSet class LispInterpreterTest { @@ -137,7 +136,7 @@ class LispInterpreterTest { LispInterpreterBuilder.setInput(createInputStreamFromString("'pickle"), "input") LispInterpreterBuilder.build().interpret() - assertEquals(format("{0}\n{1}\n{0}\n", PROMPT, "PICKLE"), outputStream.toString()) + assertEquals("$PROMPT\nPICKLE\n$PROMPT\n", outputStream.toString()) assertEquals("", errorOutputStream.toString()) } diff --git a/src/test/kotlin/token/TokenFactoryTest.kt b/src/test/kotlin/token/TokenFactoryTest.kt index a468dbd..bfd718a 100644 --- a/src/test/kotlin/token/TokenFactoryTest.kt +++ b/src/test/kotlin/token/TokenFactoryTest.kt @@ -17,68 +17,66 @@ class TokenFactoryTest { private lateinit var tokenFactory: TokenFactory private lateinit var testPosition: FilePosition + private fun createToken(text: String) = tokenFactory.createToken(text, testPosition) + @BeforeEach fun setUp() { tokenFactory = TokenFactoryImpl() testPosition = FilePosition("testFile", 0, 0) } - private fun createToken(text: String): Token { - return tokenFactory.createToken(text, testPosition) - } - @Test - fun eofTokenCreation() { + fun `create EOF token`() { assertThat(tokenFactory.createEofToken(testPosition)).isInstanceOf(Eof::class.java) } @Test - fun leftParenthesisCreation() { + fun `create left parenthesis`() { assertThat(createToken("(")).isInstanceOf(LeftParenthesis::class.java) } @Test - fun rightParenthesisCreation() { + fun `create right parenthesis`() { assertThat(createToken(")")).isInstanceOf(RightParenthesis::class.java) } @Test - fun quoteMarkCreation() { + fun `create quote mark`() { assertThat(createToken("'")).isInstanceOf(QuoteMark::class.java) } @Test - fun numberCreation() { + fun `create number`() { assertThat(createToken("987")).isInstanceOf(Number::class.java) } @Test - fun prefixedNumberCreation() { + fun `create prefixed number`() { assertThat(createToken("-987")).isInstanceOf(Number::class.java) } @Test - fun identifierCreation() { + fun `create identifier`() { assertThat(createToken("identifier")).isInstanceOf(Identifier::class.java) } @Test - fun prefixedIdentifierCreation() { + fun `create prefixed identifier`() { assertThat(createToken("-identifier")).isInstanceOf(Identifier::class.java) } @Test - fun stringCreation() { + fun `create string`() { assertThat(createToken("\"string\"")).isInstanceOf(QuotedString::class.java) } @Test - fun emptyString_ThrowsException() { + fun `empty string throws exception`() { assertThrows(EmptyTokenTextException::class.java) { createToken("") } } @Test - fun emptyTokenTextException_ContainsCorrectAttributes() { + fun `EmptyTokenTextException is cool`() { try { createToken("") } catch (e: EmptyTokenTextException) { @@ -88,22 +86,22 @@ class TokenFactoryTest { } @Test - fun badCharacter_ThrowsException() { + fun `bad character throws exception`() { assertThrows(BadCharacterException::class.java) { createToken("[abc]") } } @Test - fun backTickCreation() { + fun `create back tick`() { assertThat(createToken("`")).isInstanceOf(Backquote::class.java) } @Test - fun commaCreation() { + fun `create comma`() { assertThat(createToken(",")).isInstanceOf(Comma::class.java) } @Test - fun atSignCreation() { + fun `create at sign`() { assertThat(createToken("@")).isInstanceOf(AtSign::class.java) } }