1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::fmt;
use crate::core::Matcher;
use crate::matchers::numbers::{BeZeroMatcher, NonZeroInt};
use super::ExpectationFormat;
/// Succeeds when the actual integer value is `0`.
///
/// When negated, this matcher converts the value to its non-zero counterpart ([`NonZeroU8`],
/// [`NonZeroI32`], etc.). Otherwise, it behaves like `equal(0)`.
///
/// # Examples
///
/// ```
/// use std::num::NonZeroU32;
/// use xpct::{expect, be_zero};
///
/// let result: NonZeroU32 = expect!(10u32)
/// .to_not(be_zero())
/// .into_inner();
/// ```
///
/// [`NonZeroU8`]: std::num::NonZeroU8
/// [`NonZeroI32`]: std::num::NonZeroI32
pub fn be_zero<'a, T>() -> Matcher<'a, T, T, T::NonZero>
where
T: fmt::Debug + NonZeroInt + 'a,
{
Matcher::transform(
BeZeroMatcher::new(),
ExpectationFormat::new("to be 0", "to not be 0"),
)
}
#[cfg(test)]
mod tests {
use super::be_zero;
use crate::expect;
#[test]
fn succeeds_when_zero() {
expect!(0).to(be_zero());
}
#[test]
fn succeeds_when_not_zero() {
expect!(10).to_not(be_zero());
}
#[test]
#[should_panic]
fn fails_when_zero() {
expect!(0).to_not(be_zero());
}
#[test]
#[should_panic]
fn fails_when_not_zero() {
expect!(10).to(be_zero());
}
}