@@ -16,9 +16,9 @@ package helper
16
16
17
17
import (
18
18
"encoding/json"
19
- "strconv"
20
19
"testing"
21
20
21
+ "github.com/mitchellh/mapstructure"
22
22
"github.com/stretchr/testify/require"
23
23
yaml "gopkg.in/yaml.v2"
24
24
)
@@ -48,17 +48,16 @@ var sharedTestCases = []testCase{
48
48
{`"1tib"` , 1024 * 1024 * 1024 * 1024 , false },
49
49
{`"1pB"` , 1000 * 1000 * 1000 * 1000 * 1000 , false },
50
50
{`"1pib"` , 1024 * 1024 * 1024 * 1024 * 1024 , false },
51
- {`1e3` , 1000 , false },
52
51
{`"3ii3"` , 0 , true },
53
52
{`3ii3` , 0 , true },
54
53
{`--ii3` , 0 , true },
55
54
{`{"test":"val"}` , 0 , true },
56
- {`1e3` , 1000 , false },
55
+ // {`1e3`, 1000, false}, not supported in mapstructure
57
56
}
58
57
59
58
func TestByteSizeUnmarshalJSON (t * testing.T ) {
60
- for i , tc := range sharedTestCases {
61
- t .Run (strconv . Itoa ( i ) , func (t * testing.T ) {
59
+ for _ , tc := range sharedTestCases {
60
+ t .Run ("json/" + tc . input , func (t * testing.T ) {
62
61
var bs ByteSize
63
62
err := json .Unmarshal ([]byte (tc .input ), & bs )
64
63
if tc .expectError {
@@ -90,16 +89,34 @@ func TestByteSizeUnmarshalYAML(t *testing.T) {
90
89
}
91
90
92
91
cases := append (sharedTestCases , additionalCases ... )
93
- for i , tc := range cases {
94
- t .Run (strconv .Itoa (i ), func (t * testing.T ) {
92
+
93
+ for _ , tc := range cases {
94
+ t .Run ("yaml/" + tc .input , func (t * testing.T ) {
95
95
var bs ByteSize
96
- err := yaml .Unmarshal ([]byte (tc .input ), & bs )
96
+ yamlErr := yaml .Unmarshal ([]byte (tc .input ), & bs )
97
97
if tc .expectError {
98
- require .Error (t , err )
98
+ require .Error (t , yamlErr )
99
99
return
100
100
}
101
+ require .NoError (t , yamlErr )
102
+ require .Equal (t , tc .expected , bs )
103
+ })
104
+ t .Run ("mapstructure/" + tc .input , func (t * testing.T ) {
105
+ var bs ByteSize
106
+ var raw string
107
+ _ = yaml .Unmarshal ([]byte (tc .input ), & raw )
108
+
109
+ dc := & mapstructure.DecoderConfig {Result : & bs , DecodeHook : JSONUnmarshalerHook ()}
110
+ ms , err := mapstructure .NewDecoder (dc )
111
+ require .NoError (t , err )
101
112
113
+ err = ms .Decode (raw )
114
+ if tc .expectError {
115
+ require .Error (t , err )
116
+ return
117
+ }
102
118
require .NoError (t , err )
119
+
103
120
require .Equal (t , tc .expected , bs )
104
121
})
105
122
}
0 commit comments