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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package opt
import (
"encoding/json"
"flag"
"reflect"
"strings"
"testing"
)
func TestBool(t *testing.T) {
tests := []struct {
name string
in any
want string // JSON
wantBack any
}{
{
name: "null_for_unset",
in: struct {
True Bool
False Bool
Unset Bool
}{
True: "true",
False: "false",
},
want: `{"True":true,"False":false,"Unset":null}`,
wantBack: struct {
True Bool
False Bool
Unset Bool
}{
True: "true",
False: "false",
Unset: "unset",
},
},
{
name: "omitempty_unset",
in: struct {
True Bool
False Bool
Unset Bool `json:",omitempty"`
}{
True: "true",
False: "false",
},
want: `{"True":true,"False":false}`,
},
{
name: "unset_marshals_as_null",
in: struct {
True Bool
False Bool
Foo Bool
}{
True: "true",
False: "false",
Foo: "unset",
},
want: `{"True":true,"False":false,"Foo":null}`,
wantBack: struct {
True Bool
False Bool
Foo Bool
}{"true", "false", "unset"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j, err := json.Marshal(tt.in)
if err != nil {
t.Fatal(err)
}
if string(j) != tt.want {
t.Errorf("wrong JSON:\n got: %s\nwant: %s\n", j, tt.want)
}
wantBack := tt.in
if tt.wantBack != nil {
wantBack = tt.wantBack
}
// And back again:
newVal := reflect.New(reflect.TypeOf(tt.in))
out := newVal.Interface()
if err := json.Unmarshal(j, out); err != nil {
t.Fatalf("Unmarshal %#q: %v", j, err)
}
got := newVal.Elem().Interface()
if !reflect.DeepEqual(got, wantBack) {
t.Errorf("value mismatch\n got: %+v\nwant: %+v\n", got, wantBack)
}
})
}
}
func TestBoolEqualBool(t *testing.T) {
tests := []struct {
b Bool
v bool
want bool
}{
{"", true, false},
{"", false, false},
{"sdflk;", true, false},
{"sldkf;", false, false},
{"true", true, true},
{"true", false, false},
{"false", true, false},
{"false", false, true},
{"1", true, false}, // "1" is not true; only "true" is
{"True", true, false}, // "True" is not true; only "true" is
}
for _, tt := range tests {
if got := tt.b.EqualBool(tt.v); got != tt.want {
t.Errorf("(%q).EqualBool(%v) = %v; want %v", string(tt.b), tt.v, got, tt.want)
}
}
}
func TestUnmarshalAlloc(t *testing.T) {
b := json.Unmarshaler(new(Bool))
n := testing.AllocsPerRun(10, func() { b.UnmarshalJSON(trueBytes) })
if n > 0 {
t.Errorf("got %v allocs, want 0", n)
}
}
func TestBoolFlag(t *testing.T) {
tests := []struct {
arguments string
wantParseError bool // expect flag.Parse to error
want Bool
}{
{"", false, Bool("")},
{"-test", true, Bool("")},
{`-test=""`, true, Bool("")},
{"-test invalid", true, Bool("")},
{"-test true", false, NewBool(true)},
{"-test 1", false, NewBool(true)},
{"-test false", false, NewBool(false)},
{"-test 0", false, NewBool(false)},
}
for _, tt := range tests {
var got Bool
fs := flag.NewFlagSet(t.Name(), flag.ContinueOnError)
fs.Var(&BoolFlag{&got}, "test", "test flag")
arguments := strings.Split(tt.arguments, " ")
err := fs.Parse(arguments)
if (err != nil) != tt.wantParseError {
t.Errorf("flag.Parse(%q) returned error %v, want %v", arguments, err, tt.wantParseError)
}
if got != tt.want {
t.Errorf("flag.Parse(%q) got %q, want %q", arguments, got, tt.want)
}
}
}
|