diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-20 11:16:18 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-22 20:46:13 +0200 |
commit | 129c27ee6e9fed98dbfebeaa272fd52757b475b2 (patch) | |
tree | ba931600714e354f0c7d05ad0a598f591b0258f6 /parser/frontmatter_test.go | |
parent | 44da60d869578423dea529db62ed613588a2a560 (diff) |
parser/metadecoders: Consolidate the metadata decoders
See #5324
Diffstat (limited to 'parser/frontmatter_test.go')
-rw-r--r-- | parser/frontmatter_test.go | 266 |
1 files changed, 0 insertions, 266 deletions
diff --git a/parser/frontmatter_test.go b/parser/frontmatter_test.go index 7281ca336..d6e6e79c3 100644 --- a/parser/frontmatter_test.go +++ b/parser/frontmatter_test.go @@ -132,116 +132,6 @@ func TestInterfaceToFrontMatter(t *testing.T) { } } -func TestHandleTOMLMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("title = \"test 1\""), map[string]interface{}{"title": "test 1"}, false}, - {[]byte("a = [1, 2, 3]"), map[string]interface{}{"a": []interface{}{int64(1), int64(2), int64(3)}}, false}, - {[]byte("b = [\n[1, 2],\n[3, 4]\n]"), map[string]interface{}{"b": []interface{}{[]interface{}{int64(1), int64(2)}, []interface{}{int64(3), int64(4)}}}, false}, - // errors - {[]byte("z = [\n[1, 2]\n[3, 4]\n]"), nil, true}, - } - - for i, c := range cases { - res, err := HandleTOMLMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - -func TestHandleYAMLMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("title: test 1"), map[string]interface{}{"title": "test 1"}, false}, - {[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}, false}, - {[]byte("a:\n true: 1\n false: 2"), map[string]interface{}{"a": map[string]interface{}{"true": 1, "false": 2}}, false}, - // errors - {[]byte("z = not toml"), nil, true}, - } - - for i, c := range cases { - res, err := HandleYAMLMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - -func TestHandleJSONMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("{\"title\": \"test 1\"}"), map[string]interface{}{"title": "test 1"}, false}, - // errors - {[]byte("{noquotes}"), nil, true}, - } - - for i, c := range cases { - res, err := HandleJSONMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - -func TestHandleOrgMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("#+title: test 1\n"), map[string]interface{}{"title": "test 1"}, false}, - } - - for i, c := range cases { - res, err := HandleOrgMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - func TestFormatToLeadRune(t *testing.T) { for i, this := range []struct { kind string @@ -264,41 +154,6 @@ func TestFormatToLeadRune(t *testing.T) { } } -func TestDetectFrontMatter(t *testing.T) { - cases := []struct { - mark rune - want *FrontmatterType - }{ - // funcs are uncomparable, so we ignore FrontmatterType.Parse in these tests - {'-', &FrontmatterType{nil, []byte(YAMLDelim), []byte(YAMLDelim), false}}, - {'+', &FrontmatterType{nil, []byte(TOMLDelim), []byte(TOMLDelim), false}}, - {'{', &FrontmatterType{nil, []byte("{"), []byte("}"), true}}, - {'#', &FrontmatterType{nil, []byte("#+"), []byte("\n"), false}}, - {'$', nil}, - } - - for _, c := range cases { - res := DetectFrontMatter(c.mark) - if res == nil { - if c.want == nil { - continue - } - - t.Fatalf("want %v, got %v", *c.want, res) - } - - if !reflect.DeepEqual(res.markstart, c.want.markstart) { - t.Errorf("markstart mismatch: want %v, got %v", c.want.markstart, res.markstart) - } - if !reflect.DeepEqual(res.markend, c.want.markend) { - t.Errorf("markend mismatch: want %v, got %v", c.want.markend, res.markend) - } - if !reflect.DeepEqual(res.includeMark, c.want.includeMark) { - t.Errorf("includeMark mismatch: want %v, got %v", c.want.includeMark, res.includeMark) - } - } -} - func TestRemoveTOMLIdentifier(t *testing.T) { cases := []struct { input string @@ -321,64 +176,6 @@ func TestRemoveTOMLIdentifier(t *testing.T) { } } -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 BenchmarkFrontmatterTags(b *testing.B) { for _, frontmatter := range []string{"JSON", "YAML", "YAML2", "TOML"} { @@ -388,69 +185,6 @@ func BenchmarkFrontmatterTags(b *testing.B) { } } -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]) - } -} func doBenchmarkFrontmatter(b *testing.B, fileformat string, numTags int) { yamlTemplate := `--- name: "Tags" |