Example of property-based testing of the Vote structure #792
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This example shows how to test the Vote structure using
quickcheck
, the property-based testing tool.Two properties of the Vote structure is tested:
You will see that three distinct errors are discovered by quickcheck:
no timestamp
error: The Timestamp property of the struct is public and optional. A Vote struct can be created withnil
timestamp but we do not accept protobuf streams with no Timestamp.No such local time
error: it is possible to create a Timestamp that is invalid (before the EPOCH).IntegerOverflow
error: the generation of ValidatorIndex creates bigger numbers in some cases than allowed in ValidatorIndex. ValidatorIndex should only allow being created using valid numbers.You can see that implementing these property tests requires all involved structs to have the
Arbitrary
trait implemented. This is yet another trait that we need and we only use it in testing. Possibly we could annotate them properly so they don't end up in the release binary.