-
Notifications
You must be signed in to change notification settings - Fork 54
tests: add nightly test for Protocol Arbitrary impl to assert it covers all the Protocol variants. #76
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
tests: add nightly test for Protocol Arbitrary impl to assert it covers all the Protocol variants. #76
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,10 +85,14 @@ impl Arbitrary for Ma { | |
| #[derive(PartialEq, Eq, Clone, Debug)] | ||
| struct Proto(Protocol<'static>); | ||
|
|
||
| impl Proto { | ||
| const IMPL_VARIANT_COUNT: u8 = 32; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually thinking of directly on the enum as I am sure there are also proc-macros for this that we could enable only during testing if this nightly stuff is too fancy 😁
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, that would unnecessarily increase the public API. Leave it as is! :) |
||
| } | ||
|
|
||
| impl Arbitrary for Proto { | ||
| fn arbitrary(g: &mut Gen) -> Self { | ||
| use Protocol::*; | ||
| match u8::arbitrary(g) % 32 { | ||
| match u8::arbitrary(g) % Proto::IMPL_VARIANT_COUNT { | ||
| 0 => Proto(Dccp(Arbitrary::arbitrary(g))), | ||
| 1 => Proto(Dns(Cow::Owned(SubString::arbitrary(g).0))), | ||
| 2 => Proto(Dns4(Cow::Owned(SubString::arbitrary(g).0))), | ||
|
|
@@ -639,3 +643,12 @@ fn protocol_stack() { | |
| assert_eq!(ps, toks); | ||
| } | ||
| } | ||
|
|
||
| // Assert all `Protocol` variants are covered | ||
| // in its `Arbitrary` impl. | ||
| #[cfg(nightly)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicely done! At first I thought that we'd need the rust-version proc macro here but |
||
| #[test] | ||
| fn arbitrary_impl_for_all_proto_variants() { | ||
| let variants = core::mem::variant_count::<Protocol>() as u8; | ||
| assert_eq!(variants, Proto::IMPL_VARIANT_COUNT); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot that we need to activate features, this is 🔥🔥🔥