Skip to content

Commit

Permalink
test: move parser type node 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 7723d84 commit 1119a7e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
35 changes: 0 additions & 35 deletions kclvm/parser/src/parser/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use crate::lexer::parse_token_streams;
use crate::parse_file;
use crate::parser::Parser;
use crate::session::ParseSession;
use compiler_base_span::span::new_byte_pos;
use compiler_base_span::{FilePathMapping, SourceMap};
use expect_test::{expect, Expect};
use kclvm_span::create_session_globals_then;
use regex::Regex;
use std::path::PathBuf;
use std::sync::Arc;

fn check_parsing_file_ast_json(filename: &str, src: &str, expect: Expect) {
Expand All @@ -22,41 +16,12 @@ fn check_parsing_file_ast_json(filename: &str, src: &str, expect: Expect) {
expect.assert_eq(&actual)
}

fn check_type_str(src: &str, expect: Expect) {
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();
let actual = typ.node.to_string();
expect.assert_eq(&actual)
});
}

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_type_str() {
check_type_str(r####"int"####, expect![[r#"int"#]]);
check_type_str(r####" int "####, expect![[r#"int"#]]);

check_type_str(
r####"bool | True | int | str|str"####,
expect![[r#"bool | True | int | str | str"#]],
);
check_type_str(
r####"[ [{str: float}] | int]"####,
expect![[r#"[[{str:float}] | int]"#]],
);
}

#[test]
fn test_parse_schema_stmt() {
check_parsing_file_ast_json(
Expand Down
23 changes: 23 additions & 0 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ macro_rules! parse_type_snapshot {
};
}

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

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 @@ -89,6 +99,19 @@ pub(crate) fn parsing_type_string(src: &str) -> String {
})
}

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 fn check_result_panic_info(result: Result<(), Box<dyn Any + Send>>) {
if let Err(e) = result {
assert!(e.downcast::<String>().is_ok());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/types.rs
expression: "crate::tests::parsing_type_node_string(r####\"int\"####)"
---
int
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/types.rs
expression: "crate::tests::parsing_type_node_string(r####\" int \"####)"
---
int
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/types.rs
expression: "crate::tests::parsing_type_node_string(r####\"bool | True | int | str|str\"####)"
---
bool | True | int | str | str
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: parser/src/tests/types.rs
expression: "crate::tests::parsing_type_node_string(r####\"[ [{str: float}] | int]\"####)"
---
[[{str:float}] | int]
7 changes: 6 additions & 1 deletion kclvm/parser/src/tests/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::tests::parse_type_snapshot;
use crate::tests::{parse_type_node_snapshot, parse_type_snapshot};

parse_type_snapshot!(basic_type_0, r####"bool"####);
parse_type_snapshot!(basic_type_1, r####"int"####);
Expand All @@ -25,3 +25,8 @@ parse_type_snapshot!(literal_type_2, r####"123"####);
parse_type_snapshot!(literal_type_3, r####"123.0"####);
parse_type_snapshot!(literal_type_4, r####""abc""####);
parse_type_snapshot!(literal_type_5, r####"''"####);

parse_type_node_snapshot!(type_str_0, r####"int"####);
parse_type_node_snapshot!(type_str_1, r####" int "####);
parse_type_node_snapshot!(type_str_2, r####"bool | True | int | str|str"####);
parse_type_node_snapshot!(type_str_3, r####"[ [{str: float}] | int]"####);

0 comments on commit 1119a7e

Please sign in to comment.