-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
156 lines (144 loc) · 3.7 KB
/
decoder_test.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package properties
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/goschtalt/goschtalt/pkg/decoder"
"github.com/goschtalt/goschtalt/pkg/meta"
"github.com/stretchr/testify/assert"
)
func TestExtensions(t *testing.T) {
assert := assert.New(t)
want := []string{"properties"}
got := Decoder{}.Extensions()
assert.Empty(cmp.Diff(want, got))
}
func TestDecode(t *testing.T) {
unknown := errors.New("unknown")
tests := []struct {
description string
in string
expected meta.Object
expectedErr error
}{
{
description: "A test of empty.",
expected: meta.Object{},
}, {
description: "A test of invalid values.",
in: `=:`,
expectedErr: unknown,
}, {
description: "A small test.",
in: `# A small test
Foo.bar.0 = hello
Foo.bar.1 = cat
Foo.bar.2 = bat
Foo.ba = sheep
Foo.d = milk
Foo.l = jestor`,
expected: meta.Object{
Origins: []meta.Origin{{File: "file.properties", Line: 1, Col: 1}},
Map: map[string]meta.Object{
"Foo": {
Origins: []meta.Origin{{File: "file.properties", Line: 2, Col: 1}},
Map: map[string]meta.Object{
"ba": {
Origins: []meta.Origin{{File: "file.properties", Line: 5, Col: 1}},
Value: "sheep",
},
"bar": {
Origins: []meta.Origin{{File: "file.properties", Line: 2, Col: 1}},
Array: []meta.Object{
{
Origins: []meta.Origin{{File: "file.properties", Line: 2, Col: 1}},
Value: "hello",
},
{
Origins: []meta.Origin{{File: "file.properties", Line: 3, Col: 1}},
Value: "cat",
},
{
Origins: []meta.Origin{{File: "file.properties", Line: 4, Col: 1}},
Value: "bat",
},
},
},
"d": {
Origins: []meta.Origin{{File: "file.properties", Line: 6, Col: 1}},
Value: "milk",
},
"l": {
Origins: []meta.Origin{{File: "file.properties", Line: 7, Col: 1}},
Value: "jestor",
},
},
},
},
},
}, {
description: "A test of types.",
in: `# A test of types.
a = 250
b = 0xff
c = 077
d = 13.2
e = true
f = false`,
expected: meta.Object{
Origins: []meta.Origin{{File: "file.properties", Line: 1, Col: 1}},
Map: map[string]meta.Object{
"a": {
Origins: []meta.Origin{{File: "file.properties", Line: 2, Col: 1}},
Value: int64(250),
},
"b": {
Origins: []meta.Origin{{File: "file.properties", Line: 3, Col: 1}},
Value: int64(255),
},
"c": {
Origins: []meta.Origin{{File: "file.properties", Line: 4, Col: 1}},
Value: int64(63),
},
"d": {
Origins: []meta.Origin{{File: "file.properties", Line: 5, Col: 1}},
Value: float64(13.2),
},
"e": {
Origins: []meta.Origin{{File: "file.properties", Line: 6, Col: 1}},
Value: true,
},
"f": {
Origins: []meta.Origin{{File: "file.properties", Line: 7, Col: 1}},
Value: false,
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
var d Decoder
var got meta.Object
ctx := decoder.Context{
Filename: "file.properties",
Delimiter: ".",
}
err := d.Decode(ctx, []byte(tc.in), &got)
if tc.expectedErr == nil {
assert.NoError(err)
assert.Empty(cmp.Diff(tc.expected, got, cmpopts.IgnoreUnexported(meta.Object{})))
return
}
if errors.Is(unknown, tc.expectedErr) {
assert.Error(err)
return
}
assert.ErrorIs(err, tc.expectedErr)
})
}
}