forked from dchenk/go-render-quill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat_state_test.go
226 lines (191 loc) · 4.12 KB
/
format_state_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
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
package quill
import (
"bytes"
"sort"
"testing"
)
func TestFormatState_add(t *testing.T) {
cases := []struct {
current []*Format
keyword string
o *Op
want []*Format
}{
{
current: []*Format{}, // no formats
keyword: "italic",
o: &Op{
Data: "stuff",
Type: "text",
Attrs: map[string]string{"italic": "y"},
},
want: []*Format{
{
Val: "em",
Place: Tag,
},
},
},
{
current: []*Format{
{ // One format already set.
Val: "em",
Place: Tag,
},
},
keyword: "italic",
o: &Op{
Data: "stuff",
Type: "text",
Attrs: map[string]string{"italic": "y"},
},
want: []*Format{ // The way add works, it does not check if the format is already added.
{
Val: "em",
Place: Tag,
},
{
Val: "em",
Place: Tag,
},
},
},
}
fs := make(formatState, 0, 2) // reuse
for i, ca := range cases {
fs = ca.current
fmTer := ca.o.getFormatter(ca.keyword, nil)
fm := fmTer.Fmt()
fm.fm = fmTer
fs.add(fm)
if len(ca.want) != len(fs) {
t.Fatalf("(index %d) unequal count of formats", i)
}
for j := range fs {
if ca.want[j].Val != fs[j].Val {
t.Errorf("did not add format Val correctly (index %d); got %q", i, fs[j].Val)
}
if ca.want[j].Place != fs[j].Place {
t.Errorf("did not add format Place correctly (index %d); got %v", i, fs[j].Place)
}
if ca.want[j].Block != fs[j].Block {
t.Errorf("did not add format Block correctly (index %d); got %v", i, fs[j].Block)
}
}
}
}
func TestFormatState_closePrevious(t *testing.T) {
o1 := blankOp()
o1.Attrs["italic"] = "y"
o1.Attrs["bold"] = "y"
o2 := blankOp()
o2.Attrs["background"] = "#e0e0e0"
o2.Attrs["italic"] = "y"
cases := []formatState{
{
{"em", Tag, false, false, "", "", o1.getFormatter("italic", nil)},
{"strong", Tag, false, false, "", "", o1.getFormatter("bold", nil)},
},
{
{"background-color:#e0e0e0;", Style, false, false, "", "", o2.getFormatter("background", nil)},
{"em", Tag, false, false, "", "", o2.getFormatter("italic", nil)},
},
}
want := []string{"</strong></em>", "</em></span>"}
var buf bytes.Buffer
for i := range cases {
o := blankOp()
cases[i].closePrevious(&buf, o, false)
got := buf.String()
if got != want[i] || len(cases[i]) != 0 {
t.Errorf("closed formats wrong (index %d); wanted %q; got %q", i, want[i], got)
}
buf.Reset()
}
}
func TestFormatState_Sort(t *testing.T) {
o := &Op{
Data: "stuff",
Type: "text",
// no attributes set
}
cases := [][]struct {
Val string
Place FormatPlace
keyword string
}{
{
{"strong", Tag, "bold"},
{"em", Tag, "italic"},
},
{
{"u", Tag, "underline"},
{"align-center", Class, "align"},
{"strong", Tag, "bold"},
},
{
{"color:#e0e0e0;", Style, "color"},
{"em", Tag, "italic"},
},
{
{"em", Tag, "italic"},
{`<a href="https://widerwebs.com" target="_blank">`, Tag, "link"}, // link wrapper
},
}
want := [][]struct {
Val string
Place FormatPlace
keyword string
}{
{
{"em", Tag, "italic"},
{"strong", Tag, "bold"},
},
{
{"strong", Tag, "bold"},
{"u", Tag, "underline"},
{"align-center", Class, "align"},
},
{
{"em", Tag, "italic"},
{"color:#e0e0e0;", Style, "color"},
},
{
{`<a href="https://widerwebs.com" target="_blank">`, Tag, "link"}, // link wrapper
{"em", Tag, "italic"},
},
}
for i := range cases {
fsCase := make(formatState, 0, 1)
for _, s := range cases[i] {
fsCase = append(fsCase, &Format{
Val: s.Val,
Place: s.Place,
fm: o.getFormatter(s.keyword, nil),
})
}
sort.Sort(&fsCase)
fsWant := make(formatState, 0, 1)
for _, s := range want[i] {
fsWant = append(fsWant, &Format{
Val: s.Val,
Place: s.Place,
fm: o.getFormatter(s.keyword, nil),
})
}
ok := true
for j := range fsCase {
if fsCase[j].Val != fsWant[j].Val {
ok = false
} else if fsCase[j].Place != fsWant[j].Place {
ok = false
}
}
if !ok {
t.Errorf("bad sorting (index %d); got:\n", i)
for k := range fsCase {
t.Errorf(" (%d) %+v\n", k, fsCase[k])
}
}
}
}