-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from hauju/feature/option-result
Feature/option result
- Loading branch information
Showing
9 changed files
with
309 additions
and
69 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
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,36 +1,13 @@ | ||
use self::bool_assertion::BoolAssertion; | ||
use crate::assertions::string_assertion::StringAssertion; | ||
|
||
|
||
pub mod bool_assertion; | ||
pub mod string_assertion; | ||
pub mod numeric_assertion; | ||
pub mod option_assertion; | ||
pub mod result_assertion; | ||
|
||
|
||
pub trait ShouldString { | ||
type Assertion; | ||
fn should(self) -> Self::Assertion; | ||
} | ||
|
||
impl<T: AsRef<str>> ShouldString for T { | ||
type Assertion = StringAssertion<T>; | ||
|
||
fn should(self) -> StringAssertion<T>{ | ||
StringAssertion::new(self) | ||
} | ||
} | ||
|
||
|
||
pub trait ShouldBool { | ||
pub trait Should { | ||
type Assertion; | ||
fn should(self) -> Self::Assertion; | ||
} | ||
|
||
impl ShouldBool for bool { | ||
type Assertion = BoolAssertion; | ||
|
||
fn should(self) -> BoolAssertion { | ||
BoolAssertion::new(self) | ||
} | ||
} | ||
|
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,50 @@ | ||
use crate::Should; | ||
|
||
pub struct OptionAssertion<T> { | ||
value: Option<T>, | ||
} | ||
|
||
impl <T> Should for Option<T> { | ||
type Assertion = OptionAssertion<T>; | ||
|
||
fn should(self) -> OptionAssertion<T> { | ||
OptionAssertion::new(self) | ||
} | ||
} | ||
|
||
impl <T> OptionAssertion<T> { | ||
pub fn new(value: Option<T>) -> Self { | ||
OptionAssertion { value } | ||
} | ||
|
||
pub fn be_some(&self) -> &Self { | ||
assert!(self.value.is_some(), "Expected Some, but got None"); | ||
self | ||
} | ||
|
||
pub fn be_none(&self) -> &Self { | ||
assert!(self.value.is_none(), "Expected None, but got Some"); | ||
self | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rstest::*; | ||
use crate::assertions::*; | ||
|
||
#[rstest] | ||
#[case(None)] | ||
fn should_be_none(#[case] input: Option<String>) { | ||
input.should() | ||
.be_none(); | ||
} | ||
|
||
#[rstest] | ||
#[case(Some(42f64))] | ||
#[case(Some(0.0))] | ||
fn should_be_some(#[case] input: Option<f64>) { | ||
input.should() | ||
.be_some(); | ||
} | ||
} |
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,50 @@ | ||
use crate::Should; | ||
|
||
|
||
|
||
pub struct ResultAssertion<T, E> { | ||
value: Result<T, E>, | ||
} | ||
|
||
impl <T, E> Should for Result<T, E> { | ||
type Assertion = ResultAssertion<T, E>; | ||
|
||
fn should(self) -> ResultAssertion<T, E> { | ||
ResultAssertion::new(self) | ||
} | ||
} | ||
|
||
impl <T, E> ResultAssertion<T, E> { | ||
pub fn new(value: Result<T, E>) -> Self { | ||
ResultAssertion { value } | ||
} | ||
|
||
pub fn be_ok(&self) -> &Self { | ||
assert!(self.value.is_ok(), "Expected Ok, but got Err"); | ||
self | ||
} | ||
|
||
pub fn be_err(&self) -> &Self { | ||
assert!(self.value.is_err(), "Expected Err, but got Ok"); | ||
self | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rstest::*; | ||
use crate::assertions::*; | ||
|
||
|
||
#[rstest] | ||
#[case(Ok(42))] | ||
#[case(Ok(0))] | ||
fn should_be_ok(#[case] input: Result<i32, String>) { | ||
input.should() | ||
.be_ok(); | ||
} | ||
|
||
} |
Oops, something went wrong.