Refactor code

This commit is contained in:
Mike Cifelli 2018-05-19 11:59:51 -04:00
parent ac290d4997
commit b4cd5f2fe4
6 changed files with 30 additions and 43 deletions

View File

@ -3,8 +3,6 @@ package error
import environment.RuntimeEnvironment import environment.RuntimeEnvironment
import error.Severity.CRITICAL import error.Severity.CRITICAL
import error.Severity.WARNING import error.Severity.WARNING
import java.io.PrintStream
import java.text.MessageFormat.format
/** /**
* Prints error messages and potentially terminates the application. * Prints error messages and potentially terminates the application.
@ -23,14 +21,13 @@ class ErrorManager {
output.println(formatMessage(lispException)) output.println(formatMessage(lispException))
} }
private fun selectOutputStream(severity: Severity): PrintStream { private fun selectOutputStream(severity: Severity) =
return if (severity === WARNING) RuntimeEnvironment.output!! else RuntimeEnvironment.errorOutput!! if (severity === WARNING) RuntimeEnvironment.output!! else RuntimeEnvironment.errorOutput!!
}
private fun formatMessage(lispException: LispException): String { private fun formatMessage(lispException: LispException): String {
val severity = lispException.severity val severity = lispException.severity
val prefix = severity.toDisplayString() val prefix = severity.toDisplayString()
val message = format("[{0}] {1}", prefix, lispException.message) val message = "[$prefix] ${lispException.message}"
return severity.decorate(message, RuntimeEnvironment) return severity.decorate(message, RuntimeEnvironment)
} }

View File

@ -1,12 +1,11 @@
package error package error
import file.FilePosition import file.FilePosition
import java.text.MessageFormat.format
abstract class LineColumnException(private val position: FilePosition) : LispException() { abstract class LineColumnException(private val position: FilePosition) : LispException() {
abstract val messagePrefix: String abstract val messagePrefix: String
override val message: 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}"
} }

View File

@ -38,19 +38,13 @@ class TokenFactoryImpl : TokenFactory {
} }
} }
override fun createEofToken(position: FilePosition): Token { override fun createEofToken(position: FilePosition) = Eof("EOF", position)
return Eof("EOF", position)
}
private fun isNumeric(firstCharacter: Char, text: String): Boolean { private fun isNumeric(firstCharacter: Char, text: String) =
return isDigit(firstCharacter) || isPrefixedNumeric(firstCharacter, text) isDigit(firstCharacter) || isPrefixedNumeric(firstCharacter, text)
}
private fun isPrefixedNumeric(firstCharacter: Char, text: String): Boolean { private fun isPrefixedNumeric(firstCharacter: Char, text: String) =
return isNumberPrefix(firstCharacter) && isNextCharacterDigit(text) isNumberPrefix(firstCharacter) && isNextCharacterDigit(text)
}
private fun isNextCharacterDigit(text: String): Boolean { private fun isNextCharacterDigit(text: String) = text.length > 1 && isDigit(text[1])
return text.length > 1 && isDigit(text[1])
}
} }

View File

@ -94,7 +94,7 @@ class MainTest : SymbolAndFunctionCleaner() {
exit.expectSystemExitWithStatus(1) exit.expectSystemExitWithStatus(1)
exit.checkAssertionAfterwards { exit.checkAssertionAfterwards {
assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), systemErrLog()) assertEquals("$ANSI_PURPLE$expectedMessage$ANSI_RESET\n", systemErrLog())
assertEquals("", systemOutLog()) assertEquals("", systemOutLog())
} }
@ -106,7 +106,7 @@ class MainTest : SymbolAndFunctionCleaner() {
runInterpreterWithFile(FILE) runInterpreterWithFile(FILE)
assertEquals("", systemErrLog()) 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 @Test

View File

@ -11,7 +11,6 @@ import org.junit.Test
import testutil.TestUtilities.createInputStreamFromString import testutil.TestUtilities.createInputStreamFromString
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.PrintStream import java.io.PrintStream
import java.text.MessageFormat.format
import java.util.HashSet import java.util.HashSet
class LispInterpreterTest { class LispInterpreterTest {
@ -137,7 +136,7 @@ class LispInterpreterTest {
LispInterpreterBuilder.setInput(createInputStreamFromString("'pickle"), "input") LispInterpreterBuilder.setInput(createInputStreamFromString("'pickle"), "input")
LispInterpreterBuilder.build().interpret() 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()) assertEquals("", errorOutputStream.toString())
} }

View File

@ -17,68 +17,66 @@ class TokenFactoryTest {
private lateinit var tokenFactory: TokenFactory private lateinit var tokenFactory: TokenFactory
private lateinit var testPosition: FilePosition private lateinit var testPosition: FilePosition
private fun createToken(text: String) = tokenFactory.createToken(text, testPosition)
@BeforeEach @BeforeEach
fun setUp() { fun setUp() {
tokenFactory = TokenFactoryImpl() tokenFactory = TokenFactoryImpl()
testPosition = FilePosition("testFile", 0, 0) testPosition = FilePosition("testFile", 0, 0)
} }
private fun createToken(text: String): Token {
return tokenFactory.createToken(text, testPosition)
}
@Test @Test
fun eofTokenCreation() { fun `create EOF token`() {
assertThat(tokenFactory.createEofToken(testPosition)).isInstanceOf(Eof::class.java) assertThat(tokenFactory.createEofToken(testPosition)).isInstanceOf(Eof::class.java)
} }
@Test @Test
fun leftParenthesisCreation() { fun `create left parenthesis`() {
assertThat(createToken("(")).isInstanceOf(LeftParenthesis::class.java) assertThat(createToken("(")).isInstanceOf(LeftParenthesis::class.java)
} }
@Test @Test
fun rightParenthesisCreation() { fun `create right parenthesis`() {
assertThat(createToken(")")).isInstanceOf(RightParenthesis::class.java) assertThat(createToken(")")).isInstanceOf(RightParenthesis::class.java)
} }
@Test @Test
fun quoteMarkCreation() { fun `create quote mark`() {
assertThat(createToken("'")).isInstanceOf(QuoteMark::class.java) assertThat(createToken("'")).isInstanceOf(QuoteMark::class.java)
} }
@Test @Test
fun numberCreation() { fun `create number`() {
assertThat(createToken("987")).isInstanceOf(Number::class.java) assertThat(createToken("987")).isInstanceOf(Number::class.java)
} }
@Test @Test
fun prefixedNumberCreation() { fun `create prefixed number`() {
assertThat(createToken("-987")).isInstanceOf(Number::class.java) assertThat(createToken("-987")).isInstanceOf(Number::class.java)
} }
@Test @Test
fun identifierCreation() { fun `create identifier`() {
assertThat(createToken("identifier")).isInstanceOf(Identifier::class.java) assertThat(createToken("identifier")).isInstanceOf(Identifier::class.java)
} }
@Test @Test
fun prefixedIdentifierCreation() { fun `create prefixed identifier`() {
assertThat(createToken("-identifier")).isInstanceOf(Identifier::class.java) assertThat(createToken("-identifier")).isInstanceOf(Identifier::class.java)
} }
@Test @Test
fun stringCreation() { fun `create string`() {
assertThat(createToken("\"string\"")).isInstanceOf(QuotedString::class.java) assertThat(createToken("\"string\"")).isInstanceOf(QuotedString::class.java)
} }
@Test @Test
fun emptyString_ThrowsException() { fun `empty string throws exception`() {
assertThrows(EmptyTokenTextException::class.java) { createToken("") } assertThrows(EmptyTokenTextException::class.java) { createToken("") }
} }
@Test @Test
fun emptyTokenTextException_ContainsCorrectAttributes() { fun `EmptyTokenTextException is cool`() {
try { try {
createToken("") createToken("")
} catch (e: EmptyTokenTextException) { } catch (e: EmptyTokenTextException) {
@ -88,22 +86,22 @@ class TokenFactoryTest {
} }
@Test @Test
fun badCharacter_ThrowsException() { fun `bad character throws exception`() {
assertThrows(BadCharacterException::class.java) { createToken("[abc]") } assertThrows(BadCharacterException::class.java) { createToken("[abc]") }
} }
@Test @Test
fun backTickCreation() { fun `create back tick`() {
assertThat(createToken("`")).isInstanceOf(Backquote::class.java) assertThat(createToken("`")).isInstanceOf(Backquote::class.java)
} }
@Test @Test
fun commaCreation() { fun `create comma`() {
assertThat(createToken(",")).isInstanceOf(Comma::class.java) assertThat(createToken(",")).isInstanceOf(Comma::class.java)
} }
@Test @Test
fun atSignCreation() { fun `create at sign`() {
assertThat(createToken("@")).isInstanceOf(AtSign::class.java) assertThat(createToken("@")).isInstanceOf(AtSign::class.java)
} }
} }