forked from rclone/rclone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tristate_test.go
87 lines (80 loc) · 2.09 KB
/
tristate_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
package fs
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Check it satisfies the interface
var _ flagger = (*Tristate)(nil)
func TestTristateString(t *testing.T) {
for _, test := range []struct {
in Tristate
want string
}{
{Tristate{}, "unset"},
{Tristate{Valid: false, Value: false}, "unset"},
{Tristate{Valid: false, Value: true}, "unset"},
{Tristate{Valid: true, Value: false}, "false"},
{Tristate{Valid: true, Value: true}, "true"},
} {
got := test.in.String()
assert.Equal(t, test.want, got)
}
}
func TestTristateSet(t *testing.T) {
for _, test := range []struct {
in string
want Tristate
err bool
}{
{"", Tristate{Valid: false, Value: false}, false},
{"nil", Tristate{Valid: false, Value: false}, false},
{"null", Tristate{Valid: false, Value: false}, false},
{"UNSET", Tristate{Valid: false, Value: false}, false},
{"true", Tristate{Valid: true, Value: true}, false},
{"1", Tristate{Valid: true, Value: true}, false},
{"false", Tristate{Valid: true, Value: false}, false},
{"0", Tristate{Valid: true, Value: false}, false},
{"potato", Tristate{Valid: false, Value: false}, true},
} {
var got Tristate
err := got.Set(test.in)
if test.err {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, test.want, got)
}
}
}
func TestTristateScan(t *testing.T) {
var v Tristate
n, err := fmt.Sscan(" true ", &v)
require.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, Tristate{Valid: true, Value: true}, v)
}
func TestTristateUnmarshalJSON(t *testing.T) {
for _, test := range []struct {
in string
want Tristate
err bool
}{
{`null`, Tristate{}, false},
{`true`, Tristate{Valid: true, Value: true}, false},
{`false`, Tristate{Valid: true, Value: false}, false},
{`potato`, Tristate{}, true},
{``, Tristate{}, true},
} {
var got Tristate
err := json.Unmarshal([]byte(test.in), &got)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.Equal(t, test.want, got, test.in)
}
}