github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/parser/metadecoders/decoder_test.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package metadecoders
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  )
    22  
    23  func TestUnmarshalToMap(t *testing.T) {
    24  	c := qt.New(t)
    25  
    26  	expect := map[string]interface{}{"a": "b"}
    27  
    28  	d := Default
    29  
    30  	for i, test := range []struct {
    31  		data   string
    32  		format Format
    33  		expect interface{}
    34  	}{
    35  		{`a = "b"`, TOML, expect},
    36  		{`a: "b"`, YAML, expect},
    37  		// Make sure we get all string keys, even for YAML
    38  		{"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}}}},
    39  		{"a:\n  true: 1\n  false: 2", YAML, map[string]interface{}{"a": map[string]interface{}{"true": 1, "false": 2}}},
    40  		{`{ "a": "b" }`, JSON, expect},
    41  		{`#+a: b`, ORG, expect},
    42  		// errors
    43  		{`a = b`, TOML, false},
    44  		{`a,b,c`, CSV, false}, // Use Unmarshal for CSV
    45  	} {
    46  		msg := qt.Commentf("%d: %s", i, test.format)
    47  		m, err := d.UnmarshalToMap([]byte(test.data), test.format)
    48  		if b, ok := test.expect.(bool); ok && !b {
    49  			c.Assert(err, qt.Not(qt.IsNil), msg)
    50  		} else {
    51  			c.Assert(err, qt.IsNil, msg)
    52  			c.Assert(m, qt.DeepEquals, test.expect, msg)
    53  		}
    54  	}
    55  }
    56  
    57  func TestUnmarshalToInterface(t *testing.T) {
    58  	c := qt.New(t)
    59  
    60  	expect := map[string]interface{}{"a": "b"}
    61  
    62  	d := Default
    63  
    64  	for i, test := range []struct {
    65  		data   string
    66  		format Format
    67  		expect interface{}
    68  	}{
    69  		{`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}},
    70  		{`{ "a": "b" }`, JSON, expect},
    71  		{`#+a: b`, ORG, expect},
    72  		{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]interface{}{"date": "2020-06-26"}},
    73  		{`a = "b"`, TOML, expect},
    74  		{`a: "b"`, YAML, expect},
    75  		{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},
    76  		{"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}}}},
    77  		// errors
    78  		{`a = "`, TOML, false},
    79  	} {
    80  		msg := qt.Commentf("%d: %s", i, test.format)
    81  		m, err := d.Unmarshal([]byte(test.data), test.format)
    82  		if b, ok := test.expect.(bool); ok && !b {
    83  			c.Assert(err, qt.Not(qt.IsNil), msg)
    84  		} else {
    85  			c.Assert(err, qt.IsNil, msg)
    86  			c.Assert(m, qt.DeepEquals, test.expect, msg)
    87  		}
    88  
    89  	}
    90  }
    91  
    92  func TestUnmarshalStringTo(t *testing.T) {
    93  	c := qt.New(t)
    94  
    95  	d := Default
    96  
    97  	expectMap := map[string]interface{}{"a": "b"}
    98  
    99  	for i, test := range []struct {
   100  		data   string
   101  		to     interface{}
   102  		expect interface{}
   103  	}{
   104  		{"a string", "string", "a string"},
   105  		{`{ "a": "b" }`, make(map[string]interface{}), expectMap},
   106  		{"32", int64(1234), int64(32)},
   107  		{"32", int(1234), int(32)},
   108  		{"3.14159", float64(1), float64(3.14159)},
   109  		{"[3,7,9]", []interface{}{}, []interface{}{3, 7, 9}},
   110  		{"[3.1,7.2,9.3]", []interface{}{}, []interface{}{3.1, 7.2, 9.3}},
   111  	} {
   112  		msg := qt.Commentf("%d: %T", i, test.to)
   113  		m, err := d.UnmarshalStringTo(test.data, test.to)
   114  		if b, ok := test.expect.(bool); ok && !b {
   115  			c.Assert(err, qt.Not(qt.IsNil), msg)
   116  		} else {
   117  			c.Assert(err, qt.IsNil, msg)
   118  			c.Assert(m, qt.DeepEquals, test.expect, msg)
   119  		}
   120  
   121  	}
   122  }
   123  
   124  func TestStringifyYAMLMapKeys(t *testing.T) {
   125  	cases := []struct {
   126  		input    interface{}
   127  		want     interface{}
   128  		replaced bool
   129  	}{
   130  		{
   131  			map[interface{}]interface{}{"a": 1, "b": 2},
   132  			map[string]interface{}{"a": 1, "b": 2},
   133  			true,
   134  		},
   135  		{
   136  			map[interface{}]interface{}{"a": []interface{}{1, map[interface{}]interface{}{"b": 2}}},
   137  			map[string]interface{}{"a": []interface{}{1, map[string]interface{}{"b": 2}}},
   138  			true,
   139  		},
   140  		{
   141  			map[interface{}]interface{}{true: 1, "b": false},
   142  			map[string]interface{}{"true": 1, "b": false},
   143  			true,
   144  		},
   145  		{
   146  			map[interface{}]interface{}{1: "a", 2: "b"},
   147  			map[string]interface{}{"1": "a", "2": "b"},
   148  			true,
   149  		},
   150  		{
   151  			map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": 1}},
   152  			map[string]interface{}{"a": map[string]interface{}{"b": 1}},
   153  			true,
   154  		},
   155  		{
   156  			map[string]interface{}{"a": map[string]interface{}{"b": 1}},
   157  			map[string]interface{}{"a": map[string]interface{}{"b": 1}},
   158  			false,
   159  		},
   160  		{
   161  			[]interface{}{map[interface{}]interface{}{1: "a", 2: "b"}},
   162  			[]interface{}{map[string]interface{}{"1": "a", "2": "b"}},
   163  			false,
   164  		},
   165  	}
   166  
   167  	for i, c := range cases {
   168  		res, replaced := stringifyMapKeys(c.input)
   169  
   170  		if c.replaced != replaced {
   171  			t.Fatalf("[%d] Replaced mismatch: %t", i, replaced)
   172  		}
   173  		if !c.replaced {
   174  			res = c.input
   175  		}
   176  		if !reflect.DeepEqual(res, c.want) {
   177  			t.Errorf("[%d] given %q\nwant: %q\n got: %q", i, c.input, c.want, res)
   178  		}
   179  	}
   180  }
   181  
   182  func BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps(b *testing.B) {
   183  	maps := make([]map[interface{}]interface{}, b.N)
   184  	for i := 0; i < b.N; i++ {
   185  		maps[i] = map[interface{}]interface{}{
   186  			"a": map[interface{}]interface{}{
   187  				"b": 32,
   188  				"c": 43,
   189  				"d": map[interface{}]interface{}{
   190  					"b": 32,
   191  					"c": 43,
   192  				},
   193  			},
   194  			"b": []interface{}{"a", "b"},
   195  			"c": "d",
   196  		}
   197  	}
   198  	b.ResetTimer()
   199  	for i := 0; i < b.N; i++ {
   200  		stringifyMapKeys(maps[i])
   201  	}
   202  }
   203  
   204  func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) {
   205  	m := map[string]interface{}{
   206  		"a": map[string]interface{}{
   207  			"b": 32,
   208  			"c": 43,
   209  			"d": map[string]interface{}{
   210  				"b": 32,
   211  				"c": 43,
   212  			},
   213  		},
   214  		"b": []interface{}{"a", "b"},
   215  		"c": "d",
   216  	}
   217  
   218  	b.ResetTimer()
   219  	for i := 0; i < b.N; i++ {
   220  		stringifyMapKeys(m)
   221  	}
   222  }
   223  
   224  func BenchmarkStringifyMapKeysIntegers(b *testing.B) {
   225  	maps := make([]map[interface{}]interface{}, b.N)
   226  	for i := 0; i < b.N; i++ {
   227  		maps[i] = map[interface{}]interface{}{
   228  			1: map[interface{}]interface{}{
   229  				4: 32,
   230  				5: 43,
   231  				6: map[interface{}]interface{}{
   232  					7: 32,
   233  					8: 43,
   234  				},
   235  			},
   236  			2: []interface{}{"a", "b"},
   237  			3: "d",
   238  		}
   239  	}
   240  	b.ResetTimer()
   241  	for i := 0; i < b.N; i++ {
   242  		stringifyMapKeys(maps[i])
   243  	}
   244  }