Debian Codegraph @

tpl/transform/remarshal_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 transform_test
15
16 import (
17 "testing"
18
19 "github.com/gohugoio/hugo/htesting"
20 "github.com/gohugoio/hugo/hugolib"
21 "github.com/gohugoio/hugo/tpl/transform"
22
23 qt "github.com/frankban/quicktest"
24 )
25
26 func TestRemarshal(t *testing.T) {
27 t.Parallel()
28
29 b := hugolib.Test(t, "")
30
31 ns := transform.New(b.H.Deps)
32 c := qt.New(t)
33
34 c.Run("Roundtrip variants", func(c *qt.C) {
35 tomlExample := `title = 'Test Metadata'
36
37 [[resources]]
38 src = '**image-4.png'
39 title = 'The Fourth Image!'
40 [resources.params]
41 byline = 'picasso'
42
43 [[resources]]
44 name = 'my-cool-image-:counter'
45 src = '**.png'
46 title = 'TOML: The Image #:counter'
47 [resources.params]
48 byline = 'bep'
49 `
50
51 yamlExample := `resources:
52 - params:
53 byline: picasso
54 src: '**image-4.png'
55 title: The Fourth Image!
56 - name: my-cool-image-:counter
57 params:
58 byline: bep
59 src: '**.png'
60 title: 'TOML: The Image #:counter'
61 title: Test Metadata
62 `
63
64 jsonExample := `{
65 "resources": [
66 {
67 "params": {
68 "byline": "picasso"
69 },
70 "src": "**image-4.png",
71 "title": "The Fourth Image!"
72 },
73 {
74 "name": "my-cool-image-:counter",
75 "params": {
76 "byline": "bep"
77 },
78 "src": "**.png",
79 "title": "TOML: The Image #:counter"
80 }
81 ],
82 "title": "Test Metadata"
83 }
84 `
85 xmlExample := `<root>
86 <resources>
87 <params>
88 <byline>picasso</byline>
89 </params>
90 <src>**image-4.png</src>
91 <title>The Fourth Image!</title>
92 </resources>
93 <resources>
94 <name>my-cool-image-:counter</name>
95 <params>
96 <byline>bep</byline>
97 </params>
98 <src>**.png</src>
99 <title>TOML: The Image #:counter</title>
100 </resources>
101 <title>Test Metadata</title>
102 </root>
103 `
104
105 variants := []struct {
106 format string
107 data string
108 }{
109 {"yaml", yamlExample},
110 {"json", jsonExample},
111 {"toml", tomlExample},
112 {"TOML", tomlExample},
113 {"Toml", tomlExample},
114 {" TOML ", tomlExample},
115 {"XML", xmlExample},
116 }
117
118 for _, v1 := range variants {
119 for _, v2 := range variants {
120 // Both from and to may be the same here, but that is fine.
121 fromTo := qt.Commentf("%s => %s", v2.format, v1.format)
122
123 converted, err := ns.Remarshal(v1.format, v2.data)
124 c.Assert(err, qt.IsNil, fromTo)
125 diff := htesting.DiffStrings(v1.data, converted)
126 if len(diff) > 0 {
127 t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff)
128 }
129
130 }
131 }
132 })
133
134 c.Run("Comments", func(c *qt.C) {
135 input := `
136 Hugo = "Rules"
137
138 # It really does!
139
140 [m]
141 # A comment
142 a = "b"
143
144 `
145
146 expected := `Hugo = 'Rules'
147 [m]
148 a = 'b'
149 `
150
151 for _, format := range []string{"json", "yaml", "toml"} {
152 fromTo := qt.Commentf("%s => %s", "toml", format)
153
154 converted := input
155 var err error
156 // Do a round-trip conversion
157 for _, toFormat := range []string{format, "toml"} {
158 converted, err = ns.Remarshal(toFormat, converted)
159 c.Assert(err, qt.IsNil, fromTo)
160 }
161
162 diff := htesting.DiffStrings(expected, converted)
163 if len(diff) > 0 {
164 t.Fatalf("[%s] Expected \n%v\ngot\n>>%v\ndiff:\n%v\n", fromTo, expected, converted, diff)
165 }
166 }
167 })
168
169 // Issue 8850
170 c.Run("TOML Indent", func(c *qt.C) {
171 input := `
172
173 [params]
174 [params.variables]
175 a = "b"
176
177 `
178
179 converted, err := ns.Remarshal("toml", input)
180 c.Assert(err, qt.IsNil)
181 c.Assert(converted, qt.Equals, "[params]\n [params.variables]\n a = 'b'\n")
182 })
183
184 c.Run("Map input", func(c *qt.C) {
185 input := map[string]any{
186 "hello": "world",
187 }
188
189 output, err := ns.Remarshal("toml", input)
190 c.Assert(err, qt.IsNil)
191 c.Assert(output, qt.Equals, "hello = 'world'\n")
192 })
193
194 c.Run("Error", func(c *qt.C) {
195 _, err := ns.Remarshal("asdf", "asdf")
196 c.Assert(err, qt.Not(qt.IsNil))
197
198 _, err = ns.Remarshal("json", "asdf")
199 c.Assert(err, qt.Not(qt.IsNil))
200 })
201 }
202
203 func TestRemarshaBillionLaughs(t *testing.T) {
204 t.Parallel()
205
206 yamlBillionLaughs := `
207 a: &a [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _]
208 b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a]
209 c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b, *b]
210 d: &d [*c, *c, *c, *c, *c, *c, *c, *c, *c, *c]
211 e: &e [*d, *d, *d, *d, *d, *d, *d, *d, *d, *d]
212 f: &f [*e, *e, *e, *e, *e, *e, *e, *e, *e, *e]
213 g: &g [*f, *f, *f, *f, *f, *f, *f, *f, *f, *f]
214 h: &h [*g, *g, *g, *g, *g, *g, *g, *g, *g, *g]
215 i: &i [*h, *h, *h, *h, *h, *h, *h, *h, *h, *h]
216 `
217
218 yamlMillionLaughs := `
219 a: &a [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _]
220 b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a]
221 c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b, *b]
222 d: &d [*c, *c, *c, *c, *c, *c, *c, *c, *c, *c]
223 e: &e [*d, *d, *d, *d, *d, *d, *d, *d, *d, *d]
224 f: &f [*e, *e, *e, *e, *e, *e, *e, *e, *e, *e]
225 `
226
227 yamlTenThousandLaughs := `
228 a: &a [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _]
229 b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a]
230 c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b, *b]
231 d: &d [*c, *c, *c, *c, *c, *c, *c, *c, *c, *c]
232
233 `
234
235 yamlThousandLaughs := `
236 a: &a [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _]
237 b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a]
238 c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b, *b]
239
240 `
241
242 b := hugolib.Test(t, "")
243
244 ns := transform.New(b.H.Deps)
245
246 for _, test := range []struct {
247 name string
248 data string
249 }{
250 {"10k", yamlTenThousandLaughs},
251 {"1M", yamlMillionLaughs},
252 {"1B", yamlBillionLaughs},
253 } {
254 t.Run(test.name, func(t *testing.T) {
255 t.Parallel()
256 c := qt.New(t)
257 _, err := ns.Remarshal("json", test.data)
258 c.Assert(err, qt.Not(qt.IsNil))
259 })
260 }
261
262 // Thousand laughs should be ok.
263 // It produces about 29KB of JSON,
264 // which is still a large output for such a large input,
265 // but there may be use cases for this.
266 _, err := ns.Remarshal("json", yamlThousandLaughs)
267 c := qt.New(t)
268 c.Assert(err, qt.IsNil)
269 }