Refactor code
This commit is contained in:
parent
ac290d4997
commit
b4cd5f2fe4
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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}"
|
||||
}
|
|
@ -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])
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue