Skip to content

Commit

Permalink
test: move parser file 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 4876dce commit c492817
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 66 deletions.
38 changes: 0 additions & 38 deletions kclvm/parser/src/parser/tests.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,6 @@
use crate::parse_file;
use regex::Regex;

fn check_parsing_module(filename: &str, src: &str, expect: &str) {
let m = crate::parse_file(filename, Some(src.to_string())).expect(filename);
let actual = format!("{}\n", serde_json::ser::to_string(&m).unwrap());
assert_eq!(actual.trim(), expect.trim());
}

#[test]
fn test_parse_file() {
let filenames = vec![
"testdata/assert-01.k",
"testdata/assert-02.k",
"testdata/assert-03.k",
"testdata/assert-if-0.k",
"testdata/assert-if-1.k",
"testdata/assert-if-2.k",
"testdata/assign-01.k",
"testdata/config_expr-01.k",
"testdata/config_expr-02.k",
"testdata/config_expr-03.k",
"testdata/config_expr-04.k",
"testdata/import-01.k",
"testdata/if-01.k",
"testdata/if-02.k",
"testdata/if-03.k",
"testdata/type-01.k",
"testdata/hello_win.k",
];
for filename in filenames {
let code = std::fs::read_to_string(filename).unwrap();
let expect = std::fs::read_to_string(filename.to_string() + ".json").unwrap();
check_parsing_module(
filename.trim_start_matches("testdata/"),
code.as_str(),
expect.as_str(),
);
}
}

#[test]
fn test_parse_file_not_found() {
match parse_file("The file path is invalid", None) {
Expand Down
18 changes: 18 additions & 0 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use core::any::Any;
mod ast;
mod error_recovery;
mod expr;
mod file;
mod types;

#[macro_export]
Expand Down Expand Up @@ -65,6 +66,16 @@ macro_rules! parse_file_ast_json_snapshot {
};
}

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

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 @@ -133,6 +144,13 @@ pub(crate) fn parsing_file_ast_json(filename: &str, src: &str) -> String {
serde_json::ser::to_string(&m).unwrap()
}

pub(crate) fn parsing_file_string(filename: &str) -> String {
let code = std::fs::read_to_string(filename).unwrap();
let m =
crate::parse_file(filename.trim_start_matches("testdata/"), Some(code)).expect(filename);
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
19 changes: 19 additions & 0 deletions kclvm/parser/src/tests/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::tests::parse_file_snapshot;

parse_file_snapshot!(assert_1, "testdata/assert-01.k");
parse_file_snapshot!(assert_2, "testdata/assert-02.k");
parse_file_snapshot!(assert_3, "testdata/assert-03.k");
parse_file_snapshot!(assert_if_0, "testdata/assert-if-0.k");
parse_file_snapshot!(assert_if_1, "testdata/assert-if-1.k");
parse_file_snapshot!(assert_if_2, "testdata/assert-if-2.k");
parse_file_snapshot!(assign_1, "testdata/assign-01.k");
parse_file_snapshot!(config_expr_1, "testdata/config_expr-01.k");
parse_file_snapshot!(config_expr_2, "testdata/config_expr-02.k");
parse_file_snapshot!(config_expr_3, "testdata/config_expr-03.k");
parse_file_snapshot!(config_expr_4, "testdata/config_expr-04.k");
parse_file_snapshot!(import_1, "testdata/import-01.k");
parse_file_snapshot!(if_1, "testdata/if-01.k");
parse_file_snapshot!(if_2, "testdata/if-02.k");
parse_file_snapshot!(if_3, "testdata/if-03.k");
parse_file_snapshot!(type_1, "testdata/type-01.k");
parse_file_snapshot!(hello_win, "testdata/hello_win.k");
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
---
source: parser/src/tests/file.rs
expression: "crate::tests::parsing_file_string(\"testdata/assert-01.k\")"
---
{"filename":"assert-01.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assert":{"test":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-01.k","line":1,"column":7,"end_line":1,"end_column":8},"if_cond":null,"msg":null}},"filename":"assert-01.k","line":1,"column":0,"end_line":1,"end_column":8},{"node":{"Assert":{"test":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":2}}},"filename":"assert-01.k","line":2,"column":7,"end_line":2,"end_column":8},"if_cond":null,"msg":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"msg\"","value":"msg"}},"filename":"assert-01.k","line":2,"column":10,"end_line":2,"end_column":15}}},"filename":"assert-01.k","line":2,"column":0,"end_line":2,"end_column":15},{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-01.k","line":3,"column":7,"end_line":3,"end_column":11},"if_cond":null,"msg":null}},"filename":"assert-01.k","line":3,"column":0,"end_line":3,"end_column":11}],"comments":[]}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"filename":"assert-02.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-02.k","line":1,"column":7,"end_line":1,"end_column":11},"if_cond":null,"msg":null}},"filename":"assert-02.k","line":1,"column":0,"end_line":1,"end_column":11},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-02.k","line":2,"column":7,"end_line":2,"end_column":8},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-02.k","line":2,"column":12,"end_line":2,"end_column":13}]}},"filename":"assert-02.k","line":2,"column":7,"end_line":2,"end_column":13},"if_cond":null,"msg":null}},"filename":"assert-02.k","line":2,"column":0,"end_line":2,"end_column":13},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_x","filename":"assert-02.k","line":3,"column":0,"end_line":3,"end_column":2}],"pkgpath":"","ctx":"Store"},"filename":"assert-02.k","line":3,"column":0,"end_line":3,"end_column":2}],"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-02.k","line":3,"column":5,"end_line":3,"end_column":16},"type_annotation":null,"ty":null}},"filename":"assert-02.k","line":3,"column":0,"end_line":3,"end_column":16},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"_x","filename":"assert-02.k","line":4,"column":7,"end_line":4,"end_column":9}],"pkgpath":"","ctx":"Load"}},"filename":"assert-02.k","line":4,"column":7,"end_line":4,"end_column":9},"ops":["Eq"],"comparators":[{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-02.k","line":4,"column":13,"end_line":4,"end_column":24}]}},"filename":"assert-02.k","line":4,"column":7,"end_line":4,"end_column":24},"if_cond":null,"msg":null}},"filename":"assert-02.k","line":4,"column":0,"end_line":4,"end_column":24}],"comments":[]}
---
source: parser/src/tests/file.rs
expression: "crate::tests::parsing_file_string(\"testdata/assert-02.k\")"
---
{"filename":"assert-02.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-02.k","line":1,"column":7,"end_line":1,"end_column":11},"if_cond":null,"msg":null}},"filename":"assert-02.k","line":1,"column":0,"end_line":1,"end_column":11},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-02.k","line":2,"column":7,"end_line":2,"end_column":8},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-02.k","line":2,"column":12,"end_line":2,"end_column":13}]}},"filename":"assert-02.k","line":2,"column":7,"end_line":2,"end_column":13},"if_cond":null,"msg":null}},"filename":"assert-02.k","line":2,"column":0,"end_line":2,"end_column":13},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_x","filename":"assert-02.k","line":3,"column":0,"end_line":3,"end_column":2}],"pkgpath":"","ctx":"Store"},"filename":"assert-02.k","line":3,"column":0,"end_line":3,"end_column":2}],"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-02.k","line":3,"column":5,"end_line":3,"end_column":16},"type_annotation":null,"ty":null}},"filename":"assert-02.k","line":3,"column":0,"end_line":3,"end_column":16},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"_x","filename":"assert-02.k","line":4,"column":7,"end_line":4,"end_column":9}],"pkgpath":"","ctx":"Load"}},"filename":"assert-02.k","line":4,"column":7,"end_line":4,"end_column":9},"ops":["Eq"],"comparators":[{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-02.k","line":4,"column":13,"end_line":4,"end_column":24}]}},"filename":"assert-02.k","line":4,"column":7,"end_line":4,"end_column":24},"if_cond":null,"msg":null}},"filename":"assert-02.k","line":4,"column":0,"end_line":4,"end_column":24}],"comments":[]}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"filename":"assert-03.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"If":{"body":[{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-03.k","line":2,"column":11,"end_line":2,"end_column":15},"if_cond":null,"msg":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"Error messgae\"","value":"Error messgae"}},"filename":"assert-03.k","line":2,"column":17,"end_line":2,"end_column":32}}},"filename":"assert-03.k","line":2,"column":4,"end_line":2,"end_column":32},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-03.k","line":3,"column":11,"end_line":3,"end_column":12},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-03.k","line":3,"column":16,"end_line":3,"end_column":17}]}},"filename":"assert-03.k","line":3,"column":11,"end_line":3,"end_column":17},"if_cond":null,"msg":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"\"","value":""}},"filename":"assert-03.k","line":3,"column":19,"end_line":3,"end_column":21}}},"filename":"assert-03.k","line":3,"column":4,"end_line":3,"end_column":21},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_x","filename":"assert-03.k","line":4,"column":4,"end_line":4,"end_column":6}],"pkgpath":"","ctx":"Store"},"filename":"assert-03.k","line":4,"column":4,"end_line":4,"end_column":6}],"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-03.k","line":4,"column":9,"end_line":4,"end_column":20},"type_annotation":null,"ty":null}},"filename":"assert-03.k","line":4,"column":4,"end_line":4,"end_column":20},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"_x","filename":"assert-03.k","line":5,"column":11,"end_line":5,"end_column":13}],"pkgpath":"","ctx":"Load"}},"filename":"assert-03.k","line":5,"column":11,"end_line":5,"end_column":13},"ops":["Eq"],"comparators":[{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-03.k","line":5,"column":17,"end_line":5,"end_column":28}]}},"filename":"assert-03.k","line":5,"column":11,"end_line":5,"end_column":28},"if_cond":null,"msg":null}},"filename":"assert-03.k","line":5,"column":4,"end_line":5,"end_column":28}],"cond":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-03.k","line":1,"column":3,"end_line":1,"end_column":7},"orelse":[{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"False"}},"filename":"assert-03.k","line":7,"column":11,"end_line":7,"end_column":16},"if_cond":null,"msg":null}},"filename":"assert-03.k","line":7,"column":4,"end_line":7,"end_column":16}]}},"filename":"assert-03.k","line":1,"column":0,"end_line":8,"end_column":1}],"comments":[]}
---
source: parser/src/tests/file.rs
expression: "crate::tests::parsing_file_string(\"testdata/assert-03.k\")"
---
{"filename":"assert-03.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"If":{"body":[{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-03.k","line":2,"column":11,"end_line":2,"end_column":15},"if_cond":null,"msg":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"Error messgae\"","value":"Error messgae"}},"filename":"assert-03.k","line":2,"column":17,"end_line":2,"end_column":32}}},"filename":"assert-03.k","line":2,"column":4,"end_line":2,"end_column":32},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-03.k","line":3,"column":11,"end_line":3,"end_column":12},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"assert-03.k","line":3,"column":16,"end_line":3,"end_column":17}]}},"filename":"assert-03.k","line":3,"column":11,"end_line":3,"end_column":17},"if_cond":null,"msg":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"\"","value":""}},"filename":"assert-03.k","line":3,"column":19,"end_line":3,"end_column":21}}},"filename":"assert-03.k","line":3,"column":4,"end_line":3,"end_column":21},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"_x","filename":"assert-03.k","line":4,"column":4,"end_line":4,"end_column":6}],"pkgpath":"","ctx":"Store"},"filename":"assert-03.k","line":4,"column":4,"end_line":4,"end_column":6}],"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-03.k","line":4,"column":9,"end_line":4,"end_column":20},"type_annotation":null,"ty":null}},"filename":"assert-03.k","line":4,"column":4,"end_line":4,"end_column":20},{"node":{"Assert":{"test":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":[{"node":"_x","filename":"assert-03.k","line":5,"column":11,"end_line":5,"end_column":13}],"pkgpath":"","ctx":"Load"}},"filename":"assert-03.k","line":5,"column":11,"end_line":5,"end_column":13},"ops":["Eq"],"comparators":[{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"good case\"","value":"good case"}},"filename":"assert-03.k","line":5,"column":17,"end_line":5,"end_column":28}]}},"filename":"assert-03.k","line":5,"column":11,"end_line":5,"end_column":28},"if_cond":null,"msg":null}},"filename":"assert-03.k","line":5,"column":4,"end_line":5,"end_column":28}],"cond":{"node":{"NameConstantLit":{"value":"True"}},"filename":"assert-03.k","line":1,"column":3,"end_line":1,"end_column":7},"orelse":[{"node":{"Assert":{"test":{"node":{"NameConstantLit":{"value":"False"}},"filename":"assert-03.k","line":7,"column":11,"end_line":7,"end_column":16},"if_cond":null,"msg":null}},"filename":"assert-03.k","line":7,"column":4,"end_line":7,"end_column":16}]}},"filename":"assert-03.k","line":1,"column":0,"end_line":8,"end_column":1}],"comments":[]}
Loading

0 comments on commit c492817

Please sign in to comment.