Debian Codegraph @

output/config_test.go

raw
1 // Copyright 2024 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 output
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/media"
21 )
22
23 func TestDecodeConfig(t *testing.T) {
24 c := qt.New(t)
25
26 mediaTypes := media.Types{media.Builtin.JSONType, media.Builtin.XMLType}
27
28 tests := []struct {
29 name string
30 m map[string]any
31 shouldError bool
32 assert func(t *testing.T, name string, f Formats)
33 }{
34 {
35 "Redefine JSON",
36 map[string]any{
37 "json": map[string]any{
38 "baseName": "myindex",
39 "isPlainText": "false",
40 },
41 },
42 false,
43 func(t *testing.T, name string, f Formats) {
44 msg := qt.Commentf(name)
45 c.Assert(len(f), qt.Equals, len(DefaultFormats), msg)
46 json, _ := f.GetByName("JSON")
47 c.Assert(json.BaseName, qt.Equals, "myindex")
48 c.Assert(json.MediaType, qt.Equals, media.Builtin.JSONType)
49 c.Assert(json.IsPlainText, qt.Equals, false)
50 },
51 },
52 {
53 "Add XML format with string as mediatype",
54 map[string]any{
55 "MYXMLFORMAT": map[string]any{
56 "baseName": "myxml",
57 "mediaType": "application/xml",
58 },
59 },
60 false,
61 func(t *testing.T, name string, f Formats) {
62 c.Assert(len(f), qt.Equals, len(DefaultFormats)+1)
63 xml, found := f.GetByName("MYXMLFORMAT")
64 c.Assert(found, qt.Equals, true)
65 c.Assert(xml.BaseName, qt.Equals, "myxml")
66 c.Assert(xml.MediaType, qt.Equals, media.Builtin.XMLType)
67
68 // Verify that we haven't changed the DefaultFormats slice.
69 json, _ := f.GetByName("JSON")
70 c.Assert(json.BaseName, qt.Equals, "index")
71 },
72 },
73 {
74 "Add format unknown mediatype",
75 map[string]any{
76 "MYINVALID": map[string]any{
77 "baseName": "mymy",
78 "mediaType": "application/hugo",
79 },
80 },
81 true,
82 func(t *testing.T, name string, f Formats) {
83 },
84 },
85 }
86
87 for _, test := range tests {
88 result, err := DecodeConfig(mediaTypes, test.m)
89 msg := qt.Commentf(test.name)
90
91 if test.shouldError {
92 c.Assert(err, qt.Not(qt.IsNil), msg)
93 } else {
94 c.Assert(err, qt.IsNil, msg)
95 test.assert(t, test.name, result.Config)
96 }
97 }
98 }