Skip to content

Commit

Permalink
test: move parser ast json unit tests to snaptest
Browse files Browse the repository at this point in the history
Signed-off-by: jakezhu9 <[email protected]>
  • Loading branch information
jakezhu9 committed Oct 20, 2023
1 parent 1119a7e commit 4876dce
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 111 deletions.
111 changes: 0 additions & 111 deletions kclvm/parser/src/parser/tests.rs

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::*;

use core::any::Any;

mod ast;
mod error_recovery;
mod expr;
mod types;
Expand Down Expand Up @@ -54,6 +55,16 @@ macro_rules! parse_type_node_snapshot {
};
}

#[macro_export]
macro_rules! parse_file_ast_json_snapshot {
($name:ident, $filename:expr, $src:expr) => {
#[test]
fn $name() {
insta::assert_snapshot!($crate::tests::parsing_file_ast_json($filename, $src));
}
};
}

pub(crate) fn parsing_expr_string(src: &str) -> String {
let sm = SourceMap::new(FilePathMapping::empty());
let sf = sm.new_source_file(PathBuf::from("").into(), src.to_string());
Expand Down Expand Up @@ -112,6 +123,16 @@ pub(crate) fn parsing_type_node_string(src: &str) -> String {
})
}

pub(crate) fn parsing_file_ast_json(filename: &str, src: &str) -> String {
let m = crate::parse_file_with_global_session(
Arc::new(ParseSession::default()),
filename,
Some(src.into()),
)
.unwrap();
serde_json::ser::to_string(&m).unwrap()
}

pub fn check_result_panic_info(result: Result<(), Box<dyn Any + Send>>) {
if let Err(e) = result {
assert!(e.downcast::<String>().is_ok());
Expand Down
70 changes: 70 additions & 0 deletions kclvm/parser/src/tests/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::tests::parse_file_ast_json_snapshot;

parse_file_ast_json_snapshot!(
schema_stmt,
"hello.k",
r####"
schema TestBool:
[]
[str ]: int
[a: str]: int
[a: ...str]: int
[...str]: int
a: int
b?: str
c: int = 0
d?: str = ""
[a]
[a, b, c]
[
1
]
[
a
]
[a for a in [1, 2, 3]]
[
a for a in [1, 2, 3]
]
check:
a > 1, "msg"
name not None, "we fail here"
"####
);
parse_file_ast_json_snapshot!(assign_stmt, "hello.k", r####"a=123"####);
parse_file_ast_json_snapshot!(
if_stmt_0,
"hello.k",
r####"
a = 10
b = 12
_condition = 0
if a == 11 or b == 13: _condition = 1
elif a == 10 and b == 12: _condition = 2
condition = _condition
"####
);
parse_file_ast_json_snapshot!(
if_stmt_1,
"hello.k",
r####"
data2 = {
**{key = "value1"}
if a == 123: if b == 456: key = "value2"
}
"####
);
parse_file_ast_json_snapshot!(
basic_stmt,
"hello.k",
r####"
# comment1
a = 1
# comment22
b = 2
# comment333
c = 3 # comment4444
"####
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/ast.rs
expression: "crate::tests::parsing_file_ast_json(\"hello.k\", r####\"a=123\"####)"
---
{"filename":"hello.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":123}}},"filename":"hello.k","line":1,"column":2,"end_line":1,"end_column":5},"type_annotation":null,"ty":null}},"filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":5}],"comments":[]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/ast.rs
expression: "crate::tests::parsing_file_ast_json(\"hello.k\",\n r####\"\n# comment1\na = 1\n# comment22\nb = 2\n# comment333\nc = 3 # comment4444\n \"####)"
---
{"filename":"hello.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"hello.k","line":3,"column":0,"end_line":3,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":3,"column":0,"end_line":3,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"hello.k","line":3,"column":4,"end_line":3,"end_column":5},"type_annotation":null,"ty":null}},"filename":"hello.k","line":3,"column":0,"end_line":3,"end_column":5},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"b","filename":"hello.k","line":5,"column":0,"end_line":5,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":5,"column":0,"end_line":5,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":2}}},"filename":"hello.k","line":5,"column":4,"end_line":5,"end_column":5},"type_annotation":null,"ty":null}},"filename":"hello.k","line":5,"column":0,"end_line":5,"end_column":5},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"c","filename":"hello.k","line":7,"column":0,"end_line":7,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":7,"column":0,"end_line":7,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":3}}},"filename":"hello.k","line":7,"column":4,"end_line":7,"end_column":5},"type_annotation":null,"ty":null}},"filename":"hello.k","line":7,"column":0,"end_line":7,"end_column":5}],"comments":[{"node":{"text":"# comment1"},"filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":10},{"node":{"text":"# comment22"},"filename":"hello.k","line":4,"column":0,"end_line":4,"end_column":11},{"node":{"text":"# comment333"},"filename":"hello.k","line":6,"column":0,"end_line":6,"end_column":12},{"node":{"text":"# comment4444"},"filename":"hello.k","line":7,"column":6,"end_line":7,"end_column":19}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/ast.rs
expression: "crate::tests::parsing_file_ast_json(\"hello.k\",\n r####\"\na = 10\nb = 12\n_condition = 0\nif a == 11 or b == 13: _condition = 1\nelif a == 10 and b == 12: _condition = 2\ncondition = _condition\n \"####)"
---
{"filename":"hello.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":10}}},"filename":"hello.k","line":2,"column":4,"end_line":2,"end_column":6},"type_annotation":null,"ty":null}},"filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":6},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"b","filename":"hello.k","line":3,"column":0,"end_line":3,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":3,"column":0,"end_line":3,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":12}}},"filename":"hello.k","line":3,"column":4,"end_line":3,"end_column":6},"type_annotation":null,"ty":null}},"filename":"hello.k","line":3,"column":0,"end_line":3,"end_column":6},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_condition","filename":"hello.k","line":4,"column":0,"end_line":4,"end_column":10}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":4,"column":0,"end_line":4,"end_column":10}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":0}}},"filename":"hello.k","line":4,"column":13,"end_line":4,"end_column":14},"type_annotation":null,"ty":null}},"filename":"hello.k","line":4,"column":0,"end_line":4,"end_column":14},{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_condition","filename":"hello.k","line":5,"column":23,"end_line":5,"end_column":33}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":5,"column":23,"end_line":5,"end_column":33}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"hello.k","line":5,"column":36,"end_line":5,"end_column":37},"type_annotation":null,"ty":null}},"filename":"hello.k","line":5,"column":23,"end_line":5,"end_column":37}],"cond":{"node":{"Binary":{"left":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"hello.k","line":5,"column":3,"end_line":5,"end_column":4}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":5,"column":3,"end_line":5,"end_column":4},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":11}}},"filename":"hello.k","line":5,"column":8,"end_line":5,"end_column":10}]}},"filename":"hello.k","line":5,"column":3,"end_line":5,"end_column":21},"op":{"Bin":"Or"},"right":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"b","filename":"hello.k","line":5,"column":14,"end_line":5,"end_column":15}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":5,"column":14,"end_line":5,"end_column":15},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":13}}},"filename":"hello.k","line":5,"column":19,"end_line":5,"end_column":21}]}},"filename":"hello.k","line":5,"column":14,"end_line":5,"end_column":21}}},"filename":"hello.k","line":5,"column":3,"end_line":5,"end_column":21},"orelse":[{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_condition","filename":"hello.k","line":6,"column":26,"end_line":6,"end_column":36}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":6,"column":26,"end_line":6,"end_column":36}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":2}}},"filename":"hello.k","line":6,"column":39,"end_line":6,"end_column":40},"type_annotation":null,"ty":null}},"filename":"hello.k","line":6,"column":26,"end_line":6,"end_column":40}],"cond":{"node":{"Binary":{"left":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"hello.k","line":6,"column":5,"end_line":6,"end_column":6}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":6,"column":5,"end_line":6,"end_column":6},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":10}}},"filename":"hello.k","line":6,"column":10,"end_line":6,"end_column":12}]}},"filename":"hello.k","line":6,"column":5,"end_line":6,"end_column":24},"op":{"Bin":"And"},"right":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"b","filename":"hello.k","line":6,"column":17,"end_line":6,"end_column":18}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":6,"column":17,"end_line":6,"end_column":18},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":12}}},"filename":"hello.k","line":6,"column":22,"end_line":6,"end_column":24}]}},"filename":"hello.k","line":6,"column":17,"end_line":6,"end_column":24}}},"filename":"hello.k","line":6,"column":5,"end_line":6,"end_column":24},"orelse":[]}},"filename":"hello.k","line":6,"column":0,"end_line":7,"end_column":0}]}},"filename":"hello.k","line":5,"column":0,"end_line":7,"end_column":0},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"condition","filename":"hello.k","line":7,"column":0,"end_line":7,"end_column":9}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":7,"column":0,"end_line":7,"end_column":9}],"value":{"node":{"Identifier":{"names":[{"node":"_condition","filename":"hello.k","line":7,"column":12,"end_line":7,"end_column":22}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":7,"column":12,"end_line":7,"end_column":22},"type_annotation":null,"ty":null}},"filename":"hello.k","line":7,"column":0,"end_line":7,"end_column":22}],"comments":[]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/ast.rs
expression: "crate::tests::parsing_file_ast_json(\"hello.k\",\n r####\"\ndata2 = {\n **{key = \"value1\"}\n if a == 123: if b == 456: key = \"value2\"\n}\n \"####)"
---
{"filename":"hello.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"data2","filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":5}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":5}],"value":{"node":{"Config":{"items":[{"node":{"key":null,"value":{"node":{"Config":{"items":[{"node":{"key":{"node":{"Identifier":{"names":[{"node":"key","filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":10}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":10},"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"value1\"","value":"value1"}},"filename":"hello.k","line":3,"column":13,"end_line":3,"end_column":21},"operation":"Override","insert_index":-1},"filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":21}]}},"filename":"hello.k","line":3,"column":6,"end_line":3,"end_column":22},"operation":"Union","insert_index":-1},"filename":"hello.k","line":3,"column":4,"end_line":3,"end_column":22},{"node":{"key":null,"value":{"node":{"ConfigIfEntry":{"if_cond":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":8}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":8},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":123}}},"filename":"hello.k","line":4,"column":12,"end_line":4,"end_column":15}]}},"filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":15},"items":[{"node":{"key":null,"value":{"node":{"ConfigIfEntry":{"if_cond":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"b","filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":21}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":21},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":456}}},"filename":"hello.k","line":4,"column":25,"end_line":4,"end_column":28}]}},"filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":28},"items":[{"node":{"key":{"node":{"Identifier":{"names":[{"node":"key","filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":33}],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":33},"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"value2\"","value":"value2"}},"filename":"hello.k","line":4,"column":36,"end_line":4,"end_column":44},"operation":"Override","insert_index":-1},"filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":44}],"orelse":null}},"filename":"hello.k","line":4,"column":17,"end_line":5,"end_column":0},"operation":"Override","insert_index":-1},"filename":"hello.k","line":4,"column":17,"end_line":5,"end_column":0}],"orelse":null}},"filename":"hello.k","line":4,"column":4,"end_line":5,"end_column":0},"operation":"Union","insert_index":-1},"filename":"hello.k","line":4,"column":4,"end_line":5,"end_column":0}]}},"filename":"hello.k","line":2,"column":8,"end_line":5,"end_column":1},"type_annotation":null,"ty":null}},"filename":"hello.k","line":2,"column":0,"end_line":5,"end_column":1}],"comments":[]}
Loading

0 comments on commit 4876dce

Please sign in to comment.