From b2658a5604631341254e362b35801aa4f9bf232d Mon Sep 17 00:00:00 2001 From: Lucas Rouckhout Date: Sat, 8 May 2021 17:49:35 +0200 Subject: [PATCH] Use table-driven test to merge the true and false case of Unmarshalling --- bool_test.go | 62 ++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/bool_test.go b/bool_test.go index 16bb3b5..e9f4b85 100644 --- a/bool_test.go +++ b/bool_test.go @@ -182,49 +182,35 @@ func TestRace(t *testing.T) { wg.Wait() } -func TestFalseUnmarshall(t *testing.T) { - // Marshall a normal bool into JSON byte slice - b, err := json.Marshal(false) - if err != nil { - t.Error(err) +func TestJSONUnmarshall(t *testing.T) { + // Table of cases + cases := []struct { + boolean bool + abool *AtomicBool + }{ + {true, NewBool(true)}, + {false, NewBool(false)}, } - // Create an AtomicBool - v := New() + for _, c := range cases { + b, err := json.Marshal(c.boolean) + if err != nil { + t.Error(err) + } - // 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) - } + // Create an AtomicBool + v := New() - // Check if our AtomicBool is set to false - if v.IsSet() == true { - t.Errorf("Expected AtomicBool to represent false but IsSet() returns true") - } -} + // Try to unmarshall the JSON byte slice + // of a normal boolean into an AtomicBool + err = v.UnmarshalJSON(b) + if err != nil { + t.Error(err) + } -func TestTrueUnmarshall(t *testing.T) { - // Marshall a normal bool into JSON byte slice - 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) + if v.IsSet() != c.abool.IsSet() { + t.Errorf("Expected AtomicBool to represent %t but actual value was %t", c.abool.IsSet(), v.IsSet()) + } } }