Skip to content

Commit

Permalink
convert value getters tests to table-driven style (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
harsha-aqfer committed Apr 9, 2023
1 parent 451a4b3 commit 1fb3b90
Showing 1 changed file with 119 additions and 89 deletions.
208 changes: 119 additions & 89 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,103 +155,133 @@ func TestValue_Alias(t *testing.T) {
}

func TestValue_Getters(t *testing.T) {
reporter := newMockReporter(t)

t.Run("null", func(t *testing.T) {
var data interface{}

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, failure)
NewValue(reporter, data).IsNull().chain.assert(t, success)
})

t.Run("indirect null", func(t *testing.T) {
var data []interface{}
cases := []struct {
name string
data interface{}
wantObject chainResult
wantArray chainResult
wantString chainResult
wantNumber chainResult
wantBoolean chainResult
wantNull chainResult
wantNotNull chainResult
}{
{
name: "null",
data: nil,
wantObject: failure,
wantArray: failure,
wantString: failure,
wantNumber: failure,
wantBoolean: failure,
wantNull: success,
wantNotNull: failure,
},
{
name: "indirect null",
data: []interface{}(nil),
wantObject: failure,
wantArray: failure,
wantString: failure,
wantNumber: failure,
wantBoolean: failure,
wantNull: success,
wantNotNull: failure,
},
{
name: "bad",
data: func() {},
wantObject: failure,
wantArray: failure,
wantString: failure,
wantNumber: failure,
wantBoolean: failure,
wantNull: failure,
wantNotNull: failure,
},
{
name: "object",
data: map[string]interface{}{},
wantObject: success,
wantArray: failure,
wantString: failure,
wantNumber: failure,
wantBoolean: failure,
wantNull: failure,
wantNotNull: success,
},
{
name: "array",
data: []interface{}{},
wantObject: failure,
wantArray: success,
wantString: failure,
wantNumber: failure,
wantBoolean: failure,
wantNull: failure,
wantNotNull: success,
},
{
name: "string",
data: "",
wantObject: failure,
wantArray: failure,
wantString: success,
wantNumber: failure,
wantBoolean: failure,
wantNull: failure,
wantNotNull: success,
},
{
name: "number",
data: 0.0,
wantObject: failure,
wantArray: failure,
wantString: failure,
wantNumber: success,
wantBoolean: failure,
wantNull: failure,
wantNotNull: success,
},
{
name: "boolean",
data: false,
wantObject: failure,
wantArray: failure,
wantString: failure,
wantNumber: failure,
wantBoolean: success,
wantNull: failure,
wantNotNull: success,
},
}

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, failure)
NewValue(reporter, data).IsNull().chain.assert(t, success)
})
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)

t.Run("bad", func(t *testing.T) {
data := func() {}
NewValue(reporter, tc.data).Object().
chain.assert(t, tc.wantObject)

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, failure)
NewValue(reporter, data).IsNull().chain.assert(t, failure)
})
NewValue(reporter, tc.data).Array().
chain.assert(t, tc.wantArray)

t.Run("object", func(t *testing.T) {
data := map[string]interface{}{}

NewValue(reporter, data).Object().chain.assert(t, success)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, success)
NewValue(reporter, data).IsNull().chain.assert(t, failure)
})
NewValue(reporter, tc.data).String().
chain.assert(t, tc.wantString)

t.Run("array", func(t *testing.T) {
data := []interface{}{}

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, success)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, success)
NewValue(reporter, data).IsNull().chain.assert(t, failure)
})
NewValue(reporter, tc.data).Number().
chain.assert(t, tc.wantNumber)

t.Run("string", func(t *testing.T) {
data := ""

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, success)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, success)
NewValue(reporter, data).IsNull().chain.assert(t, failure)
})
NewValue(reporter, tc.data).Boolean().
chain.assert(t, tc.wantBoolean)

t.Run("number", func(t *testing.T) {
data := 0.0

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, success)
NewValue(reporter, data).Boolean().chain.assert(t, failure)
NewValue(reporter, data).NotNull().chain.assert(t, success)
NewValue(reporter, data).IsNull().chain.assert(t, failure)
})
NewValue(reporter, tc.data).IsNull().
chain.assert(t, tc.wantNull)

t.Run("boolean", func(t *testing.T) {
data := false

NewValue(reporter, data).Object().chain.assert(t, failure)
NewValue(reporter, data).Array().chain.assert(t, failure)
NewValue(reporter, data).String().chain.assert(t, failure)
NewValue(reporter, data).Number().chain.assert(t, failure)
NewValue(reporter, data).Boolean().chain.assert(t, success)
NewValue(reporter, data).NotNull().chain.assert(t, success)
NewValue(reporter, data).IsNull().chain.assert(t, failure)
})
NewValue(reporter, tc.data).NotNull().
chain.assert(t, tc.wantNotNull)
})
}
}

func TestValue_GetObject(t *testing.T) {
Expand Down

0 comments on commit 1fb3b90

Please sign in to comment.