Skip to content

Commit

Permalink
Convert unit tests into subtest and to be table-driven (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpjservais committed Feb 24, 2023
1 parent 79ad562 commit b47b582
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 282 deletions.
103 changes: 56 additions & 47 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,71 +423,80 @@ func TestArray_IsEqual(t *testing.T) {
value.chain.clearFailed()
})

t.Run("types", func(t *testing.T) {
t.Run("strings", func(t *testing.T) {
reporter := newMockReporter(t)

value1 := NewArray(reporter, []interface{}{"foo", "bar"})
value2 := NewArray(reporter, []interface{}{123, 456})
value3 := NewArray(reporter, []interface{}{
map[string]interface{}{
"foo": 123,
},
map[string]interface{}{
"foo": 456,
},
})
value := NewArray(reporter, []interface{}{"foo", "bar"})

value1.IsEqual([]string{"foo", "bar"})
value1.chain.assertNotFailed(t)
value1.chain.clearFailed()
value.IsEqual([]string{"foo", "bar"})
value.chain.assertNotFailed(t)
value.chain.clearFailed()

value1.IsEqual([]string{"bar", "foo"})
value1.chain.assertFailed(t)
value1.chain.clearFailed()
value.IsEqual([]string{"bar", "foo"})
value.chain.assertFailed(t)
value.chain.clearFailed()

value1.NotEqual([]string{"foo", "bar"})
value1.chain.assertFailed(t)
value1.chain.clearFailed()
value.NotEqual([]string{"foo", "bar"})
value.chain.assertFailed(t)
value.chain.clearFailed()

value1.NotEqual([]string{"bar", "foo"})
value1.chain.assertNotFailed(t)
value1.chain.clearFailed()
value.NotEqual([]string{"bar", "foo"})
value.chain.assertNotFailed(t)
value.chain.clearFailed()
})

value2.IsEqual([]int{123, 456})
value2.chain.assertNotFailed(t)
value2.chain.clearFailed()
t.Run("numbers", func(t *testing.T) {
reporter := newMockReporter(t)

value2.IsEqual([]int{456, 123})
value2.chain.assertFailed(t)
value2.chain.clearFailed()
value := NewArray(reporter, []interface{}{123, 456})

value2.NotEqual([]int{123, 456})
value2.chain.assertFailed(t)
value2.chain.clearFailed()
value.IsEqual([]int{123, 456})
value.chain.assertNotFailed(t)
value.chain.clearFailed()

value2.NotEqual([]int{456, 123})
value2.chain.assertNotFailed(t)
value2.chain.clearFailed()
value.IsEqual([]int{456, 123})
value.chain.assertFailed(t)
value.chain.clearFailed()

value.NotEqual([]int{123, 456})
value.chain.assertFailed(t)
value.chain.clearFailed()

value.NotEqual([]int{456, 123})
value.chain.assertNotFailed(t)
value.chain.clearFailed()
})

t.Run("struct", func(t *testing.T) {
reporter := newMockReporter(t)
type S struct {
Foo int `json:"foo"`
}

value3.IsEqual([]S{{123}, {456}})
value3.chain.assertNotFailed(t)
value3.chain.clearFailed()
value := NewArray(reporter, []interface{}{
map[string]interface{}{
"foo": 123,
},
map[string]interface{}{
"foo": 456,
},
})

value.IsEqual([]S{{123}, {456}})
value.chain.assertNotFailed(t)
value.chain.clearFailed()

value3.IsEqual([]S{{456}, {123}})
value3.chain.assertFailed(t)
value3.chain.clearFailed()
value.IsEqual([]S{{456}, {123}})
value.chain.assertFailed(t)
value.chain.clearFailed()

value3.NotEqual([]S{{123}, {456}})
value3.chain.assertFailed(t)
value3.chain.clearFailed()
value.NotEqual([]S{{123}, {456}})
value.chain.assertFailed(t)
value.chain.clearFailed()

value3.NotEqual([]S{{456}, {123}})
value3.chain.assertNotFailed(t)
value3.chain.clearFailed()
value.NotEqual([]S{{456}, {123}})
value.chain.assertNotFailed(t)
value.chain.clearFailed()
})

t.Run("canonization", func(t *testing.T) {
Expand Down
194 changes: 93 additions & 101 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,108 +111,100 @@ func TestMatch_Getters(t *testing.T) {
}

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

value1 := NewMatch(reporter, []string{"m"}, nil)
value2 := NewMatch(reporter, []string{}, nil)
value3 := NewMatch(reporter, nil, nil)

assert.Equal(t, []string{}, value2.Raw())
assert.Equal(t, []string{}, value3.Raw())

value1.IsEmpty()
value1.chain.assertFailed(t)
value1.chain.clearFailed()

value1.NotEmpty()
value1.chain.assertNotFailed(t)
value1.chain.clearFailed()

value2.IsEmpty()
value2.chain.assertNotFailed(t)
value2.chain.clearFailed()

value2.NotEmpty()
value2.chain.assertFailed(t)
value2.chain.clearFailed()

value3.IsEmpty()
value3.chain.assertNotFailed(t)
value3.chain.clearFailed()

value3.NotEmpty()
value3.chain.assertFailed(t)
value3.chain.clearFailed()
cases := map[string]struct {
submatch []string
expectEmpty bool
}{
"string": {submatch: []string{"m"}, expectEmpty: false},
"empty string slice": {submatch: []string{}, expectEmpty: true},
"nil": {submatch: nil, expectEmpty: true},
}

for name, instance := range cases {
t.Run(name, func(t *testing.T) {
reporter := newMockReporter(t)

if instance.expectEmpty {
NewMatch(reporter, instance.submatch, nil).IsEmpty().
chain.assertNotFailed(t)

NewMatch(reporter, instance.submatch, nil).NotEmpty().
chain.assertFailed(t)

assert.Equal(t, []string{}, NewMatch(reporter, instance.submatch, nil).Raw())
} else {
NewMatch(reporter, instance.submatch, nil).NotEmpty().
chain.assertNotFailed(t)

NewMatch(reporter, instance.submatch, nil).IsEmpty().
chain.assertFailed(t)
}
})
}
}

func TestMatch_Values(t *testing.T) {
t.Run("empty", func(t *testing.T) {
reporter := newMockReporter(t)

value1 := NewMatch(reporter, nil, nil)
value2 := NewMatch(reporter, []string{}, nil)
value3 := NewMatch(reporter, []string{"m0"}, nil)

value1.Values()
value1.chain.assertNotFailed(t)
value1.chain.clearFailed()

value1.Values("")
value1.chain.assertFailed(t)
value1.chain.clearFailed()

value2.Values()
value2.chain.assertNotFailed(t)
value2.chain.clearFailed()

value2.Values("")
value2.chain.assertFailed(t)
value2.chain.clearFailed()

value3.Values()
value3.chain.assertNotFailed(t)
value3.chain.clearFailed()

value3.Values("m0")
value3.chain.assertFailed(t)
value3.chain.clearFailed()
})

t.Run("not empty", func(t *testing.T) {
reporter := newMockReporter(t)

value := NewMatch(reporter, []string{"m0", "m1", "m2"}, nil)

value.Values("m1", "m2")
value.chain.assertNotFailed(t)
value.chain.clearFailed()

value.Values("m2", "m1")
value.chain.assertFailed(t)
value.chain.clearFailed()

value.Values("m1")
value.chain.assertFailed(t)
value.chain.clearFailed()

value.Values()
value.chain.assertFailed(t)
value.chain.clearFailed()

value.NotValues("m1", "m2")
value.chain.assertFailed(t)
value.chain.clearFailed()

value.NotValues("m2", "m1")
value.chain.assertNotFailed(t)
value.chain.clearFailed()

value.NotValues("m1")
value.chain.assertNotFailed(t)
value.chain.clearFailed()

value.NotValues()
value.chain.assertNotFailed(t)
value.chain.clearFailed()
})
type wantMatch struct {
target []string
fail bool
}

cases := map[string]struct {
submatches []string
expectMatch []wantMatch
}{
"nil match instance": {
submatches: nil,
expectMatch: []wantMatch{
{target: nil, fail: false},
{target: []string{""}, fail: true},
},
},
"empty match instance": {
submatches: []string{},
expectMatch: []wantMatch{
{target: nil, fail: false},
{target: []string{""}, fail: true},
},
},
"not empty index 0 only": {
submatches: []string{"m0"},
expectMatch: []wantMatch{
{target: nil, fail: false},
{target: []string{"m0"}, fail: true},
},
},
"not empty": {
submatches: []string{"m0", "m1", "m2"},
expectMatch: []wantMatch{
{target: nil, fail: true},
{target: []string{"m1"}, fail: true},
{target: []string{"m2", "m1"}, fail: true},
{target: []string{"m1", "m2"}, fail: false},
},
},
}

for name, instance := range cases {
t.Run(name, func(t *testing.T) {
reporter := newMockReporter(t)

for _, match := range instance.expectMatch {
if match.fail {
NewMatch(reporter, instance.submatches, nil).NotValues(match.target...).
chain.assertNotFailed(t)

NewMatch(reporter, instance.submatches, nil).Values(match.target...).
chain.assertFailed(t)

} else {
NewMatch(reporter, instance.submatches, nil).Values(match.target...).
chain.assertNotFailed(t)

NewMatch(reporter, instance.submatches, nil).NotValues(match.target...).
chain.assertFailed(t)
}
}
})
}
}
Loading

0 comments on commit b47b582

Please sign in to comment.