tpl/transform: Add transform.Unmarshal func

Fixes #5428
This commit is contained in:
Bjørn Erik Pedersen 2018-12-21 16:21:13 +01:00
parent 43f9df0194
commit 822dc627a1
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
20 changed files with 633 additions and 74 deletions

View file

@ -17,6 +17,8 @@ import (
"fmt"
"testing"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/stretchr/testify/require"
@ -41,6 +43,21 @@ func TestFormatFromString(t *testing.T) {
}
}
func TestFormatFromMediaType(t *testing.T) {
assert := require.New(t)
for i, test := range []struct {
m media.Type
expect Format
}{
{media.JSONType, JSON},
{media.YAMLType, YAML},
{media.TOMLType, TOML},
{media.CalendarType, ""},
} {
assert.Equal(test.expect, FormatFromMediaType(test.m), fmt.Sprintf("t%d", i))
}
}
func TestFormatFromFrontMatterType(t *testing.T) {
assert := require.New(t)
for i, test := range []struct {
@ -56,3 +73,28 @@ func TestFormatFromFrontMatterType(t *testing.T) {
assert.Equal(test.expect, FormatFromFrontMatterType(test.typ), fmt.Sprintf("t%d", i))
}
}
func TestFormatFromContentString(t *testing.T) {
t.Parallel()
assert := require.New(t)
for i, test := range []struct {
data string
expect interface{}
}{
{`foo = "bar"`, TOML},
{` foo = "bar"`, TOML},
{`foo="bar"`, TOML},
{`foo: "bar"`, YAML},
{`foo:"bar"`, YAML},
{`{ "foo": "bar"`, JSON},
{`asdfasdf`, Format("")},
{``, Format("")},
} {
errMsg := fmt.Sprintf("[%d] %s", i, test.data)
result := FormatFromContentString(test.data)
assert.Equal(test.expect, result, errMsg)
}
}