Files
craft-anything/crates/formats/tests/jsonc_first_object_eol.rs
RRFZ 5756393c83 Keep CRLF when adding the first JSONC object (R3.12) (#1730) (#1730)
Creating the first object in a whitespace-only or comment-only CRLF settings file now uses the file's line endings, as adding to an existing object already did. Comments and a BOM stay in place; a new empty file still uses LF.
2026-11-12 12:08:33 +07:00

17 lines
859 B
Rust

use craft_anything_formats::jsonc;
use serde_json::json;
/// Creating the first object keeps CRLF in comment-only and whitespace-only files.
#[test]
fn first_object_keeps_existing_line_endings() {
for src in ["\r\n", "// settings\r\n", "\u{feff}// settings\r\n// tail"] {
let output = jsonc::parse(src, false).unwrap().set(&["mcpServers", "example"], &json!({"command": "example"})).unwrap();
assert!(output.starts_with(src));
assert_eq!(output.matches('\n').count(), output.matches("\r\n").count(), "{output:?}");
assert_eq!(jsonc::parse(&output, false).unwrap().get(&["mcpServers", "example", "command"]), Some(json!("example")));
}
assert!(jsonc::parse("// settings\r\n", true).is_err());
let output = jsonc::parse("", false).unwrap().set(&["x"], &json!(1)).unwrap();
assert!(!output.contains('\r'));
}