Skip to content

Commit

Permalink
add test case for invalid json/yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Sep 26, 2022
1 parent e58a2a6 commit c595d04
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions kclvm/Cargo.lock

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

2 changes: 1 addition & 1 deletion kclvm/tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kclvm-error = {path = "../error", version = "0.1.0"}
kclvm-parser = {path = "../parser", version = "0.1.0"}
kclvm-sema = {path = "../sema", version = "0.1.0"}
kclvm-config = {path = "../config", version = "0.1.0"}
serde_json = "1.0"
serde_json = "1.0.85"
serde_yaml = "0.9.13"

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions kclvm/tools/src/util/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Loader<serde_json::Value> for DataLoader {
/// Load data into Json value.
fn load(&self) -> Result<serde_json::Value> {
let v = match self.kind {
LoaderKind::JSON => serde_json::to_value(&self.get_data())
LoaderKind::JSON => serde_json::from_str(&self.get_data())
.with_context(|| format!("Failed to String '{}' to Json", self.get_data()))?,
_ => {
bail!("Failed to String to Json Value")
Expand All @@ -62,7 +62,7 @@ impl Loader<serde_yaml::Value> for DataLoader {
/// Load data into Yaml value.
fn load(&self) -> Result<serde_yaml::Value> {
let v = match self.kind {
LoaderKind::YAML => serde_yaml::to_value(&self.get_data())
LoaderKind::YAML => serde_yaml::from_str(&self.get_data())
.with_context(|| format!("Failed to String '{}' to Yaml", self.get_data()))?,
_ => {
bail!("Failed to String to Yaml Value")
Expand Down
9 changes: 9 additions & 0 deletions kclvm/tools/src/util/test_datas/test_invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
5 changes: 5 additions & 0 deletions kclvm/tools/src/util/test_datas/test_invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "John Doe",
"city": "London"
invalid

29 changes: 29 additions & 0 deletions kclvm/tools/src/util/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,34 @@ websites:
}
};
}

#[test]
fn test_invalid_file() {
let invalid_json_file_path = construct_full_path("test_invalid.json").unwrap();
let json_loader =
DataLoader::new_with_file_path(LoaderKind::JSON, &invalid_json_file_path).unwrap();

match <DataLoader as Loader<serde_json::Value>>::load(&json_loader) {
Ok(_) => {
panic!("unreachable")
}
Err(err) => {
assert_eq!(format!("{:?}", err), "Failed to String 'languages:\n - Ruby\n - Perl\n - Python \nwebsites:\n YAML: yaml.org \n Ruby: ruby-lang.org \n Python: python.org \n Perl: use.perl.org' to Json\n\nCaused by:\n expected value at line 1 column 1");
}
}

let invalid_yaml_file_path = construct_full_path("test_invalid.yaml").unwrap();
let yaml_loader =
DataLoader::new_with_file_path(LoaderKind::YAML, &invalid_yaml_file_path).unwrap();

match <DataLoader as Loader<serde_yaml::Value>>::load(&yaml_loader) {
Ok(_) => {
panic!("unreachable")
}
Err(err) => {
assert_eq!(format!("{:?}", err), "Failed to String '{\n \"name\": \"John Doe\",\n \"city\": \"London\"\ninvalid\n\n' to Yaml\n\nCaused by:\n did not find expected ',' or '}' at line 4 column 1, while parsing a flow mapping");
}
}
}
}
}

0 comments on commit c595d04

Please sign in to comment.