github.com/dirkolbrich/hugo@v0.47.1/tpl/transform/remarshal_test.go (about)

     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
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/helpers"
    21  	"github.com/spf13/viper"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestRemarshal(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	v := viper.New()
    29  	v.Set("contentDir", "content")
    30  	ns := New(newDeps(v))
    31  	assert := require.New(t)
    32  
    33  	tomlExample := `title = "Test Metadata"
    34  		
    35  [[resources]]
    36    src = "**image-4.png"
    37    title = "The Fourth Image!"
    38    [resources.params]
    39      byline = "picasso"
    40  
    41  [[resources]]
    42    name = "my-cool-image-:counter"
    43    src = "**.png"
    44    title = "TOML: The Image #:counter"
    45    [resources.params]
    46      byline = "bep"
    47  `
    48  
    49  	yamlExample := `resources:
    50  - params:
    51      byline: picasso
    52    src: '**image-4.png'
    53    title: The Fourth Image!
    54  - name: my-cool-image-:counter
    55    params:
    56      byline: bep
    57    src: '**.png'
    58    title: 'TOML: The Image #:counter'
    59  title: Test Metadata
    60  `
    61  
    62  	jsonExample := `{
    63     "resources": [
    64        {
    65           "params": {
    66              "byline": "picasso"
    67           },
    68           "src": "**image-4.png",
    69           "title": "The Fourth Image!"
    70        },
    71        {
    72           "name": "my-cool-image-:counter",
    73           "params": {
    74              "byline": "bep"
    75           },
    76           "src": "**.png",
    77           "title": "TOML: The Image #:counter"
    78        }
    79     ],
    80     "title": "Test Metadata"
    81  }
    82  `
    83  
    84  	variants := []struct {
    85  		format string
    86  		data   string
    87  	}{
    88  		{"yaml", yamlExample},
    89  		{"json", jsonExample},
    90  		{"toml", tomlExample},
    91  		{"TOML", tomlExample},
    92  		{"Toml", tomlExample},
    93  		{" TOML ", tomlExample},
    94  	}
    95  
    96  	for _, v1 := range variants {
    97  		for _, v2 := range variants {
    98  			// Both from and to may be the same here, but that is fine.
    99  			fromTo := fmt.Sprintf("%s => %s", v2.format, v1.format)
   100  
   101  			converted, err := ns.Remarshal(v1.format, v2.data)
   102  			assert.NoError(err, fromTo)
   103  			diff := helpers.DiffStrings(v1.data, converted)
   104  			if len(diff) > 0 {
   105  				t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff)
   106  			}
   107  
   108  		}
   109  	}
   110  
   111  }
   112  
   113  func TestRemarshalComments(t *testing.T) {
   114  	t.Parallel()
   115  
   116  	v := viper.New()
   117  	v.Set("contentDir", "content")
   118  	ns := New(newDeps(v))
   119  
   120  	assert := require.New(t)
   121  
   122  	input := `
   123  Hugo = "Rules"
   124  		
   125  # It really does!
   126  
   127  [m]
   128  # A comment
   129  a = "b"
   130  
   131  `
   132  
   133  	expected := `
   134  Hugo = "Rules"
   135  		
   136  [m]
   137    a = "b"
   138  `
   139  
   140  	for _, format := range []string{"json", "yaml", "toml"} {
   141  		fromTo := fmt.Sprintf("%s => %s", "toml", format)
   142  
   143  		converted := input
   144  		var err error
   145  		// Do a round-trip conversion
   146  		for _, toFormat := range []string{format, "toml"} {
   147  			converted, err = ns.Remarshal(toFormat, converted)
   148  			assert.NoError(err, fromTo)
   149  		}
   150  
   151  		diff := helpers.DiffStrings(expected, converted)
   152  		if len(diff) > 0 {
   153  			t.Fatalf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v\n", fromTo, expected, converted, diff)
   154  		}
   155  	}
   156  }
   157  
   158  func TestTestRemarshalError(t *testing.T) {
   159  	t.Parallel()
   160  
   161  	v := viper.New()
   162  	v.Set("contentDir", "content")
   163  	ns := New(newDeps(v))
   164  	assert := require.New(t)
   165  
   166  	_, err := ns.Remarshal("asdf", "asdf")
   167  	assert.Error(err)
   168  
   169  	_, err = ns.Remarshal("json", "asdf")
   170  	assert.Error(err)
   171  
   172  }
   173  
   174  func TestRemarshalDetectFormat(t *testing.T) {
   175  	t.Parallel()
   176  	assert := require.New(t)
   177  
   178  	for i, test := range []struct {
   179  		data   string
   180  		expect interface{}
   181  	}{
   182  		{`foo = "bar"`, "toml"},
   183  		{`   foo = "bar"`, "toml"},
   184  		{`foo="bar"`, "toml"},
   185  		{`foo: "bar"`, "yaml"},
   186  		{`foo:"bar"`, "yaml"},
   187  		{`{ "foo": "bar"`, "json"},
   188  		{`asdfasdf`, false},
   189  		{``, false},
   190  	} {
   191  		errMsg := fmt.Sprintf("[%d] %s", i, test.data)
   192  
   193  		result, err := detectFormat(test.data)
   194  
   195  		if b, ok := test.expect.(bool); ok && !b {
   196  			assert.Error(err, errMsg)
   197  			continue
   198  		}
   199  
   200  		assert.NoError(err, errMsg)
   201  		assert.Equal(test.expect, result, errMsg)
   202  	}
   203  }