This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
scanner_test.go
111 lines (103 loc) · 3.95 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cypher_test
import (
"reflect"
"strings"
"testing"
"github.com/rafaelcaricio/cypher-parser"
)
func TestScanInput(t *testing.T) {
for _, tc := range []struct {
in string
tok cypher.Token
lit string
}{
{in: `something`, tok: cypher.IDENT, lit: "something"},
{in: `desc`, tok: cypher.DESC, lit: ""},
{in: `match`, tok: cypher.MATCH, lit: ""},
{in: `or`, tok: cypher.OR, lit: ""},
{in: `1233`, tok: cypher.INTEGER, lit: "1233"},
{in: `3.14`, tok: cypher.NUMBER, lit: "3.14"},
{in: `true`, tok: cypher.TRUE, lit: ""},
{in: `null`, tok: cypher.NULL, lit: ""},
{in: `"Hello, world!"`, tok: cypher.STRING, lit: "Hello, world!"},
{in: `"String\nwith\nnewline"`, tok: cypher.STRING, lit: "String\nwith\nnewline"},
{in: `'String\n'`, tok: cypher.STRING, lit: "String\n"},
{in: ``, tok: cypher.EOF, lit: ""},
{in: `<>`, tok: cypher.NEQ, lit: ""},
{in: `<`, tok: cypher.LT, lit: ""},
{in: `<=1`, tok: cypher.LTE, lit: ""},
{in: `>`, tok: cypher.GT, lit: ""},
{in: `>=`, tok: cypher.GTE, lit: ""},
{in: `..`, tok: cypher.DOUBLEDOT, lit: ""},
{in: `+`, tok: cypher.PLUS, lit: ""},
{in: `+=`, tok: cypher.INC, lit: ""},
{in: `//nice try`, tok: cypher.COMMENT, lit: ""},
{in: `/*nice another\n try*/`, tok: cypher.COMMENT, lit: ""},
{in: `/`, tok: cypher.DIV, lit: ""},
{in: ` `, tok: cypher.WS, lit: " "},
{in: `[`, tok: cypher.LBRACKET, lit: ""},
{in: "`nice`", tok: cypher.IDENT, lit: "nice"},
{in: "`true`", tok: cypher.IDENT, lit: "true"},
} {
s := cypher.NewScanner(strings.NewReader(tc.in))
tok, _, lit := s.Scan()
if tok != tc.tok {
t.Errorf("For input `%s` expected token '%s' got '%s' (%s)", tc.in, tc.tok, tok, lit)
} else if lit != tc.lit {
t.Errorf("Expected literal '%s' got '%s'", tc.in, lit)
}
}
}
func TestScanMultiple(t *testing.T) {
type result struct {
tok cypher.Token
pos cypher.Pos
lit string
}
exp := []result{
{tok: cypher.MATCH, pos: cypher.Pos{Line: 0, Char: 0}, lit: ""},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 5}, lit: " "},
{tok: cypher.LPAREN, pos: cypher.Pos{Line: 0, Char: 6}, lit: ""},
{tok: cypher.IDENT, pos: cypher.Pos{Line: 0, Char: 7}, lit: "n"},
{tok: cypher.COLON, pos: cypher.Pos{Line: 0, Char: 8}, lit: ""},
{tok: cypher.IDENT, pos: cypher.Pos{Line: 0, Char: 9}, lit: "Person"},
{tok: cypher.RPAREN, pos: cypher.Pos{Line: 0, Char: 15}, lit: ""},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 16}, lit: " "},
{tok: cypher.WHERE, pos: cypher.Pos{Line: 0, Char: 17}, lit: ""},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 22}, lit: " "},
{tok: cypher.IDENT, pos: cypher.Pos{Line: 0, Char: 23}, lit: "n"},
{tok: cypher.DOT, pos: cypher.Pos{Line: 0, Char: 24}, lit: ""},
{tok: cypher.IDENT, pos: cypher.Pos{Line: 0, Char: 25}, lit: "name"},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 29}, lit: " "},
{tok: cypher.EQ, pos: cypher.Pos{Line: 0, Char: 30}, lit: ""},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 31}, lit: " "},
{tok: cypher.STRING, pos: cypher.Pos{Line: 0, Char: 31}, lit: "Rafael"},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 40}, lit: " "},
{tok: cypher.RETURN, pos: cypher.Pos{Line: 0, Char: 41}, lit: ""},
{tok: cypher.WS, pos: cypher.Pos{Line: 0, Char: 47}, lit: " "},
{tok: cypher.IDENT, pos: cypher.Pos{Line: 0, Char: 48}, lit: "n"},
{tok: cypher.EOF, pos: cypher.Pos{Line: 0, Char: 50}, lit: ""},
}
// Create a scanner.
v := `MATCH (n:Person) WHERE n.name = "Rafael" RETURN n`
s := cypher.NewScanner(strings.NewReader(v))
// Continually scan until we reach the end.
var act []result
for {
tok, pos, lit := s.Scan()
act = append(act, result{tok, pos, lit})
if tok == cypher.EOF {
break
}
}
// Verify the token counts match.
if len(exp) != len(act) {
t.Fatalf("token count mismatch: exp=%d, got=%d", len(exp), len(act))
}
// Verify each token matches.
for i := range exp {
if !reflect.DeepEqual(exp[i], act[i]) {
t.Fatalf("%d. token mismatch:\n\nexp=%#v\n\ngot=%#v", i, exp[i], act[i])
}
}
}