Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to generate slice of pointers #67

Open
Higan opened this issue Apr 29, 2020 · 1 comment
Open

Not able to generate slice of pointers #67

Higan opened this issue Apr 29, 2020 · 1 comment

Comments

@Higan
Copy link

Higan commented Apr 29, 2020

Hi,

I got an error when trying to generating a slice of pointers using the code below:

package main

import (
	"github.com/leanovate/gopter"
	"github.com/leanovate/gopter/gen"
	"github.com/leanovate/gopter/prop"
	"reflect"
)

type Empty struct {
	ID uint
}

func main() {
	properties := gopter.NewProperties(nil)

	properties.Property("just test pointer", prop.ForAll(
		func(nodes []*Empty) bool {
			return true
		},
		gen.SliceOf(gen.Struct(reflect.TypeOf(&Empty{}), map[string]gopter.Gen{
			"ID": gen.UInt(),
		})),
	))
	properties.Run(gopter.ConsoleReporter(false))

}

The error was:

! just test pointer: Error on property evaluation after 0 passed tests:
   Check paniced: reflect: Call using []main.Empty as type []*main.Empty
ARG_0: []

I think reflect.TypeOf(&Empty{}) returns *main.Empty instead of main.Empty, but according to the error messages, the generated type is []main.Empty; so I just wonder whether there's something wrong with my code here.

@untoldwind
Copy link
Collaborator

At the moment you have to use "gen.StructPtr" instead of "gen.Struct" to generate pointers of structs rather than the struct itself.

I'm not sure if it would be a good idea to change the behavior of "gen.Struct" depending on the type you put in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants