2018-10-20 05:16:18 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 metadecoders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-10-20 05:16:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalToMap(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-10-20 05:16:18 -04:00
|
|
|
|
|
|
|
expect := map[string]interface{}{"a": "b"}
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
d := Default
|
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
for i, test := range []struct {
|
|
|
|
data string
|
|
|
|
format Format
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{`a = "b"`, TOML, expect},
|
|
|
|
{`a: "b"`, YAML, expect},
|
|
|
|
// Make sure we get all string keys, even for YAML
|
|
|
|
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}},
|
|
|
|
{"a:\n true: 1\n false: 2", YAML, map[string]interface{}{"a": map[string]interface{}{"true": 1, "false": 2}}},
|
|
|
|
{`{ "a": "b" }`, JSON, expect},
|
|
|
|
{`#+a: b`, ORG, expect},
|
|
|
|
// errors
|
|
|
|
{`a = b`, TOML, false},
|
2018-12-23 04:40:32 -05:00
|
|
|
{`a,b,c`, CSV, false}, // Use Unmarshal for CSV
|
2018-10-20 05:16:18 -04:00
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%d: %s", i, test.format)
|
2018-12-23 04:40:32 -05:00
|
|
|
m, err := d.UnmarshalToMap([]byte(test.data), test.format)
|
2018-10-20 05:16:18 -04:00
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(m, qt.DeepEquals, test.expect, msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalToInterface(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-10-20 05:16:18 -04:00
|
|
|
|
|
|
|
expect := map[string]interface{}{"a": "b"}
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
d := Default
|
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
for i, test := range []struct {
|
|
|
|
data string
|
|
|
|
format Format
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}},
|
|
|
|
{`{ "a": "b" }`, JSON, expect},
|
|
|
|
{`#+a: b`, ORG, expect},
|
|
|
|
{`a = "b"`, TOML, expect},
|
|
|
|
{`a: "b"`, YAML, expect},
|
2019-03-23 15:15:06 -04:00
|
|
|
{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},
|
2018-10-20 05:16:18 -04:00
|
|
|
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}},
|
|
|
|
// errors
|
|
|
|
{`a = "`, TOML, false},
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%d: %s", i, test.format)
|
2018-12-23 04:40:32 -05:00
|
|
|
m, err := d.Unmarshal([]byte(test.data), test.format)
|
2018-10-20 05:16:18 -04:00
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(m, qt.DeepEquals, test.expect, msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
func TestUnmarshalStringTo(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
|
|
|
d := Default
|
|
|
|
|
|
|
|
expectMap := map[string]interface{}{"a": "b"}
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
data string
|
|
|
|
to interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{"a string", "string", "a string"},
|
|
|
|
{`{ "a": "b" }`, make(map[string]interface{}), expectMap},
|
|
|
|
{"32", int64(1234), int64(32)},
|
|
|
|
{"32", int(1234), int(32)},
|
|
|
|
{"3.14159", float64(1), float64(3.14159)},
|
|
|
|
{"[3,7,9]", []interface{}{}, []interface{}{3, 7, 9}},
|
|
|
|
{"[3.1,7.2,9.3]", []interface{}{}, []interface{}{3.1, 7.2, 9.3}},
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%d: %T", i, test.to)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
m, err := d.UnmarshalStringTo(test.data, test.to)
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(m, qt.DeepEquals, test.expect, msg)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
func TestStringifyYAMLMapKeys(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input interface{}
|
|
|
|
want interface{}
|
|
|
|
replaced bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{"a": 1, "b": 2},
|
|
|
|
map[string]interface{}{"a": 1, "b": 2},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{"a": []interface{}{1, map[interface{}]interface{}{"b": 2}}},
|
|
|
|
map[string]interface{}{"a": []interface{}{1, map[string]interface{}{"b": 2}}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{true: 1, "b": false},
|
|
|
|
map[string]interface{}{"true": 1, "b": false},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{1: "a", 2: "b"},
|
|
|
|
map[string]interface{}{"1": "a", "2": "b"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": 1}},
|
|
|
|
map[string]interface{}{"a": map[string]interface{}{"b": 1}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]interface{}{"a": map[string]interface{}{"b": 1}},
|
|
|
|
map[string]interface{}{"a": map[string]interface{}{"b": 1}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]interface{}{map[interface{}]interface{}{1: "a", 2: "b"}},
|
|
|
|
[]interface{}{map[string]interface{}{"1": "a", "2": "b"}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
res, replaced := stringifyMapKeys(c.input)
|
|
|
|
|
|
|
|
if c.replaced != replaced {
|
|
|
|
t.Fatalf("[%d] Replaced mismatch: %t", i, replaced)
|
|
|
|
}
|
|
|
|
if !c.replaced {
|
|
|
|
res = c.input
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res, c.want) {
|
|
|
|
t.Errorf("[%d] given %q\nwant: %q\n got: %q", i, c.input, c.want, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps(b *testing.B) {
|
|
|
|
maps := make([]map[interface{}]interface{}, b.N)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
maps[i] = map[interface{}]interface{}{
|
|
|
|
"a": map[interface{}]interface{}{
|
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
|
|
|
"d": map[interface{}]interface{}{
|
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"b": []interface{}{"a", "b"},
|
|
|
|
"c": "d",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stringifyMapKeys(maps[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"a": map[string]interface{}{
|
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
|
|
|
"d": map[string]interface{}{
|
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"b": []interface{}{"a", "b"},
|
|
|
|
"c": "d",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stringifyMapKeys(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStringifyMapKeysIntegers(b *testing.B) {
|
|
|
|
maps := make([]map[interface{}]interface{}, b.N)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
maps[i] = map[interface{}]interface{}{
|
|
|
|
1: map[interface{}]interface{}{
|
|
|
|
4: 32,
|
|
|
|
5: 43,
|
|
|
|
6: map[interface{}]interface{}{
|
|
|
|
7: 32,
|
|
|
|
8: 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
2: []interface{}{"a", "b"},
|
|
|
|
3: "d",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stringifyMapKeys(maps[i])
|
|
|
|
}
|
|
|
|
}
|