noyalib 0.0.19

A pure Rust YAML library with zero unsafe code and full serde integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

//! Phase 2A mutation tests.
//!
//! Each test parses a YAML document, applies a path-targeted edit
//! via [`noyalib::cst::Document::set`] or `replace_span`, and
//! checks that the result is byte-identical to the expected output
//! — surrounding indentation, comments, and other entries are
//! preserved verbatim.

#![allow(missing_docs)]

use noyalib::Value;
use noyalib::cst::parse_document;

#[test]
fn set_replaces_only_the_target_value() {
    let mut doc = parse_document("name: foo\nversion: 0.0.1\n").unwrap();
    doc.set("version", "0.0.2").unwrap();
    assert_eq!(doc.to_string(), "name: foo\nversion: 0.0.2\n");
}

#[test]
fn set_preserves_inline_comment() {
    let mut doc = parse_document("name: foo  # the project\nversion: 0.0.1\n").unwrap();
    doc.set("version", "0.0.2").unwrap();
    assert_eq!(
        doc.to_string(),
        "name: foo  # the project\nversion: 0.0.2\n"
    );
}

#[test]
fn set_preserves_blank_lines_between_entries() {
    let src = "name: foo\n\n\nversion: 0.0.1\n";
    let mut doc = parse_document(src).unwrap();
    doc.set("version", "0.0.2").unwrap();
    assert_eq!(doc.to_string(), "name: foo\n\n\nversion: 0.0.2\n");
}

#[test]
fn set_targets_nested_mapping_value() {
    let src = "package:\n  name: foo\n  version: 0.0.1\n";
    let mut doc = parse_document(src).unwrap();
    doc.set("package.version", "0.0.2").unwrap();
    assert_eq!(doc.to_string(), "package:\n  name: foo\n  version: 0.0.2\n");
}

#[test]
fn set_targets_sequence_index() {
    let src = "items:\n  - one\n  - two\n  - three\n";
    let mut doc = parse_document(src).unwrap();
    doc.set("items[1]", "TWO").unwrap();
    assert_eq!(doc.to_string(), "items:\n  - one\n  - TWO\n  - three\n");
}

#[test]
fn set_can_introduce_quotes() {
    let mut doc = parse_document("version: 1.2\n").unwrap();
    doc.set("version", "\"1.2\"").unwrap();
    assert_eq!(doc.to_string(), "version: \"1.2\"\n");
}

#[test]
fn set_returns_path_not_found_error() {
    let mut doc = parse_document("name: foo\n").unwrap();
    let err = doc.set("missing.key", "x").unwrap_err();
    assert!(err.to_string().contains("path not found"));
}

#[test]
fn set_with_invalid_replacement_surfaces_error_on_read() {
    // Phase A.2 lazy: the local-repair fast path commits
    // optimistically when the spliced fragment passes its own
    // (scanner-level) validation. Cross-document structural errors
    // — like an unclosed flow indicator at end-of-input — only
    // surface on the next typed-view read. Callers that need an
    // eager check call [`Document::validate`].
    let mut doc = parse_document("name: foo\n").unwrap();
    // The bare `[` opens a flow sequence that is never closed —
    // structurally broken at the document level.
    doc.set("name", "[").unwrap();
    // Source reflects the optimistic splice; round-trip via the
    // green tree still works.
    assert_eq!(doc.to_string(), "name: [\n");
    // Eager check: `validate()` surfaces the document-level parse
    // error as a regular `Result` — no panic.
    let err = doc
        .validate()
        .expect_err("validate must reject broken source");
    let msg = err.to_string();
    assert!(
        msg.contains("flow") || msg.contains("expected") || msg.contains("end"),
        "validate error should reference the parse failure; got {msg:?}",
    );
    // The typed view also refuses to materialise (it panics on
    // first read), but `validate` is the supported way to detect
    // this without unwinding.
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let _ = doc.as_value();
    }));
    assert!(result.is_err(), "as_value() must panic on invalid source");
}

#[test]
fn validate_succeeds_on_freshly_parsed_document() {
    let doc = parse_document("a: 1\nb: 2\n").unwrap();
    doc.validate()
        .expect("freshly-parsed document must validate");
}

#[test]
fn validate_succeeds_after_safe_edit() {
    let mut doc = parse_document("version: 0.0.1\n").unwrap();
    doc.set("version", "0.0.2").unwrap();
    doc.validate()
        .expect("safe edit must leave document in valid state");
}

#[test]
fn validate_is_idempotent() {
    // Once validation has populated the typed cache, repeated
    // calls are cheap and still succeed — they early-return.
    let doc = parse_document("k: v\n").unwrap();
    for _ in 0..3 {
        doc.validate()
            .expect("repeated validate calls must succeed");
    }
}

#[test]
fn span_at_returns_none_for_missing_path() {
    let doc = parse_document("name: foo\n").unwrap();
    assert!(doc.span_at("missing").is_none());
}

#[test]
fn get_returns_source_slice() {
    let doc = parse_document("items:\n  - one\n  - two\n").unwrap();
    assert_eq!(doc.get("items[0]"), Some("one"));
    assert_eq!(doc.get("items[1]"), Some("two"));
}

#[test]
fn replace_span_round_trip_after_no_op() {
    let src = "key: value\nother: 1\n";
    let mut doc = parse_document(src).unwrap();
    // Replace the same bytes with themselves — should be a no-op.
    let (s, e) = doc.span_at("key").unwrap();
    let original = doc.source()[s..e].to_owned();
    doc.replace_span(s, e, &original).unwrap();
    assert_eq!(doc.to_string(), src);
}

#[test]
fn replace_span_rejects_non_char_boundary() {
    // Multi-byte UTF-8 in the source, then attempt to splice mid-character.
    let mut doc = parse_document("emoji: 🦀\n").unwrap();
    // The crab emoji is 4 bytes starting at index 7. Position 8 is mid-glyph.
    let err = doc.replace_span(8, 9, "x").unwrap_err();
    assert!(err.to_string().contains("character boundary"));
}

// ── set_value: typed mutation with style matching ────────────────

#[test]
fn set_value_string_into_plain_site_emits_plain() {
    let mut doc = parse_document("name: foo\n").unwrap();
    doc.set_value("name", &Value::String("bar".into())).unwrap();
    assert_eq!(doc.to_string(), "name: bar\n");
}

#[test]
fn set_value_string_into_double_quoted_site_quotes() {
    let mut doc = parse_document("name: \"foo\"\n").unwrap();
    doc.set_value("name", &Value::String("bar".into())).unwrap();
    assert_eq!(doc.to_string(), "name: \"bar\"\n");
}

#[test]
fn set_value_string_into_single_quoted_site_quotes() {
    let mut doc = parse_document("name: 'foo'\n").unwrap();
    doc.set_value("name", &Value::String("bar".into())).unwrap();
    assert_eq!(doc.to_string(), "name: 'bar'\n");
}

#[test]
fn set_value_string_with_embedded_quote_escapes_in_double_quoted_site() {
    let mut doc = parse_document("title: \"hello\"\n").unwrap();
    doc.set_value("title", &Value::String("she said \"hi\"".into()))
        .unwrap();
    assert_eq!(doc.to_string(), "title: \"she said \\\"hi\\\"\"\n");
}

#[test]
fn set_value_string_with_embedded_quote_doubles_in_single_quoted_site() {
    let mut doc = parse_document("title: 'a'\n").unwrap();
    doc.set_value("title", &Value::String("it's nice".into()))
        .unwrap();
    assert_eq!(doc.to_string(), "title: 'it''s nice'\n");
}

#[test]
fn set_value_string_falls_back_to_double_quoted_when_plain_is_unsafe() {
    // Existing site is a plain scalar but the new content cannot be
    // expressed plainly (`true` would resolve to a bool).
    let mut doc = parse_document("kind: foo\n").unwrap();
    doc.set_value("kind", &Value::String("true".into()))
        .unwrap();
    assert_eq!(doc.to_string(), "kind: \"true\"\n");
}

#[test]
fn set_value_number_emits_plain_regardless_of_existing_style() {
    let mut doc = parse_document("count: \"7\"\n").unwrap();
    doc.set_value("count", &Value::Number(42.into())).unwrap();
    assert_eq!(doc.to_string(), "count: 42\n");
    // Round-trip: the new value parses back as a number, not a string.
    assert_eq!(doc.as_value()["count"].as_i64(), Some(42));
}

#[test]
fn set_value_bool_and_null() {
    let mut doc = parse_document("a: 1\nb: 1\n").unwrap();
    doc.set_value("a", &Value::Bool(false)).unwrap();
    doc.set_value("b", &Value::Null).unwrap();
    assert_eq!(doc.to_string(), "a: false\nb: null\n");
}

#[test]
fn set_value_into_sequence_index_emits_matching_style() {
    let mut doc = parse_document("xs: ['one', 'two']\n").unwrap();
    doc.set_value("xs[1]", &Value::String("TWO".into()))
        .unwrap();
    assert_eq!(doc.to_string(), "xs: ['one', 'TWO']\n");
}

#[test]
fn set_value_rejects_collection_replacement() {
    let mut doc = parse_document("items: 1\n").unwrap();
    let err = doc
        .set_value("items", &Value::Sequence(vec![Value::Number(1.into())]))
        .unwrap_err();
    assert!(err.to_string().contains("collection"));
}

#[test]
fn set_value_at_block_scalar_target_collapses_to_plain_for_single_line() {
    // Phase 2: replacing a block scalar with a single-line string
    // emits a plain/quoted scalar rather than `|-\n  hello`.
    let mut doc = parse_document("text: |\n  line1\n  line2\n").unwrap();
    doc.set_value("text", &Value::String("hello".into()))
        .unwrap();
    assert!(
        doc.to_string().contains("text: hello"),
        "expected plain replacement, got: {doc}"
    );
}

#[test]
fn set_value_at_block_scalar_target_keeps_block_form_for_multiline() {
    // Multi-line replacement keeps the block-scalar form so the
    // result still looks like a block scalar.
    let mut doc = parse_document("text: |\n  line1\n  line2\n").unwrap();
    doc.set_value("text", &Value::String("alpha\nbeta\n".into()))
        .unwrap();
    let out = doc.to_string();
    assert!(out.contains("text: |\n  alpha\n  beta\n"), "got: {out}");
}

// ── remove: drop a mapping key or sequence index ─────────────────

#[test]
fn remove_middle_mapping_key() {
    let mut doc = parse_document("a: 1\nb: 2\nc: 3\n").unwrap();
    doc.remove("b").unwrap();
    assert_eq!(doc.to_string(), "a: 1\nc: 3\n");
}

#[test]
fn remove_first_mapping_key() {
    let mut doc = parse_document("a: 1\nb: 2\nc: 3\n").unwrap();
    doc.remove("a").unwrap();
    assert_eq!(doc.to_string(), "b: 2\nc: 3\n");
}

#[test]
fn remove_last_mapping_key() {
    let mut doc = parse_document("a: 1\nb: 2\nc: 3\n").unwrap();
    doc.remove("c").unwrap();
    assert_eq!(doc.to_string(), "a: 1\nb: 2\n");
}

#[test]
fn remove_nested_mapping_key() {
    let src = "package:\n  name: foo\n  version: 0.0.1\n  build: 7\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("package.version").unwrap();
    assert_eq!(doc.to_string(), "package:\n  name: foo\n  build: 7\n");
}

#[test]
fn remove_sequence_index() {
    let src = "items:\n  - one\n  - two\n  - three\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("items[1]").unwrap();
    assert_eq!(doc.to_string(), "items:\n  - one\n  - three\n");
}

#[test]
fn remove_first_sequence_item() {
    let src = "items:\n  - one\n  - two\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("items[0]").unwrap();
    assert_eq!(doc.to_string(), "items:\n  - two\n");
}

#[test]
fn remove_returns_path_not_found_for_missing_key() {
    let mut doc = parse_document("a: 1\nb: 2\n").unwrap();
    let err = doc.remove("missing").unwrap_err();
    assert!(err.to_string().contains("path not found"));
}

#[test]
fn remove_rejects_only_entry_of_mapping() {
    let mut doc = parse_document("only: 1\n").unwrap();
    let err = doc.remove("only").unwrap_err();
    assert!(err.to_string().contains("only entry"));
}

#[test]
fn remove_rejects_only_entry_of_sequence() {
    let mut doc = parse_document("xs:\n  - a\n").unwrap();
    let err = doc.remove("xs[0]").unwrap_err();
    assert!(err.to_string().contains("only entry"));
}

#[test]
fn remove_multi_line_block_scalar() {
    // Removing a key whose value is a block scalar now removes the
    // whole entry — key through its last owned line — guarded by a
    // re-parse and a typed-value oracle.
    let mut doc = parse_document("a: 1\ntext: |\n  hello\n  world\nb: 2\n").unwrap();
    doc.remove("text").unwrap();
    assert_eq!(doc.source(), "a: 1\nb: 2\n");
}

#[test]
fn remove_preserves_surrounding_comments() {
    let src = "# header\na: 1\nb: 2  # tail\nc: 3\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("b").unwrap();
    assert_eq!(doc.to_string(), "# header\na: 1\nc: 3\n");
}

// ── remove: the trivia an entry owns ─────────────────────────────
//
// An entry is more than its key and value bytes. The comment above it
// documents it, and a keep-chomped scalar's kept blank lines are its
// content — both go when it goes. A comment *after* its last content
// line documents whatever comes next, and stays.

#[test]
fn remove_takes_the_entrys_head_comment_with_it() {
    // Left behind, `# database connection settings` would silently
    // become documentation for `cache` — a plausible-looking document
    // that says something the author never wrote.
    let src = "# database connection settings\ndatabase:\n  host: localhost\ncache:\n  ttl: 60\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("database").unwrap();
    assert_eq!(doc.to_string(), "cache:\n  ttl: 60\n");
}

#[test]
fn remove_takes_a_multi_line_head_comment_with_it() {
    let src = "keep: 0\n# line one\n# line two\nblock:\n  a: 1\ntail: 9\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("block").unwrap();
    assert_eq!(doc.to_string(), "keep: 0\ntail: 9\n");
}

#[test]
fn remove_takes_the_head_comment_of_a_sequence_item() {
    let src = "items:\n  # the first one\n  - one\n  - two\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("items[0]").unwrap();
    assert_eq!(doc.to_string(), "items:\n  - two\n");
}

#[test]
fn remove_leaves_a_head_comment_detached_by_a_blank_line() {
    // The blank line is the author's way of saying the comment heads the
    // document rather than the entry, so it survives.
    let src = "# detached\n\nblock:\n  a: 1\ntail: 9\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("block").unwrap();
    assert_eq!(doc.to_string(), "# detached\n\ntail: 9\n");
}

#[test]
fn remove_leaves_a_head_comment_at_a_different_indent() {
    // `# outer note` is indented under `top`, not aligned with `b`, so it
    // is not `b`'s head comment.
    let src = "top:\n  a: 1\n    # deeper note\n  b: 2\n  c: 3\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("top.b").unwrap();
    assert_eq!(doc.to_string(), "top:\n  a: 1\n    # deeper note\n  c: 3\n");
}

#[test]
fn remove_takes_a_keep_chomped_scalars_kept_blank_lines_with_it() {
    // `|+` keeps the trailing blank lines as part of the scalar, so they
    // belong to `script` and go with it — no stray blank left behind.
    let src = "a: 1\nscript: |+\n  x\n\nb: 2\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("script").unwrap();
    assert_eq!(doc.to_string(), "a: 1\nb: 2\n");
}

#[test]
fn remove_leaves_a_trailing_comment_that_documents_the_next_entry() {
    // `# note for next` follows `outer`'s last content line and sits
    // outside its value span (`span_at("outer")` stops at `  a: 1`), so
    // removing `outer` must not take it.
    let src = "outer:\n  a: 1\n  # note for next\nnext: 2\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("outer").unwrap();
    assert_eq!(doc.to_string(), "  # note for next\nnext: 2\n");
}

#[test]
fn remove_takes_a_comment_interleaved_inside_the_value() {
    // The counterpart to the test above: `# inner note` lies *within*
    // `outer`'s value span, so it is part of the entry and goes with it.
    let src = "outer:\n  a: 1\n  # inner note\n  b: 2\nother: 3\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("outer").unwrap();
    assert_eq!(doc.to_string(), "other: 3\n");
}

#[test]
fn remove_head_comment_rules_hold_for_crlf_input() {
    let src = "keep: 0\r\n# doc for block\r\nblock:\r\n  a: 1\r\ntail: 9\r\n";
    let mut doc = parse_document(src).unwrap();
    doc.remove("block").unwrap();
    assert_eq!(doc.to_string(), "keep: 0\r\ntail: 9\r\n");
}