string escapes
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-19 20:39:55 -04:00
parent be81b9a6d6
commit e320cd1e68
2 changed files with 83 additions and 4 deletions

View File

@@ -158,3 +158,48 @@ func TestNextToken(t *testing.T) {
}
}
}
func TestStringEscapes(t *testing.T) {
input := `#!./monkey-lang
let a = "\"foo\""
let b = "\x00\x0a\x7f"
let c = "\r\n\t"
`
tests := []struct {
expectedType token.TokenType
expectedLiteral string
}{
{token.COMMENT, "!./monkey-lang"},
{token.LET, "let"},
{token.IDENT, "a"},
{token.ASSIGN, "="},
{token.STRING, "\"foo\""},
{token.LET, "let"},
{token.IDENT, "b"},
{token.ASSIGN, "="},
{token.STRING, "\x00\n\u007f"},
{token.LET, "let"},
{token.IDENT, "c"},
{token.ASSIGN, "="},
{token.STRING, "\r\n\t"},
{token.EOF, ""},
}
lexer := New(input)
for i, test := range tests {
token := lexer.NextToken()
if token.Type != test.expectedType {
t.Fatalf("tests[%d] - token type wrong. expected=%q, got=%q",
i, test.expectedType, token.Type)
}
if token.Literal != test.expectedLiteral {
t.Fatalf("tests[%d] - literal wrong. expected=%q, got=%q",
i, test.expectedLiteral, token.Literal)
}
}
}