Debian Codegraph @

parsers/huml/huml_test.go

raw
1 package huml
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestHUML_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 HUML",
18 input: []byte(``),
19 isErr: true, // HUML considers empty document as undefined
20 },
21 {
22 name: "Invalid HUML syntax",
23 input: []byte(`invalid: syntax: error`),
24 isErr: true,
25 },
26 {
27 name: "Simple key-value pairs",
28 input: []byte(`name: "test"
29 port: 8080
30 debug: true`),
31 output: map[string]any{
32 "name": "test",
33 "port": int64(8080),
34 "debug": true,
35 },
36 },
37 {
38 name: "Array values",
39 input: []byte(`tags:: "web", "api", "go"
40 ports:: 80, 443, 8080`),
41 output: map[string]any{
42 "tags": []any{"web", "api", "go"},
43 "ports": []any{int64(80), int64(443), int64(8080)},
44 },
45 },
46 {
47 name: "Nested objects",
48 input: []byte(`database::
49 host: "localhost"
50 port: 5432
51 ssl: false`),
52 output: map[string]any{
53 "database": map[string]any{
54 "host": "localhost",
55 "port": int64(5432),
56 "ssl": false,
57 },
58 },
59 },
60 {
61 name: "Complex nested structure",
62 input: []byte(`server::
63 http::
64 port: 8080
65 timeout: 30
66 https::
67 port: 8443
68 enabled: true`),
69 output: map[string]any{
70 "server": map[string]any{
71 "http": map[string]any{
72 "port": int64(8080),
73 "timeout": int64(30),
74 },
75 "https": map[string]any{
76 "port": int64(8443),
77 "enabled": true,
78 },
79 },
80 },
81 },
82 {
83 name: "Mixed types",
84 input: []byte(`string_val: "hello"
85 int_val: 42
86 float_val: 3.14
87 bool_val: true
88 array_val:: 1, 2, 3`),
89 output: map[string]any{
90 "string_val": "hello",
91 "int_val": int64(42),
92 "float_val": 3.14,
93 "bool_val": true,
94 "array_val": []any{int64(1), int64(2), int64(3)},
95 },
96 },
97 }
98
99 hp := Parser()
100
101 for _, tc := range testCases {
102 t.Run(tc.name, func(t *testing.T) {
103 out, err := hp.Unmarshal(tc.input)
104 if tc.isErr {
105 assert.NotNil(t, err)
106 } else {
107 assert.Nil(t, err)
108 assert.Equal(t, tc.output, out)
109 }
110 })
111 }
112 }
113
114 func TestHUML_Marshal(t *testing.T) {
115 testCases := []struct {
116 name string
117 input map[string]any
118 isErr bool
119 }{
120 {
121 name: "Empty map",
122 input: map[string]any{},
123 },
124 {
125 name: "Simple values",
126 input: map[string]any{
127 "name": "test",
128 "port": int64(8080),
129 "debug": true,
130 },
131 },
132 {
133 name: "Complex nested structure",
134 input: map[string]any{
135 "server": map[string]any{
136 "http": map[string]any{
137 "port": int64(8080),
138 "timeout": int64(30),
139 },
140 "https": map[string]any{
141 "port": int64(8443),
142 "enabled": true,
143 },
144 },
145 "tags": []any{"web", "api", "go"},
146 },
147 },
148 }
149
150 hp := Parser()
151
152 for _, tc := range testCases {
153 t.Run(tc.name, func(t *testing.T) {
154 out, err := hp.Marshal(tc.input)
155 if tc.isErr {
156 assert.NotNil(t, err)
157 } else {
158 assert.Nil(t, err)
159 assert.NotNil(t, out)
160
161 // Test round-trip: marshal then unmarshal should give back the same data
162 roundTrip, err := hp.Unmarshal(out)
163 assert.Nil(t, err)
164 assert.Equal(t, tc.input, roundTrip)
165 }
166 })
167 }
168 }