diff --git a/random/Factory.go b/random/Factory.go index 07014f1..da7431d 100644 --- a/random/Factory.go +++ b/random/Factory.go @@ -30,18 +30,18 @@ func (f *Factory) Make(rnd *Random, T any) (_T any) { if T == nil { return nil } - rt := reflect.TypeOf(T) - if typeFunc, ok := f.getTypes()[rt]; ok { + typ := f.typeOf(T) + if typeFunc, ok := f.getTypes()[typ]; ok { return typeFunc(rnd) } - if kindFunc, ok := f.getKinds()[rt.Kind()]; ok { - return kindFunc(rnd, reflect.TypeOf(T)) + if kindFunc, ok := f.getKinds()[typ.Kind()]; ok { + return kindFunc(rnd, typ) } return T } func (f *Factory) RegisterType(T any, ff typeFunc) { - f.getTypes()[reflect.TypeOf(T)] = ff + f.getTypes()[f.typeOf(T)] = ff } func (f *Factory) getTypes() map[reflect.Type]typeFunc { @@ -53,8 +53,11 @@ func (f *Factory) getTypes() map[reflect.Type]typeFunc { return f.types.mapping } -func (f *Factory) getRandom() *Random { - return New(CryptoSeed{}) +func (f *Factory) typeOf(T any) reflect.Type { + if rt, ok := T.(reflect.Type); ok { + return rt + } + return reflect.TypeOf(T) } func (f *Factory) int(rnd *Random, T reflect.Type) any { diff --git a/random/Factory_test.go b/random/Factory_test.go index 83cb04b..d5ead18 100644 --- a/random/Factory_test.go +++ b/random/Factory_test.go @@ -767,24 +767,67 @@ func TestFactory(t *testing.T) { }) }) }) + + s.When("type is a reflect type", func(s *testcase.Spec) { + T.Let(s, func(t *testcase.T) any { + return reflect.TypeOf((*string)(nil)).Elem() + }) + + s.Then("it will use the reflection type to decide the type", func(t *testcase.T) { + got, ok := act(t).(string) + t.Must.True(ok, "expected that creates the type described by the reflect type input argument") + t.Must.NotEmpty(got) + + }) + + s.Then("random values are returned", func(t *testcase.T) { + var got = make(map[string]struct{}) + + t.Eventually(func(it assert.It) { + got[act(t).(string)] = struct{}{} + + it.Must.True(len(got) > 1) + }) + }) + }) }) - s.Test(`.RegisterType`, func(t *testcase.T) { - type CustomType struct { - Foo int - Bar int - } + s.Describe(`.RegisterType`, func(s *testcase.Spec) { + s.Test("", func(t *testcase.T) { + type CustomType struct { + Foo int + Bar int + } - ff := factory.Get(t) + ff := factory.Get(t) - ff.RegisterType(CustomType{}, func(rnd *random.Random) any { - return CustomType{Foo: 42, Bar: rnd.Int()} + ff.RegisterType(CustomType{}, func(rnd *random.Random) any { + return CustomType{Foo: 42, Bar: rnd.Int()} + }) + + ct := ff.Make(rnd.Get(t), CustomType{}).(CustomType) + t.Must.Equal(42, ct.Foo) + t.Must.NotEmpty(ct.Bar) }) - ct := ff.Make(rnd.Get(t), CustomType{}).(CustomType) - t.Must.Equal(42, ct.Foo) - t.Must.NotEmpty(ct.Bar) + s.Test("accepts reflect.Type", func(t *testcase.T) { + type CustomType struct { + Foo int + Bar int + } + + ff := factory.Get(t) + + ff.RegisterType(reflect.TypeOf((*CustomType)(nil)).Elem(), func(rnd *random.Random) any { + return CustomType{Foo: 42, Bar: rnd.Int()} + }) + + ct := ff.Make(rnd.Get(t), CustomType{}).(CustomType) + t.Must.Equal(42, ct.Foo) + t.Must.NotEmpty(ct.Bar) + }) }) + } func TestFactoryMake_race(t *testing.T) {