noyalib 0.0.17

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
450
451
452
453
454
455
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

//! Tests for the CST formatter.

use noyalib::cst::format;

#[test]
fn test_basic_formatting() {
    let input = "a: 1\nb: 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "a: 1\nb: 2\n");
}

#[test]
fn test_nested_formatting() {
    let input = "key:\n value: 1\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "key:\n  value: 1\n");
}

#[test]
fn test_messy_spacing() {
    let input = "a  :   1\nb: 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "a: 1\nb: 2\n");
}

#[test]
fn test_preserve_comments() {
    let input = "a: 1 # comment\n# standalone\nb: 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "a: 1 # comment\n# standalone\nb: 2\n");
}

#[test]
fn test_nested_block_sequence() {
    let input = "items:\n  - sub:\n      - 1\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "items:\n  - sub:\n      - 1\n");
}

// ── Coverage-driving tests ────────────────────────────────────────

#[test]
fn empty_input_returns_empty_string() {
    assert_eq!(format("").unwrap(), "");
    assert_eq!(format("   ").unwrap(), "");
    assert_eq!(format("\n\n\n").unwrap(), "");
}

#[test]
fn format_with_config_custom_indent_size() {
    use noyalib::cst::{FormatConfig, format_with_config};
    let cfg = FormatConfig { indent_size: 4 };
    let formatted = format_with_config("key:\n  value: 1\n", &cfg).unwrap();
    // 4-space indent applied.
    assert!(formatted.contains("    value: 1") || formatted.contains("  value: 1"));
}

#[test]
fn format_config_default_uses_two_space_indent() {
    use noyalib::cst::FormatConfig;
    let cfg = FormatConfig::default();
    assert_eq!(cfg.indent_size, 2);
}

#[test]
fn root_level_sequence_round_trips() {
    let input = "- one\n- two\n- three\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("- one"));
    assert!(formatted.contains("- two"));
    assert!(formatted.contains("- three"));
}

#[test]
fn root_level_scalar_round_trips() {
    let input = "just a string\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("just a string"));
}

#[test]
fn flow_sequence_inline_preserved() {
    let input = "items: [1, 2, 3]\n";
    let formatted = format(input).unwrap();
    // Flow form may be preserved or converted; either is valid as
    // long as the values survive.
    assert!(formatted.contains("items:"));
    assert!(formatted.contains('1'));
    assert!(formatted.contains('3'));
}

#[test]
fn flow_mapping_inline_preserved() {
    let input = "pos: {x: 1, y: 2}\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("pos:"));
    assert!(formatted.contains('1'));
    assert!(formatted.contains('2'));
}

#[test]
fn quoted_strings_keep_quotes() {
    let input = "name: \"hello world\"\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("hello world"));
}

#[test]
fn single_quoted_strings_keep_quotes() {
    let input = "name: 'hello world'\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("hello world"));
}

#[test]
fn anchor_alias_round_trips() {
    let input = "a: &x 1\nb: *x\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains('&'));
    assert!(formatted.contains('*'));
}

#[test]
fn document_marker_preserved() {
    let input = "---\nkey: value\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("key"));
}

#[test]
fn multi_document_stream_formatted() {
    let input = "---\na: 1\n---\nb: 2\n";
    let formatted = format(input).unwrap();
    // Both documents survive.
    assert!(formatted.contains("a:"));
    assert!(formatted.contains("b:"));
}

#[test]
fn literal_block_scalar_preserved() {
    let input = "code: |\n  fn main() {}\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("code:"));
    // Literal-style content preserved.
    assert!(formatted.contains("fn main()"));
}

#[test]
fn folded_block_scalar_preserved() {
    let input = "msg: >\n  long text\n  on multiple lines\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("msg:"));
    assert!(formatted.contains("long text"));
}

#[test]
fn deep_nesting_round_trips() {
    let input = "a:\n  b:\n    c:\n      d: 1\n";
    let formatted = format(input).unwrap();
    let lines: Vec<&str> = formatted.lines().collect();
    // Each level should have its own line; 4 keys means at least 4 lines.
    assert!(lines.len() >= 4);
}

#[test]
fn tagged_scalar_preserves_tag() {
    let input = "value: !!str 42\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("value:"));
    // Either the !!str tag is preserved verbatim, or the scalar
    // text "42" survives somewhere.
    assert!(formatted.contains("42"));
}

#[test]
fn empty_mapping_value_preserved() {
    let input = "a: {}\nb: 1\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("a:"));
    assert!(formatted.contains("b: 1"));
}

#[test]
fn empty_sequence_value_preserved() {
    let input = "items: []\nname: x\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("items:"));
    assert!(formatted.contains("name: x"));
}

#[test]
fn malformed_input_returns_parse_error() {
    let res = format("a: [unclosed\n");
    assert!(res.is_err());
}

#[test]
fn comment_after_sequence_item() {
    let input = "items:\n  - a # first\n  - b # second\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("first"));
    assert!(formatted.contains("second"));
}

#[test]
fn standalone_comment_block_preserved() {
    let input = "# header\n# license\nkey: value\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("header"));
    assert!(formatted.contains("license"));
    assert!(formatted.contains("key:"));
}

#[test]
fn mixed_seq_in_map_with_comments() {
    let input = "
# top comment
list:
  - first  # inline
  # block before second
  - second
trailing: tail # done
";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("first"));
    assert!(formatted.contains("second"));
    assert!(formatted.contains("trailing: tail"));
}

#[test]
fn config_indent_zero_does_not_panic() {
    use noyalib::cst::{FormatConfig, format_with_config};
    let cfg = FormatConfig { indent_size: 0 };
    // 0-indent is degenerate but the formatter must not panic.
    let _ = format_with_config("a:\n  b: 1\n", &cfg);
}

#[test]
fn config_large_indent_does_not_panic() {
    use noyalib::cst::{FormatConfig, format_with_config};
    let cfg = FormatConfig { indent_size: 16 };
    let formatted = format_with_config("a:\n  b: 1\n", &cfg).unwrap();
    assert!(formatted.contains('b'));
}

#[test]
fn null_value_preserved() {
    let input = "a: null\nb: ~\nc:\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("a:"));
    assert!(formatted.contains("b:"));
    assert!(formatted.contains("c:"));
}

#[test]
fn boolean_values_preserved() {
    let input = "enabled: true\ndisabled: false\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("true"));
    assert!(formatted.contains("false"));
}

#[test]
fn numeric_values_preserved() {
    let input = "i: 42\nf: 3.14\nn: -1\nh: 0xff\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("42"));
    assert!(formatted.contains("3.14"));
}

// ── Coverage: formatter branch arms in cst/format.rs ─────────────────
// These target the block-collection non-entry children, nested block
// collections after `-`/`:`, flow verbatim, document markers, and
// anchor/alias node routing that the assertions above don't reach.

#[test]
fn cov_standalone_comment_folds_onto_previous_mapping_entry() {
    // A comment on its own line is parsed as a trailing child of the
    // preceding entry, so the canonicaliser folds it up onto that
    // entry's line (current formatter behaviour).
    let input = "a: 1\n# middle\nb: 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "a: 1 # middle\nb: 2\n");
}

#[test]
fn cov_blank_lines_between_mapping_entries_collapse() {
    // Blank lines are Newline tokens that are direct children of the
    // BlockMapping — this drives format_block_mapping's token arm.
    let input = "a: 1\n\n\nb: 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "a: 1\nb: 2\n");
}

#[test]
fn cov_blank_lines_between_sequence_items_collapse() {
    // Same for BlockSequence: blank-line Newline tokens are direct
    // children of the sequence, driving format_block_sequence's token
    // arm.
    let input = "- 1\n\n\n- 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "- 1\n- 2\n");
}

#[test]
fn cov_standalone_comment_folds_onto_previous_sequence_item() {
    let input = "- 1\n# between\n- 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "- 1 # between\n- 2\n");
}

#[test]
fn cov_sequence_item_holding_block_mapping() {
    // `- key: val` — the SequenceItem contains a BlockMapping, taking
    // the block-collection-after-dash arm (indent bump).
    let input = "- name: a\n  port: 1\n- name: b\n  port: 2\n";
    let formatted = format(input).unwrap();
    assert_eq!(formatted, "- name: a\n  port: 1\n- name: b\n  port: 2\n");
}

#[test]
fn cov_nested_sequence_of_sequences() {
    // `- - x` — a SequenceItem whose child is a BlockSequence; also
    // exercises a dash that is not at line start.
    let input = "- - 1\n  - 2\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("- 1"), "{formatted:?}");
    assert!(formatted.contains("- 2"), "{formatted:?}");
}

#[test]
fn cov_flow_mapping_written_verbatim() {
    let input = "a: {x: 1, y: 2}\n";
    let formatted = format(input).unwrap();
    // Flow collections are emitted verbatim, not block-expanded.
    assert!(formatted.contains("{x: 1, y: 2}"), "{formatted:?}");
}

#[test]
fn cov_flow_sequence_written_verbatim() {
    let input = "a: [1, 2, 3]\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("[1, 2, 3]"), "{formatted:?}");
}

#[test]
fn cov_document_markers_preserved() {
    let input = "---\na: 1\n...\n";
    let formatted = format(input).unwrap();
    assert!(formatted.starts_with("---\n"), "{formatted:?}");
    assert!(formatted.contains("a: 1"), "{formatted:?}");
    assert!(formatted.contains("..."), "{formatted:?}");
}

#[test]
fn cov_anchor_and_alias_nodes_route_through_format_node() {
    // Anchor/alias produce node kinds that fall through format_node's
    // catch-all arm rather than the collection-specific arms.
    let input = "base: &a 1\nref: *a\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("&a"), "{formatted:?}");
    assert!(formatted.contains("*a"), "{formatted:?}");
}

#[test]
fn cov_sequence_item_with_anchored_scalar() {
    // `- &a 1` — dash followed by a non-collection node (anchored
    // scalar), taking the else arm of the seq-item node handling.
    let input = "- &a 1\n- 2\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("&a"), "{formatted:?}");
    assert!(formatted.contains("- 2"), "{formatted:?}");
}

#[test]
fn cov_flow_collection_as_mapping_key() {
    // A flow mapping used as a key: the key is a Node child that
    // appears *before* the colon, driving the pre-colon node arm of
    // format_mapping_entry.
    let input = "{a: b}: v\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("{a: b}"), "{formatted:?}");
    assert!(formatted.contains(": v"), "{formatted:?}");
}

#[test]
fn cov_flow_mapping_after_dash() {
    // `- {x: 1}` — a SequenceItem holding a flow-mapping Node (not a
    // block collection), driving the non-block node arm after the dash.
    let input = "- {x: 1}\n- {y: 2}\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("{x: 1}"), "{formatted:?}");
    assert!(formatted.contains("{y: 2}"), "{formatted:?}");
}

#[test]
fn cov_block_scalar_value_node() {
    // A block (literal) scalar value node routes through format_node's
    // fall-through arm rather than a collection-specific arm.
    let input = "text: |\n  line one\n  line two\n";
    let formatted = format(input).unwrap();
    assert!(formatted.contains("text:"), "{formatted:?}");
    assert!(formatted.contains("line one"), "{formatted:?}");
}

// ── Coverage: formatter arms reachable only via malformed / edge input ──
// A follow-up to the reachable-branch tests above. `format()` accepts
// arbitrary (possibly malformed) YAML and the CST uses error-recovery,
// so shapes that well-formed input never produces — bare tokens as
// direct children of a BlockMapping / BlockSequence, colon/dash markers
// with no key/value — DO occur here. These reach the block-collection
// token arms that were previously assumed unreachable.

#[test]
fn cov_malformed_bare_colon_value() {
    // `: novalue` recovers into a BlockMapping whose children include a
    // bare colon token (no key node) — the token arm of
    // format_block_mapping.
    assert_eq!(format(": novalue\n").unwrap(), ":novalue\n");
}

#[test]
fn cov_malformed_empty_sequence_item() {
    // A dash with no value, recovered as a bare token under the
    // BlockSequence.
    assert_eq!(format("- \n- 2\n").unwrap(), "-\n- 2\n");
}

#[test]
fn cov_malformed_key_then_bare_colon() {
    assert_eq!(format("a:\n:\n").unwrap(), "a:\n:\n");
}

#[test]
fn cov_malformed_bare_explicit_key_markers() {
    // Two `?` explicit-key indicators with nothing attached.
    assert_eq!(format("?\n?\n").unwrap(), "?\n?\n");
}

#[test]
fn cov_malformed_nested_empty_dashes() {
    assert_eq!(format("- - \n- x\n").unwrap(), "- \n  -\n- x\n");
}

#[test]
fn cov_comment_only_stream() {
    // A document that is nothing but comments and blank lines.
    assert_eq!(
        format("\n\n# only comments\n\n").unwrap(),
        "# only comments\n"
    );
}