#[cfg(test)]
mod tests {
use crate::{BufferDestination, FileSource, Node, from_str, parse, stringify};
use std::fs;
use std::path::{Path, PathBuf};
fn test_file(relative: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("files")
.join(relative)
}
fn round_trip_file(file_path: impl AsRef<Path>) -> Node {
let path = file_path.as_ref();
let mut source = FileSource::new(path)
.unwrap_or_else(|e| panic!("Failed to open {}: {}", path.display(), e));
let node = parse(&mut source)
.unwrap_or_else(|e| panic!("Failed to parse {}: {}", path.display(), e));
let mut dest = BufferDestination::new();
stringify(&node, &mut dest)
.unwrap_or_else(|e| panic!("Failed to stringify {}: {}", path.display(), e));
let json_str = dest.to_string();
from_str(&json_str)
.unwrap_or_else(|e| panic!("Failed to re-parse {}: {}", path.display(), e))
}
fn json_files_in(directory: impl AsRef<Path>) -> Vec<PathBuf> {
let mut paths = Vec::new();
if let Ok(entries) = fs::read_dir(directory) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) == Some("json") {
paths.push(path);
}
}
}
paths
}
#[test]
fn test_round_trip_all_test_files() {
let files = json_files_in(test_file(""));
assert!(
!files.is_empty(),
"No JSON files found in files – check working directory"
);
for file_path in &files {
let reparsed = round_trip_file(file_path);
assert!(
reparsed.is_object() || reparsed.is_array(),
"{} did not produce an object or array after round-trip",
file_path.display()
);
}
}
#[test]
fn test_round_trip_testfile001_preserves_root_type() {
let file = test_file("testfile001.json");
let mut source = FileSource::new(&file).expect("open testfile001.json");
let original = parse(&mut source).expect("parse testfile001.json");
assert!(original.is_object(), "testfile001 root should be an object");
let reparsed = round_trip_file(&file);
assert!(
reparsed.is_object(),
"testfile001 root should still be an object after round-trip"
);
}
#[test]
fn test_round_trip_testfile001_glossary_key_present() {
let reparsed = round_trip_file(test_file("testfile001.json"));
assert!(
reparsed.get("glossary").is_some(),
"round-tripped testfile001 should have 'glossary' key"
);
assert!(
reparsed.get("glossary").unwrap().is_object(),
"'glossary' value should be an object"
);
}
#[test]
fn test_round_trip_testfile004_preserves_float_values() {
let file = test_file("testfile004.json");
let reparsed = round_trip_file(&file);
assert!(reparsed.is_object(), "testfile004 root should be an object");
let lat = reparsed
.get("latitude")
.expect("'latitude' key missing after round-trip");
let lon = reparsed
.get("longitude")
.expect("'longitude' key missing after round-trip");
let lat_val = lat.as_f64().expect("'latitude' should be numeric");
let lon_val = lon.as_f64().expect("'longitude' should be numeric");
assert!(
(lat_val - 48.8581_f64).abs() < 1e-4,
"latitude value changed after round-trip: got {}",
lat_val
);
assert!(
(lon_val - 2.29469_f64).abs() < 1e-4,
"longitude value changed after round-trip: got {}",
lon_val
);
}
#[test]
fn test_round_trip_testfile010_employees_key_present() {
let file = test_file("testfile010.json");
let reparsed = round_trip_file(&file);
assert!(reparsed.is_object(), "testfile010 root should be an object");
let employees = reparsed
.get("Employees")
.expect("'Employees' key missing after round-trip");
assert!(
employees.is_array(),
"'Employees' should be an array after round-trip"
);
let arr = employees.as_array().unwrap();
assert!(!arr.is_empty(), "'Employees' array should not be empty");
}
#[test]
fn test_round_trip_testfile010_employee_count_preserved() {
let mut source =
FileSource::new(test_file("testfile010.json")).expect("open testfile010.json");
let original = parse(&mut source).expect("parse testfile010.json");
let original_count = original
.get("Employees")
.and_then(|e| e.as_array())
.map(|a| a.len())
.expect("original Employees array");
let reparsed = round_trip_file(test_file("testfile010.json"));
let reparsed_count = reparsed
.get("Employees")
.and_then(|e| e.as_array())
.map(|a| a.len())
.expect("reparsed Employees array");
assert_eq!(
original_count, reparsed_count,
"Employee count changed after round-trip"
);
}
#[test]
fn test_round_trip_testfile020_root_is_array() {
let reparsed = round_trip_file(test_file("testfile020.json"));
assert!(
reparsed.is_array(),
"testfile020 root should remain an array after round-trip"
);
}
#[test]
fn test_round_trip_testfile020_user_count_preserved() {
let mut source =
FileSource::new(test_file("testfile020.json")).expect("open testfile020.json");
let original = parse(&mut source).expect("parse testfile020.json");
let original_count = original
.as_array()
.map(|a| a.len())
.expect("testfile020 should be an array");
let reparsed = round_trip_file(test_file("testfile020.json"));
let reparsed_count = reparsed
.as_array()
.map(|a| a.len())
.expect("reparsed testfile020 should be an array");
assert_eq!(
original_count, reparsed_count,
"User count changed after round-trip"
);
}
#[test]
fn test_round_trip_testfile005_escape_sequences() {
let file = test_file("testfile005.json");
let mut source = FileSource::new(&file).expect("open testfile005.json");
let original = parse(&mut source).expect("parse testfile005.json");
assert!(original.is_object());
let reparsed = round_trip_file(&file);
assert!(
reparsed.is_object(),
"testfile005 should remain an object after round-trip"
);
}
#[test]
fn test_round_trip_preserves_boolean_values() {
let json = r#"{"enabled": true, "disabled": false}"#;
let original = from_str(json).expect("parse boolean JSON");
let mut dest = BufferDestination::new();
stringify(&original, &mut dest).expect("stringify boolean JSON");
let reparsed = from_str(&dest.to_string()).expect("reparse boolean JSON");
assert_eq!(
reparsed.get("enabled").and_then(|n| n.as_bool()),
Some(true)
);
assert_eq!(
reparsed.get("disabled").and_then(|n| n.as_bool()),
Some(false)
);
}
#[test]
fn test_round_trip_preserves_null_value() {
let json = r#"{"key": null}"#;
let original = from_str(json).expect("parse null JSON");
let mut dest = BufferDestination::new();
stringify(&original, &mut dest).expect("stringify null JSON");
let reparsed = from_str(&dest.to_string()).expect("reparse null JSON");
let null_node = reparsed
.get("key")
.expect("'key' should exist after round-trip");
assert!(null_node.is_null(), "'key' should be null after round-trip");
}
#[test]
fn test_round_trip_preserves_integer_values() {
let json = r#"{"count": 42, "offset": -7}"#;
let original = from_str(json).expect("parse integer JSON");
let mut dest = BufferDestination::new();
stringify(&original, &mut dest).expect("stringify integer JSON");
let reparsed = from_str(&dest.to_string()).expect("reparse integer JSON");
assert_eq!(reparsed.get("count").and_then(|n| n.as_i64()), Some(42));
assert_eq!(reparsed.get("offset").and_then(|n| n.as_i64()), Some(-7));
}
#[test]
fn test_round_trip_preserves_string_values() {
let json = r#"{"greeting": "hello world", "empty": ""}"#;
let original = from_str(json).expect("parse string JSON");
let mut dest = BufferDestination::new();
stringify(&original, &mut dest).expect("stringify string JSON");
let reparsed = from_str(&dest.to_string()).expect("reparse string JSON");
assert_eq!(
reparsed.get("greeting").and_then(|n| n.as_str()),
Some("hello world")
);
assert_eq!(reparsed.get("empty").and_then(|n| n.as_str()), Some(""));
}
#[test]
fn test_round_trip_preserves_nested_array_of_objects() {
let json = r#"{"items": [{"id": 1, "name": "one"}, {"id": 2, "name": "two"}]}"#;
let original = from_str(json).expect("parse nested JSON");
let mut dest = BufferDestination::new();
stringify(&original, &mut dest).expect("stringify nested JSON");
let reparsed = from_str(&dest.to_string()).expect("reparse nested JSON");
let items = reparsed
.get("items")
.and_then(|n| n.as_array())
.expect("'items' array missing after round-trip");
assert_eq!(items.len(), 2, "item count should be 2 after round-trip");
assert_eq!(items[0].get("id").and_then(|n| n.as_i64()), Some(1));
assert_eq!(items[1].get("name").and_then(|n| n.as_str()), Some("two"));
}
#[test]
fn test_stringify_output_is_valid_for_all_test_files() {
let files = json_files_in(test_file(""));
for file_path in &files {
let mut source = FileSource::new(file_path)
.unwrap_or_else(|e| panic!("open {}: {}", file_path.display(), e));
let node = parse(&mut source)
.unwrap_or_else(|e| panic!("parse {}: {}", file_path.display(), e));
let mut dest = BufferDestination::new();
stringify(&node, &mut dest)
.unwrap_or_else(|e| panic!("stringify {}: {}", file_path.display(), e));
let json_str = dest.to_string();
assert!(
!json_str.is_empty(),
"stringify produced empty output for {}",
file_path.display()
);
assert!(
from_str(&json_str).is_ok(),
"stringify output for {} is not valid JSON",
file_path.display()
);
}
}
}