Files
craft-anything/crates/formats/tests/regression_jsonc_comment_only.rs
BFlatandAfloat b9ca099903 More format parsers keep to their declared lengths and refuse impossible values (#722)
Replays #688 and #690–#715. DEX, Java, SQLite, Android, ZIP, ASAR, file system, PyInstaller, JSON config and packet parsers keep to their declared lengths and refuse impossible values.

Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com>
2026-11-04 01:43:07 +07:00

21 lines
945 B
Rust

use craft_anything_formats::jsonc::parse;
use serde_json::json;
#[test]
fn the_first_setting_keeps_comment_only_documents() {
for original in
["// local preferences\n", "// local preferences", "/* keep this explanation */\n\n", "\u{feff}// local preferences\n", "\u{feff}", " \t\r\n"]
{
let edited = parse(original, false).unwrap().set(&["mcp", "servers", "sample"], &json!({"command": "sample-cli"})).unwrap();
assert!(edited.starts_with(original), "lost the original prefix: {edited:?}");
assert_eq!(parse(&edited, false).unwrap().get(&["mcp", "servers", "sample"]), Some(json!({"command": "sample-cli"})));
}
}
#[test]
fn a_truly_empty_file_still_gets_a_fresh_object() {
let edited = parse("", true).unwrap().set(&["enabled"], &json!(true)).unwrap();
assert_eq!(edited, "{\n \"enabled\": true\n}\n");
assert_eq!(parse(&edited, true).unwrap().get(&["enabled"]), Some(json!(true)));
}