Debian Codegraph @

parser/metadecoders/decoder_test.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package metadecoders
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 func TestUnmarshalXML(t *testing.T) {
23 c := qt.New(t)
24
25 xmlDoc := `<?xml version="1.0" encoding="utf-8" standalone="yes"?>
26 <rss version="2.0"
27 xmlns:atom="http://www.w3.org/2005/Atom">
28 <channel>
29 <title>Example feed</title>
30 <link>https://example.com/</link>
31 <description>Example feed</description>
32 <generator>Hugo -- gohugo.io</generator>
33 <language>en-us</language>
34 <copyright>Example</copyright>
35 <lastBuildDate>Fri, 08 Jan 2021 14:44:10 +0000</lastBuildDate>
36 <atom:link href="https://example.com/feed.xml" rel="self" type="application/rss+xml"/>
37 <item>
38 <title>Example title</title>
39 <link>https://example.com/2021/11/30/example-title/</link>
40 <pubDate>Tue, 30 Nov 2021 15:00:00 +0000</pubDate>
41 <guid>https://example.com/2021/11/30/example-title/</guid>
42 <description>Example description</description>
43 </item>
44 </channel>
45 </rss>`
46
47 expect := map[string]any{
48 "-atom": "http://www.w3.org/2005/Atom", "-version": "2.0",
49 "channel": map[string]any{
50 "copyright": "Example",
51 "description": "Example feed",
52 "generator": "Hugo -- gohugo.io",
53 "item": map[string]any{
54 "description": "Example description",
55 "guid": "https://example.com/2021/11/30/example-title/",
56 "link": "https://example.com/2021/11/30/example-title/",
57 "pubDate": "Tue, 30 Nov 2021 15:00:00 +0000",
58 "title": "Example title",
59 },
60 "language": "en-us",
61 "lastBuildDate": "Fri, 08 Jan 2021 14:44:10 +0000",
62 "link": []any{"https://example.com/", map[string]any{
63 "-href": "https://example.com/feed.xml",
64 "-rel": "self",
65 "-type": "application/rss+xml",
66 }},
67 "title": "Example feed",
68 },
69 }
70
71 d := Default
72
73 m, err := d.Unmarshal([]byte(xmlDoc), XML)
74 c.Assert(err, qt.IsNil)
75 c.Assert(m, qt.DeepEquals, expect)
76 }
77
78 func TestUnmarshalToMap(t *testing.T) {
79 c := qt.New(t)
80
81 expect := map[string]any{"a": "b"}
82
83 d := Default
84
85 for i, test := range []struct {
86 data string
87 format Format
88 expect any
89 }{
90 {`a = "b"`, TOML, expect},
91 {`a: "b"`, YAML, expect},
92 // Make sure we get all string keys, even for YAML
93 {"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": uint64(2), "d": []any{uint64(3), uint64(4)}}}},
94 {"a:\n true: 1\n false: 2", YAML, map[string]any{"a": map[string]any{"true": uint64(1), "false": uint64(2)}}},
95 {`{ "a": "b" }`, JSON, expect},
96 {`<root><a>b</a></root>`, XML, expect},
97 {`#+a: b`, ORG, expect},
98 // errors
99 {`a = b`, TOML, false},
100 {`a,b,c`, CSV, false}, // Use Unmarshal for CSV
101 {`<root>just a string</root>`, XML, false},
102 } {
103 msg := qt.Commentf("%d: %s", i, test.format)
104 m, err := d.UnmarshalToMap([]byte(test.data), test.format)
105 if b, ok := test.expect.(bool); ok && !b {
106 c.Assert(err, qt.Not(qt.IsNil), msg)
107 } else {
108 c.Assert(err, qt.IsNil, msg)
109 c.Assert(m, qt.DeepEquals, test.expect, msg)
110 }
111 }
112 }
113
114 func TestUnmarshalToInterface(t *testing.T) {
115 c := qt.New(t)
116
117 expect := map[string]any{"a": "b"}
118
119 d := Default
120
121 for i, test := range []struct {
122 data []byte
123 format Format
124 expect any
125 }{
126 {[]byte(`[ "Brecker", "Blake", "Redman" ]`), JSON, []any{"Brecker", "Blake", "Redman"}},
127 {[]byte(`{ "a": "b" }`), JSON, expect},
128 {[]byte(``), JSON, map[string]any{}},
129 {[]byte(nil), JSON, map[string]any{}},
130 {[]byte(`#+a: b`), ORG, expect},
131 {[]byte("#+a: foo bar\n#+a: baz"), ORG, map[string]any{"a": []string{string("foo bar"), string("baz")}}},
132 {[]byte(`#+DATE: <2020-06-26 Fri>`), ORG, map[string]any{"date": "2020-06-26"}},
133 {[]byte(`#+LASTMOD: <2020-06-26 Fri>`), ORG, map[string]any{"lastmod": "2020-06-26"}},
134 {[]byte(`#+FILETAGS: :work:`), ORG, map[string]any{"filetags": []string{"work"}}},
135 {[]byte(`#+FILETAGS: :work:fun:`), ORG, map[string]any{"filetags": []string{"work", "fun"}}},
136 {[]byte(`#+PUBLISHDATE: <2020-06-26 Fri>`), ORG, map[string]any{"publishdate": "2020-06-26"}},
137 {[]byte(`#+EXPIRYDATE: <2020-06-26 Fri>`), ORG, map[string]any{"expirydate": "2020-06-26"}},
138 {[]byte(`a = "b"`), TOML, expect},
139 {[]byte(`a: "b"`), YAML, expect},
140 {[]byte(`<root><a>b</a></root>`), XML, expect},
141 {[]byte(`a,b,c`), CSV, [][]string{{"a", "b", "c"}}},
142 {[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": uint64(2), "d": []any{uint64(3), uint64(4)}}}},
143 // errors
144 {[]byte(`a = "`), TOML, false},
145 } {
146 msg := qt.Commentf("%d: %s", i, test.format)
147 m, err := d.Unmarshal(test.data, test.format)
148 if b, ok := test.expect.(bool); ok && !b {
149 c.Assert(err, qt.Not(qt.IsNil), msg)
150 } else {
151 c.Assert(err, qt.IsNil, msg)
152 c.Assert(m, qt.DeepEquals, test.expect, msg)
153 }
154
155 }
156 }
157
158 func TestUnmarshalStringTo(t *testing.T) {
159 c := qt.New(t)
160
161 d := Default
162
163 expectMap := map[string]any{"a": "b"}
164
165 for i, test := range []struct {
166 data string
167 to any
168 expect any
169 }{
170 {"a string", "string", "a string"},
171 {`{ "a": "b" }`, make(map[string]any), expectMap},
172 {"32", int64(1234), int64(32)},
173 {"32", int(1234), int(32)},
174 {"3.14159", float64(1), float64(3.14159)},
175 {"[3,7,9]", []any{}, []any{uint64(3), uint64(7), uint64(9)}},
176 {"[3.1,7.2,9.3]", []any{}, []any{3.1, 7.2, 9.3}},
177 } {
178 msg := qt.Commentf("%d: %T", i, test.to)
179 m, err := d.UnmarshalStringTo(test.data, test.to)
180 if b, ok := test.expect.(bool); ok && !b {
181 c.Assert(err, qt.Not(qt.IsNil), msg)
182 } else {
183 c.Assert(err, qt.IsNil, msg)
184 c.Assert(m, qt.DeepEquals, test.expect, msg)
185 }
186
187 }
188 }
189
190 func TestCalculateAliasLimit(t *testing.T) {
191 c := qt.New(t)
192
193 const kb = 1024
194
195 c.Assert(calculateCollectionAliasLimit(0), qt.Equals, 100)
196 c.Assert(calculateCollectionAliasLimit(500), qt.Equals, 100)
197 c.Assert(calculateCollectionAliasLimit(1*kb), qt.Equals, 100)
198 c.Assert(calculateCollectionAliasLimit(2*kb), qt.Equals, 5000)
199 c.Assert(calculateCollectionAliasLimit(8*kb), qt.Equals, 5000)
200 c.Assert(calculateCollectionAliasLimit(12*kb), qt.Equals, 10000)
201 c.Assert(calculateCollectionAliasLimit(10000*kb), qt.Equals, 10000)
202 }
203
204 func BenchmarkDecodeYAMLToMap(b *testing.B) {
205 d := Default
206
207 data := []byte(`
208 a:
209 v1: 32
210 v2: 43
211 v3: "foo"
212 b:
213 - a
214 - b
215 c: "d"
216
217 `)
218
219 for b.Loop() {
220 _, err := d.UnmarshalToMap(data, YAML)
221 if err != nil {
222 b.Fatal(err)
223 }
224 }
225 }
226
227 func BenchmarkUnmarshalBillionLaughs(b *testing.B) {
228 yamlBillionLaughs := []byte(`
229 a: &a [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _]
230 b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a]
231 c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b, *b]
232 d: &d [*c, *c, *c, *c, *c, *c, *c, *c, *c, *c]
233 e: &e [*d, *d, *d, *d, *d, *d, *d, *d, *d, *d]
234 f: &f [*e, *e, *e, *e, *e, *e, *e, *e, *e, *e]
235 g: &g [*f, *f, *f, *f, *f, *f, *f, *f, *f, *f]
236 h: &h [*g, *g, *g, *g, *g, *g, *g, *g, *g, *g]
237 i: &i [*h, *h, *h, *h, *h, *h, *h, *h, *h, *h]
238 `)
239
240 yamlFrontMatter := []byte(`
241 title: mysect
242 tags: [tag1, tag2]
243 params:
244 color: blue
245 `)
246
247 yamlTests := []struct {
248 Title string
249 Content []byte
250 IsExpectedToFailValidation bool
251 }{
252 {"Billion Laughs", yamlBillionLaughs, true},
253 {"YAML Front Matter", yamlFrontMatter, false},
254 }
255
256 for _, tt := range yamlTests {
257
258 b.Run(tt.Title+" no validation", func(b *testing.B) {
259 for b.Loop() {
260 var v any
261 if err := unmarshalYamlNoValidation(tt.Content, &v); err != nil {
262 b.Fatal(err)
263 }
264 }
265 })
266
267 b.Run(tt.Title+" with validation", func(b *testing.B) {
268 for b.Loop() {
269 var v any
270 err := UnmarshalYaml(tt.Content, &v)
271 if tt.IsExpectedToFailValidation {
272 if err == nil {
273 b.Fatal("expected to fail validation but did not")
274 }
275 } else {
276 if err != nil {
277 b.Fatal(err)
278 }
279 }
280 }
281 })
282 }
283 }