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