Skip to content

Commit

Permalink
test: change validate tool tests to insta snapshots
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jan 11, 2024
1 parent 26b3e71 commit 8f6b9f5
Show file tree
Hide file tree
Showing 46 changed files with 4,312 additions and 137 deletions.
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ located_yaml = "0.2.1"
[dev-dependencies]
pretty_assertions = "1.2.1"
criterion = "0.3"
insta = "1.8.0"

[[bench]]
name = "benchmark"
Expand Down
6 changes: 3 additions & 3 deletions kclvm/tools/src/format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ const TEST_CASES: &[&str; 22] = &[
];

fn read_data(data_name: &str) -> (String, String) {
let src = std::fs::read_to_string(&format!(
let src = std::fs::read_to_string(format!(
"./src/format/test_data/format_data/{}{}",
data_name, FILE_INPUT_SUFFIX
))
.unwrap();

(
format_source("", &src, &Default::default()).unwrap().0,
std::fs::read_to_string(&format!(
std::fs::read_to_string(format!(
"./src/format/test_data/format_data/{}{}",
data_name, FILE_OUTPUT_SUFFIX
))
Expand Down Expand Up @@ -187,7 +187,7 @@ fn test_format_integration_konfig() -> Result<()> {
let files = get_files(konfig_path, true, true, ".k");
for file in &files {
// Skip test and hidden files.
if file.ends_with("_test.k") || file.starts_with("_") {
if file.ends_with("_test.k") || file.starts_with('_') {
continue;
}
assert!(
Expand Down
6 changes: 3 additions & 3 deletions kclvm/tools/src/testing/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct TestCase;

/// Load test suite from path
pub fn load_test_suites<P: AsRef<str>>(path: P, opts: &TestOptions) -> Result<Vec<TestSuite>> {
let pkg_list = get_pkg_list(&path.as_ref())?;
let pkg_list = get_pkg_list(path.as_ref())?;
let mut suites = vec![];
for pkg in &pkg_list {
let (normal_files, test_files) = get_test_files(pkg)?;
Expand Down Expand Up @@ -166,12 +166,12 @@ fn get_test_files<P: AsRef<Path>>(pkg: P) -> Result<(Vec<String>, Vec<String>)>
let files = get_kcl_files(pkg, false)?;
let normal_files = files
.iter()
.filter(|x| !x.starts_with("_") && !x.ends_with(TEST_FILE_SUFFIX))
.filter(|x| !x.starts_with('_') && !x.ends_with(TEST_FILE_SUFFIX))
.cloned()
.collect::<Vec<String>>();
let test_files = files
.iter()
.filter(|x| !x.starts_with("_") && x.ends_with(TEST_FILE_SUFFIX))
.filter(|x| !x.starts_with('_') && x.ends_with(TEST_FILE_SUFFIX))
.cloned()
.collect::<Vec<String>>();
Ok((normal_files, test_files))
Expand Down
22 changes: 11 additions & 11 deletions kclvm/tools/src/vet/expr_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ impl ExprGenerator<located_yaml::Yaml> for ExprBuilder {
}
}
located_yaml::YamlElt::Real(j_float) => {
if let Some(number_lit) = j_float.parse::<f64>().ok() {
if format!("{}", number_lit) != j_float.to_string() {
if let Ok(number_lit) = j_float.parse::<f64>() {
if format!("{}", number_lit) != *j_float {
bail!("{FAIL_LOAD_VALIDATED_ERR_MSG}, Unsupported Number Type",)
}
Ok(node_ref!(
Expand Down Expand Up @@ -314,10 +314,10 @@ impl ExprGenerator<located_yaml::Yaml> for ExprBuilder {
names: vec![Node::new(
s_name.to_string(),
loc.0.clone(),
loc.1.clone() as u64,
loc.2.clone() as u64,
loc.3.clone() as u64,
loc.4.clone() as u64
loc.1,
loc.2,
loc.3,
loc.4
)],
pkgpath: String::new(),
ctx: ExprContext::Load
Expand Down Expand Up @@ -363,7 +363,7 @@ impl ExprGenerator<json_spanned_value::Spanned<json_spanned_value::Value>> for E
loc
)),
json_spanned_value::Value::Bool(j_bool) => {
let name_const = match NameConstant::try_from(j_bool.clone()) {
let name_const = match NameConstant::try_from(*j_bool) {
Ok(nc) => nc,
Err(err) => {
bail!("{FAIL_LOAD_VALIDATED_ERR_MSG}, {err}")
Expand Down Expand Up @@ -483,10 +483,10 @@ impl ExprGenerator<json_spanned_value::Spanned<json_spanned_value::Value>> for E
names: vec![Node::new(
s_name.to_string(),
loc.0.clone(),
loc.1.clone(),
loc.2.clone(),
loc.3.clone(),
loc.4.clone()
loc.1,
loc.2,
loc.3,
loc.4
)],
pkgpath: String::new(),
ctx: ExprContext::Load
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
---
source: tools/src/vet/tests.rs
expression: got_ast_json_str
---
{
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": {
"data": {
"args": [],
"config": {
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": {
"data": {
"items": [
{
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": {
"insert_index": -1,
"key": {
"column": 4,
"end_column": 9,
"end_line": 3,
"filename": "<workspace>/simple.k.json",
"line": 3,
"node": {
"data": {
"is_long_string": false,
"raw_value": "\"age\"",
"value": "age"
},
"type": "StringLit"
}
},
"operation": "Union",
"value": {
"column": 11,
"end_column": 13,
"end_line": 3,
"filename": "<workspace>/simple.k.json",
"line": 3,
"node": {
"data": {
"binary_suffix": null,
"value": {
"data": 18,
"type": "Int"
}
},
"type": "NumberLit"
}
}
}
},
{
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": {
"insert_index": -1,
"key": {
"column": 4,
"end_column": 13,
"end_line": 4,
"filename": "<workspace>/simple.k.json",
"line": 4,
"node": {
"data": {
"is_long_string": false,
"raw_value": "\"message\"",
"value": "message"
},
"type": "StringLit"
}
},
"operation": "Union",
"value": {
"column": 15,
"end_column": 30,
"end_line": 4,
"filename": "<workspace>/simple.k.json",
"line": 4,
"node": {
"data": {
"is_long_string": false,
"raw_value": "\"This is Alice\"",
"value": "This is Alice"
},
"type": "StringLit"
}
}
}
},
{
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": {
"insert_index": -1,
"key": {
"column": 4,
"end_column": 10,
"end_line": 2,
"filename": "<workspace>/simple.k.json",
"line": 2,
"node": {
"data": {
"is_long_string": false,
"raw_value": "\"name\"",
"value": "name"
},
"type": "StringLit"
}
},
"operation": "Union",
"value": {
"column": 12,
"end_column": 19,
"end_line": 2,
"filename": "<workspace>/simple.k.json",
"line": 2,
"node": {
"data": {
"is_long_string": false,
"raw_value": "\"Alice\"",
"value": "Alice"
},
"type": "StringLit"
}
}
}
}
]
},
"type": "Config"
}
},
"kwargs": [],
"name": {
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": {
"ctx": "Load",
"names": [
{
"column": 0,
"end_column": 1,
"end_line": 5,
"filename": "<workspace>/simple.k.json",
"line": 1,
"node": "simple"
}
],
"pkgpath": ""
}
}
},
"type": "Schema"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: tools/src/vet/tests.rs
expression: got_ast_json_str
---
{
"column": 0,
"end_column": 1,
"end_line": 1,
"filename": "<workspace>/plain_value.k.json",
"line": 1,
"node": {
"data": {
"binary_suffix": null,
"value": {
"data": 1,
"type": "Int"
}
},
"type": "NumberLit"
}
}
Loading

0 comments on commit 8f6b9f5

Please sign in to comment.