Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add #[track_caller] to all assertion methods #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/assertions/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl<S: PartialEq + Debug, R> EqualityAssertion<S, R> for Subject<'_, S, (), R>
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn is_equal_to<B: Borrow<S>>(&self, expected: B) -> R {
if self.actual().eq(expected.borrow()) {
self.new_result().do_ok()
Expand All @@ -46,6 +47,8 @@ where
.do_fail()
}
}

#[track_caller]
fn is_not_equal_to<B: Borrow<S>>(&self, expected: B) -> R {
if !self.actual().ne(expected.borrow()) {
self.new_result().do_fail()
Expand Down Expand Up @@ -74,6 +77,7 @@ impl<S: PartialOrd + Debug, R> ComparableAssertion<S, R> for Subject<'_, S, (),
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn is_at_least<B: Borrow<S>>(&self, expected: B) -> R {
if self.actual().ge(expected.borrow()) {
self.new_result().do_ok()
Expand All @@ -83,6 +87,7 @@ where
}
}

#[track_caller]
fn is_at_most<B: Borrow<S>>(&self, expected: B) -> R {
if self.actual().le(expected.borrow()) {
self.new_result().do_ok()
Expand All @@ -92,6 +97,7 @@ where
}
}

#[track_caller]
fn is_greater_than<B: Borrow<S>>(&self, expected: B) -> R {
if self.actual().gt(expected.borrow()) {
self.new_result().do_ok()
Expand All @@ -101,6 +107,7 @@ where
}
}

#[track_caller]
fn is_less_than<B: Borrow<S>>(&self, expected: B) -> R {
if self.actual().lt(expected.borrow()) {
self.new_result().do_ok()
Expand Down
2 changes: 2 additions & 0 deletions src/assertions/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<R> BooleanAssertion<R> for Subject<'_, bool, (), R>
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn is_true(&self) -> R {
if *self.actual() {
self.new_result().do_ok()
Expand All @@ -46,6 +47,7 @@ where
}
}

#[track_caller]
fn is_false(&self) -> R {
if !self.actual() {
self.new_result().do_ok()
Expand Down
2 changes: 2 additions & 0 deletions src/assertions/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ where
self
}

#[track_caller]
fn is_approx_equal_to<B: Borrow<S>>(&self, expected: B) -> R {
let diff = (*self.actual() - *expected.borrow()).abs();
let tolerance: S = self.option().abs_tol + self.option().rel_tol * *expected.borrow();
Expand Down Expand Up @@ -144,6 +145,7 @@ where
)
}

#[track_caller]
fn is_approx_equal_to<B: Borrow<S>>(&self, expected: B) -> R
where
FloatTolerance<S>: Default,
Expand Down
10 changes: 10 additions & 0 deletions src/assertions/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ where
S: Iterator<Item = T> + Clone,
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn contains<B>(&self, element: B) -> R
where
B: Borrow<T>,
Expand All @@ -267,6 +268,7 @@ where
check_contains(self.new_result(), self.actual().clone(), element.borrow())
}

#[track_caller]
fn does_not_contain<B>(&self, element: B) -> R
where
B: Borrow<T>,
Expand All @@ -275,6 +277,7 @@ where
check_does_not_contain(self.new_result(), self.actual().clone(), element.borrow())
}

#[track_caller]
fn contains_exactly<EI: Iterator<Item = T> + Clone>(self, expected_iter: EI) -> R
where
T: PartialEq + Debug,
Expand All @@ -297,6 +300,7 @@ where
}
}

#[track_caller]
fn contains_exactly_in_order<EI: Iterator<Item = T> + Clone>(self, expected_iter: EI) -> R
where
T: PartialEq + Debug,
Expand Down Expand Up @@ -326,6 +330,7 @@ where
}
}

#[track_caller]
fn contains_all_of<EI: Iterator<Item = T> + Clone>(self, expected_iter: EI) -> R
where
T: PartialEq + Debug,
Expand Down Expand Up @@ -353,6 +358,7 @@ where
}
}

#[track_caller]
fn does_not_contain_any<EI: Iterator<Item = T> + Clone>(&self, elements: EI) -> R
where
T: PartialEq + Debug,
Expand Down Expand Up @@ -384,6 +390,7 @@ where
}
}

#[track_caller]
fn contains_all_of_in_order<EI: Iterator<Item = T> + Clone>(self, expected_iter: EI) -> R
where
T: PartialEq + Debug,
Expand Down Expand Up @@ -420,20 +427,23 @@ where
}
}

#[track_caller]
fn is_empty(&self) -> R
where
T: Debug,
{
check_is_empty(self.new_result(), self.actual().clone())
}

#[track_caller]
fn is_not_empty(&self) -> R
where
T: Debug,
{
check_is_not_empty(self.new_result(), self.actual().clone())
}

#[track_caller]
fn has_length(&self, length: usize) -> R
where
T: Debug,
Expand Down
10 changes: 10 additions & 0 deletions src/assertions/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl<'a, K, V, R> MapAssertion<'a, K, V, R> for Subject<'a, HashMap<K, V>, (), R
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn has_length(&self, length: usize) -> R {
self.new_subject(
&self.actual().keys().len(),
Expand All @@ -142,20 +143,23 @@ where
.is_equal_to(length)
}

#[track_caller]
fn is_empty(&self) -> R
where
K: Debug,
{
check_is_empty(self.new_result(), self.actual().keys())
}

#[track_caller]
fn is_not_empty(&self) -> R
where
K: Debug,
{
check_is_not_empty(self.new_result(), self.actual().keys())
}

#[track_caller]
fn contains_key<BK>(&self, key: BK) -> R
where
BK: Borrow<K>,
Expand All @@ -164,6 +168,7 @@ where
check_contains(self.new_result(), self.actual().keys(), &key.borrow())
}

#[track_caller]
fn does_not_contain_key<BK>(&self, key: BK) -> R
where
BK: Borrow<K>,
Expand All @@ -172,6 +177,7 @@ where
check_does_not_contain(self.new_result(), self.actual().keys(), &key.borrow())
}

#[track_caller]
fn contains_entry<BK, BV>(&self, key: BK, value: BV) -> R
where
BK: Borrow<K>,
Expand Down Expand Up @@ -214,6 +220,7 @@ where
}
}

#[track_caller]
fn does_not_contain_entry<BK, BV>(&self, key: BK, value: BV) -> R
where
BK: Borrow<K>,
Expand Down Expand Up @@ -241,6 +248,7 @@ where
}
}

#[track_caller]
fn contains_at_least<BM>(&self, expected: BM) -> R
where
BM: Borrow<HashMap<K, V>>,
Expand All @@ -264,6 +272,7 @@ where
.do_fail()
}

#[track_caller]
fn does_not_contain_any<BM>(&self, expected: BM) -> R
where
BM: Borrow<HashMap<K, V>>,
Expand All @@ -285,6 +294,7 @@ where
return self.new_result().do_ok();
}

#[track_caller]
fn contains_exactly<BM>(&self, expected: BM) -> R
where
BM: Borrow<HashMap<K, V>>,
Expand Down
3 changes: 3 additions & 0 deletions src/assertions/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl<'a, T, R> OptionAssertion<'a, T, R> for Subject<'a, Option<T>, (), R>
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn is_none(&self) -> R
where
T: PartialEq + Debug,
Expand All @@ -67,6 +68,7 @@ where
}
}

#[track_caller]
fn is_some(&self) -> R
where
T: PartialEq + Debug,
Expand All @@ -81,6 +83,7 @@ where
}
}

#[track_caller]
fn has_value<B>(&self, expected: B) -> R
where
B: Borrow<T>,
Expand Down
4 changes: 4 additions & 0 deletions src/assertions/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl<R, OK: Debug, ERR: Debug> ResultAssertion<R, OK, ERR> for Subject<'_, Resul
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn is_ok(&self) -> R {
if self.actual().is_ok() {
self.new_result().do_ok()
Expand All @@ -66,6 +67,7 @@ where
}
}

#[track_caller]
fn is_err(&self) -> R {
if self.actual().is_err() {
self.new_result().do_ok()
Expand All @@ -79,6 +81,7 @@ where
}
}

#[track_caller]
fn has_ok<B: Borrow<OK>>(&self, expected: B) -> R
where
OK: PartialEq,
Expand All @@ -98,6 +101,7 @@ where
}
}

#[track_caller]
fn has_err<B: Borrow<ERR>>(&self, expected: B) -> R
where
ERR: PartialEq,
Expand Down
5 changes: 5 additions & 0 deletions src/assertions/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl<'a, T, R> SetAssertion<'a, HashSet<T>, T, R> for Subject<'a, HashSet<T>, ()
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn has_length(&self, length: usize) -> R {
self.new_subject(
&self.actual().len(),
Expand All @@ -88,13 +89,15 @@ where
.is_equal_to(length)
}

#[track_caller]
fn is_empty(&self) -> R
where
T: Debug,
{
check_is_empty(self.new_result(), self.actual().iter())
}

#[track_caller]
fn contains<B: Borrow<T>>(&self, expected: B) -> R
where
T: PartialEq + Eq + Debug + Hash,
Expand All @@ -103,6 +106,7 @@ where
.contains(expected.borrow())
}

#[track_caller]
fn does_not_contain<B>(&self, element: B) -> R
where
B: Borrow<T>,
Expand All @@ -112,6 +116,7 @@ where
.does_not_contain(element.borrow())
}

#[track_caller]
fn does_not_contain_any<B: Borrow<Vec<T>>>(&self, elements: B) -> R
where
T: PartialEq + Debug,
Expand Down
5 changes: 5 additions & 0 deletions src/assertions/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ impl<R> StringAssertion<R> for Subject<'_, String, (), R>
where
AssertionResult: AssertionStrategy<R>,
{
#[track_caller]
fn is_same_string_to<E: Into<String>>(&self, expected: E) -> R {
let subject: Subject<String, (), R> = self.new_subject(self.actual(), None, ());
EqualityAssertion::is_equal_to(&subject, expected.into())
}

#[track_caller]
fn contains<E: Into<String>>(&self, expected: E) -> R {
let expected_str = expected.into();
if self.actual().contains(&expected_str) {
Expand All @@ -64,6 +66,7 @@ where
}
}

#[track_caller]
fn does_not_contain<E: Into<String>>(&self, value: E) -> R {
let expected_str = value.into();
if self.actual().contains(&expected_str) {
Expand All @@ -76,6 +79,7 @@ where
}
}

#[track_caller]
fn starts_with<E: Into<String>>(&self, expected: E) -> R {
let expected_str = expected.into();
if self.actual().starts_with(&expected_str) {
Expand All @@ -88,6 +92,7 @@ where
}
}

#[track_caller]
fn ends_with<E: Into<String>>(&self, expected: E) -> R {
let expected_str = expected.into();
if self.actual().ends_with(&expected_str) {
Expand Down
Loading