-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdecoder_test.go
120 lines (113 loc) · 2.87 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
package plist
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestToJSON(t *testing.T) {
t.Parallel()
/*
testdata layout:
- *.plist - Opened as a reader for ToJSON.
- *.json - The expected output. If not exists, check ToJSON's error.
- *.error - The expected error.
*/
testFiles, err := filepath.Glob(filepath.Join("testdata", "*.plist"))
require.NoError(t, err)
require.NotEmpty(t, testFiles, "Ensure glob matches something.")
for _, testFilePath := range testFiles {
testFilePath := testFilePath // enable parallel sub-tests
baseName := filepath.Base(testFilePath)
t.Run(baseName, func(t *testing.T) {
t.Parallel()
rootName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
expectedFilePath := filepath.Join("testdata", rootName+".json")
expectedErrFilePath := filepath.Join("testdata", rootName+".error")
testFile, err := os.Open(testFilePath)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, testFile.Close())
})
expectErr := ""
expected, err := os.ReadFile(expectedFilePath)
if errors.Is(err, os.ErrNotExist) {
var errBuf []byte
errBuf, err = os.ReadFile(expectedErrFilePath)
expectErr = strings.TrimSpace(string(errBuf))
}
assert.NoError(t, err)
buf, err := ToJSON(testFile)
if expectErr != "" {
assert.Empty(t, string(buf))
assert.EqualError(t, err, expectErr)
return
}
if assert.NoError(t, err) {
assert.JSONEq(t, string(expected), string(buf))
}
})
}
}
func TestToGo(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
fileName string
expected interface{}
}{
{
fileName: "time-machine-backup-compare.plist",
expected: map[string]interface{}{
"Changes": []interface{}{
map[string]interface{}{
"AddedItem": map[string]interface{}{
"Path": "/some/file/path",
"Size": int64(153),
},
},
map[string]interface{}{
"RemovedItem": map[string]interface{}{
"Path": "/some/other/file/path",
"Size": int64(854071),
},
},
},
"Totals": map[string]interface{}{
"AddedSize": int64(500382415),
"ChangedSize": int64(0),
"RemovedSize": int64(492709651),
},
},
},
{
fileName: "all-datatypes.plist",
expected: []interface{}{
true,
false,
int64(123),
123e10,
"some string",
[]byte("base64 data"),
time.Date(2020, time.January, 1, 1, 0, 0, 0, time.UTC),
},
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.fileName, func(t *testing.T) {
t.Parallel()
f, err := os.Open(filepath.Join("testdata", tc.fileName))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, f.Close())
})
value, err := newDecoder(f).toGo()
if assert.NoError(t, err) {
assert.Equal(t, tc.expected, value)
}
})
}
}