2019-01-02 05:58:32 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2018-12-21 10:21:13 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/hugio"
|
2020-06-30 10:11:05 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
2018-12-21 10:21:13 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/media"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-12-21 10:21:13 -05:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testJSON = `
|
|
|
|
|
|
|
|
{
|
|
|
|
"ROOT_KEY": {
|
|
|
|
"title": "example glossary",
|
|
|
|
"GlossDiv": {
|
|
|
|
"title": "S",
|
|
|
|
"GlossList": {
|
|
|
|
"GlossEntry": {
|
|
|
|
"ID": "SGML",
|
|
|
|
"SortAs": "SGML",
|
|
|
|
"GlossTerm": "Standard Generalized Markup Language",
|
|
|
|
"Acronym": "SGML",
|
|
|
|
"Abbrev": "ISO 8879:1986",
|
|
|
|
"GlossDef": {
|
|
|
|
"para": "A meta-markup language, used to create markup languages such as DocBook.",
|
|
|
|
"GlossSeeAlso": ["GML", "XML"]
|
|
|
|
},
|
|
|
|
"GlossSee": "markup"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ resource.ReadSeekCloserResource = (*testContentResource)(nil)
|
|
|
|
|
|
|
|
type testContentResource struct {
|
|
|
|
content string
|
|
|
|
mime media.Type
|
|
|
|
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testContentResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) {
|
|
|
|
return hugio.NewReadSeekerNoOpCloserFromString(t.content), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testContentResource) MediaType() media.Type {
|
|
|
|
return t.mime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testContentResource) Key() string {
|
|
|
|
return t.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
|
|
|
|
v := viper.New()
|
|
|
|
ns := New(newDeps(v))
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-12-21 10:21:13 -05:00
|
|
|
|
|
|
|
assertSlogan := func(m map[string]interface{}) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(m["slogan"], qt.Equals, "Hugo Rocks!")
|
2018-12-21 10:21:13 -05:00
|
|
|
}
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
for _, test := range []struct {
|
2018-12-23 04:40:32 -05:00
|
|
|
data interface{}
|
|
|
|
options interface{}
|
|
|
|
expect interface{}
|
2018-12-21 10:21:13 -05:00
|
|
|
}{
|
2018-12-23 04:40:32 -05:00
|
|
|
{`{ "slogan": "Hugo Rocks!" }`, nil, func(m map[string]interface{}) {
|
2018-12-21 10:21:13 -05:00
|
|
|
assertSlogan(m)
|
|
|
|
}},
|
2018-12-23 04:40:32 -05:00
|
|
|
{`slogan: "Hugo Rocks!"`, nil, func(m map[string]interface{}) {
|
2018-12-21 10:21:13 -05:00
|
|
|
assertSlogan(m)
|
|
|
|
}},
|
2018-12-23 04:40:32 -05:00
|
|
|
{`slogan = "Hugo Rocks!"`, nil, func(m map[string]interface{}) {
|
2018-12-21 10:21:13 -05:00
|
|
|
assertSlogan(m)
|
|
|
|
}},
|
2018-12-23 10:43:04 -05:00
|
|
|
{testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]interface{}) {
|
2018-12-21 10:21:13 -05:00
|
|
|
assertSlogan(m)
|
|
|
|
}},
|
2018-12-23 10:43:04 -05:00
|
|
|
{testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]interface{}) {
|
2018-12-21 10:21:13 -05:00
|
|
|
assertSlogan(m)
|
|
|
|
}},
|
2018-12-23 10:43:04 -05:00
|
|
|
{testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]interface{}) {
|
2018-12-21 10:21:13 -05:00
|
|
|
assertSlogan(m)
|
|
|
|
}},
|
2018-12-23 10:43:04 -05:00
|
|
|
{testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00
|
2018-12-23 04:40:32 -05:00
|
|
|
1999,Chevy,"Venture ""Extended Edition""","",4900.00`, mime: media.CSVType}, nil, func(r [][]string) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(r), qt.Equals, 2)
|
2018-12-23 04:40:32 -05:00
|
|
|
first := r[0]
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(first), qt.Equals, 5)
|
|
|
|
c.Assert(first[1], qt.Equals, "Ford")
|
2018-12-23 04:40:32 -05:00
|
|
|
}},
|
2018-12-23 15:08:12 -05:00
|
|
|
{testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
2018-12-23 04:40:32 -05:00
|
|
|
|
|
|
|
}},
|
|
|
|
{"a,b,c", nil, func(r [][]string) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
2018-12-23 04:40:32 -05:00
|
|
|
|
|
|
|
}},
|
2018-12-23 15:08:12 -05:00
|
|
|
{"a;b;c", map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
2018-12-23 04:40:32 -05:00
|
|
|
|
|
|
|
}},
|
2018-12-23 10:43:04 -05:00
|
|
|
{testContentResource{key: "r1", content: `
|
2018-12-23 04:40:32 -05:00
|
|
|
% This is a comment
|
2018-12-23 15:08:12 -05:00
|
|
|
a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment": "%"}, func(r [][]string) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
2018-12-23 04:40:32 -05:00
|
|
|
|
|
|
|
}},
|
2018-12-21 10:21:13 -05:00
|
|
|
// errors
|
2018-12-23 04:40:32 -05:00
|
|
|
{"thisisnotavaliddataformat", nil, false},
|
2018-12-23 10:43:04 -05:00
|
|
|
{testContentResource{key: "r1", content: `invalid&toml"`, mime: media.TOMLType}, nil, false},
|
|
|
|
{testContentResource{key: "r1", content: `unsupported: MIME"`, mime: media.CalendarType}, nil, false},
|
2018-12-23 04:40:32 -05:00
|
|
|
{"thisisnotavaliddataformat", nil, false},
|
|
|
|
{`{ notjson }`, nil, false},
|
|
|
|
{tstNoStringer{}, nil, false},
|
2018-12-21 10:21:13 -05:00
|
|
|
} {
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
ns.cache.Clear()
|
|
|
|
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
if test.options != nil {
|
|
|
|
args = []interface{}{test.options, test.data}
|
|
|
|
} else {
|
|
|
|
args = []interface{}{test.data}
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := ns.Unmarshal(args...)
|
2018-12-21 10:21:13 -05:00
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2018-12-21 10:21:13 -05:00
|
|
|
} else if fn, ok := test.expect.(func(m map[string]interface{})); ok {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-12-21 10:21:13 -05:00
|
|
|
m, ok := result.(map[string]interface{})
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(ok, qt.Equals, true)
|
2018-12-21 10:21:13 -05:00
|
|
|
fn(m)
|
2018-12-23 04:40:32 -05:00
|
|
|
} else if fn, ok := test.expect.(func(r [][]string)); ok {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-12-23 04:40:32 -05:00
|
|
|
r, ok := result.([][]string)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(ok, qt.Equals, true)
|
2018-12-23 04:40:32 -05:00
|
|
|
fn(r)
|
2018-12-21 10:21:13 -05:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.Equals, test.expect)
|
2018-12-21 10:21:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUnmarshalString(b *testing.B) {
|
|
|
|
v := viper.New()
|
|
|
|
ns := New(newDeps(v))
|
|
|
|
|
|
|
|
const numJsons = 100
|
|
|
|
|
|
|
|
var jsons [numJsons]string
|
|
|
|
for i := 0; i < numJsons; i++ {
|
|
|
|
jsons[i] = strings.Replace(testJSON, "ROOT_KEY", fmt.Sprintf("root%d", i), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
result, err := ns.Unmarshal(jsons[rand.Intn(numJsons)])
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
b.Fatal("no result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUnmarshalResource(b *testing.B) {
|
|
|
|
v := viper.New()
|
|
|
|
ns := New(newDeps(v))
|
|
|
|
|
|
|
|
const numJsons = 100
|
|
|
|
|
|
|
|
var jsons [numJsons]testContentResource
|
|
|
|
for i := 0; i < numJsons; i++ {
|
|
|
|
key := fmt.Sprintf("root%d", i)
|
|
|
|
jsons[i] = testContentResource{key: key, content: strings.Replace(testJSON, "ROOT_KEY", key, 1), mime: media.JSONType}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
result, err := ns.Unmarshal(jsons[rand.Intn(numJsons)])
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
b.Fatal("no result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|