Skip to content

Commit

Permalink
test: add missing tests for all assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
IndecisiveCrab committed Nov 14, 2023
1 parent 63532f0 commit c1ff406
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 25 deletions.
13 changes: 11 additions & 2 deletions src/basic/try_equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ where
T: TryInto<AssertedType>,
<T as TryInto<AssertedType>>::Error: std::fmt::Debug,
{
// TODO: improve assertion here
let expected: AssertedType = expected.try_into().unwrap();
let conversion_result: Result<AssertedType, _> = expected.try_into();

implementation::assert(
conversion_result.is_ok(),
"TryInto conversion to succeed",
&conversion_result,
);

#[allow(clippy::unwrap_used)]
let expected = conversion_result.unwrap();

implementation::assert_equals(self.value, expected);
}
}
13 changes: 11 additions & 2 deletions src/basic/try_not_equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ where
T: TryInto<AssertedType>,
<T as TryInto<AssertedType>>::Error: std::fmt::Debug,
{
// TODO: improve assertion here
let expected: AssertedType = expected.try_into().unwrap();
let conversion_result: Result<AssertedType, _> = expected.try_into();

implementation::assert(
conversion_result.is_ok(),
"TryInto conversion to succeed",
&conversion_result,
);

#[allow(clippy::unwrap_used)]
let expected = conversion_result.unwrap();

implementation::assert_not_equals(self.value, expected);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
clippy::empty_structs_with_brackets,
clippy::error_impl_error,
clippy::expect_used,
clippy::unwrap_used,
clippy::panic,
clippy::todo,
clippy::try_err,
Expand Down
1 change: 1 addition & 0 deletions src/result/is_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ where
pub fn is_err(self) -> ErrAsserter<ErrValue> {
implementation::assert(self.value.is_err(), "Result to be an error", &self.value);

#[allow(clippy::unwrap_used)]
let value = self.value.err().unwrap();

ErrAsserter { value }
Expand Down
1 change: 1 addition & 0 deletions src/result/is_ok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ where
pub fn is_ok(self) -> OkAsserter<OkValue> {
implementation::assert(self.value.is_ok(), "Result to be Ok", &self.value);

#[allow(clippy::unwrap_used)]
let value = self.value.unwrap();

OkAsserter { value }
Expand Down
42 changes: 21 additions & 21 deletions tests/is_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,72 +18,72 @@ mod assert_result {
}
}

mod assert_error_as_string {
mod assert_error_equals {
use super::*;
use crate::setup::NonComparableError;
use crate::setup::{ComparableError, ConvertableError};

#[test]
fn succeeds() {
let result: Result<(), NonComparableError> =
Err(NonComparableError(String::from("Hello There")));
let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));

assert_that(result)
.is_err()
.and_error_to_string_equals(String::from("Hello There"));
.and_error_equals(ComparableError(String::from("Hello There")));
}

#[test]
fn succeeds_with_trait() {
let result: Result<(), NonComparableError> =
Err(NonComparableError(String::from("Hello There")));
fn succeeds_with_another_error_by_trait_conversion() {
let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));

assert_that(result)
.is_err()
.and_error_to_string_equals("Hello There");
.and_error_equals(ConvertableError(String::from("Hello There")));
}

#[test]
#[should_panic = "assertion failed: `(left == right)`"]
fn fails() {
let result: Result<(), NonComparableError> =
Err(NonComparableError(String::from("Hello There")));
let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));

assert_that(result)
.is_err()
.and_error_to_string_equals("yo");
.and_error_equals(ConvertableError(String::from("yo")));
}
}

mod assert_error_equals {
mod assert_error_as_string {
use super::*;
use crate::setup::{ComparableError, ConvertableError};
use crate::setup::NonComparableError;

#[test]
fn succeeds() {
let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));
let result: Result<(), NonComparableError> =
Err(NonComparableError(String::from("Hello There")));

assert_that(result)
.is_err()
.and_error_equals(ComparableError(String::from("Hello There")));
.and_error_to_string_equals(String::from("Hello There"));
}

#[test]
fn succeeds_with_another_error_by_trait_conversion() {
let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));
fn succeeds_with_trait() {
let result: Result<(), NonComparableError> =
Err(NonComparableError(String::from("Hello There")));

assert_that(result)
.is_err()
.and_error_equals(ConvertableError(String::from("Hello There")));
.and_error_to_string_equals("Hello There");
}

#[test]
#[should_panic = "assertion failed: `(left == right)`"]
fn fails() {
let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));
let result: Result<(), NonComparableError> =
Err(NonComparableError(String::from("Hello There")));

assert_that(result)
.is_err()
.and_error_equals(ConvertableError(String::from("yo")));
.and_error_to_string_equals("yo");
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/not_equals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use smoothy::assert_that;

mod succeeds {
use super::*;

#[test]
fn with_numbers() {
assert_that(12).not_equals(21);
}

#[test]
fn with_strings() {
assert_that(String::from("Yo")).not_equals("Hello There!");
}
}

mod fails {
use super::*;

#[test]
#[should_panic = "assertion failed: `(left != right)`"]
fn with_matching_values() {
assert_that(12).not_equals(12);
}
}
26 changes: 26 additions & 0 deletions tests/try_equals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use smoothy::assert_that;

mod succeeds {
use super::*;

#[test]
fn with_numbers() {
assert_that(42u8).try_into_equals(42i8);
}
}

mod fails {
use super::*;

#[test]
#[should_panic = "assertion failed:"]
fn with_conversion_error() {
assert_that(42u8).try_into_equals(-42i8);
}

#[test]
#[should_panic = "assertion failed: `(left == right)`"]
fn with_numbers() {
assert_that(42u8).try_into_equals(100i8);
}
}
26 changes: 26 additions & 0 deletions tests/try_not_equals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use smoothy::assert_that;

mod succeeds {
use super::*;

#[test]
fn with_numbers() {
assert_that(42u8).try_into_not_equals(100i8);
}
}

mod fails {
use super::*;

#[test]
#[should_panic = "assertion failed:"]
fn with_conversion_error() {
assert_that(42u8).try_into_not_equals(-100i8);
}

#[test]
#[should_panic = "assertion failed: `(left != right)`"]
fn with_numbers() {
assert_that(42u8).try_into_not_equals(42i8);
}
}

0 comments on commit c1ff406

Please sign in to comment.