r3bl_tui 0.7.7

TUI library to build modern apps inspired by React, Elm, with Flexbox, CSS, editor component, emoji support, and more
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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
// Copyright (c) 2025 R3BL LLC. Licensed under Apache License, Version 2.0.

//! ## Snapshot tests for legacy parser
//!
//! This module contains comprehensive tests that verify the legacy parser produces
//! correct output for ALL markdown inputs using the test data from
//! `conformance_test_data`.
//!
//! These tests ensure that:
//! 1. The parser handles all valid markdown constructs correctly
//! 2. Edge cases and invalid inputs are handled gracefully
//! 3. The parser output remains consistent across code changes

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use crate::{BulletKind, CodeBlockLine, HeadingLevel, List};
    use crate::{CodeBlockLineContent, HeadingData, HyperlinkData, MdDocument, MdElement,
                MdLineFragment, ZeroCopyGapBuffer, md_parser::conformance_test_data::*,
                parse_markdown};

    /// Macro for creating a List from elements
    #[allow(unused_macros)]
    macro_rules! list {
        ( $($elem:expr),* $(,)? ) => {
            List::from(vec![$($elem),*])
        };
    }

    /// Helper to assert document has expected number of elements
    fn assert_doc_len(doc: &MdDocument, expected: usize) {
        assert_eq!(
            doc.len(),
            expected,
            "Document has {} elements, expected {}",
            doc.len(),
            expected
        );
    }

    /// Helper to assert an element is text with specific fragments
    fn assert_text_element(element: &MdElement, expected_fragments: &[MdLineFragment]) {
        match element {
            MdElement::Text(fragments) => {
                assert_eq!(
                    fragments.len(),
                    expected_fragments.len(),
                    "Text element has {} fragments, expected {}",
                    fragments.len(),
                    expected_fragments.len()
                );
                for (i, (actual, expected)) in
                    fragments.iter().zip(expected_fragments.iter()).enumerate()
                {
                    assert_eq!(
                        actual, expected,
                        "Fragment {i} mismatch: {actual:?} != {expected:?}"
                    );
                }
            }
            _ => panic!("Expected Text element, got {element:?}"),
        }
    }

    /// Helper to assert an element is a heading
    fn assert_heading_element(element: &MdElement, level: usize, text: &str) {
        match element {
            MdElement::Heading(HeadingData {
                level: actual_level,
                text: actual_text,
            }) => {
                assert_eq!(actual_level.level, level, "Heading level mismatch");
                assert_eq!(*actual_text, text, "Heading text mismatch");
            }
            _ => panic!("Expected Heading element, got {element:?}"),
        }
    }

    /// Helper to assert metadata elements
    fn assert_title_element(element: &MdElement, expected: &str) {
        match element {
            MdElement::Title(actual) => assert_eq!(*actual, expected, "Title mismatch"),
            _ => panic!("Expected Title element, got {element:?}"),
        }
    }

    fn assert_tags_element(element: &MdElement, expected: &[&str]) {
        match element {
            MdElement::Tags(tags) => {
                assert_eq!(tags.len(), expected.len(), "Tags count mismatch");
                for (actual, expected) in tags.iter().zip(expected.iter()) {
                    assert_eq!(actual, expected, "Tag mismatch");
                }
            }
            _ => panic!("Expected Tags element, got {element:?}"),
        }
    }

    fn assert_authors_element(element: &MdElement, expected: &[&str]) {
        match element {
            MdElement::Authors(authors) => {
                assert_eq!(authors.len(), expected.len(), "Authors count mismatch");
                for (actual, expected) in authors.iter().zip(expected.iter()) {
                    assert_eq!(actual, expected, "Author mismatch");
                }
            }
            _ => panic!("Expected Authors element, got {element:?}"),
        }
    }

    fn assert_date_element(element: &MdElement, expected: &str) {
        match element {
            MdElement::Date(actual) => assert_eq!(*actual, expected, "Date mismatch"),
            _ => panic!("Expected Date element, got {element:?}"),
        }
    }

    // =============================================================================
    // Small valid input tests.
    // =============================================================================

    #[test]
    fn test_small_empty_string() {
        let gap_buffer = ZeroCopyGapBuffer::from(EMPTY_STRING);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 0);
    }

    #[test]
    fn test_small_only_newlines() {
        // ONLY_NEWLINES = "\n\n\n".
        let gap_buffer = ZeroCopyGapBuffer::from(ONLY_NEWLINES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 3);
        // Each newline becomes an empty Text element.
        for element in doc.iter() {
            assert_text_element(element, &[]);
        }
    }

    #[test]
    fn test_small_single_line_no_newline() {
        // SINGLE_LINE_NO_NEWLINE = "Hello World".
        let gap_buffer = ZeroCopyGapBuffer::from(SINGLE_LINE_NO_NEWLINE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(&doc[0], &[MdLineFragment::Plain("Hello World")]);
    }

    #[test]
    fn test_small_single_line_with_newline() {
        // SINGLE_LINE_WITH_NEWLINE = "Hello World\n".
        let gap_buffer = ZeroCopyGapBuffer::from(SINGLE_LINE_WITH_NEWLINE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(&doc[0], &[MdLineFragment::Plain("Hello World")]);
    }

    #[test]
    fn test_small_simple_inline_code() {
        // SIMPLE_INLINE_CODE = "first\n`second`".
        let gap_buffer = ZeroCopyGapBuffer::from(SIMPLE_INLINE_CODE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 2);
        assert_text_element(&doc[0], &[MdLineFragment::Plain("first")]);
        assert_text_element(&doc[1], &[MdLineFragment::InlineCode("second")]);
    }

    #[test]
    fn test_small_inline_code_variations() {
        // INLINE_CODE_VARIATIONS contains multiple inline code examples.
        let gap_buffer = ZeroCopyGapBuffer::from(INLINE_CODE_VARIATIONS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // This test data has multiple lines with inline code.
        assert!(!doc.is_empty());
        for element in doc.iter() {
            match element {
                MdElement::Text(fragments) => {
                    // Verify inline code fragments exist.
                    let has_inline_code = fragments
                        .iter()
                        .any(|f| matches!(f, MdLineFragment::InlineCode(_)));
                    assert!(
                        has_inline_code || fragments.is_empty(),
                        "Expected inline code or empty line"
                    );
                }
                _ => panic!("Expected only Text elements"),
            }
        }
    }

    #[test]
    fn test_small_inline_code_with_unicode() {
        // INLINE_CODE_WITH_UNICODE = "`code 🎯`".
        let gap_buffer = ZeroCopyGapBuffer::from(INLINE_CODE_WITH_UNICODE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(&doc[0], &[MdLineFragment::InlineCode("code 🎯")]);
    }

    #[test]
    fn test_small_bold_text() {
        // BOLD_TEXT = "This is *bold* text".
        let gap_buffer = ZeroCopyGapBuffer::from(BOLD_TEXT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(
            &doc[0],
            &[
                MdLineFragment::Plain("This is "),
                MdLineFragment::Bold("bold"),
                MdLineFragment::Plain(" text"),
            ],
        );
    }

    #[test]
    fn test_small_italic_text() {
        // ITALIC_TEXT = "This is _italic_ text".
        let gap_buffer = ZeroCopyGapBuffer::from(ITALIC_TEXT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(
            &doc[0],
            &[
                MdLineFragment::Plain("This is "),
                MdLineFragment::Italic("italic"),
                MdLineFragment::Plain(" text"),
            ],
        );
    }

    #[test]
    fn test_small_mixed_formatting() {
        // MIXED_FORMATTING contains mixed bold, italic, and inline code.
        let gap_buffer = ZeroCopyGapBuffer::from(MIXED_FORMATTING);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert!(!doc.is_empty());
        // Verify mixed formatting elements exist.
        for element in doc.iter() {
            if let MdElement::Text(fragments) = element {
                let has_formatting = fragments.iter().any(|f| {
                    matches!(
                        f,
                        MdLineFragment::Bold(_)
                            | MdLineFragment::Italic(_)
                            | MdLineFragment::InlineCode(_)
                    )
                });
                assert!(has_formatting || fragments.is_empty());
            }
        }
    }

    #[test]
    fn test_small_links() {
        // LINKS = "Check out [Rust](https://rust-lang.org) website".
        let gap_buffer = ZeroCopyGapBuffer::from(LINKS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(
            &doc[0],
            &[
                MdLineFragment::Plain("Check out "),
                MdLineFragment::Link(HyperlinkData {
                    text: "Rust",
                    url: "https://rust-lang.org",
                }),
                MdLineFragment::Plain(" website"),
            ],
        );
    }

    #[test]
    fn test_small_images() {
        // IMAGES = "![Alt text](https://example.com/image.png)".
        let gap_buffer = ZeroCopyGapBuffer::from(IMAGES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_text_element(
            &doc[0],
            &[MdLineFragment::Image(HyperlinkData {
                text: "Alt text",
                url: "https://example.com/image.png",
            })],
        );
    }

    #[test]
    fn test_small_metadata_title() {
        // METADATA_TITLE = "@title: My Document Title".
        let gap_buffer = ZeroCopyGapBuffer::from(METADATA_TITLE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_title_element(&doc[0], "My Document Title");
    }

    #[test]
    fn test_small_metadata_tags() {
        // METADATA_TAGS = "@tags: rust, programming, tutorial".
        let gap_buffer = ZeroCopyGapBuffer::from(METADATA_TAGS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_tags_element(&doc[0], &["rust", "programming", "tutorial"]);
    }

    #[test]
    fn test_small_metadata_authors() {
        // METADATA_AUTHORS = "@authors: John Doe, Jane Smith".
        let gap_buffer = ZeroCopyGapBuffer::from(METADATA_AUTHORS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_authors_element(&doc[0], &["John Doe", "Jane Smith"]);
    }

    #[test]
    fn test_small_metadata_date() {
        // METADATA_DATE = "@date: 2025-01-01".
        let gap_buffer = ZeroCopyGapBuffer::from(METADATA_DATE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_date_element(&doc[0], "2025-01-01");
    }

    #[test]
    fn test_small_special_characters() {
        // SPECIAL_CHARACTERS contains special characters like.
        // !@#$%^&*()_+-=[]{}|;':",./<>?
        let gap_buffer = ZeroCopyGapBuffer::from(SPECIAL_CHARACTERS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        // Special characters should be preserved as plain text.
        match &doc[0] {
            MdElement::Text(fragments) => {
                assert!(!fragments.is_empty());
                // All special characters should be in Plain fragments.
                for fragment in fragments.iter() {
                    assert!(matches!(fragment, MdLineFragment::Plain(_)));
                }
            }
            _ => panic!("Expected Text element"),
        }
    }

    #[test]
    fn test_small_unicode_content() {
        // UNICODE_CONTENT contains text with emojis.
        let gap_buffer = ZeroCopyGapBuffer::from(UNICODE_CONTENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert!(!doc.is_empty());
        // Unicode should be preserved in the parsed content.
        for element in doc.iter() {
            if let MdElement::Text(fragments) = element {
                // Check that unicode is preserved in fragments.
                for fragment in fragments.iter() {
                    match fragment {
                        MdLineFragment::Plain(text)
                        | MdLineFragment::InlineCode(text) => {
                            // Just verify text exists, don't assume all fragments have.
                            // unicode
                            let _ = text;
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    #[test]
    fn test_small_emoji_h1_simple() {
        // EMOJI_H1_SIMPLE = "# Heading with emoji 😀".
        let gap_buffer = ZeroCopyGapBuffer::from(EMOJI_H1_SIMPLE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_heading_element(&doc[0], 1, "Heading with emoji 😀");
    }

    #[test]
    fn test_small_emoji_h2_simple() {
        // EMOJI_H2_SIMPLE = "## Subheading with emoji 😀".
        let gap_buffer = ZeroCopyGapBuffer::from(EMOJI_H2_SIMPLE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_heading_element(&doc[0], 2, "Subheading with emoji 😀");
    }

    #[test]
    fn test_small_emoji_multiple() {
        // EMOJI_MULTIPLE = "# Multiple emojis 😀🚀📝".
        let gap_buffer = ZeroCopyGapBuffer::from(EMOJI_MULTIPLE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 1);
        assert_heading_element(&doc[0], 1, "Multiple emojis 😀🚀📝");
    }

    #[test]
    fn test_small_real_world_content() {
        // SMALL_REAL_WORLD_CONTENT is a complete document with metadata, headings, lists,
        // code blocks.
        let gap_buffer = ZeroCopyGapBuffer::from(SMALL_REAL_WORLD_CONTENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");

        // Should have multiple elements including metadata, headings, text, lists, and
        // code blocks.
        assert!(doc.len() > 5, "Expected complex document structure");

        // Verify document has various element types.
        let has_title = doc.iter().any(|e| matches!(e, MdElement::Title(_)));
        let has_tags = doc.iter().any(|e| matches!(e, MdElement::Tags(_)));
        let has_heading = doc.iter().any(|e| matches!(e, MdElement::Heading(_)));
        let has_list = doc.iter().any(|e| matches!(e, MdElement::SmartList(_)));
        let has_code = doc.iter().any(|e| matches!(e, MdElement::CodeBlock(_)));

        assert!(has_title, "Document should have a title");
        assert!(has_tags, "Document should have tags");
        assert!(has_heading, "Document should have headings");
        assert!(has_list, "Document should have lists");
        assert!(has_code, "Document should have code blocks");
    }

    #[test]
    fn test_small_ex_editor_content() {
        // EX_EDITOR_CONTENT is a complex document with various markdown features.
        let gap_buffer = ZeroCopyGapBuffer::from(EX_EDITOR_CONTENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");

        // Should be a complex document.
        assert!(
            doc.len() > 10,
            "Expected complex document with many elements"
        );

        // Verify presence of various markdown features.
        let mut has_metadata = false;
        let mut has_emoji_heading = false;
        let mut has_nested_list = false;
        let mut has_checkbox = false;

        for element in doc.iter() {
            match element {
                MdElement::Title(_)
                | MdElement::Tags(_)
                | MdElement::Authors(_)
                | MdElement::Date(_) => {
                    has_metadata = true;
                }
                MdElement::Heading(data) if data.text.chars().any(|c| c as u32 > 127) => {
                    has_emoji_heading = true;
                }
                MdElement::SmartList((lines, _, indent)) => {
                    if *indent > 0 {
                        has_nested_list = true;
                    }
                    // Check for checkboxes in list items.
                    for line in lines.iter() {
                        if line
                            .iter()
                            .any(|f| matches!(f, MdLineFragment::Checkbox(_)))
                        {
                            has_checkbox = true;
                        }
                    }
                }
                _ => {}
            }
        }

        assert!(has_metadata, "Document should have metadata");
        assert!(has_emoji_heading, "Document should have emoji in headings");
        assert!(
            has_nested_list || has_checkbox,
            "Document should have advanced list features"
        );
    }

    // =============================================================================
    // Medium valid input tests.
    // =============================================================================

    #[test]
    fn test_medium_multiple_lines() {
        // MULTIPLE_LINES contains multiple lines of text.
        let gap_buffer = ZeroCopyGapBuffer::from(MULTIPLE_LINES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have multiple text elements.
        assert!(doc.len() > 1, "Expected multiple lines");
        for element in doc.iter() {
            assert!(
                matches!(element, MdElement::Text(_)),
                "Expected only text elements"
            );
        }
    }

    #[test]
    fn test_medium_heading_basic() {
        // HEADING_BASIC = "# Main Heading\nSome content"
        let gap_buffer = ZeroCopyGapBuffer::from(HEADING_BASIC);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        assert_doc_len(&doc, 2);
        assert_heading_element(&doc[0], 1, "Main Heading");
        assert_text_element(&doc[1], &[MdLineFragment::Plain("Some content")]);
    }

    #[test]
    fn test_medium_multiple_headings() {
        let gap_buffer = ZeroCopyGapBuffer::from(MULTIPLE_HEADINGS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have multiple headings.
        let heading_count = doc
            .iter()
            .filter(|e| matches!(e, MdElement::Heading(_)))
            .count();
        assert!(
            heading_count >= 2,
            "Expected multiple headings, found {heading_count}"
        );
    }

    #[test]
    fn test_medium_all_heading_levels() {
        // ALL_HEADING_LEVELS contains headings from H1 to H6.
        let gap_buffer = ZeroCopyGapBuffer::from(ALL_HEADING_LEVELS);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");

        // Should have at least 6 headings.
        let heading_count = doc
            .iter()
            .filter(|e| matches!(e, MdElement::Heading(_)))
            .count();
        assert!(heading_count >= 6, "Expected at least 6 headings");

        // Verify heading levels 1-6 are present.
        for level in 1..=6 {
            let has_level = doc.iter().any(|e| {
                matches!(e, MdElement::Heading(HeadingData { level: l, .. }) if l.level == level)
            });
            assert!(has_level, "Missing heading level {level}");
        }
    }

    #[test]
    fn test_medium_unordered_list_simple() {
        let gap_buffer = ZeroCopyGapBuffer::from(UNORDERED_LIST_SIMPLE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have unordered list items.
        let has_unordered_list = doc
            .iter()
            .any(|e| matches!(e, MdElement::SmartList((_, BulletKind::Unordered, _))));
        assert!(has_unordered_list, "Expected unordered list");
    }

    #[test]
    fn test_medium_ordered_list_simple() {
        let gap_buffer = ZeroCopyGapBuffer::from(ORDERED_LIST_SIMPLE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have ordered list items.
        let has_ordered_list = doc
            .iter()
            .any(|e| matches!(e, MdElement::SmartList((_, BulletKind::Ordered(_), _))));
        assert!(has_ordered_list, "Expected ordered list");
    }

    #[test]
    fn test_medium_nested_unordered_list() {
        let gap_buffer = ZeroCopyGapBuffer::from(NESTED_UNORDERED_LIST);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have nested lists (indent > 0)
        let has_nested = doc
            .iter()
            .any(|e| matches!(e, MdElement::SmartList((_, _, indent)) if *indent > 0));
        assert!(has_nested, "Expected nested list items");
    }

    #[test]
    fn test_medium_nested_ordered_list() {
        let gap_buffer = ZeroCopyGapBuffer::from(NESTED_ORDERED_LIST);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have nested ordered lists.
        let has_nested_ordered = doc.iter().any(|e| matches!(e, MdElement::SmartList((_, BulletKind::Ordered(_), indent)) if *indent > 0));
        assert!(has_nested_ordered, "Expected nested ordered list");
    }

    #[test]
    fn test_medium_checkboxes() {
        // CHECKBOXES contains checked and unchecked checkboxes in lists.
        let gap_buffer = ZeroCopyGapBuffer::from(CHECKBOXES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");

        // Find lists with checkboxes.
        let mut found_checked = false;
        let mut found_unchecked = false;

        for element in doc.iter() {
            if let MdElement::SmartList((lines, _, _)) = element {
                for line in lines.iter() {
                    for fragment in line.iter() {
                        match fragment {
                            MdLineFragment::Checkbox(true) => found_checked = true,
                            MdLineFragment::Checkbox(false) => found_unchecked = true,
                            _ => {}
                        }
                    }
                }
            }
        }

        assert!(found_checked, "Expected at least one checked checkbox");
        assert!(found_unchecked, "Expected at least one unchecked checkbox");
    }

    #[test]
    fn test_medium_mixed_list_types() {
        let gap_buffer = ZeroCopyGapBuffer::from(MIXED_LIST_TYPES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have both ordered and unordered lists.
        let has_ordered = doc
            .iter()
            .any(|e| matches!(e, MdElement::SmartList((_, BulletKind::Ordered(_), _))));
        let has_unordered = doc
            .iter()
            .any(|e| matches!(e, MdElement::SmartList((_, BulletKind::Unordered, _))));
        assert!(has_ordered && has_unordered, "Expected mixed list types");
    }

    #[test]
    fn test_medium_code_block_bash() {
        let gap_buffer = ZeroCopyGapBuffer::from(CODE_BLOCK_BASH);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have bash code block.
        let has_bash_code = doc.iter().any(|e| {
            matches!(e, MdElement::CodeBlock(lines) if lines.iter().any(|l| l.language == Some("bash")))
        });
        assert!(has_bash_code, "Expected bash code block");
    }

    #[test]
    fn test_medium_code_block_rust() {
        // CODE_BLOCK_RUST contains a Rust code block.
        let gap_buffer = ZeroCopyGapBuffer::from(CODE_BLOCK_RUST);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");

        // Find the code block.
        let code_block = doc.iter().find(|e| matches!(e, MdElement::CodeBlock(_)));
        assert!(code_block.is_some(), "Expected a code block");

        if let Some(MdElement::CodeBlock(lines)) = code_block {
            // Should have start tag, content, and end tag.
            assert!(lines.len() >= 3, "Code block should have at least 3 lines");

            // Check language is Rust.
            assert_eq!(lines[0].language, Some("rust"));
            assert_eq!(lines[0].content, CodeBlockLineContent::StartTag);

            // Last line should be end tag.
            let last = &lines[lines.len() - 1];
            assert_eq!(last.content, CodeBlockLineContent::EndTag);

            // Middle lines should be code content.
            for i in 1..lines.len() - 1 {
                assert!(matches!(lines[i].content, CodeBlockLineContent::Text(_)));
            }
        }
    }

    #[test]
    fn test_medium_code_block_no_language() {
        let gap_buffer = ZeroCopyGapBuffer::from(CODE_BLOCK_NO_LANGUAGE);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have code block without language.
        let has_plain_code = doc.iter().any(|e| {
            matches!(e, MdElement::CodeBlock(lines) if lines.iter().any(|l| l.language.is_none()))
        });
        assert!(has_plain_code, "Expected code block without language");
    }

    #[test]
    fn test_medium_empty_code_block() {
        let gap_buffer = ZeroCopyGapBuffer::from(EMPTY_CODE_BLOCK);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should have empty code block (just start and end tags)
        let has_empty_code = doc
            .iter()
            .any(|e| matches!(e, MdElement::CodeBlock(lines) if lines.len() <= 3));
        assert!(has_empty_code, "Expected empty code block");
    }

    #[test]
    fn test_medium_formatting_edge_cases() {
        let gap_buffer = ZeroCopyGapBuffer::from(FORMATTING_EDGE_CASES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should handle edge cases gracefully.
        assert!(!doc.is_empty(), "Document should not be empty");
    }

    #[test]
    fn test_medium_nested_formatting() {
        let gap_buffer = ZeroCopyGapBuffer::from(NESTED_FORMATTING);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should handle nested formatting.
        let has_formatting = doc.iter().any(|e| {
            matches!(e, MdElement::Text(fragments) if fragments.iter().any(|f|
                matches!(f, MdLineFragment::Bold(_) | MdLineFragment::Italic(_) | MdLineFragment::InlineCode(_))
            ))
        });
        assert!(has_formatting, "Expected formatted text");
    }

    #[test]
    fn test_medium_edge_case_empty_lines() {
        let gap_buffer = ZeroCopyGapBuffer::from(EDGE_CASE_EMPTY_LINES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should handle empty lines correctly.
        assert!(!doc.is_empty(), "Document should parse empty lines");
    }

    #[test]
    fn test_medium_edge_case_whitespace_lines() {
        let gap_buffer = ZeroCopyGapBuffer::from(EDGE_CASE_WHITESPACE_LINES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should handle whitespace lines correctly.
        assert!(!doc.is_empty(), "Document should parse whitespace lines");
    }

    #[test]
    fn test_medium_edge_case_trailing_spaces() {
        let gap_buffer = ZeroCopyGapBuffer::from(EDGE_CASE_TRAILING_SPACES);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should handle trailing spaces correctly.
        assert!(
            !doc.is_empty(),
            "Document should parse with trailing spaces"
        );
    }

    #[test]
    fn test_medium_emoji_start_middle_end() {
        let gap_buffer = ZeroCopyGapBuffer::from(EMOJI_START_MIDDLE_END);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Should handle emojis in various positions.
        let has_emoji_content = doc.iter().any(|e| match e {
            MdElement::Text(fragments) => fragments.iter().any(|f| match f {
                MdLineFragment::Plain(text) => text.chars().any(|c| c as u32 > 127),
                _ => false,
            }),
            MdElement::Heading(data) => data.text.chars().any(|c| c as u32 > 127),
            _ => false,
        });
        assert!(has_emoji_content, "Expected emoji content");
    }

    #[test]
    fn test_medium_blog_post_document() {
        let gap_buffer = ZeroCopyGapBuffer::from(BLOG_POST_DOCUMENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Blog post should have various elements.
        assert!(doc.len() > 5, "Blog post should have multiple elements");
        let has_heading = doc.iter().any(|e| matches!(e, MdElement::Heading(_)));
        let has_text = doc.iter().any(|e| matches!(e, MdElement::Text(_)));
        assert!(
            has_heading && has_text,
            "Blog post should have headings and text"
        );
    }

    // =============================================================================
    // Large valid input tests.
    // =============================================================================

    #[test]
    fn test_large_complex_nested_document() {
        let gap_buffer = ZeroCopyGapBuffer::from(COMPLEX_NESTED_DOCUMENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Complex document should have many varied elements.
        assert!(doc.len() > 10, "Complex document should have many elements");
        // Verify variety of content types.
        let element_types = [
            doc.iter().any(|e| matches!(e, MdElement::Heading(_))),
            doc.iter().any(|e| matches!(e, MdElement::SmartList(_))),
            doc.iter().any(|e| matches!(e, MdElement::CodeBlock(_))),
            doc.iter().any(|e| matches!(e, MdElement::Text(_))),
        ];
        assert!(
            element_types.iter().all(|&x| x),
            "Complex document should have varied content"
        );
    }

    #[test]
    fn test_large_tutorial_document() {
        let gap_buffer = ZeroCopyGapBuffer::from(TUTORIAL_DOCUMENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Tutorial should have structured content.
        assert!(doc.len() > 10, "Tutorial should have substantial content");
        // Should have sections with headings.
        let heading_count = doc
            .iter()
            .filter(|e| matches!(e, MdElement::Heading(_)))
            .count();
        assert!(heading_count >= 3, "Tutorial should have multiple sections");
    }

    // =============================================================================
    // Invalid input tests.
    // =============================================================================

    #[test]
    fn test_invalid_malformed_syntax() {
        let gap_buffer = ZeroCopyGapBuffer::from(MALFORMED_SYNTAX);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        // Malformed syntax should still parse something.
        assert!(
            remainder.is_empty() || !doc.is_empty(),
            "Parser should handle malformed syntax gracefully"
        );
    }

    #[test]
    fn test_invalid_unclosed_formatting() {
        let gap_buffer = ZeroCopyGapBuffer::from(UNCLOSED_FORMATTING);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        // Unclosed formatting should still parse.
        assert!(
            remainder.is_empty() || !doc.is_empty(),
            "Parser should handle unclosed formatting gracefully"
        );
    }

    // =============================================================================
    // Jumbo/Real world file tests
    // =============================================================================

    #[test]
    fn test_jumbo_real_world_editor() {
        let gap_buffer = ZeroCopyGapBuffer::from(REAL_WORLD_EDITOR_CONTENT);
        let (remainder, doc) = parse_markdown(&gap_buffer).unwrap();
        assert_eq!(remainder, "");
        // Real world content should be substantial and varied.
        assert!(doc.len() > 20, "Real world document should be large");

        // Should have all major element types.
        let has_metadata = doc.iter().any(|e| {
            matches!(
                e,
                MdElement::Title(_)
                    | MdElement::Tags(_)
                    | MdElement::Authors(_)
                    | MdElement::Date(_)
            )
        });
        let has_headings = doc.iter().any(|e| matches!(e, MdElement::Heading(_)));
        let has_lists = doc.iter().any(|e| matches!(e, MdElement::SmartList(_)));
        let has_code = doc.iter().any(|e| matches!(e, MdElement::CodeBlock(_)));
        let has_text = doc.iter().any(|e| matches!(e, MdElement::Text(_)));

        assert!(has_metadata, "Real world doc should have metadata");
        assert!(has_headings, "Real world doc should have headings");
        assert!(has_lists, "Real world doc should have lists");
        assert!(has_code, "Real world doc should have code blocks");
        assert!(has_text, "Real world doc should have text");
    }
}