|
| 1 | +pub mod prettify; |
| 2 | +pub mod read_write_json; |
| 3 | + |
| 4 | +#[cfg(test)] |
| 5 | +mod tests { |
| 6 | + use crate::prettify::correct_path; |
| 7 | + |
| 8 | + use super::*; |
| 9 | + fn generate_pretty_json(input_json: &str) -> String { |
| 10 | + let ser_json = serde_json::from_str(input_json).expect("Failed to parse the json input"); |
| 11 | + let prettified_json: String = prettify::prettify_value(ser_json, "", false); |
| 12 | + let mut final_pretty_json: String = correct_path(&prettified_json); |
| 13 | + final_pretty_json.push('\n'); //this is done automatically when saving the json to a file |
| 14 | + final_pretty_json |
| 15 | + } |
| 16 | + |
| 17 | + #[test] |
| 18 | + fn test_single_json_object() { |
| 19 | + let input_json = r#"[{"Key":"val"}]"#; |
| 20 | + let expected = r#"[ |
| 21 | + { "Key": "val" } |
| 22 | +] |
| 23 | +"#; |
| 24 | + let final_pretty_json = generate_pretty_json(input_json); |
| 25 | + assert_eq!(final_pretty_json, expected); |
| 26 | + } |
| 27 | + |
| 28 | + #[test] |
| 29 | + fn test_non_absolute_path_json() { |
| 30 | + let input_json = r#"[{"Path":"?"},{"Path":"src/dir/main.nr"}]"#; |
| 31 | + let expected = r#"[ |
| 32 | + { "Path": "?" }, |
| 33 | + { "Path": "src/dir/main.nr" } |
| 34 | +] |
| 35 | +"#; |
| 36 | + let final_pretty_json = generate_pretty_json(input_json); |
| 37 | + assert_eq!(final_pretty_json, expected); |
| 38 | + } |
| 39 | + |
| 40 | + #[test] |
| 41 | + fn test_absolute_path_json() { |
| 42 | + let input_json = r#"[{"Path":"?"},{"Path":"some/absolute/path/src/dir/main.nr"}]"#; |
| 43 | + let expected = r#"[ |
| 44 | + { "Path": "?" }, |
| 45 | + { "Path": "<relative-to-this>/src/dir/main.nr" } |
| 46 | +] |
| 47 | +"#; |
| 48 | + let final_pretty_json = generate_pretty_json(input_json); |
| 49 | + assert_eq!(final_pretty_json, expected); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_basic_nested_array_json() { |
| 54 | + let input_json = r#"[{"arr":[{"nested_arr":[{"key":"val"}]}]}]"#; |
| 55 | + let expected = r#"[ |
| 56 | + { "arr": [ |
| 57 | + { "nested_arr": [ |
| 58 | + { "key": "val" } |
| 59 | + ] } |
| 60 | + ] } |
| 61 | +] |
| 62 | +"#; |
| 63 | + let final_pretty_json = generate_pretty_json(input_json); |
| 64 | + assert_eq!(final_pretty_json, expected); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_basic_nested_json_objects() { |
| 69 | + let input_json = r#"[{"key":{"inner_key1":"inner_value1","inner_key2":"inner_value2"}}]"#; |
| 70 | + let expected = r#"[ |
| 71 | + { "key": { "inner_key1": "inner_value1", "inner_key2": "inner_value2" } } |
| 72 | +] |
| 73 | +"#; |
| 74 | + let final_pretty_json = generate_pretty_json(input_json); |
| 75 | + assert_eq!(final_pretty_json, expected); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_arrays_nested_objects_full_json() { |
| 80 | + let input_json = r#"[{"a":"111"},{"b":[]},{"c":[{"arr":"arr1", "abb" : 1},"#.to_string() |
| 81 | + + r#"{"arr":"arr2","abb" : 2},{"arr":"arr3","abb" : 3}]},{"long":"a1","along1":"a2"},"# |
| 82 | + + r#"{ "Value": { "variable_id": 0, "value": { "kind": "Int", "i": 4, "type_id": 1 } } }]"#; |
| 83 | + |
| 84 | + let expected = r#"[ |
| 85 | + { "a": "111" }, |
| 86 | + { "b": [] }, |
| 87 | + { "c": [ |
| 88 | + { "arr": "arr1", "abb": 1 }, |
| 89 | + { "arr": "arr2", "abb": 2 }, |
| 90 | + { "arr": "arr3", "abb": 3 } |
| 91 | + ] }, |
| 92 | + { "long": "a1", "along1": "a2" }, |
| 93 | + { "Value": { "variable_id": 0, "value": { "kind": "Int", "i": 4, "type_id": 1 } } } |
| 94 | +] |
| 95 | +"#; |
| 96 | + let final_pretty_json = generate_pretty_json(&input_json); |
| 97 | + assert_eq!(final_pretty_json, expected); |
| 98 | + } |
| 99 | +} |
0 commit comments