remarshal_test.go (4498B)
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.NewIntegrationTestBuilder( 30 hugolib.IntegrationTestConfig{T: t}, 31 ).Build() 32 33 ns := transform.New(b.H.Deps) 34 c := qt.New(t) 35 36 c.Run("Roundtrip variants", func(c *qt.C) { 37 tomlExample := `title = 'Test Metadata' 38 39 [[resources]] 40 src = '**image-4.png' 41 title = 'The Fourth Image!' 42 [resources.params] 43 byline = 'picasso' 44 45 [[resources]] 46 name = 'my-cool-image-:counter' 47 src = '**.png' 48 title = 'TOML: The Image #:counter' 49 [resources.params] 50 byline = 'bep' 51 ` 52 53 yamlExample := `resources: 54 - params: 55 byline: picasso 56 src: '**image-4.png' 57 title: The Fourth Image! 58 - name: my-cool-image-:counter 59 params: 60 byline: bep 61 src: '**.png' 62 title: 'TOML: The Image #:counter' 63 title: Test Metadata 64 ` 65 66 jsonExample := `{ 67 "resources": [ 68 { 69 "params": { 70 "byline": "picasso" 71 }, 72 "src": "**image-4.png", 73 "title": "The Fourth Image!" 74 }, 75 { 76 "name": "my-cool-image-:counter", 77 "params": { 78 "byline": "bep" 79 }, 80 "src": "**.png", 81 "title": "TOML: The Image #:counter" 82 } 83 ], 84 "title": "Test Metadata" 85 } 86 ` 87 xmlExample := `<root> 88 <resources> 89 <params> 90 <byline>picasso</byline> 91 </params> 92 <src>**image-4.png</src> 93 <title>The Fourth Image!</title> 94 </resources> 95 <resources> 96 <name>my-cool-image-:counter</name> 97 <params> 98 <byline>bep</byline> 99 </params> 100 <src>**.png</src> 101 <title>TOML: The Image #:counter</title> 102 </resources> 103 <title>Test Metadata</title> 104 </root> 105 ` 106 107 variants := []struct { 108 format string 109 data string 110 }{ 111 {"yaml", yamlExample}, 112 {"json", jsonExample}, 113 {"toml", tomlExample}, 114 {"TOML", tomlExample}, 115 {"Toml", tomlExample}, 116 {" TOML ", tomlExample}, 117 {"XML", xmlExample}, 118 } 119 120 for _, v1 := range variants { 121 for _, v2 := range variants { 122 // Both from and to may be the same here, but that is fine. 123 fromTo := qt.Commentf("%s => %s", v2.format, v1.format) 124 125 converted, err := ns.Remarshal(v1.format, v2.data) 126 c.Assert(err, qt.IsNil, fromTo) 127 diff := htesting.DiffStrings(v1.data, converted) 128 if len(diff) > 0 { 129 t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff) 130 } 131 132 } 133 } 134 }) 135 136 c.Run("Comments", func(c *qt.C) { 137 input := ` 138 Hugo = "Rules" 139 140 # It really does! 141 142 [m] 143 # A comment 144 a = "b" 145 146 ` 147 148 expected := `Hugo = 'Rules' 149 [m] 150 a = 'b' 151 ` 152 153 for _, format := range []string{"json", "yaml", "toml"} { 154 fromTo := qt.Commentf("%s => %s", "toml", format) 155 156 converted := input 157 var err error 158 // Do a round-trip conversion 159 for _, toFormat := range []string{format, "toml"} { 160 converted, err = ns.Remarshal(toFormat, converted) 161 c.Assert(err, qt.IsNil, fromTo) 162 } 163 164 diff := htesting.DiffStrings(expected, converted) 165 if len(diff) > 0 { 166 t.Fatalf("[%s] Expected \n%v\ngot\n>>%v\ndiff:\n%v\n", fromTo, expected, converted, diff) 167 } 168 } 169 }) 170 171 // Issue 8850 172 c.Run("TOML Indent", func(c *qt.C) { 173 input := ` 174 175 [params] 176 [params.variables] 177 a = "b" 178 179 ` 180 181 converted, err := ns.Remarshal("toml", input) 182 c.Assert(err, qt.IsNil) 183 c.Assert(converted, qt.Equals, "[params]\n [params.variables]\n a = 'b'\n\n\n") 184 }) 185 186 c.Run("Map input", func(c *qt.C) { 187 input := map[string]any{ 188 "hello": "world", 189 } 190 191 output, err := ns.Remarshal("toml", input) 192 c.Assert(err, qt.IsNil) 193 c.Assert(output, qt.Equals, "hello = 'world'\n") 194 }) 195 196 c.Run("Error", func(c *qt.C) { 197 _, err := ns.Remarshal("asdf", "asdf") 198 c.Assert(err, qt.Not(qt.IsNil)) 199 200 _, err = ns.Remarshal("json", "asdf") 201 c.Assert(err, qt.Not(qt.IsNil)) 202 }) 203 }