Debian Codegraph @

tpl/transform/unmarshal_test.go

raw
1 // Copyright 2019 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 "context"
18 "fmt"
19 "math/rand"
20 "strings"
21 "testing"
22
23 "github.com/gohugoio/hugo/hugolib"
24 "github.com/gohugoio/hugo/tpl/transform"
25
26 "github.com/gohugoio/hugo/common/hugio"
27 "github.com/gohugoio/hugo/resources/resource"
28
29 "github.com/gohugoio/hugo/media"
30
31 qt "github.com/frankban/quicktest"
32 )
33
34 const (
35 testJSON = `
36
37 {
38 "ROOT_KEY": {
39 "title": "example glossary",
40 "GlossDiv": {
41 "title": "S",
42 "GlossList": {
43 "GlossEntry": {
44 "ID": "SGML",
45 "SortAs": "SGML",
46 "GlossTerm": "Standard Generalized Markup Language",
47 "Acronym": "SGML",
48 "Abbrev": "ISO 8879:1986",
49 "GlossDef": {
50 "para": "A meta-markup language, used to create markup languages such as DocBook.",
51 "GlossSeeAlso": ["GML", "XML"]
52 },
53 "GlossSee": "markup"
54 }
55 }
56 }
57 }
58 }
59
60 `
61 )
62
63 var _ resource.ReadSeekCloserResource = (*testContentResource)(nil)
64
65 type testContentResource struct {
66 content string
67 mime media.Type
68
69 key string
70 }
71
72 func (t testContentResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) {
73 return hugio.NewReadSeekerNoOpCloserFromString(t.content), nil
74 }
75
76 func (t testContentResource) MediaType() media.Type {
77 return t.mime
78 }
79
80 func (t testContentResource) Key() string {
81 return t.key
82 }
83
84 func TestUnmarshal(t *testing.T) {
85 b := hugolib.Test(t, "")
86
87 ns := transform.New(b.H.Deps)
88
89 assertSlogan := func(m map[string]any) {
90 b.Assert(m["slogan"], qt.Equals, "Hugo Rocks!")
91 }
92
93 for _, test := range []struct {
94 data any
95 options any
96 expect any
97 }{
98 {`{ "slogan": "Hugo Rocks!" }`, nil, func(m map[string]any) {
99 assertSlogan(m)
100 }},
101 {`slogan: "Hugo Rocks!"`, nil, func(m map[string]any) {
102 assertSlogan(m)
103 }},
104 {`slogan = "Hugo Rocks!"`, nil, func(m map[string]any) {
105 assertSlogan(m)
106 }},
107 {testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.Builtin.YAMLType}, nil, func(m map[string]any) {
108 assertSlogan(m)
109 }},
110 {testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.Builtin.JSONType}, nil, func(m map[string]any) {
111 assertSlogan(m)
112 }},
113 {testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.Builtin.TOMLType}, nil, func(m map[string]any) {
114 assertSlogan(m)
115 }},
116 {testContentResource{key: "r1", content: `<root><slogan>Hugo Rocks!</slogan></root>"`, mime: media.Builtin.XMLType}, nil, func(m map[string]any) {
117 assertSlogan(m)
118 }},
119 {testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00
120 1999,Chevy,"Venture ""Extended Edition""","",4900.00`, mime: media.Builtin.CSVType}, nil, func(r [][]string) {
121 b.Assert(len(r), qt.Equals, 2)
122 first := r[0]
123 b.Assert(len(first), qt.Equals, 5)
124 b.Assert(first[1], qt.Equals, "Ford")
125 }},
126 {testContentResource{key: "r1", content: `a;b;c`, mime: media.Builtin.CSVType}, map[string]any{"delimiter": ";"}, func(r [][]string) {
127 b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
128 }},
129 {testContentResource{key: "r1", content: `{p: [a, b]}`, mime: media.Builtin.JSONType}, map[string]any{"format": "yaml"}, func(m map[string]any) {
130 b.Assert(m, qt.DeepEquals, map[string]any{"p": []any{"a", "b"}})
131 }},
132 {"a,b,c", nil, func(r [][]string) {
133 b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
134 }},
135 {"a;b;c", map[string]any{"delimiter": ";"}, func(r [][]string) {
136 b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
137 }},
138 {testContentResource{key: "r1", content: `
139 % This is a comment
140 a;b;c`, mime: media.Builtin.CSVType}, map[string]any{"DElimiter": ";", "Comment": "%"}, func(r [][]string) {
141 b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
142 }},
143 {``, nil, nil},
144 {` `, nil, nil},
145 {`{p: [a, b]}`, map[string]any{"format": "yaml"}, func(m map[string]any) {
146 b.Assert(m, qt.DeepEquals, map[string]any{"p": []any{"a", "b"}})
147 }},
148 // errors
149 {"thisisnotavaliddataformat", nil, false},
150 {testContentResource{key: "r1", content: `invalid&toml"`, mime: media.Builtin.TOMLType}, nil, false},
151 {testContentResource{key: "r1", content: `unsupported: MIME"`, mime: media.Builtin.CalendarType}, nil, false},
152 {"thisisnotavaliddataformat", nil, false},
153 {`{ notjson }`, nil, false},
154 {tstNoStringer{}, nil, false},
155 {"<root><a>notjson</a></root>", map[string]any{"format": "json"}, false},
156 {"anything", map[string]any{"format": "invalidformat"}, false},
157 } {
158
159 ns.Reset()
160
161 var args []any
162
163 if test.options != nil {
164 args = []any{test.options, test.data}
165 } else {
166 args = []any{test.data}
167 }
168
169 result, err := ns.Unmarshal(args...)
170
171 if bb, ok := test.expect.(bool); ok && !bb {
172 b.Assert(err, qt.Not(qt.IsNil))
173 } else if fn, ok := test.expect.(func(m map[string]any)); ok {
174 b.Assert(err, qt.IsNil)
175 m, ok := result.(map[string]any)
176 b.Assert(ok, qt.Equals, true)
177 fn(m)
178 } else if fn, ok := test.expect.(func(r [][]string)); ok {
179 b.Assert(err, qt.IsNil)
180 r, ok := result.([][]string)
181 b.Assert(ok, qt.Equals, true)
182 fn(r)
183 } else {
184 b.Assert(err, qt.IsNil)
185 b.Assert(result, qt.Equals, test.expect)
186 }
187
188 }
189 }
190
191 func BenchmarkUnmarshalString(b *testing.B) {
192 bb := hugolib.Test(b, "")
193
194 ns := transform.New(bb.H.Deps)
195
196 const numJsons = 100
197
198 var jsons [numJsons]string
199 for i := range numJsons {
200 jsons[i] = strings.Replace(testJSON, "ROOT_KEY", fmt.Sprintf("root%d", i), 1)
201 }
202
203 ctx := context.Background()
204
205 for b.Loop() {
206 result, err := ns.Unmarshal(ctx, jsons[rand.Intn(numJsons)])
207 if err != nil {
208 b.Fatal(err)
209 }
210 if result == nil {
211 b.Fatal("no result")
212 }
213 }
214 }
215
216 func BenchmarkUnmarshalResource(b *testing.B) {
217 bb := hugolib.Test(b, "")
218
219 ns := transform.New(bb.H.Deps)
220
221 const numJsons = 100
222
223 var jsons [numJsons]testContentResource
224 for i := range numJsons {
225 key := fmt.Sprintf("root%d", i)
226 jsons[i] = testContentResource{key: key, content: strings.Replace(testJSON, "ROOT_KEY", key, 1), mime: media.Builtin.JSONType}
227 }
228
229 ctx := context.Background()
230
231 for b.Loop() {
232 result, err := ns.Unmarshal(ctx, jsons[rand.Intn(numJsons)])
233 if err != nil {
234 b.Fatal(err)
235 }
236 if result == nil {
237 b.Fatal("no result")
238 }
239 }
240 }