Clean up the scanner type test

This commit is contained in:
Mike Cifelli 2018-07-24 18:10:19 -04:00
parent 8a970b6797
commit d376c30d7a
1 changed files with 101 additions and 131 deletions

View File

@ -1,12 +1,11 @@
package scanner package scanner
import error.Severity.ERROR import org.assertj.core.api.Assertions.assertThat
import org.junit.Assert.assertEquals import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.Assert.assertNotNull import org.junit.jupiter.api.BeforeEach
import org.junit.Assert.assertTrue import org.junit.jupiter.api.Test
import org.junit.Before
import org.junit.Test
import scanner.LispScanner.UnterminatedStringException import scanner.LispScanner.UnterminatedStringException
import testutil.TestUtilities.assertIsErrorWithMessage
import testutil.TestUtilities.createInputStreamFromString import testutil.TestUtilities.createInputStreamFromString
import token.AtSign import token.AtSign
import token.Backquote import token.Backquote
@ -23,156 +22,127 @@ import token.TokenFactory.BadCharacterException
class LispScannerTypeTest { class LispScannerTypeTest {
private var expectedTypes: MutableList<Class<out Token>>? = null private val expectedTypes = mutableListOf<Class<out Token>>()
private fun assertTokenTypesMatch(input: String) { private fun assertTokenTypesMatch(input: String) {
val stringInputStream = createInputStreamFromString(input) val stringInputStream = createInputStreamFromString(input)
val lispScanner = LispScanner(stringInputStream, "testFile") val lispScanner = LispScanner(stringInputStream, "testFile")
for (type in expectedTypes!!) for (type in expectedTypes)
assertTrue(type.isInstance(lispScanner.nextToken)) assertThat(type.isInstance(lispScanner.nextToken)).isTrue()
assertTrue(lispScanner.nextToken is Eof) assertThat(lispScanner.nextToken is Eof).isTrue()
} }
@Before @BeforeEach
fun setUp() { fun setUp() {
expectedTypes = mutableListOf() expectedTypes.clear()
} }
@Test @Test
fun givenEmptyFile_ReturnsCorrectTypes() { fun `types for an empty file`() {
val input = "" val input = ""
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test(expected = BadCharacterException::class) @Test
fun givenBadCharacter_ThrowsException() { fun `bad character is not accepted`() {
val input = "[" val input = "["
assertTokenTypesMatch(input) assertThrows(BadCharacterException::class.java) { assertTokenTypesMatch(input) }
} }
@Test @Test
fun givenBadCharacter_ExceptionContainsCorrectSeverity() { fun `BadCharacterException is cool`() {
val input = "abc\ndef[" val input = "abc\ndef["
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
try { try {
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} catch (e: BadCharacterException) { } catch (e: BadCharacterException) {
assertEquals(ERROR, e.severity) assertIsErrorWithMessage(e)
} }
} }
@Test @Test
fun givenBadCharacter_ExceptionContainsMessage() { fun `types for nil`() {
val input = "abc\ndef["
expectedTypes!!.add(Identifier::class.java)
expectedTypes!!.add(Identifier::class.java)
try {
assertTokenTypesMatch(input)
} catch (e: BadCharacterException) {
val message = e.message
assertNotNull(message)
assertTrue(message.length > 0)
}
}
@Test
fun givenNil_ReturnsCorrectTypes() {
val input = "()" val input = "()"
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenListOfNumbers_ReturnsCorrectTypes() { fun `types for a list of numbers`() {
val input = "(1 2)" val input = "(1 2)"
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenString_ReturnsCorrectTypes() { fun `types for a string`() {
val input = "\"string\"" val input = "\"string\""
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() { fun `types for a string with escaped double quote`() {
val input = "\"string \n hi \\\" bye\"" val input = "\"string \n hi \\\" bye\""
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() { fun `types for a string with an escaped double quote and comment`() {
val input = "\"string \n hi \\\" ; bye\"" val input = "\"string \n hi \\\" ; bye\""
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
assertTokenTypesMatch(input)
}
@Test(expected = UnterminatedStringException::class)
fun givenUnterminatedString_ThrowsException() {
val input = "\"oh no!"
expectedTypes!!.add(QuotedString::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenUnterminatedString_ExceptionHasCorrectSeverity() { fun `types for an unterminated string`() {
val input = "\"oh no!" val input = "\"oh no!"
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
assertThrows(UnterminatedStringException::class.java) { assertTokenTypesMatch(input) }
}
@Test
fun `UnterminatedStringException is cool`() {
val input = "\"oh no!"
expectedTypes.add(QuotedString::class.java)
try { try {
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} catch (e: LispScanner.UnterminatedStringException) { } catch (e: UnterminatedStringException) {
assertEquals(ERROR, e.severity) assertIsErrorWithMessage(e)
} }
} }
@Test @Test
fun givenUnterminatedString_ExceptionContainsMessage() { fun `types for an identifier`() {
val input = "\"oh no!"
expectedTypes!!.add(QuotedString::class.java)
try {
assertTokenTypesMatch(input)
} catch (e: LispScanner.UnterminatedStringException) {
val message = e.message
assertNotNull(message)
assertTrue(message.length > 0)
}
}
@Test
fun givenIdentifier_ReturnsCorrectTypes() {
val input = "abcdefgHIJKLMNOP1234" val input = "abcdefgHIJKLMNOP1234"
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedIdentifiers_ReturnsCorrectTypes() { fun `types for prefixed identifiers`() {
val input = "-a +b" val input = "-a +b"
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@ -180,123 +150,123 @@ class LispScannerTypeTest {
@Test @Test
fun givenSingleDigitNumber_ReturnsCorrectTypes() { fun givenSingleDigitNumber_ReturnsCorrectTypes() {
val input = "1" val input = "1"
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedNegativeNumber_ReturnsCorrectTypes() { fun `types for a prefixed negative number`() {
val input = "-1" val input = "-1"
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedNumber_ReturnsCorrectTypes() { fun `types for a prefixed positive number`() {
val input = "+1" val input = "+1"
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedNegativeNumberAndIdentifier_ReturnsCorrectTypes() { fun `types for a prefixed negative number and an identifier`() {
val input = "-1apple" val input = "-1apple"
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedNumberAndIdentifier_ReturnsCorrectTypes() { fun `types for a prefixed positive number and an identifier`() {
val input = "+1apple" val input = "+1apple"
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedNegativeNumberAndString_ReturnsCorrectTypes() { fun `types for a prefixed negative number and a string`() {
val input = "-1\"apple\"" val input = "-1\"apple\""
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenPrefixedNumberAndString_ReturnsCorrectTypes() { fun `types for a prefixed positive number and a string`() {
val input = "+1\"apple\"" val input = "+1\"apple\""
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenMultipleDigitNumber_ReturnsCorrectTypes() { fun `types for a mulitple digit number`() {
val input = "1234567890" val input = "1234567890"
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenQuote_ReturnsCorrectTypes() { fun `types for a quote`() {
val input = "'" val input = "'"
expectedTypes!!.add(QuoteMark::class.java) expectedTypes.add(QuoteMark::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() { fun `types for several expressions with no whitespace`() {
val input = "xxx\"hi\"999()'aaa" val input = "xxx\"hi\"999()'aaa"
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(QuotedString::class.java) expectedTypes.add(QuotedString::class.java)
expectedTypes!!.add(Number::class.java) expectedTypes.add(Number::class.java)
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
expectedTypes!!.add(QuoteMark::class.java) expectedTypes.add(QuoteMark::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenFunctionCall_ReturnsCorrectTypes() { fun `types for a function call`() {
val input = "(defun myFunction (x)\n (print x))" val input = "(defun myFunction (x)\n (print x))"
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }
@Test @Test
fun givenBackTickExpression_ReturnsCorrectTypes() { fun `types for a back tick expression`() {
val input = "`(list ,a ,@b)" val input = "`(list ,a ,@b)"
expectedTypes!!.add(Backquote::class.java) expectedTypes.add(Backquote::class.java)
expectedTypes!!.add(LeftParenthesis::class.java) expectedTypes.add(LeftParenthesis::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(Comma::class.java) expectedTypes.add(Comma::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(Comma::class.java) expectedTypes.add(Comma::class.java)
expectedTypes!!.add(AtSign::class.java) expectedTypes.add(AtSign::class.java)
expectedTypes!!.add(Identifier::class.java) expectedTypes.add(Identifier::class.java)
expectedTypes!!.add(RightParenthesis::class.java) expectedTypes.add(RightParenthesis::class.java)
assertTokenTypesMatch(input) assertTokenTypesMatch(input)
} }