-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
90 lines (83 loc) · 2.34 KB
/
scanner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package lox_test
import (
"testing"
"github.com/brunokim/kilox"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestScanner(t *testing.T) {
tests := []struct {
text string
want []lox.Token
}{
{"a", []lox.Token{token(lox.Identifier, "a"), token(lox.EOF, "")}},
{" a", []lox.Token{token(lox.Identifier, "a"), token(lox.EOF, "")}},
{" a ", []lox.Token{token(lox.Identifier, "a"), token(lox.EOF, "")}},
{"a + 1", []lox.Token{
token(lox.Identifier, "a"),
token(lox.Plus, "+"),
literalToken(lox.Number, "1", 1.0),
token(lox.EOF, "")}},
{"!(x and false)", []lox.Token{
token(lox.Bang, "!"),
token(lox.LeftParen, "("),
token(lox.Identifier, "x"),
token(lox.And, "and"),
token(lox.False, "false"),
token(lox.RightParen, ")"),
token(lox.EOF, ""),
}},
{`"abc \" def \\ ghi"`, []lox.Token{
literalToken(lox.String, `"abc \" def \\ ghi"`, `abc " def \ ghi`),
token(lox.EOF, ""),
}},
{"\"abc\ndef\"", []lox.Token{
literalToken(lox.String, "\"abc\ndef\"", "abc\ndef"),
token(lox.EOF, ""),
}},
}
for _, test := range tests {
t.Run(test.text, func(t *testing.T) {
s := lox.NewScanner(test.text)
tokens, err := s.ScanTokens()
if err != nil {
t.Fatalf("want nil, got err: %v", err)
}
opts := cmp.Options{cmpopts.IgnoreFields(lox.Token{}, "Line")}
if d := cmp.Diff(test.want, tokens, opts); d != "" {
t.Errorf("(-want, +got)%s", d)
}
})
}
}
func TestScannerError(t *testing.T) {
tests := []struct {
text string
want string
}{
{"a % b", "line 1: unexpected character: %"},
{"a \n% \nb", "line 2: unexpected character: %"},
{`"unterminated`, "line 1: unterminated string"},
{`"unterminated`, "line 1: unterminated string"},
{`"unterminated
`, "line 3: unterminated string"},
{`"invalid \n escape"`, "line 1: invalid escaped character 'n' in string"},
{`a ^ b
"str\b
`, `line 1: unexpected character: ^
line 2: invalid escaped character 'b' in string
line 3: unterminated string`},
}
for _, test := range tests {
t.Run(test.text, func(t *testing.T) {
s := lox.NewScanner(test.text)
tokens, err := s.ScanTokens()
if err == nil {
t.Fatalf("want err, got tokens %v for text %q", tokens, test.text)
}
if d := cmp.Diff(test.want, err.Error()); d != "" {
t.Errorf("(-want, +got)%s", d)
}
})
}
}