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