-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrawoption_test.go
97 lines (81 loc) · 2.68 KB
/
rawoption_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
package figtree
import (
"encoding/json"
"testing"
yaml "gopkg.in/yaml.v3"
"github.com/stretchr/testify/assert"
)
func TestOptionInterface(t *testing.T) {
f := func(_ option) bool {
return true
}
assert.True(t, f(&BoolOption{}))
assert.True(t, f(&ByteOption{}))
assert.True(t, f(&Complex128Option{}))
assert.True(t, f(&Complex64Option{}))
assert.True(t, f(&ErrorOption{}))
assert.True(t, f(&Float32Option{}))
assert.True(t, f(&Float64Option{}))
assert.True(t, f(&IntOption{}))
assert.True(t, f(&Int16Option{}))
assert.True(t, f(&Int32Option{}))
assert.True(t, f(&Int64Option{}))
assert.True(t, f(&Int8Option{}))
assert.True(t, f(&RuneOption{}))
assert.True(t, f(&StringOption{}))
assert.True(t, f(&UintOption{}))
assert.True(t, f(&Uint16Option{}))
assert.True(t, f(&Uint32Option{}))
assert.True(t, f(&Uint64Option{}))
assert.True(t, f(&Uint8Option{}))
assert.True(t, f(&UintptrOption{}))
}
func TestStringOptionYAML(t *testing.T) {
s := ""
err := yaml.Unmarshal([]byte(`""`), &s)
assert.NoError(t, err)
assert.Equal(t, s, "")
type testType struct {
String StringOption `yaml:"string,omitempty"`
}
tt := testType{}
err = yaml.Unmarshal([]byte(`string: ""`), &tt)
assert.NoError(t, err)
assert.Equal(t, StringOption{Source: tSrc("yaml", 1, 9), Value: "", Defined: true}, tt.String)
tt = testType{}
err = yaml.Unmarshal([]byte(`string: "value"`), &tt)
assert.NoError(t, err)
assert.Equal(t, StringOption{Source: tSrc("yaml", 1, 9), Value: "value", Defined: true}, tt.String)
}
func TestStringOptionJSON(t *testing.T) {
type testType struct {
String StringOption `json:"string,omitempty"`
}
tt := testType{}
err := json.Unmarshal([]byte(`{"string": ""}`), &tt)
assert.NoError(t, err)
assert.Equal(t, StringOption{Source: NewSource("json"), Value: "", Defined: true}, tt.String)
tt = testType{}
err = json.Unmarshal([]byte(`{"string": "value"}`), &tt)
assert.NoError(t, err)
assert.Equal(t, StringOption{Source: NewSource("json"), Value: "value", Defined: true}, tt.String)
}
func TestBoolOptionYAML(t *testing.T) {
type testType struct {
Bool BoolOption `yaml:"bool,omitempty"`
}
tt := testType{}
err := yaml.Unmarshal([]byte(`bool: true`), &tt)
assert.NoError(t, err)
assert.Equal(t, BoolOption{Source: tSrc("yaml", 1, 7), Value: true, Defined: true}, tt.Bool)
tt = testType{}
err = yaml.Unmarshal([]byte(`bool: false`), &tt)
assert.NoError(t, err)
assert.Equal(t, BoolOption{Source: tSrc("yaml", 1, 7), Value: false, Defined: true}, tt.Bool)
tt = testType{
Bool: NewBoolOption(true),
}
err = yaml.Unmarshal([]byte(`bool: false`), &tt)
assert.NoError(t, err)
assert.Equal(t, BoolOption{Source: tSrc("yaml", 1, 7), Value: false, Defined: true}, tt.Bool)
}