mirror of https://github.com/tevino/abool.git
Use table-driven test to merge the true and false case of Unmarshalling
This commit is contained in:
parent
e91174bed9
commit
b2658a5604
46
bool_test.go
46
bool_test.go
|
@ -182,9 +182,18 @@ 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 {
|
||||||
|
boolean bool
|
||||||
|
abool *AtomicBool
|
||||||
|
}{
|
||||||
|
{true, NewBool(true)},
|
||||||
|
{false, NewBool(false)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
b, err := json.Marshal(c.boolean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -192,40 +201,17 @@ func TestFalseUnmarshall(t *testing.T) {
|
||||||
// Create an AtomicBool
|
// Create an AtomicBool
|
||||||
v := New()
|
v := New()
|
||||||
|
|
||||||
// Try to unmarshall the JSON byte slice of a
|
// Try to unmarshall the JSON byte slice
|
||||||
// a normal bool into an AtomicBool
|
// of a normal boolean into an AtomicBool
|
||||||
err = v.UnmarshalJSON(b)
|
err = v.UnmarshalJSON(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if our AtomicBool is set to false
|
if v.IsSet() != c.abool.IsSet() {
|
||||||
if v.IsSet() == true {
|
t.Errorf("Expected AtomicBool to represent %t but actual value was %t", c.abool.IsSet(), v.IsSet())
|
||||||
t.Errorf("Expected AtomicBool to represent false but IsSet() returns true")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleAtomicBool() {
|
func ExampleAtomicBool() {
|
||||||
|
|
Loading…
Reference in New Issue