Debian Codegraph @

parsers/toml/toml_test.go

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