Debian Codegraph @

token_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqlparser
18
19 import (
20 "fmt"
21 "testing"
22 )
23
24 func TestLiteralID(t *testing.T) {
25 testcases := []struct {
26 in string
27 id int
28 out string
29 }{{
30 in: "`aa`",
31 id: ID,
32 out: "aa",
33 }, {
34 in: "```a```",
35 id: ID,
36 out: "`a`",
37 }, {
38 in: "`a``b`",
39 id: ID,
40 out: "a`b",
41 }, {
42 in: "`a``b`c",
43 id: ID,
44 out: "a`b",
45 }, {
46 in: "`a``b",
47 id: LEX_ERROR,
48 out: "a`b",
49 }, {
50 in: "`a``b``",
51 id: LEX_ERROR,
52 out: "a`b`",
53 }, {
54 in: "``",
55 id: LEX_ERROR,
56 out: "",
57 }}
58
59 for _, tcase := range testcases {
60 tkn := NewStringTokenizer(tcase.in)
61 id, out := tkn.Scan()
62 if tcase.id != id || string(out) != tcase.out {
63 t.Errorf("Scan(%s): %d, %s, want %d, %s", tcase.in, id, out, tcase.id, tcase.out)
64 }
65 }
66 }
67
68 func tokenName(id int) string {
69 if id == STRING {
70 return "STRING"
71 } else if id == LEX_ERROR {
72 return "LEX_ERROR"
73 }
74 return fmt.Sprintf("%d", id)
75 }
76
77 func TestString(t *testing.T) {
78 testcases := []struct {
79 in string
80 id int
81 want string
82 }{{
83 in: "''",
84 id: STRING,
85 want: "",
86 }, {
87 in: "''''",
88 id: STRING,
89 want: "'",
90 }, {
91 in: "'hello'",
92 id: STRING,
93 want: "hello",
94 }, {
95 in: "'\\n'",
96 id: STRING,
97 want: "\n",
98 }, {
99 in: "'\\nhello\\n'",
100 id: STRING,
101 want: "\nhello\n",
102 }, {
103 in: "'a''b'",
104 id: STRING,
105 want: "a'b",
106 }, {
107 in: "'a\\'b'",
108 id: STRING,
109 want: "a'b",
110 }, {
111 in: "'\\'",
112 id: LEX_ERROR,
113 want: "'",
114 }, {
115 in: "'",
116 id: LEX_ERROR,
117 want: "",
118 }, {
119 in: "'hello\\'",
120 id: LEX_ERROR,
121 want: "hello'",
122 }, {
123 in: "'hello",
124 id: LEX_ERROR,
125 want: "hello",
126 }, {
127 in: "'hello\\",
128 id: LEX_ERROR,
129 want: "hello",
130 }}
131
132 for _, tcase := range testcases {
133 id, got := NewStringTokenizer(tcase.in).Scan()
134 if tcase.id != id || string(got) != tcase.want {
135 t.Errorf("Scan(%q) = (%s, %q), want (%s, %q)", tcase.in, tokenName(id), got, tokenName(tcase.id), tcase.want)
136 }
137 }
138 }
139
140 func TestSplitStatement(t *testing.T) {
141 testcases := []struct {
142 in string
143 sql string
144 rem string
145 }{{
146 in: "select * from table",
147 sql: "select * from table",
148 }, {
149 in: "select * from table; ",
150 sql: "select * from table",
151 rem: " ",
152 }, {
153 in: "select * from table; select * from table2;",
154 sql: "select * from table",
155 rem: " select * from table2;",
156 }, {
157 in: "select * from /* comment */ table;",
158 sql: "select * from /* comment */ table",
159 }, {
160 in: "select * from /* comment ; */ table;",
161 sql: "select * from /* comment ; */ table",
162 }, {
163 in: "select * from table where semi = ';';",
164 sql: "select * from table where semi = ';'",
165 }, {
166 in: "-- select * from table",
167 sql: "-- select * from table",
168 }, {
169 in: " ",
170 sql: " ",
171 }, {
172 in: "",
173 sql: "",
174 }}
175
176 for _, tcase := range testcases {
177 sql, rem, err := SplitStatement(tcase.in)
178 if err != nil {
179 t.Errorf("EndOfStatementPosition(%s): ERROR: %v", tcase.in, err)
180 continue
181 }
182
183 if tcase.sql != sql {
184 t.Errorf("EndOfStatementPosition(%s) got sql \"%s\" want \"%s\"", tcase.in, sql, tcase.sql)
185 }
186
187 if tcase.rem != rem {
188 t.Errorf("EndOfStatementPosition(%s) got remainder \"%s\" want \"%s\"", tcase.in, rem, tcase.rem)
189 }
190 }
191 }