-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setter(custom) which allows a custom setter implementation (#154)
- Loading branch information
Showing
5 changed files
with
131 additions
and
23 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
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,88 @@ | ||
#[macro_use] | ||
extern crate pretty_assertions; | ||
#[macro_use] | ||
extern crate derive_builder; | ||
|
||
#[derive(Debug, PartialEq, Default, Builder, Clone)] | ||
#[builder(setter(skip = "false"), default)] | ||
struct SetterCustom { | ||
#[builder(setter(custom = "true"))] | ||
setter_custom_by_explicit_opt_in: u32, | ||
#[builder(setter(custom))] | ||
setter_custom_shorthand: u32, | ||
#[builder(setter(custom = "false"))] | ||
setter_custom_by_explicit_opt_out: u32, | ||
#[builder(setter(custom = "true"), default="4")] | ||
setter_custom_with_explicit_default: u32, | ||
#[builder(setter(custom = "true", strip_option))] | ||
setter_custom_with_strip_option: Option<u32>, | ||
} | ||
|
||
// compile test | ||
#[allow(dead_code)] | ||
impl SetterCustomBuilder { | ||
// only possible if setter was skipped | ||
fn setter_custom_by_explicit_opt_in(&mut self) -> &mut Self { | ||
self.setter_custom_by_explicit_opt_in = Some(1); | ||
self | ||
} | ||
|
||
// only possible if setter was skipped | ||
fn setter_custom_shorthand(&mut self) -> &mut Self { | ||
self.setter_custom_shorthand = Some(2); | ||
self | ||
} | ||
|
||
// only possible if setter was skipped | ||
fn setter_custom_with_explicit_default(&mut self) -> &mut Self { | ||
self.setter_custom_with_explicit_default = Some(43); | ||
self | ||
} | ||
|
||
// only possible if setter was skipped | ||
fn setter_custom_with_strip_option(&mut self) -> &mut Self { | ||
self.setter_custom_with_strip_option = Some(Some(6)); | ||
self | ||
} | ||
} | ||
|
||
#[test] | ||
fn setter_custom_defaults() { | ||
let x: SetterCustom = SetterCustomBuilder::default() | ||
.build() | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
x, | ||
SetterCustom { | ||
setter_custom_by_explicit_opt_in: 0, | ||
setter_custom_shorthand: 0, | ||
setter_custom_by_explicit_opt_out: 0, | ||
setter_custom_with_explicit_default: 4, | ||
setter_custom_with_strip_option: None, | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn setter_custom_setters_called() { | ||
let x: SetterCustom = SetterCustomBuilder::default() | ||
.setter_custom_by_explicit_opt_in() // set to 1 | ||
.setter_custom_shorthand() // set to 2 | ||
.setter_custom_by_explicit_opt_out(42) | ||
.setter_custom_with_explicit_default() // set to 43 | ||
.setter_custom_with_strip_option() // set to 6 | ||
.build() | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
x, | ||
SetterCustom { | ||
setter_custom_by_explicit_opt_in: 1, | ||
setter_custom_shorthand: 2, | ||
setter_custom_by_explicit_opt_out: 42, | ||
setter_custom_with_explicit_default: 43, | ||
setter_custom_with_strip_option: Some(6) | ||
} | ||
); | ||
} |
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