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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*!
# Writing Custom Matchers
How to write custom matchers for your tests.
[↩︎ Back to User Docs](crate::docs)
If none of the provided matchers suit your needs, xpct allows you to write
custom matchers. There are a few ways to do this. In increasing order of
complexity and flexibility, you can:
1. Compose existing matchers. This is the simplest approach, but doesn't let you
customize the formatting of the failure output.
2. Implement [`Match`]. This lets you customize the formatting of the failure
output.
3. Implement [`TransformMatch`]. This is like [`Match`], but additionally allows
you to write matchers that transform values (like the [`be_some`] and
[`be_ok`] matchers do).
## Composing existing matchers
The simplest way to make custom matchers is to just compose existing matchers.
The combinator matchers [`each`], [`any`], and [`all`] are useful for this.
```
use std::fmt;
use xpct::{each, be_lt, be_gt};
use xpct::core::Matcher;
pub fn be_between<'a, Actual, Low, High>(
low: &'a Low,
high: &'a High,
) -> Matcher<'a, Actual, Actual>
where
Actual: PartialOrd<Low> + PartialOrd<High> + fmt::Debug + 'a,
Low: fmt::Debug,
High: fmt::Debug,
{
each(move |ctx| {
ctx.borrow().to(be_gt(low)).to(be_lt(high));
})
}
```
## Implementing `Match`
The next simplest way is to implement the [`Match`] trait. This is how many of
the provided matchers are implemented. Here's an implementation of the [`equal`]
matcher.
```
use xpct::core::Match;
use xpct::matchers::Mismatch;
pub struct EqualMatcher<Expected> {
expected: Expected,
}
impl<Expected> EqualMatcher<Expected> {
pub fn new(expected: Expected) -> Self {
Self { expected }
}
}
impl<Expected, Actual> Match<Actual> for EqualMatcher<Expected>
where
Actual: PartialEq<Expected> + Eq,
{
type Fail = Mismatch<Expected, Actual>;
fn matches(&mut self, actual: &Actual) -> xpct::Result<bool> {
Ok(actual == &self.expected)
}
fn fail(self, actual: Actual) -> Self::Fail {
Mismatch {
actual,
expected: self.expected,
}
}
}
```
Now let's make a function to call this matcher ergonomically from tests!
Basically, we just need to write a function which returns a [`Matcher`].
To make `EqualMatcher` into a `Matcher`, you just need to wrap it with
[`Matcher::new`]. This method also accepts the formatter which is used to format
the output. Thankfully, you don't need to write the formatting logic yourself to
get pretty output! Because our matcher returns a [`Mismatch`] when it fails, we
can use any formatter which accepts a [`Mismatch`], like the provided
[`MismatchFormat`].
```
# use xpct::matchers::equal::EqualMatcher;
use std::fmt;
use xpct::expect;
use xpct::core::Matcher;
use xpct::format::MismatchFormat;
pub fn equal<'a, Actual, Expected>(expected: Expected) -> Matcher<'a, Actual, Actual>
where
Actual: fmt::Debug + PartialEq<Expected> + Eq + 'a,
Expected: fmt::Debug + 'a,
{
Matcher::new(
EqualMatcher::new(expected),
MismatchFormat::new("to equal", "to not equal"),
)
}
```
What if we wanted to make a matcher which is the negated version of
`EqualMatcher`, like `not_equal`? For a matcher created by implementing
[`Match`], we can call [`Matcher::neg`] to negate it.
```
# use xpct::matchers::equal::EqualMatcher;
use std::fmt;
use xpct::expect;
use xpct::core::Matcher;
use xpct::format::MismatchFormat;
pub fn not_equal<'a, Actual, Expected>(expected: Expected) -> Matcher<'a, Actual, Actual>
where
Actual: fmt::Debug + PartialEq<Expected> + Eq + 'a,
Expected: fmt::Debug + 'a,
{
Matcher::neg(
EqualMatcher::new(expected),
// Remember that we need to flip these cases, because `actual !=
// expected` is now the *positive* case and `actual == expected` is now
// the *negative* case.
MismatchFormat::new("to not equal", "to equal"),
)
}
expect!("disco").to(not_equal("not disco"));
```
## Implementing `TransformMatch`
The major limitation of [`Match`] is that it always returns the same value that
was passed in. If you need it to transform the value like the [`be_some`] and
[`be_ok`] matchers do, you can implement the [`TransformMatch`] trait.
```
use std::marker::PhantomData;
use xpct::core::{Matcher, TransformMatch, MatchOutcome};
use xpct::matchers::Expectation;
pub struct BeOkMatcher<T, E> {
// Matchers created by implementing `TransformMatch` will often need to use
// `PhantomData` so they know their input and output types.
marker: PhantomData<(T, E)>,
}
impl<T, E> BeOkMatcher<T, E> {
pub fn new() -> Self {
Self {
marker: PhantomData,
}
}
}
impl<T, E> TransformMatch for BeOkMatcher<T, E> {
// The type the matcher accepts.
type In = Result<T, E>;
// In the positive case, this should return the `Ok` value.
type PosOut = T;
// In the negative case, this should return the `Err` value.
type NegOut = E;
// We use the `Expectation` type here to include the actual value in the
// failure output.
type PosFail = Expectation<Result<T, E>>;
type NegFail = Expectation<Result<T, E>>;
fn match_pos(
self,
actual: Self::In,
) -> xpct::Result<MatchOutcome<Self::PosOut, Self::PosFail>> {
match actual {
Ok(value) => Ok(MatchOutcome::Success(value)),
Err(err) => Ok(MatchOutcome::Fail(Expectation { actual: Err(err) })),
}
}
fn match_neg(
self,
actual: Self::In,
) -> xpct::Result<MatchOutcome<Self::NegOut, Self::NegFail>> {
match actual {
Ok(value) => Ok(MatchOutcome::Fail(Expectation { actual: Ok(value) })),
Err(error) => Ok(MatchOutcome::Success(error)),
}
}
}
```
You'll see the terms "pos" and "neg", short for *positive* and *negative*,
throughout the API. These refer to whether a matcher is negated (negative)
or not negated (positive).
If a matcher is negated (the negative case), it means that we're expecting it to
fail. If a matcher is *not* negated (the positive case), it means we're
expecting it to succeed.
Now let's make some functions for invoking our matcher.
```
use std::fmt;
# use xpct::matchers::result::BeOkMatcher;
use xpct::core::{Matcher, NegFormat};
use xpct::format::ExpectationFormat;
// `ExpectationFormat` is a simple formatter that just returns the actual value
// and a static message.
fn result_format<T>() -> ExpectationFormat<T> {
ExpectationFormat::new("to be Ok(_)", "to be Err(_)")
}
pub fn be_ok<'a, T, E>() -> Matcher<'a, Result<T, E>, T, E>
where
T: fmt::Debug + 'a,
E: fmt::Debug + 'a,
{
// For matchers implemented with `TransformMatch`, you use
// `Matcher::transform`.
Matcher::transform(BeOkMatcher::new(), result_format())
}
pub fn be_err<'a, T, E>() -> Matcher<'a, Result<T, E>, E, T>
where
T: fmt::Debug + 'a,
E: fmt::Debug + 'a,
{
// You can use `Matcher::transform_neg` to negate a matcher created by
// implementing `TransformMatch`. You can use `NegFormat` to negate the
// formatter.
Matcher::transform_neg(BeOkMatcher::new(), NegFormat(result_format()))
}
```
[`TransformMatch`]: crate::core::TransformMatch
[`each`]: crate::each
[`any`]: crate::any
[`all`]: crate::all
[`be_some`]: crate::be_some
[`be_ok`]: crate::be_ok
[`Match`]: crate::core::Match
[`equal`]: crate::equal
[`Matcher`]: crate::core::Matcher
[`Matcher::transform`]: crate::core::Matcher::transform
[`Mismatch`]: crate::matchers::Mismatch
[`MismatchFormat`]: crate::format::MismatchFormat
[`Matcher::new`]: crate::core::Matcher::new
[`Matcher::neg`]: crate::core::Matcher::neg
*/