From 7e19cb1ae5d2d44c729e423e94c09ef29c7f42d7 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 21:42:56 +0800 Subject: [PATCH] test: complete tests process --- src/to-json/src/lib.rs | 1 + src/to-json/src/tests.rs | 71 ++++++++++++++++++++++++++++++++++++ src/to-json/tests/convert.rs | 23 ------------ 3 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 src/to-json/src/tests.rs delete mode 100644 src/to-json/tests/convert.rs diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 768c005..f04b9c3 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1,3 +1,4 @@ mod ffi; +mod tests; mod parser; mod convert; diff --git a/src/to-json/src/tests.rs b/src/to-json/src/tests.rs new file mode 100644 index 0000000..3a63c23 --- /dev/null +++ b/src/to-json/src/tests.rs @@ -0,0 +1,71 @@ +use crate::convert::to_json; + +const JSON_TEST_CONTENT: &str = r#" +{ + "int": 123, + "bool": true, + "float": 3.141592, + "string": "json test", + "array": [1, 2, 3, 4, 5], + "object": { + "sub": "test" + } +} +"#; + +const YAML_TEST_CONTENT: &str = r#" +int: 123 +bool: true +float: 3.141592 +string: "json test" +array: + - 1 + - 2 + - 3 + - 4 + - 5 +object: + sub: test +"#; + +const TOML_TEST_CONTENT: &str = r#" +int = 123 +bool = true +float = 3.141592 +string = "json test" +array = [ 1, 2, 3, 4, 5 ] + +[object] +sub = "test" +"#; + +fn format_json(raw: &str) -> String { + match to_json(raw) { + Some(data) => data, + None => panic!("JSON format error"), + } +} + +#[test] +fn json_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(JSON_TEST_CONTENT).unwrap()), + ); +} + +#[test] +fn yaml_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(YAML_TEST_CONTENT).unwrap()), + ); +} + +#[test] +fn toml_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(TOML_TEST_CONTENT).unwrap()), + ); +} diff --git a/src/to-json/tests/convert.rs b/src/to-json/tests/convert.rs deleted file mode 100644 index 59da73e..0000000 --- a/src/to-json/tests/convert.rs +++ /dev/null @@ -1,23 +0,0 @@ -const JSON_TEST_CONTENT: &str = r#" -{ - "int": 123, - "float": 2.3333, - "string": "dnomd343", - "array": [1, 2, 3, 4, 5], - "dict": { - "test": "demo" - } -} -"#; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn json_to_json() { - println!("{}", JSON_TEST_CONTENT); - assert_eq!(1, 2); - } - -}