Use table-driven test to merge the true and false case of Unmarshalling

This commit is contained in:
Lucas Rouckhout 2021-05-08 17:49:35 +02:00
parent e91174bed9
commit b2658a5604
1 changed files with 24 additions and 38 deletions

View File

@ -182,49 +182,35 @@ func TestRace(t *testing.T) {
wg.Wait() wg.Wait()
} }
func TestFalseUnmarshall(t *testing.T) { func TestJSONUnmarshall(t *testing.T) {
// Marshall a normal bool into JSON byte slice // Table of cases
b, err := json.Marshal(false) cases := []struct {
if err != nil { boolean bool
t.Error(err) abool *AtomicBool
}{
{true, NewBool(true)},
{false, NewBool(false)},
} }
// Create an AtomicBool for _, c := range cases {
v := New() b, err := json.Marshal(c.boolean)
if err != nil {
t.Error(err)
}
// Try to unmarshall the JSON byte slice of a // Create an AtomicBool
// a normal bool into an AtomicBool v := New()
err = v.UnmarshalJSON(b)
if err != nil {
t.Error(err)
}
// Check if our AtomicBool is set to false // Try to unmarshall the JSON byte slice
if v.IsSet() == true { // of a normal boolean into an AtomicBool
t.Errorf("Expected AtomicBool to represent false but IsSet() returns true") err = v.UnmarshalJSON(b)
} if err != nil {
} t.Error(err)
}
func TestTrueUnmarshall(t *testing.T) { if v.IsSet() != c.abool.IsSet() {
// Marshall a normal bool into JSON byte slice t.Errorf("Expected AtomicBool to represent %t but actual value was %t", c.abool.IsSet(), v.IsSet())
b, err := json.Marshal(true) }
if err != nil {
t.Error(err)
}
// Create an AtomicBool
v := New()
// Try to unmarshall the JSON byte slice of a
// a normal bool into an AtomicBool
err = v.UnmarshalJSON(b)
if err != nil {
t.Error(err)
}
// Check if our AtomicBool is set to false
if v.IsSet() == false {
t.Errorf("Expected AtomicBool to represent true but IsSet() returns false. %+v", v)
} }
} }