-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support array generators (addresses #86)
- Loading branch information
1 parent
f9f2f29
commit 69954c9
Showing
16 changed files
with
910 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package arbitrary_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/leanovate/gopter/arbitrary" | ||
) | ||
|
||
func TestArbitrariesArrays(t *testing.T) { | ||
arbitraries := arbitrary.DefaultArbitraries() | ||
|
||
gen := arbitraries.GenForType(reflect.TypeOf([20]int{})) | ||
value, ok := gen.Sample() | ||
if !ok { | ||
t.Errorf("Invalid value %#v", value) | ||
} | ||
if _, ok = value.([20]int); !ok { | ||
t.Errorf("Invalid value %#v", value) | ||
} | ||
|
||
gen = arbitraries.GenForType(reflect.TypeOf([10]string{})) | ||
value, ok = gen.Sample() | ||
if !ok { | ||
t.Errorf("Invalid value %#v", value) | ||
} | ||
if _, ok = value.([10]string); !ok { | ||
t.Errorf("Invalid value %#v", value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package gen | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/leanovate/gopter" | ||
) | ||
|
||
// ArrayOfN generates an array of generated elements with definied length | ||
func ArrayOfN(desiredlen int, elementGen gopter.Gen, typeOverrides ...reflect.Type) gopter.Gen { | ||
var typeOverride reflect.Type | ||
if len(typeOverrides) > 1 { | ||
panic("too many type overrides specified, at most 1 may be provided.") | ||
} else if len(typeOverrides) == 1 { | ||
typeOverride = typeOverrides[0] | ||
} | ||
return func(genParams *gopter.GenParameters) *gopter.GenResult { | ||
result, elementSieve, elementShrinker := genArray(elementGen, genParams, desiredlen, typeOverride) | ||
|
||
genResult := gopter.NewGenResult(result.Interface(), ArrayShrinkerOne(elementShrinker)) | ||
if elementSieve != nil { | ||
genResult.Sieve = func(v interface{}) bool { | ||
rv := reflect.ValueOf(v) | ||
return rv.Len() == desiredlen && forAllSieve(elementSieve)(v) | ||
} | ||
} else { | ||
genResult.Sieve = func(v interface{}) bool { | ||
return reflect.ValueOf(v).Len() == desiredlen | ||
} | ||
} | ||
return genResult | ||
} | ||
} | ||
|
||
func genArray(elementGen gopter.Gen, genParams *gopter.GenParameters, desiredlen int, typeOverride reflect.Type) (reflect.Value, func(interface{}) bool, gopter.Shrinker) { | ||
element := elementGen(genParams) | ||
elementSieve := element.Sieve | ||
elementShrinker := element.Shrinker | ||
|
||
sliceType := typeOverride | ||
if sliceType == nil { | ||
sliceType = element.ResultType | ||
} | ||
|
||
arrayType := reflect.ArrayOf(desiredlen, sliceType) | ||
result := reflect.New(arrayType).Elem() | ||
|
||
for i := 0; i < desiredlen; i++ { | ||
value, ok := element.Retrieve() | ||
|
||
if ok { | ||
if value == nil { | ||
result.Index(i).Set(reflect.Zero(sliceType)) | ||
} else { | ||
result.Index(i).Set(reflect.ValueOf(value)) | ||
} | ||
} | ||
element = elementGen(genParams) | ||
} | ||
|
||
return result, elementSieve, elementShrinker | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package gen_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/leanovate/gopter" | ||
"github.com/leanovate/gopter/gen" | ||
) | ||
|
||
func TestArrayOfN(t *testing.T) { | ||
genParams := gopter.DefaultGenParameters() | ||
genParams.MaxSize = 50 | ||
elementGen := gen.Const("element") | ||
arrayGen := gen.ArrayOfN(20, elementGen) | ||
|
||
for i := 0; i < 100; i++ { | ||
sample, ok := arrayGen(genParams).Retrieve() | ||
|
||
if !ok { | ||
t.Error("Sample was not ok") | ||
} | ||
strings, ok := sample.([20]string) | ||
if !ok { | ||
t.Errorf("Sample not slice of string: %#v", sample) | ||
} else { | ||
if len(strings) > 50 { | ||
t.Errorf("Sample has invalid length: %#v", len(strings)) | ||
} | ||
for _, str := range strings { | ||
if str != "element" { | ||
t.Errorf("Sample contains invalid value: %#v", sample) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.