type_test.go
1package document
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestNodeToTextContent(t *testing.T) {
10 tests := []struct {
11 name string
12 content string
13 want []TextContent
14 }{
15 {
16 name: "plain text",
17 content: "hello world",
18 want: []TextContent{{Type: 0, Content: "hello world"}},
19 },
20 {
21 name: "bold",
22 content: "<bold>bold text</bold>",
23 want: []TextContent{{Type: TextBold, Content: "bold text"}},
24 },
25 {
26 name: "underline",
27 content: "<underline>underline text</underline>",
28 want: []TextContent{{Type: TextUnderline, Content: "underline text"}},
29 },
30 {
31 name: "italic",
32 content: "<italic>italic text</italic>",
33 want: []TextContent{{Type: TextItalic, Content: "italic text"}},
34 },
35 {
36 name: "mixed formatting",
37 content: "normal <bold>bold</bold> normal",
38 want: []TextContent{
39 {Type: 0, Content: "normal "},
40 {Type: TextBold, Content: "bold"},
41 {Type: 0, Content: " normal"},
42 },
43 },
44 {
45 name: "nested bold+underline",
46 content: "<bold><underline>text</underline></bold>",
47 want: []TextContent{{Type: TextBold | TextUnderline, Content: "text"}},
48 },
49 {
50 name: "overlapping tags",
51 content: "<bold>bold <underline>bold+underline</underline> bold</bold>",
52 want: []TextContent{
53 {Type: TextBold, Content: "bold "},
54 {Type: TextBold | TextUnderline, Content: "bold+underline"},
55 {Type: TextBold, Content: " bold"},
56 },
57 },
58 {
59 name: "unmatched closing tag ignored",
60 content: "</bold>text</bold>",
61 want: []TextContent{{Type: 0, Content: "text"}},
62 },
63 {
64 name: "unclosed opening tag applies to rest",
65 content: "<bold>text",
66 want: []TextContent{{Type: TextBold, Content: "text"}},
67 },
68 }
69
70 for _, tt := range tests {
71 t.Run(tt.name, func(t *testing.T) {
72 n := Node{Type: Text, Content: tt.content}
73 got := NodeToTextContent(n)
74 assert.Equal(t, tt.want, got)
75 })
76 }
77}