-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from greyblake/arbitrary
Support of Arbitrary for integers
- Loading branch information
Showing
30 changed files
with
390 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,10 @@ | ||
use nutype::nutype; | ||
|
||
#[nutype( | ||
derive(Debug, PartialEq, Deref, AsRef), | ||
sanitize(with = |mut guests| { guests.sort(); guests }), | ||
validate(predicate = |guests| !guests.is_empty() ), | ||
validate(greater = 0, less = 24, predicate = |x| x == &4), | ||
derive(Debug, PartialEq, Deref, AsRef, Default), | ||
default = 4 | ||
)] | ||
pub struct GuestList(Vec<String>); | ||
pub struct Hour(i32); | ||
|
||
fn main() { | ||
// Empty list is not allowed | ||
assert_eq!( | ||
GuestList::new(vec![]), | ||
Err(GuestListError::PredicateViolated) | ||
); | ||
|
||
// Create the list of our guests | ||
let guest_list = GuestList::new(vec![ | ||
"Seneca".to_string(), | ||
"Marcus Aurelius".to_string(), | ||
"Socrates".to_string(), | ||
"Epictetus".to_string(), | ||
]) | ||
.unwrap(); | ||
|
||
// The list is sorted (thanks to sanitize) | ||
assert_eq!( | ||
guest_list.as_ref(), | ||
&[ | ||
"Epictetus".to_string(), | ||
"Marcus Aurelius".to_string(), | ||
"Seneca".to_string(), | ||
"Socrates".to_string(), | ||
] | ||
); | ||
|
||
// Since GuestList derives Deref, we can use methods from `Vec<T>` | ||
// due to deref coercion (if it's a good idea or not, it's left up to you to decide!). | ||
assert_eq!(guest_list.len(), 4); | ||
|
||
for guest in guest_list.iter() { | ||
println!("{guest}"); | ||
} | ||
} | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "integer_arbitrary" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
arbitrary = "1.3.2" | ||
arbtest = "0.2.0" | ||
nutype = { path = "../../nutype", features = ["arbitrary"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use arbitrary::Arbitrary; | ||
use nutype::nutype; | ||
|
||
// Inclusive boundaries. 1 and 6 are included, so the value can only be 1, 2, 3, 4, 5 or 6. | ||
#[nutype( | ||
validate(greater_or_equal = 1, less_or_equal = 6), | ||
derive(Arbitrary, Debug) | ||
)] | ||
struct GermanTaxClass(i128); | ||
|
||
// Exclusive boundaries. | ||
// | ||
// -2 and 2 are excluded, so the value can only be -1, 0 or 1. | ||
#[nutype( | ||
validate(greater = -2, less = 2), | ||
derive(Arbitrary, Debug), | ||
)] | ||
struct MinusOneOrZeroOrOne(i128); | ||
|
||
// Since the upper limit for i8 is 127, the GreaterThan125 can only be 126 or 127. | ||
#[nutype(validate(greater = 125), derive(Arbitrary, Debug))] | ||
struct GreaterThan125(i8); | ||
|
||
// Since the upper limit for i8 is 127, the GreaterOrEqual125 can only be 125, 126 or 127. | ||
#[nutype(validate(greater_or_equal = 125), derive(Arbitrary, Debug))] | ||
struct GreaterOrEqual125(i8); | ||
|
||
// u128::MIN is 0, so the LessThan2 can only be 0, 1 | ||
#[nutype(validate(less = 2), derive(Arbitrary, Debug))] | ||
struct LessThan2(u128); | ||
|
||
// u128::MIN is 0, so the LessOrEqual2 can only be 0, 1, 2 | ||
#[nutype(validate(less = 2), derive(Arbitrary, Debug))] | ||
struct LessOrEqual2(u128); | ||
|
||
fn main() { | ||
arbtest::builder().run(|u| { | ||
let tax_class = GermanTaxClass::arbitrary(u)?.into_inner(); | ||
assert!(tax_class >= 1); | ||
assert!(tax_class <= 6); | ||
Ok(()) | ||
}); | ||
|
||
arbtest::builder().run(|u| { | ||
let value = GreaterThan125::arbitrary(u)?.into_inner(); | ||
assert!(value == 126 || value == 127); | ||
Ok(()) | ||
}); | ||
|
||
arbtest::builder().run(|u| { | ||
let value = GreaterOrEqual125::arbitrary(u)?.into_inner(); | ||
assert!(value == 125 || value == 126 || value == 127); | ||
Ok(()) | ||
}); | ||
|
||
arbtest::builder().run(|u| { | ||
let value = MinusOneOrZeroOrOne::arbitrary(u)?.into_inner(); | ||
assert!(value == -1 || value == 0 || value == 1); | ||
Ok(()) | ||
}); | ||
|
||
arbtest::builder().run(|u| { | ||
let value = LessThan2::arbitrary(u)?.into_inner(); | ||
assert!(value == 0 || value == 1); | ||
Ok(()) | ||
}); | ||
|
||
arbtest::builder().run(|u| { | ||
let value = LessOrEqual2::arbitrary(u)?.into_inner(); | ||
assert!(value == 0 || value == 1 || value == 2); | ||
Ok(()) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.