tdoc 0.10.0

Library and assorted CLI tools for working with FTML (Formatted Text Markup Language) documents
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
use std::io::Cursor;
use tdoc::{
    formatter::Formatter, ftml, markdown, parse, write, ChecklistItem, Document, InlineStyle,
    Paragraph, ParagraphType, Span,
};

#[test]
fn test_parsing_simple_paragraphs() {
    let tests: Vec<(&str, Document)> = vec![
        ("<p>This is a test.</p>", ftml! { p { "This is a test." } }),
        ("<p>one</p><p>two</p>", ftml! { p { "one" } p { "two" } }),
        (
            "<blockquote><p>one</p></blockquote><p>two</p>",
            ftml! { quote { p { "one" } } p { "two" } },
        ),
        (
            "<p><b>Bold</b> text.</p>",
            ftml! { p { b { "Bold" } " text." } },
        ),
        (
            "<p><b>Bold<br />text.</b></p>",
            ftml! { p { b { "Bold\n" "text." } } },
        ),
        (
            "<p>Test <b>bold</b></p>",
            ftml! { p { "Test " b { "bold" } } },
        ),
        ("<h1> Hello World! </h1>", ftml! { h1 { "Hello World!" } }),
        ("<p>A<br/> B</p>", ftml! { p { "A" "\n" "B" } }),
        (
            "<ul><li><p>a</p></li><li><p>b</p></li></ul>",
            ftml! { ul { li { p { "a" } } li { p { "b" } } } },
        ),
        (
            "<ul><li><p>a</p></li><li><p>b</p><p>c</p></li></ul>",
            ftml! { ul { li { p { "a" } } li { p { "b" } p { "c" } } } },
        ),
    ];

    for (input, expected) in tests {
        let result = parse(Cursor::new(input)).unwrap();
        assert_eq!(result, expected, "Failed for input: {}", input);
    }
}

#[test]
fn test_parsing_and_writing_styles() {
    let simple_tests: Vec<(&str, Document)> = vec![
        ("This is a test.", ftml! { p { "This is a test." } }),
        (
            "&emsp14;This is a test.",
            ftml! { p { " This is a test." } },
        ),
        (
            "This is a test.&emsp14;",
            ftml! { p { "This is a test. " } },
        ),
        ("A&emsp14;&emsp14;&emsp14;B", ftml! { p { "A   B" } }),
    ];

    let indented_tests: Vec<(&str, Document)> = vec![
        (
            "This is a <b>test</b>.",
            ftml! { p { "This is a " b { "test" } "." } },
        ),
        (
            "This is a <b> test </b>.",
            ftml! { p { "This is a " b { " test " } "." } },
        ),
        (
            "This is a <b><i>second</i> test</b>.",
            ftml! { p { "This is a " b { i { "second" } " test" } "." } },
        ),
    ];

    fn check_parse_and_write(input: &str, expected_doc: &Document, expected_output: &str) {
        let parsed_doc = parse(Cursor::new(&format!("<p>{}</p>\n", input))).unwrap();
        assert_eq!(parsed_doc, *expected_doc, "Parsing failed for: {}", input);

        let mut buf = Vec::new();
        write(&mut buf, expected_doc).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert_eq!(output, expected_output, "Writing failed for: {}", input);
    }

    for (input, expected_doc) in simple_tests {
        let expected_output = format!("<p>{}</p>\n", input);
        check_parse_and_write(input, &expected_doc, &expected_output);
    }

    for (input, expected_doc) in indented_tests {
        let expected_output = format!("<p>{}</p>\n", input);
        check_parse_and_write(input, &expected_doc, &expected_output);
    }
}

#[test]
fn test_parsing_inline_styles() {
    let tests: Vec<(&str, Document)> = vec![
        (
            "This is a <b>test</b>.",
            ftml! { p { "This is a " b { "test" } "." } },
        ),
        (
            "This is a <b> test </b>.",
            ftml! { p { "This is a " b { " test " } "." } },
        ),
        (
            "This is a <b><i>second</i> test</b>.",
            ftml! { p { "This is a " b { i { "second" } " test" } "." } },
        ),
        (
            "Visit <a href=\"https://example.com\">Example</a>",
            ftml! { p { "Visit " link { "https://example.com" "Example" } } },
        ),
    ];

    for (input, expected) in tests {
        let full_input = format!("<p>{}</p>", input);
        let doc = parse(Cursor::new(full_input)).unwrap();
        assert_eq!(doc, expected, "Failed for: {}", input);
    }
}

#[test]
fn test_write_spaces() {
    let tests: Vec<(&str, Document)> = vec![
        (
            "<p>&emsp14;This is a test.</p>\n",
            ftml! { p { " This is a test." } },
        ),
        (
            "<p>This is a test.&emsp14;</p>\n",
            ftml! { p { "This is a test. " } },
        ),
        (
            "<p>A&emsp14;&emsp14;&emsp14;B</p>\n",
            ftml! { p { "A   B" } },
        ),
    ];

    for (expected_output, expected_doc) in tests {
        let mut buf = Vec::new();
        write(&mut buf, &expected_doc).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert_eq!(output, expected_output);
    }
}

#[test]
fn test_code_block_html_roundtrip() {
    let input = "<pre>fn main() {}\nprintln!(\"hi\");</pre>";
    let doc = parse(Cursor::new(input)).unwrap();
    let expected = ftml! { code { "fn main() {}\nprintln!(\"hi\");" } };
    assert_eq!(doc, expected);

    let mut buf = Vec::new();
    write(&mut buf, &doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, "<pre>\nfn main() {}\nprintln!(\"hi\");\n</pre>\n");
}

#[test]
fn test_code_block_html_roundtrip_with_leading_newline() {
    let input = "<pre>\nfn main() {}\nprintln!(\"hi\");\n</pre>\n";
    let doc = parse(Cursor::new(input)).unwrap();

    let mut buf = Vec::new();
    write(&mut buf, &doc).unwrap();
    let output = String::from_utf8(buf).unwrap();

    assert_eq!(output, input);
}

#[test]
fn test_formatter_code_block_ascii() {
    let doc = ftml! { code { "fn main() {}\nprintln!(\"hi\");" } };
    let mut output = Vec::new();
    let mut formatter = Formatter::new_ascii(&mut output);
    let fence = "-".repeat(formatter.style.wrap_width);
    formatter.write_document(&doc).unwrap();
    let rendered = String::from_utf8(output).unwrap();
    let body = "fn main() {}\nprintln!(\"hi\");";
    let expected = format!("{fence}\n{body}\n{fence}\n", fence = fence, body = body);
    assert_eq!(rendered, expected);
}

#[test]
fn test_formatter_code_block_wrapping() {
    let doc = ftml! { code { "abcdefghijk" } };
    let mut output = Vec::new();
    let mut formatter = Formatter::new_ascii(&mut output);
    formatter.style.wrap_width = 8;
    let fence = "-".repeat(formatter.style.wrap_width);
    formatter.write_document(&doc).unwrap();
    let rendered = String::from_utf8(output).unwrap();
    let expected = format!("{fence}\nabcdefgh\nijk\n{fence}\n", fence = fence);
    assert_eq!(rendered, expected);
}

#[test]
fn test_markdown_code_block_roundtrip() {
    let input = "```\nfn main() {}\nprintln!(\"hi\");\n```\n";
    let doc = markdown::parse(Cursor::new(input)).unwrap();
    // Trailing newline from markdown syntax is stripped during parsing
    let expected = ftml! { code { "fn main() {}\nprintln!(\"hi\");" } };
    assert_eq!(doc, expected);

    let mut output = Vec::new();
    markdown::write(&mut output, &doc).unwrap();
    let markdown_out = String::from_utf8(output).unwrap();
    assert_eq!(
        markdown_out,
        "```\nfn main() {}\nprintln!(\"hi\");\n```\n\n"
    );
}

#[test]
fn test_markdown_code_block_trims_leading_newline() {
    let input = "<pre>\nfn main() {}\nprintln!(\"hi\");\n</pre>\n";
    let doc = parse(Cursor::new(input)).unwrap();

    let mut output = Vec::new();
    markdown::write(&mut output, &doc).unwrap();
    let markdown_out = String::from_utf8(output).unwrap();

    // Trailing newline is stripped, but writer adds one before closing fence
    assert_eq!(
        markdown_out,
        "```\nfn main() {}\nprintln!(\"hi\");\n```\n\n"
    );
}

#[test]
fn test_markdown_code_block_preserves_blank_first_line() {
    let input = "<pre>\n\nfn main() {}\nprintln!(\"hi\");\n</pre>\n";
    let doc = parse(Cursor::new(input)).unwrap();

    let mut output = Vec::new();
    markdown::write(&mut output, &doc).unwrap();
    let markdown_out = String::from_utf8(output).unwrap();

    // Trailing newline is stripped, but writer adds one before closing fence
    assert_eq!(
        markdown_out,
        "```\n\nfn main() {}\nprintln!(\"hi\");\n```\n\n"
    );
}

#[test]
fn test_parse_code_block_trims_leading_newline() {
    let input = "<pre>\nfn main() {}\n</pre>";
    let doc = parse(Cursor::new(input)).unwrap();
    // Both leading and trailing newlines are stripped
    let expected = ftml! { code { "fn main() {}" } };
    assert_eq!(doc, expected);
}

#[test]
fn test_parse_code_block_preserves_blank_first_line() {
    let input = "<pre>\n\nfn main() {}\n</pre>";
    let doc = parse(Cursor::new(input)).unwrap();
    // Leading newline is stripped, but blank line after it is preserved; trailing newline is stripped
    let expected = ftml! { code { "\nfn main() {}" } };
    assert_eq!(doc, expected);
}

#[test]
fn test_markdown_soft_break_collapses() {
    let input = "Hello\nworld\nagain\n";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    let expected = ftml! { p { "Hello world again" } };
    assert_eq!(parsed, expected);
}

#[test]
fn test_markdown_hard_break_preserved() {
    let input = "Hello  \nworld\n";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    let expected = ftml! { p { "Hello" "\n" "world" } };
    assert_eq!(parsed, expected);
}

#[test]
fn test_simple_paragraph_roundtrip() {
    let input = "<p>This is a test.</p>";
    let doc = parse(Cursor::new(input)).unwrap();
    let mut buf = Vec::new();
    write(&mut buf, &doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, format!("{}\n", input));
}

#[test]
fn test_paragraph_with_styles_roundtrip() {
    let input = "<p>This is <i>a little more complex</i> test.</p>";
    let doc = parse(Cursor::new(input)).unwrap();
    let mut buf = Vec::new();
    write(&mut buf, &doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, format!("{}\n", input));
}

#[test]
fn test_html_checklist_roundtrip() {
    let input = r#"<ul>
  <li><input type="checkbox" checked /> This one is done</li>
  <li><input type="checkbox" /> This one is not done</li>
  <li>
    <input type="checkbox" /> This one is not done and also contains a very
    large amount of text that will wrap onto multiple lines in the terminal
    output.
  </li>
</ul>"#;

    let expected_doc = ftml! {
        checklist {
            done { "This one is done" }
            todo { "This one is not done" }
            todo { "This one is not done and also contains a very large amount of text that will wrap onto multiple lines in the terminal output." }
        }
    };

    let parsed = parse(Cursor::new(input)).unwrap();
    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    let expected_output = r#"<ul>
  <li><input type="checkbox" checked /> This one is done</li>
  <li><input type="checkbox" /> This one is not done</li>
  <li>
    <input type="checkbox" /> This one is not done and also contains a very large amount of text that
    will wrap onto multiple lines in the terminal output.
  </li>
</ul>
"#;
    assert_eq!(output, expected_output);
}

#[test]
fn test_markdown_checklist_roundtrip() {
    let input = "- [x] First item\n- [ ] Second item\n";
    let expected_doc = ftml! {
        checklist {
            done { "First item" }
            todo { "Second item" }
        }
    };

    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    markdown::write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, input);
}

#[test]
fn test_html_nested_checklist_roundtrip() {
    let input = r#"<ul>
  <li>
    <input type="checkbox" checked /> Parent
    <ul>
      <li><input type="checkbox" /> Child</li>
    </ul>
  </li>
</ul>"#;

    let child = ChecklistItem::new(false).with_content(vec![Span::new_text("Child")]);
    let parent = ChecklistItem::new(true)
        .with_content(vec![Span::new_text("Parent")])
        .with_children(vec![child]);
    let expected_doc = Document::new().with_paragraphs(vec![
        Paragraph::new_checklist().with_checklist_items(vec![parent])
    ]);

    let parsed = parse(Cursor::new(input)).unwrap();
    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    let reparsed = parse(Cursor::new(&output)).unwrap();
    assert_eq!(reparsed, expected_doc);
}

#[test]
fn test_html_checklist_with_bold_text() {
    let input = r#"<ul>
  <li><input type="checkbox" checked /> This one has <b>bold</b> text</li>
  <li><input type="checkbox" /> This one has <i>italic</i> text</li>
</ul>
"#;

    let expected_doc = ftml! {
        checklist {
            done { "This one has " b { "bold" } " text" }
            todo { "This one has " i { "italic" } " text" }
        }
    };

    let parsed = parse(Cursor::new(input)).unwrap();
    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    let reparsed = parse(Cursor::new(&output)).unwrap();
    assert_eq!(reparsed, expected_doc);
}

#[test]
fn test_markdown_nested_checklist_roundtrip() {
    let input = "- [x] Parent\n  - [ ] Child\n";
    let child = ChecklistItem::new(false).with_content(vec![Span::new_text("Child")]);
    let parent = ChecklistItem::new(true)
        .with_content(vec![Span::new_text("Parent")])
        .with_children(vec![child]);
    let expected_doc = Document::new().with_paragraphs(vec![
        Paragraph::new_checklist().with_checklist_items(vec![parent])
    ]);

    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    markdown::write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, input);
}

#[test]
fn test_markdown_wikilink_roundtrip() {
    let input = "[[WikiLink]]";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();

    let expected_doc = ftml! {
        p { link { "WikiLink" "WikiLink" } }
    };

    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    markdown::write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, "[WikiLink](WikiLink)\n");
}

#[test]
fn test_markdown_wikilink_with_label_roundtrip() {
    let input = "[[WikiLink|Custom label]]";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();

    let expected_doc = ftml! {
        p { link { "WikiLink" "Custom label" } }
    };

    assert_eq!(parsed, expected_doc);

    let mut buf = Vec::new();
    markdown::write(&mut buf, &expected_doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, "[Custom label](WikiLink)\n");
}

#[test]
fn test_markdown_nested_unordered_lists() {
    let input = "- top level\n  - second level\n";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        ul {
            li {
                p { "top level" }
                ul {
                    li { p { "second level" } }
                }
            }
        }
    };

    assert_eq!(parsed, expected);
}

#[test]
fn test_markdown_nested_mixed_lists() {
    let input = "1. ordered item\n   - unordered child\n     - [x] nested task\n";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        ol {
            li {
                p { "ordered item" }
                ul {
                    li {
                        p { "unordered child" }
                        checklist {
                            done { "nested task" }
                        }
                    }
                }
            }
        }
    };

    assert_eq!(parsed, expected);
}

#[test]
fn test_markdown_nested_lists_in_blockquote() {
    let input = "> - quoted bullet\n>   1. quoted number\n>      - [ ] quoted task\n";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        quote {
            ul {
                li {
                    p { "quoted bullet" }
                    ol {
                        li {
                            p { "quoted number" }
                            checklist {
                                todo { "quoted task" }
                            }
                        }
                    }
                }
            }
        }
    };

    assert_eq!(parsed, expected);
}

#[test]
fn test_markdown_ignores_html_comments() {
    let input = "Before\n\n<!-- comment line 1\nline 2\n-->\n\nAfter\n";
    let parsed = markdown::parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        p { "Before" }
        p { "After" }
    };

    assert_eq!(parsed, expected);
}

#[test]
fn test_formatter_checklist_output() {
    let doc = ftml! {
        checklist {
            done { "This one is done" }
            todo { "This one is not done" }
            todo { "This one is not done and also contains a very large amount of text that will wrap onto multiple lines in the terminal output." }
        }
    };

    let mut buf = Vec::new();
    Formatter::new_ascii(&mut buf).write_document(&doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    let expected = "[✓] This one is done\n[ ] This one is not done\n[ ] This one is not done and also contains a very large amount of text\n    that will wrap onto multiple lines in the terminal output.\n";
    assert_eq!(output, expected);
}

#[test]
fn test_formatter_nested_checklist_output() {
    let child = ChecklistItem::new(false).with_content(vec![Span::new_text("Child")]);
    let parent = ChecklistItem::new(true)
        .with_content(vec![Span::new_text("Parent")])
        .with_children(vec![child]);
    let doc = Document::new().with_paragraphs(vec![
        Paragraph::new_checklist().with_checklist_items(vec![parent])
    ]);

    let mut buf = Vec::new();
    Formatter::new_ascii(&mut buf).write_document(&doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    let expected = "[✓] Parent\n    [ ] Child\n";
    assert_eq!(output, expected);
}

#[test]
fn test_header_roundtrip() {
    let input = "<h1>Header</h1>";
    let doc = parse(Cursor::new(input)).unwrap();
    let mut buf = Vec::new();
    write(&mut buf, &doc).unwrap();
    let output = String::from_utf8(buf).unwrap();
    assert_eq!(output, format!("{}\n", input));
}

#[test]
fn test_nested_list() {
    let input = r#"<ul>
<li>
<ul>
<li>
<p>a</p>
</li>
</ul>
</li>
<li>
<p>b</p>
<p>c</p>
</li>
</ul>"#;

    let expected = ftml! {
        ul {
            li { ul { li { p { "a" } } } }
            li { p { "b" } p { "c" } }
        }
    };

    let result = parse(Cursor::new(input)).unwrap();
    assert_eq!(result, expected);
}

#[test]
fn test_parsing_errors() {
    let tests = vec![
        ("This is a test.", "Unexpected text content"),
        ("<p>one<p>two</p></p>", "Closing unopened paragraph"),
        ("<blockquote>one</blockquote>", "Unexpected text content"),
    ];

    for (input, expected_error) in tests {
        let result = parse(Cursor::new(input));
        assert!(result.is_err(), "Expected error for input: {}", input);
        let error = result.unwrap_err();
        assert!(
            error
                .to_string()
                .to_lowercase()
                .contains(&expected_error.to_lowercase()),
            "Error '{}' should contain '{}' for input: {}",
            error,
            expected_error,
            input
        );
    }
}

#[test]
fn test_trim_whitespace() {
    // Test the whitespace trimming functionality
    let input = "<p>  \n  test, test.  \n  </p>";
    let doc = parse(Cursor::new(input)).unwrap();

    // Should trim leading/trailing whitespace
    assert_eq!(doc.paragraphs[0].content()[0].text, "test, test.");
}

#[test]
fn test_parsing_hard_newlines() {
    let input = r#"<p>
    This is a paragraph that contains a very long line of <b>highlighted text
    to force the formatter to break<br />
    the<br />
    line<br />
	in the middle.</b> But afterwards, of course, things should continue
    normally.
  </p>"#;

    let parsed_doc = parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        p {
            "This is a paragraph that contains a very long line of "
            b {
                "highlighted text to force the formatter to break\n"
                "the\n"
                "line\n"
                "in the middle."
            }
            " But afterwards, of course, things should continue normally."
        }
    };

    assert_eq!(parsed_doc, expected);
}

#[test]
fn test_all_inline_styles() {
    let tests = vec![
        ("<p><b>bold</b></p>", InlineStyle::Bold),
        ("<p><i>italic</i></p>", InlineStyle::Italic),
        ("<p><u>underline</u></p>", InlineStyle::Underline),
        ("<p><s>strike</s></p>", InlineStyle::Strike),
        ("<p><mark>highlight</mark></p>", InlineStyle::Highlight),
        ("<p><code>code</code></p>", InlineStyle::Code),
    ];

    for (input, expected_style) in tests {
        let doc = parse(Cursor::new(input)).unwrap();
        assert_eq!(doc.paragraphs[0].content()[0].style, expected_style);
    }
}

#[test]
fn test_all_paragraph_types() {
    let tests = vec![
        ("<p>text</p>", ParagraphType::Text),
        ("<h1>header1</h1>", ParagraphType::Header1),
        ("<h2>header2</h2>", ParagraphType::Header2),
        ("<h3>header3</h3>", ParagraphType::Header3),
        (
            "<blockquote><p>quote</p></blockquote>",
            ParagraphType::Quote,
        ),
        (
            "<ul><li><p>item</p></li></ul>",
            ParagraphType::UnorderedList,
        ),
        ("<ol><li><p>item</p></li></ol>", ParagraphType::OrderedList),
    ];

    for (input, expected_type) in tests {
        let doc = parse(Cursor::new(input)).unwrap();
        assert_eq!(doc.paragraphs[0].paragraph_type(), expected_type);
    }
}

#[test]
fn test_list_item_parsing() {
    // Test case specifically for the testdocument.ftml parsing error
    // This should reproduce the "Non-inline token: l" error before the fix
    let input = r#"<ul>
  <li>
    <p>Test item</p>
  </li>
</ul>"#;

    let parsed_doc = parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        ul {
            li { p { "Test item" } }
        }
    };

    assert_eq!(parsed_doc, expected);
}

#[test]
fn test_complex_list_structure() {
    // Test case for complex list structures like in testdocument.ftml
    let input = r#"<ul>
  <li>
    <p>First item</p>
  </li>
  <li>
    <p>Second item with multiple paragraphs</p>
    <p>This is the second paragraph of the same list item</p>
  </li>
  <li>
    <blockquote>
      <p>A list item containing a quote</p>
    </blockquote>
  </li>
</ul>"#;

    let parsed_doc = parse(Cursor::new(input)).unwrap();
    let expected = ftml! {
        ul {
            li { p { "First item" } }
            li {
                p { "Second item with multiple paragraphs" }
                p { "This is the second paragraph of the same list item" }
            }
            li {
                quote { p { "A list item containing a quote" } }
            }
        }
    };

    assert_eq!(parsed_doc, expected);
}

#[test]
fn test_testdocument_specific_structure() {
    // Test a specific structure from testdocument.ftml that might be causing the issue
    let input = r#"<ul>
  <li>
    <p><b>Bold</b> text.</p>
  </li>
</ul>"#;

    // This should reproduce the actual error if it's related to inline elements within list items
    let _parsed_doc = parse(Cursor::new(input)).unwrap();
}

#[test]
fn test_testdocument_exact_section() {
    // Test the exact structure from testdocument.ftml that should be failing
    let input = r#"<ul>
  <li>
    <p><b>Bold</b> text.</p>
  </li>

  <li>
    <p><i>Italic</i> text.</p>
  </li>

  <li>
    <p><mark>Highlighted</mark> text.</p>
  </li>

  <li>
    <p><u>Underlined</u> text.</p>
  </li>

  <li>
    <p><s>Striked</s> text.</p>
  </li>

  <li>
    <p>Text formatted as <code>code</code>.</p>
  </li>
</ul>"#;

    let _parsed_doc = parse(Cursor::new(input)).unwrap();
}

#[test]
fn test_nested_lists_structure() {
    // This is the actual problematic structure from testdocument.ftml
    // Nested list inside list item
    let input = r#"<ul>
    <li>
      <ol>
        <li>
          <p>One</p>
        </li>
      </ol>
    </li>
</ul>"#;

    let _parsed_doc = parse(Cursor::new(input));
    // This should fail with "Non-inline token: l" error
}