Skip to content

Commit

Permalink
Merge pull request #103 from steffnova/feature/issues/102
Browse files Browse the repository at this point in the history
🤖[feature/issues/102] Add tests for Array shrinker
  • Loading branch information
steffnova committed May 5, 2023
2 parents c26a9e6 + fc0e053 commit be86615
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions shrinker/array.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package shrinker

import (
"fmt"

"github.com/steffnova/go-check/arbitrary"
)

func Array(original arbitrary.Arbitrary, shrinkers []Shrinker) Shrinker {
switch {
case original.Value.Len() != len(original.Elements):
return Fail(fmt.Errorf("Invalid number of elements. Expected: %d", len(original.Elements)))
case len(original.Elements) != len(shrinkers):
return Fail(fmt.Errorf("Number of shrinkers: %d must match number of elements: %d", len(shrinkers), len(original.Elements)))
default:
return Chain(
CollectionElement(shrinkers...),
Expand Down
72 changes: 72 additions & 0 deletions shrinker/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package shrinker

import (
"reflect"
"testing"

"github.com/steffnova/go-check/arbitrary"
"github.com/steffnova/go-check/constraints"
)

func TestArray(t *testing.T) {

testCases := map[string]func(t *testing.T){
"InvalidOriginal": func(t *testing.T) {
arr := [10]uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
arb := arbitrary.Arbitrary{Value: reflect.ValueOf(arr)}
shrinker := Array(arb, nil)
if _, _, err := shrinker(arbitrary.Arbitrary{}, true); err == nil {
t.Fatalf("Expected error when original arbitrary is invalid")
}
},
"InvalidShrinkers": func(t *testing.T) {
arr := [10]uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
elements := make([]arbitrary.Arbitrary, len(arr))
for index, arr := range arr {
elements[index] = arbitrary.Arbitrary{Value: reflect.ValueOf(arr)}
}
arb := arbitrary.Arbitrary{Value: reflect.ValueOf(arr), Elements: elements}
shrinker := Array(arb, []Shrinker{})
if _, _, err := shrinker(arb, true); err == nil {
t.Fatalf("Expected error when original arbitrary is invalid")
}
},
"ErrorWhenArbitraryIsNotArray": func(t *testing.T) {
arr := [10]uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
arb := arbitrary.Arbitrary{Value: reflect.ValueOf(arr)}
shrinker := Array(arb, nil)
if _, _, err := shrinker(arbitrary.Arbitrary{}, true); err == nil {
t.Fatalf("Expected error when passed arbitrary is not a array")
}
},
"ShrinkingFinishes": func(t *testing.T) {
arr := [10]uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
elements := make([]arbitrary.Arbitrary, len(arr))
shrinkers := make([]Shrinker, len(arr))

for index, arr := range arr {
elements[index] = arbitrary.Arbitrary{Value: reflect.ValueOf(arr)}
shrinkers[index] = Uint64(constraints.Uint64Default())
}

arb := arbitrary.Arbitrary{Value: reflect.ValueOf(arr), Elements: elements}
shrinker := Array(arb, shrinkers)

for shrinker != nil {
var err error
arb, shrinker, err = shrinker(arb, true)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}

if arb.Value.Interface().([10]uint64) != [10]uint64{0, 0, 0, 0, 0, 0, 0, 0, 0} {
t.Fatalf("Shrinking failed")
}
},
}

for name, testCase := range testCases {
t.Run(name, testCase)
}
}

0 comments on commit be86615

Please sign in to comment.