Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: move parser unit tests to snaptest #794

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
374 changes: 0 additions & 374 deletions kclvm/parser/src/parser/tests.rs

Large diffs are not rendered by default.

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

use core::any::Any;

mod ast;
mod error_recovery;
mod expr;
mod file;
mod types;

#[macro_export]
macro_rules! parse_expr_snapshot {
Expand All @@ -33,6 +36,46 @@ macro_rules! parse_module_snapshot {
};
}

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

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

#[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));
}
};
}

#[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 @@ -65,6 +108,49 @@ pub(crate) fn parsing_module_string(src: &str) -> String {
}
}

pub(crate) fn parsing_type_string(src: &str) -> String {
let sm = SourceMap::new(FilePathMapping::empty());
sm.new_source_file(PathBuf::from("").into(), src.to_string());
let sess = &ParseSession::with_source_map(Arc::new(sm));

create_session_globals_then(|| {
let stream = parse_token_streams(sess, src, new_byte_pos(0));
let mut parser = Parser::new(sess, stream);
let typ = parser.parse_type_annotation();
format!("{typ:?}\n")
})
}

pub(crate) fn parsing_type_node_string(src: &str) -> String {
let sm = SourceMap::new(FilePathMapping::empty());
sm.new_source_file(PathBuf::from("").into(), src.to_string());
let sess = &ParseSession::with_source_map(Arc::new(sm));

create_session_globals_then(|| {
let stream = parse_token_streams(sess, src, new_byte_pos(0));
let mut parser = Parser::new(sess, stream);
let typ = parser.parse_type_annotation();
typ.node.to_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(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
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
"####
);
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
@@ -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
Loading