forked from mirror/abool
Add more cases and some refine to existing
This commit is contained in:
parent
b5c1b6e635
commit
f55eb7cf2a
69
bool_test.go
69
bool_test.go
|
@ -182,34 +182,65 @@ func TestRace(t *testing.T) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONUnmarshal(t *testing.T) {
|
func TestJSONCompatibleWithBuiltinBool(t *testing.T) {
|
||||||
// Table of cases
|
for _, value := range []bool{true, false} {
|
||||||
cases := []struct {
|
// Test bool -> bytes -> AtomicBool
|
||||||
boolean bool
|
|
||||||
abool *AtomicBool
|
|
||||||
}{
|
|
||||||
{true, NewBool(true)},
|
|
||||||
{false, NewBool(false)},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range cases {
|
// act 1. bool -> bytes
|
||||||
b, err := json.Marshal(c.boolean)
|
buf, err := json.Marshal(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Fatalf("json.Marshal(%t) failed: %s", value, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an AtomicBool
|
// act 2. bytes -> AtomicBool
|
||||||
v := New()
|
//
|
||||||
|
|
||||||
// Try to unmarshall the JSON byte slice
|
// Try to unmarshall the JSON byte slice
|
||||||
// of a normal boolean into an AtomicBool
|
// of a normal boolean into an AtomicBool
|
||||||
err = v.UnmarshalJSON(b)
|
//
|
||||||
|
// Create an AtomicBool with the oppsite default to ensure the unmarshal process did the work
|
||||||
|
ab := NewBool(!value)
|
||||||
|
err = json.Unmarshal(buf, ab)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Fatalf(`json.Unmarshal("%s", %T) failed: %s`, buf, ab, err)
|
||||||
|
}
|
||||||
|
// assert
|
||||||
|
if ab.IsSet() != value {
|
||||||
|
t.Fatalf("Expected AtomicBool to represent %t but actual value was %t", value, ab.IsSet())
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.IsSet() != c.abool.IsSet() {
|
// Test AtomicBool -> bytes -> bool
|
||||||
t.Errorf("Expected AtomicBool to represent %t but actual value was %t", c.abool.IsSet(), v.IsSet())
|
|
||||||
|
// act 3. AtomicBool -> bytes
|
||||||
|
buf, err = json.Marshal(ab)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("json.Marshal(%T) failed: %s", ab, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// using the opposite value for the same reason as the former case
|
||||||
|
b := ab.IsNotSet()
|
||||||
|
// act 4. bytes -> bool
|
||||||
|
err = json.Unmarshal(buf, &b)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf(`json.Unmarshal("%s", %T) failed: %s`, buf, &b, err)
|
||||||
|
}
|
||||||
|
// assert
|
||||||
|
if b != ab.IsSet() {
|
||||||
|
t.Fatalf(`json.Unmarshal("%s", %T) didn't work, expected %t, got %t`, buf, ab, ab.IsSet(), b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalJSONErrorNoWrite(t *testing.T) {
|
||||||
|
for _, val := range []bool{true, false} {
|
||||||
|
ab := NewBool(val)
|
||||||
|
oldVal := ab.IsSet()
|
||||||
|
buf := []byte("invalid-json")
|
||||||
|
err := json.Unmarshal(buf, ab)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf(`Error expected from json.Unmarshal("%s", %T)`, buf, ab)
|
||||||
|
}
|
||||||
|
if oldVal != ab.IsSet() {
|
||||||
|
t.Fatal("Failed json.Unmarshal modified the value of AtomicBool which is not expected")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue