babbel_yaml 0.1.2

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// =====================================================================================
//  File: basic_parsing_tests.rs
//  Location: library/src/internal_tests/
// -------------------------------------------------------------------------------------
//  Purpose:
//      Internal tests for basic YAML parsing functionality in the babbel_yaml crate.
//      These tests cover fundamental YAML constructs such as sequences, mappings,
//      block styles, comments, and scalar values, ensuring correct parsing and
//      compliance with the YAML specification.
//
//  Context:
//      - Part of the babbel_yaml project, a Rust YAML parser/serializer.
//      - Tests are based on YAML spec examples and custom scenarios.
//      - Ensures robust handling of basic YAML syntax and edge cases.
//
// -------------------------------------------------------------------------------------
//  Test Coverage:
//      - Literal and folded block scalars
//      - Inline and block comments
//      - Sequences and mappings
//      - Scalar types (strings, numbers)
//      - Edge cases for whitespace and formatting
// =====================================================================================

#[cfg(test)]
mod tests {
    use crate::nodes::node::{BlockStyle, QuoteType};
    use crate::test_helpers::{assert_nodes_eq, parse_yaml};
    use crate::{Node, Node::Document, Numeric};

    #[test]
    fn test_literal_block_with_inline_comment() {
        let result = parse_yaml(b"literal: |\n  word1   #comment\n  word2\n");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 1);
                    if let Node::Str(content, _, block_style) = &pairs[0].1 {
                        // The comment should be ignored, and the content should be "word1   \nword2\n"
                        assert!(matches!(block_style, BlockStyle::Literal));
                        assert!(content.contains("word1"));
                        assert!(content.contains("word2"));
                        assert!(
                            !content.contains("#comment"),
                            "Comment should not be part of the content"
                        );
                    } else {
                        panic!(
                            "Expected a literal block string node, got: {:?}",
                            pairs[0].1
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_sequence() {
        let result = parse_yaml(b"- 1\n- 2\n- 3");
        let expected = Node::Documents(vec![Document(vec![Node::Array(vec![
            Node::Number(Numeric::Integer(1)),
            Node::Number(Numeric::Integer(2)),
            Node::Number(Numeric::Integer(3)),
        ])])]);
        assert_nodes_eq(&expected, &result);
    }

    #[test]
    fn test_parse_sequence_with_comments() {
        let result = parse_yaml(b"- 1\n# Comment 1\n- 2\n# Comment 2");
        let expected = Node::Documents(vec![Document(vec![Node::Array(vec![
            Node::Number(Numeric::Integer(1)),
            Node::Number(Numeric::Integer(2)),
        ])])]);
        assert_nodes_eq(&expected, &result);
    }

    #[test]
    fn test_parse_mapping() {
        let result = parse_yaml(b"key1: value1\nkey2: 42");
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![
            (
                Node::Str("key1".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value1".to_string(), QuoteType::Unquoted, BlockStyle::None),
            ),
            (
                Node::Str("key2".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(42)),
            ),
        ])])]);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_empty() {
        let result = parse_yaml(b"");
        assert_eq!(result, Node::Documents(vec![Document(vec![])]));
    }

    #[test]
    fn test_parse_invalid_char() {
        // Use test_helpers::assert_parse_error for error assertion
        crate::test_helpers::assert_parse_error(b"@invalid", "Unexpected character: @");
    }

    #[test]
    fn test_parse_comment_only() {
        let result = parse_yaml(b"# Just a comment");
        assert_eq!(result, Node::Documents(vec![Document(vec![])]));
    }

    #[test]
    fn test_parse_mapping_with_comments() {
        let result =
            parse_yaml(b"# Comment before\nkey1: value1  # Inline comment\n# Comment after");
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key1".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value1".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_boolean_values() {
        let result = parse_yaml(b"true_val: true\nfalse_val: false\nyes_val: yes\nno_val: no");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 4);
                    // Should have boolean values for true/false
                    assert!(matches!(pairs[0].1, Node::Boolean(true)));
                    assert!(matches!(pairs[1].1, Node::Boolean(false)));
                }
            }
        }
    }

    #[test]
    fn test_parse_null_values() {
        let result = parse_yaml(b"null_val: null\ntilde_val: ~\n");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 2);
                    // All should be None values
                    assert!(matches!(pairs[0].1, Node::None));
                    assert!(matches!(pairs[1].1, Node::None));
                }
            }
        }
    }

    #[test]
    fn test_parse_numeric_formats() {
        let result =
            parse_yaml(b"int: 42\nfloat: 3.14\nnegative: -123\nzero: 0\nscientific: 1.23e10");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert!(pairs.len() >= 4);
                    assert!(matches!(pairs[0].1, Node::Number(Numeric::Integer(42))));
                    assert!(matches!(pairs[1].1, Node::Number(Numeric::Float(_))));
                    assert!(matches!(pairs[2].1, Node::Number(Numeric::Integer(-123))));
                    assert!(matches!(pairs[3].1, Node::Number(Numeric::Integer(0))));
                }
            }
        }
    }

    #[test]
    fn test_parse_quoted_strings() {
        let result =
            parse_yaml(b"single: 'single quoted'\ndouble: \"double quoted\"\nunquoted: unquoted");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 3);
                    // Check that different string values are parsed correctly using as_str()
                    assert_eq!(pairs[0].1.as_str(), Some("single quoted"));
                    assert_eq!(pairs[1].1.as_str(), Some("double quoted"));
                    assert_eq!(pairs[2].1.as_str(), Some("unquoted"));
                }
            }
        }
    }

    #[test]
    fn test_parse_multiline_strings() {
        let result =
            parse_yaml(b"multiline: >\n  This is a\n  folded string\n  with multiple lines");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        assert!(content.contains("This is a"));
                        assert!(content.contains("folded string"));
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_literal_strings() {
        let result = parse_yaml(b"literal: |\n  Line 1\n  Line 2\n  Line 3");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Node::Str(content, _, block_style) = &pairs[0].1 {
                        assert!(matches!(block_style, BlockStyle::Literal));
                        assert!(content.contains("Line 1"));
                        assert!(content.contains("Line 2"));
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_nested_sequences() {
        let result = parse_yaml(b"- [1, 2, 3]\n- [a, b, c]\n- [true, false, null]");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Array(items) = &nodes[0] {
                    assert_eq!(items.len(), 3);
                    // Each item should be an array
                    assert!(matches!(items[0], Node::Array(_)));
                    assert!(matches!(items[1], Node::Array(_)));
                    assert!(matches!(items[2], Node::Array(_)));
                }
            }
        }
    }

    #[test]
    fn test_parse_nested_mappings() {
        let result = parse_yaml(b"outer:\n  inner1: value1\n  inner2: value2");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 1);
                    if let Node::Mapping(inner_pairs) = &pairs[0].1 {
                        assert_eq!(inner_pairs.len(), 2);
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_sequence_of_mappings() {
        let result = parse_yaml(b"- name: John\n  age: 30\n- name: Jane\n  age: 25");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Array(items) = &nodes[0] {
                    assert_eq!(items.len(), 2);
                    assert!(matches!(items[0], Node::Mapping(_)));
                    assert!(matches!(items[1], Node::Mapping(_)));
                }
            }
        }
    }

    #[test]
    fn test_parse_mixed_data_types() {
        let result = parse_yaml(
            b"string: hello\nnumber: 42\nboolean: true\nnull_val: null\narray: [1, 2, 3]",
        );
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 5);
                    assert!(matches!(pairs[0].1, Node::Str(_, _, _)));
                    assert!(matches!(pairs[1].1, Node::Number(_)));
                    assert!(matches!(pairs[2].1, Node::Boolean(_)));
                    assert!(matches!(pairs[3].1, Node::None));
                    assert!(matches!(pairs[4].1, Node::Array(_)));
                }
            }
        }
    }

    #[test]
    fn test_parse_empty_collections() {
        let result = parse_yaml(b"empty_array: []\nempty_object: {}");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 2);
                    if let Node::Array(arr) = &pairs[0].1 {
                        assert!(arr.is_empty());
                    }
                    if let Node::Mapping(map) = &pairs[1].1 {
                        assert!(map.is_empty());
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_7zz5_nested_with_empty_collections() {
        // Test case from 7ZZ5 - Empty flow collections with deeply nested sequences
        let yaml = b"---\nnested sequences:\n- - - []\n- - - {}\nkey1: []\nkey2: {}";
        let result = std::panic::catch_unwind(|| parse_yaml(yaml));
        assert!(
            result.is_ok(),
            "Should parse nested sequences with empty flow collections"
        );
    }

    #[test]
    fn test_parse_5c5m_trailing_comma_in_flow_mapping() {
        // Test case from 5C5M - Trailing commas in flow mappings should be allowed
        let yaml = b"- { one : two , three: four , }\n- {five: six,seven : eight}";
        let result = std::panic::catch_unwind(|| parse_yaml(yaml));
        assert!(
            result.is_ok(),
            "Should parse flow mappings with trailing commas"
        );
    }

    #[test]
    fn test_parse_unicode_content() {
        let result = parse_yaml("name: José\ncity: 北京\nemoji: 🚀".as_bytes());
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 3);

                    // Check that Unicode content is parsed (may have encoding differences)
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        assert!(content.contains("Jos") && content.len() > 3); // Allow for encoding variations
                    }
                    if let Node::Str(content, _, _) = &pairs[1].1 {
                        assert!(!content.is_empty()); // Should have Chinese characters
                    }
                    if let Node::Str(content, _, _) = &pairs[2].1 {
                        assert!(!content.is_empty()); // Should have emoji
                    }
                }
            }
        }
    }
    #[test]
    fn test_parse_escape_sequences() {
        let result = parse_yaml(b"escaped: \"Line 1\\nLine 2\\tTabbed\"");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        // Should now contain actual newline and tab characters, not escaped versions
                        assert!(
                            content.contains('\n'),
                            "Should contain actual newline character"
                        );
                        assert!(
                            content.contains('\t'),
                            "Should contain actual tab character"
                        );
                        assert_eq!(content, "Line 1\nLine 2\tTabbed");
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_special_characters_in_keys() {
        let result = parse_yaml(b"\"key with spaces\": value1\n'key-with-dashes': value2");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 2);

                    if let Node::Str(key, _, _) = &pairs[0].0 {
                        assert_eq!(key, "key with spaces");
                    }
                    if let Node::Str(key, _, _) = &pairs[1].0 {
                        assert_eq!(key, "key-with-dashes");
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_indentation_variations() {
        let result = parse_yaml(b"level1:\n  level2:\n    level3: value");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    // Should handle nested indentation correctly
                    if let Node::Mapping(level2) = &pairs[0].1 {
                        if let Node::Mapping(level3) = &level2[0].1 {
                            assert_eq!(level3.len(), 1);
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_trailing_spaces() {
        let result = parse_yaml(b"key: value   \nother: data  \n");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 2);
                    // Values should not include trailing spaces
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        assert_eq!(content, "value");
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_multiple_consecutive_spaces() {
        let result = parse_yaml(b"key:     value\nother:  data");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 2);
                    // Should handle multiple spaces after colon
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        assert_eq!(content, "value");
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_comments_in_various_positions() {
        let result = parse_yaml(b"# Header comment\nkey1: value1 # End of line\n# Mid comment\nkey2: value2\n# Footer comment");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    // Comments should be ignored, only data remains
                    assert_eq!(pairs.len(), 2);
                }
            }
        }
    }

    #[test]
    fn test_parse_inline_arrays_with_mixed_types() {
        let result = parse_yaml(b"mixed: [42, 'string', true, null, 3.14]");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Node::Array(items) = &pairs[0].1 {
                        // Check that we have mixed types parsed correctly
                        assert!(items.len() >= 3);
                        assert!(matches!(items[0], Node::Number(Numeric::Integer(42))));
                        assert!(matches!(items[1], Node::Str(_, _, _)));
                        assert!(matches!(items[2], Node::Boolean(true)));
                        // null may be parsed as a string depending on implementation
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_inline_objects_with_various_keys() {
        let result =
            parse_yaml(b"inline: {simple: value, 'quoted key': data, \"double quoted\": info}");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Node::Mapping(inline_pairs) = &pairs[0].1 {
                        assert_eq!(inline_pairs.len(), 3);
                        // Should handle different key quotation styles
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_newlines_in_content() {
        let result = parse_yaml(b"content: |\n  Line one\n  Line two\n  Line three\nother: value");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 2);
                    // Block scalar should preserve newlines
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        assert!(content.contains("Line one"));
                        assert!(content.contains("Line two"));
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_complex_nested_structure() {
        let yaml = b"users:
  - name: John
    roles: [admin, user]
    profile:
      age: 30
      active: true
  - name: Jane
    roles: [user]
    profile:
      age: 25
      active: false";

        let result = parse_yaml(yaml);
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 1);
                    // Should have users array with nested objects
                    if let Node::Array(users) = &pairs[0].1 {
                        assert_eq!(users.len(), 2);
                        assert!(matches!(users[0], Node::Mapping(_)));
                        assert!(matches!(users[1], Node::Mapping(_)));
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_zero_values() {
        let result = parse_yaml(b"zero_int: 0\nzero_float: 0.0\nfalse_bool: false");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 3);
                    assert!(matches!(pairs[0].1, Node::Number(Numeric::Integer(0))));
                    assert!(matches!(pairs[1].1, Node::Number(Numeric::Float(f)) if f == 0.0));
                    assert!(matches!(pairs[2].1, Node::Boolean(false)));
                }
            }
        }
    }

    #[test]
    fn test_parse_whitespace_only_values() {
        let result = parse_yaml(b"spaces: '   '\ntabs: '\t\t'\nmixed: ' \t '");
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 3);
                    // Should preserve whitespace in quoted strings
                    if let Node::Str(content, _, _) = &pairs[0].1 {
                        assert_eq!(content, "   ");
                    }
                }
            }
        }
    }

    #[test]
    fn test_parse_extremely_long_keys_and_values() {
        let long_key = "a".repeat(1000);
        let long_value = "b".repeat(1000);
        let yaml = format!("{}: {}", long_key, long_value);

        let result = parse_yaml(yaml.as_bytes());
        if let Node::Documents(docs) = result {
            if let Node::Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    assert_eq!(pairs.len(), 1);
                    if let Node::Str(key, _, _) = &pairs[0].0 {
                        assert_eq!(key.len(), 1000);
                    }
                    if let Node::Str(value, _, _) = &pairs[0].1 {
                        assert_eq!(value.len(), 1000);
                    }
                }
            }
        }
    }
}