2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-03-10 18:17:39 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2016-12-26 16:23:20 -05:00
|
|
|
"bytes"
|
|
|
|
"reflect"
|
2015-03-10 18:17:39 -04:00
|
|
|
"testing"
|
2018-10-20 11:38:49 -04:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
2015-03-10 18:17:39 -04:00
|
|
|
)
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
func TestInterfaceToConfig(t *testing.T) {
|
|
|
|
cases := []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
input any
|
2018-10-20 11:38:49 -04:00
|
|
|
format metadecoders.Format
|
|
|
|
want []byte
|
|
|
|
isErr bool
|
2016-12-26 16:23:20 -05:00
|
|
|
}{
|
|
|
|
// TOML
|
2022-03-17 17:03:27 -04:00
|
|
|
{map[string]any{}, metadecoders.TOML, nil, false},
|
2016-12-26 16:23:20 -05:00
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{"title": "test' 1"},
|
2018-10-20 11:38:49 -04:00
|
|
|
metadecoders.TOML,
|
2021-07-27 13:07:10 -04:00
|
|
|
[]byte("title = \"test' 1\"\n"),
|
2016-12-26 16:23:20 -05:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// YAML
|
2022-03-17 17:03:27 -04:00
|
|
|
{map[string]any{}, metadecoders.YAML, []byte("{}\n"), false},
|
2016-12-26 16:23:20 -05:00
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{"title": "test 1"},
|
2018-10-20 11:38:49 -04:00
|
|
|
metadecoders.YAML,
|
2016-12-26 16:23:20 -05:00
|
|
|
[]byte("title: test 1\n"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// JSON
|
2022-03-17 17:03:27 -04:00
|
|
|
{map[string]any{}, metadecoders.JSON, []byte("{}\n"), false},
|
2016-12-26 16:23:20 -05:00
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{"title": "test 1"},
|
2018-10-20 11:38:49 -04:00
|
|
|
metadecoders.JSON,
|
2016-12-26 16:23:20 -05:00
|
|
|
[]byte("{\n \"title\": \"test 1\"\n}\n"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Errors
|
2018-10-20 11:38:49 -04:00
|
|
|
{nil, metadecoders.TOML, nil, true},
|
2022-03-17 17:03:27 -04:00
|
|
|
{map[string]any{}, "foo", nil, true},
|
2016-12-26 16:23:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
err := InterfaceToConfig(c.input, c.format, &buf)
|
2016-12-26 16:23:20 -05:00
|
|
|
if err != nil {
|
|
|
|
if c.isErr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("[%d] unexpected error value: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(buf.Bytes(), c.want) {
|
|
|
|
t.Errorf("[%d] not equal:\nwant %q,\n got %q", i, c.want, buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|