-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
108 lines (97 loc) · 2.4 KB
/
Copy pathdecoder_test.go
File metadata and controls
108 lines (97 loc) · 2.4 KB
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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
// SPDX-License-Identifier: Apache-2.0
package json
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{"json"}
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: "Invalid json.",
in: `{ a b }`,
expectedErr: unknown,
}, {
description: "A small test.",
in: `{ "a": { "b": { "c": "123" } }, "d": { "e": [ "fog", "dog" ] } }`,
expected: meta.Object{
Origins: []meta.Origin{{File: "file.json"}},
Map: map[string]meta.Object{
"a": {
Origins: []meta.Origin{{File: "file.json"}},
Map: map[string]meta.Object{
"b": {
Origins: []meta.Origin{{File: "file.json"}},
Map: map[string]meta.Object{
"c": {
Origins: []meta.Origin{{File: "file.json"}},
Value: "123",
},
},
},
},
},
"d": {
Origins: []meta.Origin{{File: "file.json"}},
Map: map[string]meta.Object{
"e": {
Origins: []meta.Origin{{File: "file.json"}},
Array: []meta.Object{
{
Origins: []meta.Origin{{File: "file.json"}},
Value: "fog",
},
{
Origins: []meta.Origin{{File: "file.json"}},
Value: "dog",
},
},
},
},
},
},
},
},
}
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.json",
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{})))
}
if errors.Is(unknown, tc.expectedErr) {
assert.NotNil(err)
return
}
assert.ErrorIs(err, tc.expectedErr)
})
}
}