Skip to content

Commit 5af0dbc

Browse files
committed
chore(parser): add tests
1 parent ea5bd85 commit 5af0dbc

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

parser/src/tests.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ fn test_parser_valid_patterns() {
153153
awk;
154154
1 + 1 \n { print }
155155
{ print }
156+
a in arr { print }
156157
";
157158
const BODY: &str = "(body (Print))";
158159
test_parser!(source => {
@@ -171,6 +172,7 @@ fn test_parser_valid_patterns() {
171172
(Some("(Add 1 1)"), None),
172173
(None, Some(BODY)),
173174
(None, Some(BODY)),
175+
(Some("(In awk::arr awk::a)"), Some("(body (Print))")),
174176
],
175177
});
176178
}
@@ -331,3 +333,105 @@ fn test_parser_for_loop() {
331333

332334
test_parser!(is_err!("{ for(x in array; a; b) {} }"));
333335
}
336+
337+
#[test]
338+
fn test_parser_logical_operators() {
339+
let source = r"
340+
{ a && b && c == 3 }
341+
{ a || b > 2 || c }
342+
{ 1 ~ /a/ || b && c }
343+
{ !a }
344+
{ !(a && b) }
345+
";
346+
test_parser!(source => {
347+
rules: [
348+
(None, Some("(body (And (And awk::a awk::b) (Eq awk::c 3)))")),
349+
(None, Some("(body (Or (Or awk::a (Gt awk::b 2)) awk::c))")),
350+
(None, Some("(body (Or (Matches 1 /a/) (And awk::b awk::c)))")),
351+
(None, Some("(body (Negation awk::a))")),
352+
(None, Some("(body (Negation (And awk::a awk::b)))")),
353+
],
354+
});
355+
}
356+
357+
#[test]
358+
fn test_parser_delete() {
359+
let source = r"
360+
{ delete arr[k] }
361+
{ delete arr[i, j] }
362+
{ delete arr }
363+
";
364+
test_parser!(source => {
365+
rules: [
366+
(None, Some("(body (delete (Index awk::arr awk::k)))")),
367+
(None, Some("(body (delete (Index awk::arr awk::i awk::j)))")),
368+
(None, Some("(body (delete awk::arr))")),
369+
],
370+
});
371+
}
372+
373+
#[test]
374+
fn test_parser_if() {
375+
let source = r"
376+
{ if (a) print }
377+
{ if (k in arr) print; else if (x) print; else print; }
378+
{ if (x == 1 && 2) print 1; else print 0 }
379+
";
380+
test_parser!(source => {
381+
rules: [
382+
(None, Some("(body (if awk::a (body (Print))))")),
383+
(
384+
None,
385+
Some("(body (if (In awk::arr awk::k) (body (Print)) (else (body (if awk::x \
386+
(body (Print)) (else (body (Print))))))))")
387+
),
388+
(
389+
None,
390+
Some("(body (if (And (Eq awk::x 1) 2) (body (Print 1)) (else (body (Print 0)))))")
391+
),
392+
],
393+
});
394+
}
395+
396+
#[test]
397+
fn test_parser_dangling_else() {
398+
let source = "{ if (a) if (b) print 1; else print 2 }";
399+
test_parser!(source => {
400+
rules: [
401+
(None, Some(
402+
"(body (if awk::a (body (if awk::b (body (Print 1)) (else (body (Print 2)))))))"
403+
)),
404+
],
405+
});
406+
}
407+
408+
#[test]
409+
fn test_parser_while() {
410+
let source = r"
411+
{ while (a < 10) a++ }
412+
{ while (1) { print a; if (a > 5) break } }
413+
";
414+
test_parser!(source => {
415+
rules: [
416+
(None, Some("(body (while (Lt awk::a 10) (body (IncrementR awk::a))))")),
417+
(None, Some(concat!(
418+
"(body (while 1 ",
419+
"(body (Print awk::a) (if (Gt awk::a 5) (body (break))))))"
420+
))),
421+
],
422+
});
423+
}
424+
425+
#[test]
426+
fn test_parser_do_while() {
427+
let source = r"
428+
{ do { a++ } while (a < 10) }
429+
{ do { print; break } while (a--) }
430+
";
431+
test_parser!(source => {
432+
rules: [
433+
(None, Some("(body (do-while (body (IncrementR awk::a)) (Lt awk::a 10)))")),
434+
(None, Some("(body (do-while (body (Print) (break)) (DecrementR awk::a)))")),
435+
],
436+
});
437+
}

0 commit comments

Comments
 (0)