...
1 package toml
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/require"
8 )
9
10
11 const original = `# key-value pair
12 key = "value"
13 list = ["item1", "item2", "item3"]
14
15 [map]
16 key = "value"
17
18 # nested
19 # map
20 [nested_map]
21 [nested_map.map]
22 key = "value"
23 list = [
24 "item1",
25 "item2",
26 "item3",
27 ]
28 `
29
30
31 const encoded = `key = 'value'
32 list = ['item1', 'item2', 'item3']
33
34 [map]
35 key = 'value'
36
37 [nested_map]
38 [nested_map.map]
39 key = 'value'
40 list = ['item1', 'item2', 'item3']
41 `
42
43
44 var data = map[string]any{
45 "key": "value",
46 "list": []any{
47 "item1",
48 "item2",
49 "item3",
50 },
51 "map": map[string]any{
52 "key": "value",
53 },
54 "nested_map": map[string]any{
55 "map": map[string]any{
56 "key": "value",
57 "list": []any{
58 "item1",
59 "item2",
60 "item3",
61 },
62 },
63 },
64 }
65
66 func TestCodec_Encode(t *testing.T) {
67 codec := Codec{}
68
69 b, err := codec.Encode(data)
70 require.NoError(t, err)
71
72 assert.Equal(t, encoded, string(b))
73 }
74
75 func TestCodec_Decode(t *testing.T) {
76 t.Run("OK", func(t *testing.T) {
77 codec := Codec{}
78
79 v := map[string]any{}
80
81 err := codec.Decode([]byte(original), v)
82 require.NoError(t, err)
83
84 assert.Equal(t, data, v)
85 })
86
87 t.Run("InvalidData", func(t *testing.T) {
88 codec := Codec{}
89
90 v := map[string]any{}
91
92 err := codec.Decode([]byte(`invalid data`), v)
93 require.Error(t, err)
94
95 t.Logf("decoding failed as expected: %s", err)
96 })
97 }
98
View as plain text