-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile_test.go
116 lines (84 loc) · 2.43 KB
/
file_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
package configr
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func resetGlobals() func() {
RegisteredFileEncoders = make(map[string]Encoder)
RegisteredFileDecoders = make(map[string]FileDecoder)
ExtensionToDecoderName = make(map[string]string)
ExtensionToEncoderName = make(map[string]string)
return func() {
resetGlobals()
}
}
func Test_ItRegistersNameAsAFileDecoderExtension(t *testing.T) {
defer resetGlobals()()
name := "json"
source := FileDecoderAdapter(nil)
expectedExtensions := map[string]string{
name: name,
}
RegisterFileDecoder(name, source)
assert.Equal(t, expectedExtensions, ExtensionToDecoderName)
}
func Test_ItRegistersAllFileExtensionsForDecoder(t *testing.T) {
defer resetGlobals()()
name := "json"
source := FileDecoderAdapter(nil)
expectedExtensions := map[string]string{
"js": name,
"JSON": name,
name: name,
}
RegisterFileDecoder(name, source, "js", "JSON", "json")
assert.Equal(t, expectedExtensions, ExtensionToDecoderName)
}
func Test_ItInfersEncodingNameByFileExtension(t *testing.T) {
defer resetGlobals()()
ExtensionToDecoderName["js"] = "json"
f := NewFile("/tmp/config.js")
assert.Equal(t, "json", f.encodingName)
}
func Test_ItErrorsIfItCantFindFileEncoding(t *testing.T) {
defer resetGlobals()()
f := NewFile("/tmp/config.js")
_, err := f.Unmarshal([]string{}, nil)
assert.EqualError(t, err, ErrUnknownEncoding.Error())
}
func Test_ItRegistersNameAsAnEncoderExtension(t *testing.T) {
defer resetGlobals()()
name := "json"
source := EncoderAdapter(nil)
expectedExtensions := map[string]string{
name: name,
}
RegisterFileEncoder(name, source)
assert.Equal(t, expectedExtensions, ExtensionToEncoderName)
}
func Test_ItRegistersAllFileExtensionsForEncoder(t *testing.T) {
defer resetGlobals()()
name := "json"
source := EncoderAdapter(nil)
expectedExtensions := map[string]string{
"js": name,
"JSON": name,
name: name,
}
RegisterFileEncoder(name, source, "js", "JSON", "json")
assert.Equal(t, expectedExtensions, ExtensionToEncoderName)
}
func Test_MarshalingErrorsIfItCantFindFileEncoding(t *testing.T) {
defer resetGlobals()()
f := NewFile("/tmp/config.js")
_, err := f.Marshal(nil)
assert.EqualError(t, err, ErrUnknownEncoding.Error())
}
type MockFileSourcer struct {
mock.Mock
}
func (m MockFileSourcer) Unmarshal(b []byte, v interface{}) error {
args := m.Called(b, v)
return args.Error(0)
}