github.com/gohugoio/hugo@v0.88.1/parser/frontmatter_test.go (about)

     1  // Copyright 2015 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 parser
    15  
    16  import (
    17  	"bytes"
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/parser/metadecoders"
    22  )
    23  
    24  func TestInterfaceToConfig(t *testing.T) {
    25  	cases := []struct {
    26  		input  interface{}
    27  		format metadecoders.Format
    28  		want   []byte
    29  		isErr  bool
    30  	}{
    31  		// TOML
    32  		{map[string]interface{}{}, metadecoders.TOML, nil, false},
    33  		{
    34  			map[string]interface{}{"title": "test' 1"},
    35  			metadecoders.TOML,
    36  			[]byte("title = \"test' 1\"\n"),
    37  			false,
    38  		},
    39  
    40  		// YAML
    41  		{map[string]interface{}{}, metadecoders.YAML, []byte("{}\n"), false},
    42  		{
    43  			map[string]interface{}{"title": "test 1"},
    44  			metadecoders.YAML,
    45  			[]byte("title: test 1\n"),
    46  			false,
    47  		},
    48  
    49  		// JSON
    50  		{map[string]interface{}{}, metadecoders.JSON, []byte("{}\n"), false},
    51  		{
    52  			map[string]interface{}{"title": "test 1"},
    53  			metadecoders.JSON,
    54  			[]byte("{\n   \"title\": \"test 1\"\n}\n"),
    55  			false,
    56  		},
    57  
    58  		// Errors
    59  		{nil, metadecoders.TOML, nil, true},
    60  		{map[string]interface{}{}, "foo", nil, true},
    61  	}
    62  
    63  	for i, c := range cases {
    64  		var buf bytes.Buffer
    65  
    66  		err := InterfaceToConfig(c.input, c.format, &buf)
    67  		if err != nil {
    68  			if c.isErr {
    69  				continue
    70  			}
    71  			t.Fatalf("[%d] unexpected error value: %v", i, err)
    72  		}
    73  
    74  		if !reflect.DeepEqual(buf.Bytes(), c.want) {
    75  			t.Errorf("[%d] not equal:\nwant %q,\n got %q", i, c.want, buf.Bytes())
    76  		}
    77  	}
    78  }