Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 153 additions & 127 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions assertr-derive/tests/04-equality-check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ fn main() {
data: (42, 100),
};

assert_that_ref(&foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
id: any(),
name: any(),
data: any(),
});

assert_that_ref(&foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
id: eq(1),
name: eq("bob".to_string()),
data: any(),
});

assert_that_panic_by(|| {
assert_that_ref(&foo)
foo.must()
.with_location(false)
.is_equal_to(FooAssertrEq {
.be_equal_to(FooAssertrEq {
id: eq(1),
name: eq("otto".to_string()),
data: any(),
Expand Down
4 changes: 2 additions & 2 deletions assertr-derive/tests/05-replace-field-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ fn main() {
bar: Bar { id: 42 },
};

assert_that_ref(&foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
id: any(),
bar: any(),
});

assert_that_ref(&foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
id: eq(1),
bar: eq(BarAssertrEq { id: eq(42) }),
});
Expand Down
4 changes: 2 additions & 2 deletions assertr-derive/tests/06-replace-deep-field-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ fn main() {
bars2: HashMap::new(),
};

assert_that_ref(&foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
id: any(),
bars: any(),
bars2: any(),
});

assert_that_ref(&foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
id: eq(1),
bars: eq(vec![BarAssertrEq { id: eq(42) }]),
bars2: eq(HashMap::new()),
Expand Down
2 changes: 1 addition & 1 deletion assertr-derive/tests/07-derive-impl-for-reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ fn main() {
// This must compile without errors.
// It should only compile when AssertrPartialEq
// was not only implemented for Bar, but also for &Bar!
assert_that(bars_refs).contains(BarAssertrEq { id: any() });
bars_refs.must().contain(BarAssertrEq { id: any() });
}
2 changes: 1 addition & 1 deletion assertr-derive/tests/08-default-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
field_3: 3,
};

assert_that(foo).is_equal_to(FooAssertrEq {
foo.must().be_equal_to(FooAssertrEq {
field_1: eq(1),
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion assertr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "assertr"
version = "0.3.9"
version = "0.4.0"
edition = "2024"
rust-version = "1.85.0"
authors = ["Lukas Potthast <privat@lukas-potthast.de>"]
Expand Down
29 changes: 23 additions & 6 deletions assertr/src/assertions/alloc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ pub trait BoxAssertions<'t, M: Mode> {
/// If this fails in capturing mode, a panic is raised!
fn has_type<E: 'static>(self) -> AssertThat<'t, E, M>;

/// If this fails in capturing mode, a panic is raised!
fn have_type<E: 'static>(self) -> AssertThat<'t, E, M>
where
Self: Sized,
{
self.has_type()
}

/// If this fails in capturing mode, a panic is raised!
fn has_type_ref<E: 'static>(&'t self) -> AssertThat<'t, &'t E, M>;

/// If this fails in capturing mode, a panic is raised!
fn have_type_ref<E: 'static>(&'t self) -> AssertThat<'t, &'t E, M> {
self.has_type_ref()
}
}

impl<'t, M: Mode> BoxAssertions<'t, M> for AssertThat<'t, Box<dyn Any>, M> {
Expand Down Expand Up @@ -197,15 +210,19 @@ mod tests {
fn succeeds_when_type_of_contained_value_matches_expected_type() {
let boxed_any: Box<dyn Any> = Box::new("foo");

assert_that(boxed_any).has_type::<&str>().is_equal_to("foo");
boxed_any
.must()
.have_type::<&str>()
.and()
.be_equal_to("foo");
}

#[test]
fn panics_when_type_of_contained_value_does_not_match_expected_type() {
let boxed_any: Box<dyn Any> = Box::new("foo");

assert_that_panic_by(|| {
assert_that(boxed_any)
assert_that_owned(boxed_any)
.with_location(false)
.has_type::<u32>();
})
Expand All @@ -229,7 +246,7 @@ mod tests {
fn succeeds_when_type_matches() {
let actual: Box<dyn Any> = Box::new(String::from("foo"));

assert_that(actual)
assert_that_owned(actual)
.has_type_ref::<String>()
.is_equal_to(&String::from("foo"));
}
Expand All @@ -239,7 +256,7 @@ mod tests {
let actual: Box<dyn Any> = Box::new(String::from("foo"));

assert_that_panic_by(|| {
assert_that(actual)
assert_that_owned(actual)
.with_location(false)
.has_type_ref::<u32>();
})
Expand All @@ -258,7 +275,7 @@ mod tests {
let actual: Box<dyn Any> = Box::new("foo");

assert_that_panic_by(|| {
assert_that(actual)
assert_that_owned(actual)
.with_location(false)
.has_type_ref::<u32>();
})
Expand All @@ -278,7 +295,7 @@ mod tests {
let actual: Box<dyn Any> = Box::new(Foo {});

assert_that_panic_by(|| {
assert_that(actual)
assert_that_owned(actual)
.with_location(false)
.has_type_ref::<u32>();
})
Expand Down
34 changes: 23 additions & 11 deletions assertr/src/assertions/alloc/panic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ use super::boxed::BoxAssertions;
pub trait PanicValueAssertions<'t, M: Mode> {
fn has_type<E: 'static>(self) -> AssertThat<'t, E, M>;

fn have_type<E: 'static>(self) -> AssertThat<'t, E, M>
where
Self: Sized,
{
self.has_type()
}

/// NOTE: If this fails in capturing mode, a panic is raised!
fn has_type_ref<E: 'static>(&'t self) -> AssertThat<'t, &'t E, M>;

/// NOTE: If this fails in capturing mode, a panic is raised!
fn have_type_ref<E: 'static>(&'t self) -> AssertThat<'t, &'t E, M> {
self.has_type_ref()
}
}

impl<'t, M: Mode> PanicValueAssertions<'t, M> for AssertThat<'t, PanicValue, M> {
Expand Down Expand Up @@ -72,21 +84,21 @@ mod tests {
fn succeeds_when_type_matches() {
let actual = PanicValue(Box::new(String::from("foo")));

assert_that_ref(&actual)
.has_type::<String>()
.is_equal_to(String::from("foo"));
actual.must()
.have_type::<String>()
.be_equal_to(String::from("foo"));

assert_that(actual)
.has_type::<String>()
.is_equal_to(String::from("foo"));
actual.must()
.have_type::<String>()
.be_equal_to(String::from("foo"));
}

#[test]
fn panics_when_type_does_not_match() {
let actual = PanicValue(Box::new(String::from("foo")));

assert_that_panic_by(|| {
assert_that(actual).with_location(false).has_type::<u32>();
actual.must().with_location(false).have_type::<u32>();
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
Expand All @@ -107,7 +119,7 @@ mod tests {
fn succeeds_when_type_matches() {
let actual = PanicValue(Box::new(String::from("foo")));

assert_that(actual)
assert_that!(actual)
.has_type_ref::<String>()
.is_equal_to(&String::from("foo"));
}
Expand All @@ -117,7 +129,7 @@ mod tests {
let actual = PanicValue(Box::new(String::from("foo")));

assert_that_panic_by(|| {
assert_that(actual)
assert_that!(actual)
.with_location(false)
.has_type_ref::<u32>();
})
Expand All @@ -136,7 +148,7 @@ mod tests {
let actual = PanicValue(Box::new("foo"));

assert_that_panic_by(|| {
assert_that(actual)
assert_that!(actual)
.with_location(false)
.has_type_ref::<u32>();
})
Expand All @@ -156,7 +168,7 @@ mod tests {
let actual = PanicValue(Box::new(Foo {}));

assert_that_panic_by(|| {
assert_that(actual)
assert_that!(actual)
.with_location(false)
.has_type_ref::<u32>();
})
Expand Down
Loading