diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-02-12 17:39:11 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-02-12 19:14:02 +0100 |
commit | 10a917dfdce8851666c5b89ebc02af6f6c84ab59 (patch) | |
tree | 6c89d369f612f12b5e6156d909ff392152f62680 /parser/frontmatter_test.go | |
parent | d4beef0d2bb8f6481fa80e1d938454a7d4e38814 (diff) | |
download | hugo-10a917dfdce8851666c5b89ebc02af6f6c84ab59.tar.gz hugo-10a917dfdce8851666c5b89ebc02af6f6c84ab59.zip |
parser: Tune stringifyMapKeys
```bash
benchmark old ns/op new ns/op delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4 3269 3053 -6.61%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4 4.79 5.23 +9.19%
BenchmarkStringifyMapKeysIntegers-4 2707 2320 -14.30%
benchmark old allocs new allocs delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4 16 6 -62.50%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4 0 0 +0.00%
BenchmarkStringifyMapKeysIntegers-4 16 6 -62.50%
benchmark old bytes new bytes delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4 1080 1008 -6.67%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4 0 0 +0.00%
BenchmarkStringifyMapKeysIntegers-4 1080 1008 -6.67%
```
Diffstat (limited to 'parser/frontmatter_test.go')
-rw-r--r-- | parser/frontmatter_test.go | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/parser/frontmatter_test.go b/parser/frontmatter_test.go index 4d28facb2..7281ca336 100644 --- a/parser/frontmatter_test.go +++ b/parser/frontmatter_test.go @@ -323,37 +323,56 @@ func TestRemoveTOMLIdentifier(t *testing.T) { func TestStringifyYAMLMapKeys(t *testing.T) { cases := []struct { - input interface{} - want map[string]interface{} + 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 := stringifyMapKeys(c.input) + 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) } @@ -407,7 +426,7 @@ func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - stringifyYAMLMapKeys(m) + stringifyMapKeys(m) } } |