Debian Codegraph @

src/kj/parse/char-test.c++

raw symbols SCIP
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #include "char.h"
23 #include "../string.h"
24 #include <kj/compat/gtest.h>
25
26 namespace kj {
27 namespace parse {
28 namespace {
29
30 typedef IteratorInput<char, const char*> Input;
31 typedef Span<const char*> TestLocation;
32
33 TEST(CharParsers, ExactChar) {
34 constexpr auto parser = exactChar<'a'>();
35
36 {
37 StringPtr text = "a";
38 Input input(text.begin(), text.end());
39 EXPECT_TRUE(parser(input) != nullptr);
40 EXPECT_TRUE(input.atEnd());
41 }
42
43 {
44 StringPtr text = "b";
45 Input input(text.begin(), text.end());
46 EXPECT_TRUE(parser(input) == nullptr);
47 EXPECT_FALSE(input.atEnd());
48 }
49 }
50
51 TEST(CharParsers, ExactString) {
52 constexpr auto parser = exactString("foo");
53
54 {
55 StringPtr text = "foobar";
56 Input input(text.begin(), text.end());
57 EXPECT_TRUE(parser(input) != nullptr);
58 ASSERT_FALSE(input.atEnd());
59 EXPECT_EQ('b', input.current());
60 }
61
62 {
63 StringPtr text = "bar";
64 Input input(text.begin(), text.end());
65 EXPECT_TRUE(parser(input) == nullptr);
66 EXPECT_FALSE(input.atEnd());
67 EXPECT_EQ('b', input.current());
68 }
69 }
70
71 TEST(CharParsers, CharRange) {
72 constexpr auto parser = charRange('a', 'z');
73
74 {
75 StringPtr text = "a";
76 Input input(text.begin(), text.end());
77 Maybe<char> result = parser(input);
78 KJ_IF_MAYBE(value, result) {
79 EXPECT_EQ('a', *value);
80 } else {
81 ADD_FAILURE() << "Expected parse result, got null.";
82 }
83 EXPECT_TRUE(input.atEnd());
84 }
85
86 {
87 StringPtr text = "n";
88 Input input(text.begin(), text.end());
89 Maybe<char> result = parser(input);
90 KJ_IF_MAYBE(value, result) {
91 EXPECT_EQ('n', *value);
92 } else {
93 ADD_FAILURE() << "Expected parse result, got null.";
94 }
95 EXPECT_TRUE(input.atEnd());
96 }
97
98 {
99 StringPtr text = "z";
100 Input input(text.begin(), text.end());
101 Maybe<char> result = parser(input);
102 KJ_IF_MAYBE(value, result) {
103 EXPECT_EQ('z', *value);
104 } else {
105 ADD_FAILURE() << "Expected parse result, got null.";
106 }
107 EXPECT_TRUE(input.atEnd());
108 }
109
110 {
111 StringPtr text = "`";
112 Input input(text.begin(), text.end());
113 Maybe<char> result = parser(input);
114 EXPECT_TRUE(result == nullptr);
115 EXPECT_FALSE(input.atEnd());
116 }
117
118 {
119 StringPtr text = "{";
120 Input input(text.begin(), text.end());
121 Maybe<char> result = parser(input);
122 EXPECT_TRUE(result == nullptr);
123 EXPECT_FALSE(input.atEnd());
124 }
125
126 {
127 StringPtr text = "A";
128 Input input(text.begin(), text.end());
129 Maybe<char> result = parser(input);
130 EXPECT_TRUE(result == nullptr);
131 EXPECT_FALSE(input.atEnd());
132 }
133 }
134
135 TEST(CharParsers, AnyOfChars) {
136 constexpr auto parser = anyOfChars("axn2B");
137
138 {
139 StringPtr text = "a";
140 Input input(text.begin(), text.end());
141 Maybe<char> result = parser(input);
142 KJ_IF_MAYBE(value, result) {
143 EXPECT_EQ('a', *value);
144 } else {
145 ADD_FAILURE() << "Expected parse result, got null.";
146 }
147 EXPECT_TRUE(input.atEnd());
148 }
149
150 {
151 StringPtr text = "n";
152 Input input(text.begin(), text.end());
153 Maybe<char> result = parser(input);
154 KJ_IF_MAYBE(value, result) {
155 EXPECT_EQ('n', *value);
156 } else {
157 ADD_FAILURE() << "Expected parse result, got null.";
158 }
159 EXPECT_TRUE(input.atEnd());
160 }
161
162 {
163 StringPtr text = "B";
164 Input input(text.begin(), text.end());
165 Maybe<char> result = parser(input);
166 KJ_IF_MAYBE(value, result) {
167 EXPECT_EQ('B', *value);
168 } else {
169 ADD_FAILURE() << "Expected parse result, got null.";
170 }
171 EXPECT_TRUE(input.atEnd());
172 }
173
174 {
175 StringPtr text = "b";
176 Input input(text.begin(), text.end());
177 Maybe<char> result = parser(input);
178 EXPECT_TRUE(result == nullptr);
179 EXPECT_FALSE(input.atEnd());
180 }
181
182 {
183 StringPtr text = "j";
184 Input input(text.begin(), text.end());
185 Maybe<char> result = parser(input);
186 EXPECT_TRUE(result == nullptr);
187 EXPECT_FALSE(input.atEnd());
188 }
189
190 {
191 StringPtr text = "A";
192 Input input(text.begin(), text.end());
193 Maybe<char> result = parser(input);
194 EXPECT_TRUE(result == nullptr);
195 EXPECT_FALSE(input.atEnd());
196 }
197 }
198
199 TEST(CharParsers, CharGroupCombo) {
200 constexpr auto parser =
201 many(charRange('0', '9').orRange('a', 'z').orRange('A', 'Z').orAny("-_"));
202
203 {
204 StringPtr text = "foo1-bar2_baz3@qux";
205 Input input(text.begin(), text.end());
206 Maybe<Array<char>> result = parser(input);
207 KJ_IF_MAYBE(value, result) {
208 EXPECT_EQ("foo1-bar2_baz3", str(*value));
209 } else {
210 ADD_FAILURE() << "Expected parse result, got null.";
211 }
212 EXPECT_FALSE(input.atEnd());
213 }
214 }
215
216 TEST(CharParsers, Identifier) {
217 constexpr auto parser = identifier;
218
219 {
220 StringPtr text = "helloWorld123 ";
221 Input input(text.begin(), text.end());
222 Maybe<String> result = parser(input);
223 KJ_IF_MAYBE(value, result) {
224 EXPECT_EQ("helloWorld123", *value);
225 } else {
226 ADD_FAILURE() << "Expected string, got null.";
227 }
228 EXPECT_FALSE(input.atEnd());
229 }
230 }
231
232 TEST(CharParsers, Integer) {
233 constexpr auto parser = integer;
234
235 {
236 StringPtr text = "12349";
237 Input input(text.begin(), text.end());
238 Maybe<uint64_t> result = parser(input);
239 KJ_IF_MAYBE(value, result) {
240 EXPECT_EQ(12349u, *value);
241 } else {
242 ADD_FAILURE() << "Expected integer, got null.";
243 }
244 EXPECT_TRUE(input.atEnd());
245 }
246
247 {
248 StringPtr text = "0x1aF0";
249 Input input(text.begin(), text.end());
250 Maybe<uint64_t> result = parser(input);
251 KJ_IF_MAYBE(value, result) {
252 EXPECT_EQ(0x1aF0u, *value);
253 } else {
254 ADD_FAILURE() << "Expected integer, got null.";
255 }
256 EXPECT_TRUE(input.atEnd());
257 }
258
259 {
260 StringPtr text = "064270";
261 Input input(text.begin(), text.end());
262 Maybe<uint64_t> result = parser(input);
263 KJ_IF_MAYBE(value, result) {
264 EXPECT_EQ(064270u, *value);
265 } else {
266 ADD_FAILURE() << "Expected integer, got null.";
267 }
268 EXPECT_TRUE(input.atEnd());
269 }
270 }
271
272 TEST(CharParsers, Number) {
273 constexpr auto parser = number;
274
275 {
276 StringPtr text = "12345";
277 Input input(text.begin(), text.end());
278 Maybe<double> result = parser(input);
279 KJ_IF_MAYBE(value, result) {
280 EXPECT_EQ(12345, *value);
281 } else {
282 ADD_FAILURE() << "Expected number, got null.";
283 }
284 EXPECT_TRUE(input.atEnd());
285 }
286
287 {
288 StringPtr text = "123.25";
289 Input input(text.begin(), text.end());
290 Maybe<double> result = parser(input);
291 KJ_IF_MAYBE(value, result) {
292 EXPECT_EQ(123.25, *value);
293 } else {
294 ADD_FAILURE() << "Expected number, got null.";
295 }
296 EXPECT_TRUE(input.atEnd());
297 }
298
299 {
300 StringPtr text = "123e10";
301 Input input(text.begin(), text.end());
302 Maybe<double> result = parser(input);
303 KJ_IF_MAYBE(value, result) {
304 EXPECT_EQ(123e10, *value);
305 } else {
306 ADD_FAILURE() << "Expected number, got null.";
307 }
308 EXPECT_TRUE(input.atEnd());
309 }
310
311 {
312 StringPtr text = "123.25E+10";
313 Input input(text.begin(), text.end());
314 Maybe<double> result = parser(input);
315 KJ_IF_MAYBE(value, result) {
316 EXPECT_EQ(123.25E+10, *value);
317 } else {
318 ADD_FAILURE() << "Expected number, got null.";
319 }
320 EXPECT_TRUE(input.atEnd());
321 }
322
323 {
324 StringPtr text = "25e-2";
325 Input input(text.begin(), text.end());
326 Maybe<double> result = parser(input);
327 KJ_IF_MAYBE(value, result) {
328 EXPECT_EQ(25e-2, *value);
329 } else {
330 ADD_FAILURE() << "Expected number, got null.";
331 }
332 EXPECT_TRUE(input.atEnd());
333 }
334 }
335
336 TEST(CharParsers, DoubleQuotedString) {
337 constexpr auto parser = doubleQuotedString;
338
339 {
340 StringPtr text = "\"hello\"";
341 Input input(text.begin(), text.end());
342 Maybe<String> result = parser(input);
343 KJ_IF_MAYBE(value, result) {
344 EXPECT_EQ("hello", *value);
345 } else {
346 ADD_FAILURE() << "Expected \"hello\", got null.";
347 }
348 EXPECT_TRUE(input.atEnd());
349 }
350
351 {
352 StringPtr text = "\"test\\a\\b\\f\\n\\r\\t\\v\\\'\\\"\\\?\\x01\\x20\\2\\34\\156\"";
353 Input input(text.begin(), text.end());
354 Maybe<String> result = parser(input);
355 KJ_IF_MAYBE(value, result) {
356 EXPECT_EQ("test\a\b\f\n\r\t\v\'\"\?\x01\x20\2\34\156", *value);
357 } else {
358 ADD_FAILURE() << "Expected string, got null.";
359 }
360 EXPECT_TRUE(input.atEnd());
361 }
362
363 {
364 StringPtr text = "\"foo'bar\"";
365 Input input(text.begin(), text.end());
366 Maybe<String> result = parser(input);
367 KJ_IF_MAYBE(value, result) {
368 EXPECT_EQ("foo'bar", *value);
369 } else {
370 ADD_FAILURE() << "Expected string, got null.";
371 }
372 EXPECT_TRUE(input.atEnd());
373 }
374 }
375
376 TEST(CharParsers, SingleQuotedString) {
377 constexpr auto parser = singleQuotedString;
378
379 {
380 StringPtr text = "\'hello\'";
381 Input input(text.begin(), text.end());
382 Maybe<String> result = parser(input);
383 KJ_IF_MAYBE(value, result) {
384 EXPECT_EQ("hello", *value);
385 } else {
386 ADD_FAILURE() << "Expected \"hello\", got null.";
387 }
388 EXPECT_TRUE(input.atEnd());
389 }
390
391 {
392 StringPtr text = "\'test\\a\\b\\f\\n\\r\\t\\v\\\'\\\"\\\?\x01\2\34\156\'";
393 Input input(text.begin(), text.end());
394 Maybe<String> result = parser(input);
395 KJ_IF_MAYBE(value, result) {
396 EXPECT_EQ("test\a\b\f\n\r\t\v\'\"\?\x01\2\34\156", *value);
397 } else {
398 ADD_FAILURE() << "Expected string, got null.";
399 }
400 EXPECT_TRUE(input.atEnd());
401 }
402
403 {
404 StringPtr text = "\'foo\"bar\'";
405 Input input(text.begin(), text.end());
406 Maybe<String> result = parser(input);
407 KJ_IF_MAYBE(value, result) {
408 EXPECT_EQ("foo\"bar", *value);
409 } else {
410 ADD_FAILURE() << "Expected string, got null.";
411 }
412 EXPECT_TRUE(input.atEnd());
413 }
414 }
415
416 } // namespace
417 } // namespace parse
418 } // namespace kj