More tests for the Tokenizer

This commit is contained in:
Pat Garrity 2024-02-24 11:26:12 -06:00
parent 64a0e8bd92
commit cd98dc0342
Signed by: pfm
GPG key ID: 5CA5D21BAB7F3A76

View file

@ -109,6 +109,43 @@ class TokenizerTests extends munit.FunSuite:
)
}
test("should capture all supported char escapes in a string literal") {
assertTokens(
"\"\\n\\r\\b\\f\\t\\\\\\\"\\\'\"",
Right(Token.StringLiteral("\n\r\b\f\t\\\"\'", Nil))
)
}
test("should capture all supported char escapes in character literals") {
assertTokens("\'\\n\'", Right(Token.CharacterLiteral('\n', Nil)))
assertTokens("\'\\r\'", Right(Token.CharacterLiteral('\r', Nil)))
assertTokens("\'\\b\'", Right(Token.CharacterLiteral('\b', Nil)))
assertTokens("\'\\f\'", Right(Token.CharacterLiteral('\f', Nil)))
assertTokens("\'\\t\'", Right(Token.CharacterLiteral('\t', Nil)))
assertTokens("\'\\\\\'", Right(Token.CharacterLiteral('\\', Nil)))
assertTokens("\'\\\"\'", Right(Token.CharacterLiteral('"', Nil)))
assertTokens("\'\\'\'", Right(Token.CharacterLiteral('\'', Nil)))
}
test("should handle a basic string literal") {
assertTokens("\"foo\"", Right(Token.StringLiteral("foo", Nil)))
}
test("should handle a string literal with an invalid character escape") {
// Note: The character escape is simply not emitted, but we are able to
// successfully close out the string, which allows us to recover and parse
// more content.
assertTokens(
"\"foo\\!bar\"",
Right(
Token.StringLiteral(
"foobar",
List(Tokenizer.Error.InvalidCharEscape(pos(6, 1, 6), '!'))
)
)
)
}
private def assertTokens(
source: String,
expectedOutput: Either[Tokenizer.Error, Token]*