|
|
|
|
@ -12,9 +12,10 @@ import ( |
|
|
|
|
|
|
|
|
|
func TestBool(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
in any |
|
|
|
|
want string // JSON
|
|
|
|
|
name string |
|
|
|
|
in any |
|
|
|
|
want string // JSON
|
|
|
|
|
wantBack any |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "null_for_unset", |
|
|
|
|
@ -27,6 +28,15 @@ func TestBool(t *testing.T) { |
|
|
|
|
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", |
|
|
|
|
@ -40,6 +50,24 @@ func TestBool(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
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) { |
|
|
|
|
@ -51,6 +79,10 @@ func TestBool(t *testing.T) { |
|
|
|
|
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() |
|
|
|
|
@ -58,8 +90,8 @@ func TestBool(t *testing.T) { |
|
|
|
|
t.Fatalf("Unmarshal %#q: %v", j, err) |
|
|
|
|
} |
|
|
|
|
got := newVal.Elem().Interface() |
|
|
|
|
if !reflect.DeepEqual(tt.in, got) { |
|
|
|
|
t.Errorf("value mismatch\n got: %+v\nwant: %+v\n", got, tt.in) |
|
|
|
|
if !reflect.DeepEqual(got, wantBack) { |
|
|
|
|
t.Errorf("value mismatch\n got: %+v\nwant: %+v\n", got, wantBack) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
@ -79,11 +111,12 @@ func TestBoolEqualBool(t *testing.T) { |
|
|
|
|
{"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) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|