What is the correct way to implement testing/quick.Generator?

50 views
Skip to first unread message

Patrick Redmond

unread,
Jan 19, 2017, 3:20:25 AM1/19/17
to golang-nuts
I was trying to implement a custom `Generator` for a struct for use with `testing/quick.Check`. The property function passed to `testing/quick.Check` defines the type of its own arguments, so I need to make sure the custom `Generator` function will be used in either the case where the property function takes a pointer or the case where the property function takes a value. However, the behavior that results is strange whether the `Generator` implementation is a pointer receiver or a value receiver.

[The following example programs are identical except for the `Generator` implementation.]

1. `Generator` is a pointer receiver. https://play.golang.org/p/tJI17AS8Xq
   * The property taking a value uses a built-in `Generator`, and fails the test.
   * The property taking a pointer passes the test.

2. `Generator` is a value receiver. https://play.golang.org/p/MAC3AAn81w
   * The property taking a value passes the test.
   * The property taking a pointer causes the testing library to dereference a `nil` internally and panics.

What is the recommended way of implementing `Generator` such that the trivial test used in the examples passes irrespective of the type of the property function?
Reply all
Reply to author
Forward
0 new messages