leanovate / gopter

GOlang Property TestER
MIT License
599 stars 40 forks source link

Arbitraries.GenForType does not support arrays #86

Closed Wessie closed 6 months ago

Wessie commented 6 months ago

passing an array type to GenForType returns nil, and this only panics once the properties are actually ran so it isn't obvious what the issue is, it produces the following stack trace:

! roundtrip: Error on property evaluation after 0 passed tests: Check
   paniced: runtime error: invalid memory address or nil pointer dereference
goroutine 19 [running]:
runtime/debug.Stack()
        /usr/local/go/src/runtime/debug/stack.go:24 +0x5e
github.com/leanovate/gopter/prop.ForAll.SaveProp.func3.1()
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/prop.go:20 +0x6b
panic({0x6c7ec0?, 0x96c6a0?})
        /usr/local/go/src/runtime/panic.go:914 +0x21f
github.com/leanovate/gopter/prop.ForAll.func1(0xc0000be2e0)
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/prop/forall.go:33 +0xc7
github.com/leanovate/gopter/prop.ForAll.SaveProp.func3(0x7fc1b4964b98?)
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/prop.go:25 +0x5b
github.com/leanovate/gopter.Prop.Check.func1(0x0, 0xc000090580)
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/prop.go:53 +0x132
github.com/leanovate/gopter.(*runner).runWorkers(0xc000094f90)
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/runner.go:49 +0x1cd
github.com/leanovate/gopter.Prop.Check(0xc000090560, 0xc0000f0b80)
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/prop.go:112 +0x1be
github.com/leanovate/gopter.(*Properties).Run(0xc0000d9f38, {0x78f100, 0xc0000be2a0})
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/properties.go:37 +0xa9
github.com/leanovate/gopter.(*Properties).TestingRun(0xc0000d9f38, 0xc000084ea0, {0x0, 0x0, 0x6b14a0?})
        /home/wessie/go/pkg/mod/github.com/leanovate/gopter@v0.2.9/properties.go:56 +0xfc

GenForType should probably panic if there is an unsupported type

untoldwind commented 6 months ago

Can you provide a short example to reproduce this?

Wessie commented 6 months ago
a := arbitrary.DefaultArbitraries()
gen := a.GenForType(reflect.TypeOf([20]byte{}))
fmt.Println(gen)

will print <nil> https://go.dev/play/p/1_Mxhuc5GKl

untoldwind commented 6 months ago

Can you check if the current master branch fixes your problem?

Wessie commented 6 months ago

Yes, this fixes array support, but it would still be a nice quality of life change if passing an unsupported type to GenForType actually panicked with a reasonable message indicating such.

Also there is a typo in the array_shink.go filename

Wessie commented 6 months ago

I'm sorry, it seems panicking on unsupported types is a breaking change.

Since struct types ignore unsupported fields it means anyone generating structs with unsupported fields now gets a panic instead.

It's fine for the map/slice/array versions but structs explicitly ignore fields https://github.com/leanovate/gopter/blob/master/arbitrary/gen_for_kind.go#L331 if GenForType returns nil.

Sorry for the shortsightedness, maybe we can move the check into Arbitraries.ForAll instead.

untoldwind commented 6 months ago

Removed the panic for now, have to dig a bit deeper into the ForAll