1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
package toml
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTOML_Unmarshal(t *testing.T) {
testCases := []struct {
name string
input []byte
output map[string]interface{}
isErr bool
}{
{
name: "Empty TOML",
input: []byte(``),
output: map[string]interface{}{},
},
{
name: "Valid TOML",
input: []byte(`key = "val"
name = "test"
number = 2
`),
output: map[string]interface{}{
"key": "val",
"name": "test",
"number": int64(2),
},
},
{
name: "Invalid TOML - missing end quotes",
input: []byte(`key = "val`),
isErr: true,
},
{
name: "Complex TOML - All types",
input: []byte(`array = [ 1, 2, 3 ]
boolean = true
color = "gold"
number = 123
string = "Hello World"
[object]
a = "b"
c = "d"`),
output: map[string]interface{}{
"array": []interface{}{int64(1), int64(2), int64(3)},
"boolean": true,
"color": "gold",
"number": int64(123),
"object": map[string]interface{}{"a": "b", "c": "d"},
"string": "Hello World",
},
},
{
name: "Invalid TOML - missing equal",
input: []byte(`key "val"`),
isErr: true,
},
}
tp := Parser()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := tp.Unmarshal(tc.input)
if tc.isErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tc.output, out)
}
})
}
}
func TestTOML_Marshal(t *testing.T) {
testCases := []struct {
name string
input map[string]interface{}
output []byte
isErr bool
}{
{
name: "Empty TOML",
input: map[string]interface{}{},
output: []byte(nil),
},
{
name: "Valid TOML",
input: map[string]interface{}{
"key": "val",
"name": "test",
"number": 2.0,
},
output: []byte(`key = "val"
name = "test"
number = 2.0
`),
},
{
name: "Complex TOML - All types",
input: map[string]interface{}{
"array": []interface{}{1, 2, 3, 4, 5},
"boolean": true,
"color": "gold",
"number": 123,
"object": map[string]interface{}{"a": "b", "c": "d"},
"string": "Hello World",
},
output: []byte(`array = [1, 2, 3, 4, 5]
boolean = true
color = "gold"
number = 123
string = "Hello World"
[object]
a = "b"
c = "d"
`),
},
}
tp := Parser()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := tp.Marshal(tc.input)
if tc.isErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tc.output, out)
}
})
}
}
|