From 032e1d84cff332cc604b0bd52c8b152026f561e9 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Thu, 4 Nov 2021 15:03:34 +0800 Subject: [PATCH] nac3parser: add and fix tests due to comment support --- nac3parser/src/parser.rs | 63 +++ ...ser__parser__tests__comment_ambiguity.snap | 448 ++++++++++++++++++ ...c3parser__parser__tests__more_comment.snap | 313 ++++++++++++ ...ac3parser__parser__tests__parse_class.snap | 14 +- ...er__parser__tests__parse_if_elif_else.snap | 7 +- ...c3parser__parser__tests__parse_kwargs.snap | 3 +- ...c3parser__parser__tests__parse_lambda.snap | 4 +- ...3parser__parser__tests__parse_print_2.snap | 3 +- ...ser__parser__tests__parse_print_hello.snap | 4 +- ...c3parser__parser__tests__parse_tuples.snap | 3 +- ...parser__parser__tests__sample_comment.snap | 75 +++ 11 files changed, 928 insertions(+), 9 deletions(-) create mode 100644 nac3parser/src/snapshots/nac3parser__parser__tests__comment_ambiguity.snap create mode 100644 nac3parser/src/snapshots/nac3parser__parser__tests__more_comment.snap create mode 100644 nac3parser/src/snapshots/nac3parser__parser__tests__sample_comment.snap diff --git a/nac3parser/src/parser.rs b/nac3parser/src/parser.rs index 3df481b2..825a14c7 100644 --- a/nac3parser/src/parser.rs +++ b/nac3parser/src/parser.rs @@ -164,4 +164,67 @@ class Foo(A, B): let parse_ast = parse_expression(&source).unwrap(); insta::assert_debug_snapshot!(parse_ast); } + + #[test] + fn test_more_comment() { + let source = "\ +a: int # nac3: sf1 +# nac3: sdf4 +for i in (1, '12'): # nac3: sf2 + a: int +# nac3: 3 +# nac3: 5 +while i < 2: # nac3: 4 + # nac3: real pass + pass + # nac3: expr1 + # nac3: expr3 + 1 + 2 # nac3: expr2 + # nac3: if3 + # nac3: if1 + if 1: # nac3: if2 + 3"; + insta::assert_debug_snapshot!(parse_program(&source).unwrap()); + } + + #[test] + fn test_sample_comment() { + let source = "\ +# nac3: while1 +# nac3: while2 +# normal comment +while test: # nac3: while3 + # nac3: simple assign0 + a = 3 # nac3: simple assign1 +"; + insta::assert_debug_snapshot!(parse_program(&source).unwrap()); + } + + #[test] + fn test_comment_ambiguity() { + let source = "\ +if a: d; # nac3: for d +if b: c # nac3: for c +if d: # nac3: for if d + b; b + 3; # nac3: for b + 3 +a = 3; a + 3; b = a; # nac3: notif +# nac3: smallsingle1 +# nac3: smallsingle3 +aa = 3 # nac3: smallsingle2 +if a: # nac3: small2 + a +for i in a: # nac3: for1 + pass +"; + insta::assert_debug_snapshot!(parse_program(&source).unwrap()); + } + + #[test] + fn test_comment_should_fail() { + let source = "\ +if a: # nac3: something +a = 3 +"; + assert!(parse_program(&source).is_err()); + } } diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__comment_ambiguity.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__comment_ambiguity.snap new file mode 100644 index 00000000..e46deac7 --- /dev/null +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__comment_ambiguity.snap @@ -0,0 +1,448 @@ +--- +source: nac3parser/src/parser.rs +expression: parse_program(&source).unwrap() + +--- +[ + Located { + location: Location { + row: 1, + column: 1, + }, + custom: (), + node: If { + test: Located { + location: Location { + row: 1, + column: 4, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 1, + column: 7, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 1, + column: 7, + }, + custom: (), + node: Name { + id: "d", + ctx: Load, + }, + }, + config_comment: [ + "for d", + ], + }, + }, + ], + orelse: [], + config_comment: [], + }, + }, + Located { + location: Location { + row: 2, + column: 1, + }, + custom: (), + node: If { + test: Located { + location: Location { + row: 2, + column: 4, + }, + custom: (), + node: Name { + id: "b", + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 2, + column: 7, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 2, + column: 7, + }, + custom: (), + node: Name { + id: "c", + ctx: Load, + }, + }, + config_comment: [ + "for c", + ], + }, + }, + ], + orelse: [], + config_comment: [], + }, + }, + Located { + location: Location { + row: 3, + column: 1, + }, + custom: (), + node: If { + test: Located { + location: Location { + row: 3, + column: 4, + }, + custom: (), + node: Name { + id: "d", + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 4, + column: 5, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 4, + column: 5, + }, + custom: (), + node: Name { + id: "b", + ctx: Load, + }, + }, + config_comment: [], + }, + }, + Located { + location: Location { + row: 4, + column: 8, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 4, + column: 10, + }, + custom: (), + node: BinOp { + left: Located { + location: Location { + row: 4, + column: 8, + }, + custom: (), + node: Name { + id: "b", + ctx: Load, + }, + }, + op: Add, + right: Located { + location: Location { + row: 4, + column: 12, + }, + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + }, + }, + config_comment: [ + "for b + 3", + ], + }, + }, + ], + orelse: [], + config_comment: [ + "for if d", + ], + }, + }, + Located { + location: Location { + row: 5, + column: 1, + }, + custom: (), + node: Assign { + targets: [ + Located { + location: Location { + row: 5, + column: 1, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + ], + value: Located { + location: Location { + row: 5, + column: 5, + }, + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + type_comment: None, + config_comment: [], + }, + }, + Located { + location: Location { + row: 5, + column: 8, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 5, + column: 10, + }, + custom: (), + node: BinOp { + left: Located { + location: Location { + row: 5, + column: 8, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + op: Add, + right: Located { + location: Location { + row: 5, + column: 12, + }, + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + }, + }, + config_comment: [], + }, + }, + Located { + location: Location { + row: 5, + column: 15, + }, + custom: (), + node: Assign { + targets: [ + Located { + location: Location { + row: 5, + column: 15, + }, + custom: (), + node: Name { + id: "b", + ctx: Load, + }, + }, + ], + value: Located { + location: Location { + row: 5, + column: 19, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + type_comment: None, + config_comment: [ + "notif", + ], + }, + }, + Located { + location: Location { + row: 8, + column: 1, + }, + custom: (), + node: Assign { + targets: [ + Located { + location: Location { + row: 8, + column: 1, + }, + custom: (), + node: Name { + id: "aa", + ctx: Load, + }, + }, + ], + value: Located { + location: Location { + row: 8, + column: 6, + }, + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + type_comment: None, + config_comment: [ + "smallsingle1", + "smallsingle3", + "smallsingle2", + ], + }, + }, + Located { + location: Location { + row: 9, + column: 1, + }, + custom: (), + node: If { + test: Located { + location: Location { + row: 9, + column: 4, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 10, + column: 5, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 10, + column: 5, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + config_comment: [], + }, + }, + ], + orelse: [], + config_comment: [ + "small2", + ], + }, + }, + Located { + location: Location { + row: 11, + column: 1, + }, + custom: (), + node: For { + target: Located { + location: Location { + row: 11, + column: 5, + }, + custom: (), + node: Name { + id: "i", + ctx: Load, + }, + }, + iter: Located { + location: Location { + row: 11, + column: 10, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 12, + column: 5, + }, + custom: (), + node: Pass { + config_comment: [], + }, + }, + ], + orelse: [], + type_comment: None, + config_comment: [ + "for1", + ], + }, + }, +] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__more_comment.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__more_comment.snap new file mode 100644 index 00000000..790bb7f3 --- /dev/null +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__more_comment.snap @@ -0,0 +1,313 @@ +--- +source: nac3parser/src/parser.rs +expression: parse_program(&source).unwrap() + +--- +[ + Located { + location: Location { + row: 1, + column: 1, + }, + custom: (), + node: AnnAssign { + target: Located { + location: Location { + row: 1, + column: 1, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + annotation: Located { + location: Location { + row: 1, + column: 4, + }, + custom: (), + node: Name { + id: "int", + ctx: Load, + }, + }, + value: None, + simple: true, + config_comment: [ + "sf1", + ], + }, + }, + Located { + location: Location { + row: 2, + column: 1, + }, + custom: (), + node: For { + target: Located { + location: Location { + row: 3, + column: 5, + }, + custom: (), + node: Name { + id: "i", + ctx: Load, + }, + }, + iter: Located { + location: Location { + row: 3, + column: 11, + }, + custom: (), + node: Tuple { + elts: [ + Located { + location: Location { + row: 3, + column: 11, + }, + custom: (), + node: Constant { + value: Int( + 1, + ), + kind: None, + }, + }, + Located { + location: Location { + row: 3, + column: 15, + }, + custom: (), + node: Constant { + value: Str( + "12", + ), + kind: None, + }, + }, + ], + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 4, + column: 5, + }, + custom: (), + node: AnnAssign { + target: Located { + location: Location { + row: 4, + column: 5, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + annotation: Located { + location: Location { + row: 4, + column: 8, + }, + custom: (), + node: Name { + id: "int", + ctx: Load, + }, + }, + value: None, + simple: true, + config_comment: [], + }, + }, + ], + orelse: [], + type_comment: None, + config_comment: [ + "sdf4", + "sf2", + ], + }, + }, + Located { + location: Location { + row: 5, + column: 1, + }, + custom: (), + node: While { + test: Located { + location: Location { + row: 7, + column: 9, + }, + custom: (), + node: Compare { + left: Located { + location: Location { + row: 7, + column: 7, + }, + custom: (), + node: Name { + id: "i", + ctx: Load, + }, + }, + ops: [ + Lt, + ], + comparators: [ + Located { + location: Location { + row: 7, + column: 11, + }, + custom: (), + node: Constant { + value: Int( + 2, + ), + kind: None, + }, + }, + ], + }, + }, + body: [ + Located { + location: Location { + row: 9, + column: 5, + }, + custom: (), + node: Pass { + config_comment: [ + "real pass", + ], + }, + }, + Located { + location: Location { + row: 12, + column: 5, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 12, + column: 7, + }, + custom: (), + node: BinOp { + left: Located { + location: Location { + row: 12, + column: 5, + }, + custom: (), + node: Constant { + value: Int( + 1, + ), + kind: None, + }, + }, + op: Add, + right: Located { + location: Location { + row: 12, + column: 9, + }, + custom: (), + node: Constant { + value: Int( + 2, + ), + kind: None, + }, + }, + }, + }, + config_comment: [ + "expr1", + "expr3", + "expr2", + ], + }, + }, + Located { + location: Location { + row: 13, + column: 5, + }, + custom: (), + node: If { + test: Located { + location: Location { + row: 15, + column: 8, + }, + custom: (), + node: Constant { + value: Int( + 1, + ), + kind: None, + }, + }, + body: [ + Located { + location: Location { + row: 16, + column: 9, + }, + custom: (), + node: Expr { + value: Located { + location: Location { + row: 16, + column: 9, + }, + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + config_comment: [], + }, + }, + ], + orelse: [], + config_comment: [ + "if3", + "if1", + "if2", + ], + }, + }, + ], + orelse: [], + config_comment: [ + "3", + "5", + "4", + ], + }, + }, +] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_class.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_class.snap index 869669e9..46501f0c 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_class.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_class.snap @@ -1,6 +1,7 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_program(&source).unwrap() + --- [ Located { @@ -74,12 +75,15 @@ expression: parse_program(&source).unwrap() column: 3, }, custom: (), - node: Pass, + node: Pass { + config_comment: [], + }, }, ], decorator_list: [], returns: None, type_comment: None, + config_comment: [], }, }, Located { @@ -145,16 +149,20 @@ expression: parse_program(&source).unwrap() column: 3, }, custom: (), - node: Pass, + node: Pass { + config_comment: [], + }, }, ], decorator_list: [], returns: None, type_comment: None, + config_comment: [], }, }, ], decorator_list: [], + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_if_elif_else.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_if_elif_else.snap index 4b34c0e4..13f0bca8 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_if_elif_else.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_if_elif_else.snap @@ -1,5 +1,5 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_ast --- @@ -45,6 +45,7 @@ expression: parse_ast kind: None, }, }, + config_comment: [], }, }, ], @@ -90,6 +91,7 @@ expression: parse_ast kind: None, }, }, + config_comment: [], }, }, ], @@ -114,12 +116,15 @@ expression: parse_ast kind: None, }, }, + config_comment: [], }, }, ], + config_comment: [], }, }, ], + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_kwargs.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_kwargs.snap index d31a3921..6dd94aae 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_kwargs.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_kwargs.snap @@ -1,5 +1,5 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_ast --- @@ -73,6 +73,7 @@ expression: parse_ast ], }, }, + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_lambda.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_lambda.snap index 2f70292d..a9aca7e9 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_lambda.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_lambda.snap @@ -1,6 +1,7 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_ast + --- [ Located { @@ -85,6 +86,7 @@ expression: parse_ast }, }, }, + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_2.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_2.snap index 6fe4cad3..92e1c949 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_2.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_2.snap @@ -1,5 +1,5 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_ast --- @@ -60,6 +60,7 @@ expression: parse_ast keywords: [], }, }, + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_hello.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_hello.snap index b4e78d8d..7c9a72ca 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_hello.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_print_hello.snap @@ -1,6 +1,7 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_ast + --- [ Located { @@ -46,6 +47,7 @@ expression: parse_ast keywords: [], }, }, + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_tuples.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_tuples.snap index 26243f56..66f2fdd6 100644 --- a/nac3parser/src/snapshots/nac3parser__parser__tests__parse_tuples.snap +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__parse_tuples.snap @@ -1,5 +1,5 @@ --- -source: parser/src/parser.rs +source: nac3parser/src/parser.rs expression: parse_program(&source).unwrap() --- @@ -86,6 +86,7 @@ expression: parse_program(&source).unwrap() }, }, type_comment: None, + config_comment: [], }, }, ] diff --git a/nac3parser/src/snapshots/nac3parser__parser__tests__sample_comment.snap b/nac3parser/src/snapshots/nac3parser__parser__tests__sample_comment.snap new file mode 100644 index 00000000..8e34a64f --- /dev/null +++ b/nac3parser/src/snapshots/nac3parser__parser__tests__sample_comment.snap @@ -0,0 +1,75 @@ +--- +source: nac3parser/src/parser.rs +expression: parse_program(&source).unwrap() + +--- +[ + Located { + location: Location { + row: 1, + column: 1, + }, + custom: (), + node: While { + test: Located { + location: Location { + row: 4, + column: 7, + }, + custom: (), + node: Name { + id: "test", + ctx: Load, + }, + }, + body: [ + Located { + location: Location { + row: 6, + column: 5, + }, + custom: (), + node: Assign { + targets: [ + Located { + location: Location { + row: 6, + column: 5, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + ], + value: Located { + location: Location { + row: 6, + column: 9, + }, + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + type_comment: None, + config_comment: [ + "simple assign0", + "simple assign1", + ], + }, + }, + ], + orelse: [], + config_comment: [ + "while1", + "while2", + "while3", + ], + }, + }, +]