-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrender_test.go
154 lines (138 loc) · 4.04 KB
/
render_test.go
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
package quill
import (
"bytes"
"io/ioutil"
"strconv"
"testing"
)
func TestSimple(t *testing.T) {
cases := map[string]struct {
ops string
want string
}{
"empty": {
ops: `[{"insert": "\n"}]`,
want: "<p><br></p>",
},
"two paragraphs (single op)": {
ops: `[{"insert": "line1\nline2\n"}]`,
want: "<p>line1</p><p>line2</p>",
},
"blank line": {
ops: `[{"insert": "line1\n\nline3\n"}]`,
want: "<p>line1</p><p><br></p><p>line3</p>",
},
"blockquote": {
ops: `[{"insert": "bkqt"}, {"attributes": {"blockquote": true}, "insert": "\n"}]`,
want: "<blockquote>bkqt</blockquote>",
},
"color": {
ops: `[{"attributes": {"color": "#a10000"}, "insert": "colored"}, {"insert": "\n"}]`,
want: `<p><span style="color:#a10000;">colored</span></p>`,
},
"strikethrough": {
ops: `[{"attributes":{"strike":true},"insert":"striked"},{"insert":"\n"}]`,
want: "<p><s>striked</s></p>",
},
"list": {
ops: `[{"insert":"abc "},{"attributes":{"bold":true},"insert":"bld"},{"attributes":{"list":"bullet"},"insert":"\n"}]`,
want: "<ul><li>abc <strong>bld</strong></li></ul>",
},
"image": {
ops: `[{"insert":{"image":"source-url"}},{"insert":"\n"}]`,
want: `<p><img src="source-url"></p>`,
},
"image wrapped": {
ops: `[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`,
want: `<p>text <img src="source-url"> more text</p>`,
},
"background": {
ops: `[{"insert":"abc "},{"attributes":{"background":"#66a3e0"},"insert":"bkg colored"},{"insert":" plain\n"}]`,
want: `<p>abc <span style="background-color:#66a3e0;">bkg colored</span> plain</p>`,
},
"underlined": {
ops: `[{"attributes":{"underline":true},"insert":"underlined"},{"insert":"\n"}]`,
want: "<p><u>underlined</u></p>",
},
"size": {
ops: `[{"insert":"stuff "},
{"insert":"large","attributes":{"size":"large"}},{"insert":" other "},
{"insert":"small","attributes":{"size":"small"}},{"insert":"\n"}]`,
want: `<p>stuff <span class="ql-size-large">large</span> other <span class="ql-size-small">small</span></p>`,
},
"superscript": {
ops: `[{"insert":"plain"},{"attributes":{"script":"super"},"insert":"super"},{"insert":"\n"}]`,
want: "<p>plain<sup>super</sup></p>",
},
"subscript": {
ops: `[{"insert":"plain"},{"attributes":{"script":"sub"},"insert":"sub"},{"insert":"\n"}]`,
want: "<p>plain<sub>sub</sub></p>",
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
got, err := Render([]byte(tc.ops))
if err != nil {
t.Fatalf("%s", err)
}
if string(got) != tc.want {
t.Errorf("bad rendering; got: %s", got)
}
})
}
}
func TestRender(t *testing.T) {
pairNames := []string{"ops1", "nested", "ordering", "list1", "list2", "list3", "list4", "indent", "code1", "code2", "code3"}
for _, n := range pairNames {
t.Run(n, func(t *testing.T) {
ops, err := ioutil.ReadFile("./testdata/" + n + ".json")
if err != nil {
t.Fatalf("could not read %s.json; %s", n, err)
}
html, err := ioutil.ReadFile("./testdata/" + n + ".html")
if err != nil {
t.Fatalf("could not read %s.html; %s", n, err)
}
got, err := Render(ops)
if err != nil {
t.Errorf("error rendering; %v", err)
}
if !bytes.Equal(html, got) {
t.Errorf("bad rendering:\nwanted: \n%s\ngot: \n%s", html, got)
}
})
}
}
func TestClassesList(t *testing.T) {
cases := []struct {
classes []string
expect string
}{
{nil, ""},
{[]string{}, ""},
{[]string{"abc"}, ` class="abc"`},
{[]string{"abc", "ee-abcd"}, ` class="abc ee-abcd"`},
}
for i, tc := range cases {
t.Run("case_"+strconv.Itoa(i), func(t *testing.T) {
got := classesList(tc.classes)
if got != tc.expect {
t.Errorf("expected %q but got %q", tc.expect, got)
}
})
}
}
func BenchmarkRender_ops1(b *testing.B) {
bts, err := ioutil.ReadFile("./testdata/ops1.json")
if err != nil {
b.Fatalf("could not read ops file: %s", err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
bts, err := Render(bts)
if err != nil {
b.Errorf("error rendering: %s", err)
}
_ = bts
}
}