Debian Codegraph @

internal/markup/markup_test.go

raw symbols SCIP
1 package markup
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/gabriel-vasile/mimetype/internal/scan"
8 )
9
10 var getAnAttributeTestCases = []struct {
11 in string
12 name string
13 value string
14 hasMore bool
15 }{{
16 "", "", "", false,
17 }, {
18 "''", "", "", false,
19 }, {
20 `""`, "", "", false,
21 }, {
22 `"abc`, "", "", false,
23 }, {
24 "1>", "1", "", false,
25 }, {
26 "A>", "A", "", false,
27 }, {
28 "a>", "a", "", false,
29 }, {
30 "abc>", "abc", "", false,
31 }, {
32 "'abc'", "", "", false,
33 }, {
34 "'abc'>", "'abc'", "", false,
35 }, {
36 // > as attribute ender
37 "meta1=meta>", "meta1", "meta", false,
38 }, {
39 "meta2=META>", "meta2", "META", false,
40 }, {
41 `meta3="meta">`, "meta3", "meta", false,
42 }, {
43 `meta4="'meta">`, "meta4", "'meta", false,
44 }, {
45 " meta5 = meta >", "meta5", "meta", true,
46 }, {
47 " meta6 =' meta '>", "meta6", " meta ", false,
48 }, {
49 ` meta7 =' "meta '>`, "meta7", ` "meta `, false,
50 }, {
51 ` mEtA7 =' "meta '>`, "mEtA7", ` "meta `, false,
52 // / as attribute ender
53 }, {
54 // when the value is unquoted / right after is a parse warning
55 "meta1=meta/", "meta1", "", false,
56 }, {
57 "meta2=META/", "meta2", "", false,
58 }, {
59 "meta3=meta /", "meta3", "meta", true,
60 }, {
61 "meta4=META /", "meta4", "META", true,
62 }, {
63 `meta5="meta"/`, "meta5", "meta", true,
64 }, {
65 `meta6="'meta"/`, "meta6", "'meta", true,
66 }, {
67 " meta7 = meta /", "meta7", "meta", true,
68 }, {
69 " meta8 =' meta '/", "meta8", " meta ", true,
70 }, {
71 ` meta9 =' "meta '/`, "meta9", ` "meta `, true,
72 }, {
73 ` meta0 /`, "meta0", ``, true,
74 }, {
75 "; charset=UTF-8", ";", "", true,
76 }, {
77 ` http-equiv="content-type" content="text/html; charset=iso-8859-15">`, "http-equiv", `content-type`, true,
78 }}
79
80 func TestGetAnAttribute(t *testing.T) {
81 for _, tc := range getAnAttributeTestCases {
82 t.Run(tc.in, func(t *testing.T) {
83 s := scan.Bytes(tc.in)
84 name, value, hasMore := GetAnAttribute(&s)
85 if string(name) != tc.name {
86 t.Errorf("name: got: %s, want: %s", name, tc.name)
87 }
88 if string(value) != tc.value {
89 t.Errorf("value: got: %s, want: %s", value, tc.value)
90 }
91 if hasMore != tc.hasMore {
92 t.Errorf("hasMore: got: %t, want: %t", hasMore, tc.hasMore)
93 }
94 })
95 }
96 }
97 func FuzzGetAnAttribute(f *testing.F) {
98 for _, t := range getAnAttributeTestCases {
99 f.Add([]byte(t.in))
100 }
101
102 f.Fuzz(func(t *testing.T, d []byte) {
103 s := scan.Bytes(d)
104 GetAnAttribute(&s)
105 })
106 }
107
108 var getAValueTestCases = []struct {
109 in string
110 out string
111 hasMore bool
112 }{{
113 "", "", false,
114 }, {
115 " ", "", false,
116 }, {
117 "''", "", false,
118 }, {
119 `""`, "", false,
120 }, {
121 `"abc`, "", false,
122 }, {
123 ">", "", false,
124 }, {
125 "1>", "1", false,
126 }, {
127 "A>", "A", false,
128 }, {
129 "a>", "a", false,
130 }, {
131 "abc>", "abc", false,
132 }, {
133 "ABCXYZ>", "ABCXYZ", false,
134 }, {
135 "'abc'", "abc", false,
136 }, {
137 "'abc'>", "abc", false,
138 }, {
139 "abc def=ghi", "abc", true,
140 }, {
141 "abc >", "abc", true,
142 }, {
143 "'abc' >", "abc", true,
144 }, {
145 "'ABCXYZ' >", "ABCXYZ", true,
146 }, {
147 `"abc" >`, "abc", true,
148 }}
149
150 func TestGetAValue(t *testing.T) {
151 for _, tc := range getAValueTestCases {
152 t.Run(tc.in, func(t *testing.T) {
153 s := scan.Bytes(tc.in)
154 got, hasMore := getAValue(&s)
155 if string(got) != tc.out {
156 t.Errorf("got: %s, want: %s", got, tc.out)
157 }
158 if hasMore != tc.hasMore {
159 t.Errorf("hasMore: got: %t, want: %t", hasMore, tc.hasMore)
160 }
161 })
162 }
163 }
164 func FuzzGetAValue(f *testing.F) {
165 for _, tc := range getAValueTestCases {
166 f.Add([]byte(tc.in))
167 }
168
169 f.Fuzz(func(t *testing.T, d []byte) {
170 s := scan.Bytes(d)
171 getAValue(&s)
172 })
173 }
174
175 func TestGetAllAttributes(t *testing.T) {
176 tcases := []struct {
177 in string
178 expected [][2]string
179 }{{
180 "", [][2]string{},
181 }, {
182 // doesn't have ending >
183 "a", [][2]string{},
184 }, {
185 // doesn't have ending >
186 "abc", [][2]string{},
187 }, {
188 "a b c", [][2]string{{"a", ""}, {"b", ""}},
189 }, {
190 "abc abc abc", [][2]string{{"abc", ""}, {"abc", ""}},
191 }, {
192 "a=1 b=2 c=3", [][2]string{{"a", "1"}, {"b", "2"}, {"c", ""}},
193 }, {
194 "a=1 b c=3", [][2]string{{"a", "1"}, {"b", ""}, {"c", ""}},
195 }, {
196 "a b=2 c", [][2]string{{"a", ""}, {"b", "2"}},
197 }, {
198 ">", [][2]string{},
199 }, {
200 "a>", [][2]string{{"a", ""}},
201 }, {
202 "abc>", [][2]string{{"abc", ""}},
203 }, {
204 "a b c>", [][2]string{{"a", ""}, {"b", ""}, {"c", ""}},
205 }, {
206 "a b/ c>", [][2]string{{"a", ""}, {"b", ""}, {"c", ""}},
207 }, {
208 "/a b/ c>", [][2]string{{"a", ""}, {"b", ""}, {"c", ""}},
209 }, {
210 "a b abc/>", [][2]string{{"a", ""}, {"b", ""}, {"abc", ""}},
211 }}
212
213 getAll := func(in string) [][2]string {
214 s := scan.Bytes(in)
215 ret := [][2]string{}
216 for {
217 name, value, _ := GetAnAttribute(&s)
218 if len(name) == 0 {
219 return ret
220 }
221 ret = append(ret, [2]string{string(name), string(value)})
222 }
223 }
224
225 for _, tc := range tcases {
226 t.Run(tc.in, func(t *testing.T) {
227 got := getAll(tc.in)
228 if !reflect.DeepEqual(got, tc.expected) {
229 t.Errorf("got: %v, want: %v", got, tc.expected)
230 }
231 })
232 }
233 }
234
235 func TestSkipAComment(t *testing.T) {
236 tcases := []struct {
237 in string
238 out string
239 skipped bool
240 }{{
241 "", "", false,
242 }, {
243 "abc", "abc", false,
244 }, {
245 "<!--", "<!--", false, // not ending comment
246 }, {
247 "<!-- abc -->", "", true, // regular comment
248 }, {
249 "<!-->", "", true, // the beginning and ending -- are the same chars
250 }}
251 for _, tc := range tcases {
252 t.Run(tc.in, func(t *testing.T) {
253 s := scan.Bytes(tc.in)
254 skipped := SkipAComment(&s)
255 if tc.skipped != skipped {
256 t.Errorf("skipped got: %v, want: %v", skipped, tc.skipped)
257 }
258 if string(s) != tc.out {
259 t.Errorf("got: %v, want: %v", string(s), tc.out)
260 }
261 })
262 }
263 }