xpct 0.5.1

An extensible test assertion library
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use std::fmt;
use std::marker::PhantomData;

use crate::core::{
    strings, style, Color, Format, Formatter, MatchFailure, Matcher, OutputStyle, TextColor,
    TextStyle,
};
use crate::matchers::diff::{Diff, DiffKind, DiffTag, Diffable, EqDiffMatcher};

const FORMAT_PLACEHOLDER: &str = "%s";

/// A configuration option for [`DiffStyle`].
///
/// This represents a generic value that differs when an element is added, removed, or remains
/// unchanged.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffSegmentStyle<T> {
    /// The element is in the actual value but not the expected value.
    pub insert: T,

    /// The element is in the expected value but not the actual value.
    pub delete: T,

    /// The element is the same in both the actual and expected values.
    pub equal: T,
}

/// The styling for string diffs when using [`DiffFormat`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct StringDiffStyle {
    /// The text styling to use for substrings that are added or removed.
    pub style: DiffSegmentStyle<OutputStyle>,

    /// Format strings used to wrap additions and deletions.
    ///
    /// Out of the box, string diffs are impossible to read with text styling disabled. Since they
    /// diff by character/grapheme instead of by line, there's nowhere to put a gutter like `git
    /// diff` has. You can add strings before and after additions and deletions to make it possible
    /// to see the changes without text styling.
    ///
    /// Each format string replaces the first occurrence of `%s` with the added or removed text.
    /// Note that if the format string doesn't contain a `%s`, that text will not be shown.
    ///
    /// # Examples
    ///
    /// ```
    /// use xpct::format::diff::DiffSegmentStyle;
    ///
    /// let style = DiffSegmentStyle {
    ///     insert: String::from("+(%s)"),
    ///     delete: String::from("-(%s)"),
    ///     equal: String::from("%s"),
    /// };
    /// ```
    pub format: DiffSegmentStyle<String>,
}

impl StringDiffStyle {
    /// The provided styling, used by [`eq_diff`].
    ///
    /// You can use this as a starting point for customizing the provided styling. The value
    /// returned by this method may change and is not part of the public API.
    pub fn provided() -> Self {
        Self {
            style: DiffSegmentStyle {
                insert: OutputStyle {
                    style: TextStyle::BOLD | TextStyle::REVERSED,
                    color: TextColor {
                        fg: Some(Color::BrightGreen),
                        bg: None,
                    },
                },
                delete: OutputStyle {
                    style: TextStyle::BOLD | TextStyle::UNDERLINE,
                    color: TextColor {
                        fg: Some(Color::BrightRed),
                        bg: None,
                    },
                },
                equal: OutputStyle::default(),
            },
            format: DiffSegmentStyle {
                insert: String::from("%s"),
                delete: String::from("%s"),
                equal: String::from("%s"),
            },
        }
    }
}

impl Default for StringDiffStyle {
    fn default() -> Self {
        Self {
            style: DiffSegmentStyle {
                insert: OutputStyle::default(),
                delete: OutputStyle::default(),
                equal: OutputStyle::default(),
            },
            format: DiffSegmentStyle {
                insert: String::from("%s"),
                delete: String::from("%s"),
                equal: String::from("%s"),
            },
        }
    }
}

/// The styling for diffs of collections when using [`DiffFormat`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CollectionDiffStyle {
    /// The text styling to use for each element in the collection.
    ///
    /// You can change the styling of elements to represent values that have been added or removed.
    pub element_style: DiffSegmentStyle<OutputStyle>,

    /// The characters to show in the gutter to represent changes.
    ///
    /// The diff output shows '+' and '-' characters in the gutter, like `git diff`. You can
    /// customize which characters are used.
    pub gutter_char: DiffSegmentStyle<char>,

    /// The text styling to use for the characters in the gutter.
    pub gutter_style: DiffSegmentStyle<OutputStyle>,
}

impl CollectionDiffStyle {
    /// The provided styling, used by [`eq_diff`].
    ///
    /// You can use this as a starting point for customizing the provided styling. The value
    /// returned by this method may change and is not part of the public API.
    pub fn provided() -> Self {
        Self {
            element_style: DiffSegmentStyle {
                insert: OutputStyle {
                    style: TextStyle::BOLD,
                    color: TextColor {
                        fg: Some(Color::BrightGreen),
                        bg: None,
                    },
                },
                delete: OutputStyle {
                    style: TextStyle::BOLD,
                    color: TextColor {
                        fg: Some(Color::BrightRed),
                        bg: None,
                    },
                },
                equal: OutputStyle::default(),
            },
            gutter_char: DiffSegmentStyle {
                insert: '+',
                delete: '-',
                equal: ' ',
            },
            gutter_style: DiffSegmentStyle {
                insert: OutputStyle {
                    style: TextStyle::empty(),
                    color: TextColor {
                        fg: Some(Color::BrightGreen),
                        bg: None,
                    },
                },
                delete: OutputStyle {
                    style: TextStyle::empty(),
                    color: TextColor {
                        fg: Some(Color::BrightRed),
                        bg: None,
                    },
                },
                equal: OutputStyle::default(),
            },
        }
    }
}

impl Default for CollectionDiffStyle {
    fn default() -> Self {
        Self {
            element_style: DiffSegmentStyle {
                insert: OutputStyle::default(),
                delete: OutputStyle::default(),
                equal: OutputStyle::default(),
            },
            gutter_char: DiffSegmentStyle {
                insert: ' ',
                delete: ' ',
                equal: ' ',
            },
            gutter_style: DiffSegmentStyle {
                insert: OutputStyle::default(),
                delete: OutputStyle::default(),
                equal: OutputStyle::default(),
            },
        }
    }
}

/// The style sheet for [`DiffFormat`].
///
/// If the provided text styling for diffs is inaccessible for you, or you prefer to have text
/// styling disabled, you can use this style sheet to customize the styling of diffs.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DiffStyle {
    /// The styling for string diffs.
    pub string: StringDiffStyle,

    /// The styling for diffs of collections.
    pub collection: CollectionDiffStyle,
}

impl DiffStyle {
    /// The provided styling, used by [`eq_diff`].
    ///
    /// You can use this as a starting point for customizing the provided styling. The value
    /// returned by this method may change and is not part of the public API.
    pub fn provided() -> Self {
        Self {
            string: StringDiffStyle::provided(),
            collection: CollectionDiffStyle::provided(),
        }
    }
}

/// A formatter for [`Diff`] values.
///
/// [`Diff`]: crate::matchers::diff::Diff
#[derive(Debug)]
pub struct DiffFormat<Actual, Expected> {
    style: DiffStyle,
    marker: PhantomData<(Actual, Expected)>,
}

impl<Actual, Expected> DiffFormat<Actual, Expected> {
    /// Create a new [`DiffFormat`] from the given style sheet.
    pub fn new(style: DiffStyle) -> Self {
        Self {
            style,
            marker: PhantomData,
        }
    }
}

impl<Actual, Expected> Format for DiffFormat<Actual, Expected>
where
    Actual: fmt::Debug,
    Expected: Diffable<Actual> + fmt::Debug,
{
    type Value = MatchFailure<Diff>;

    fn fmt(&self, f: &mut Formatter, value: Self::Value) -> crate::Result<()> {
        let diff = value.unwrap();

        f.set_style(style::important());

        if value.is_pos() {
            f.write_str("Expected these to be equal:\n");
        } else {
            f.write_str("Expected these to not be equal:\n");
        }

        f.reset_style();

        match Expected::KIND {
            DiffKind::String => {
                f.indented(style::indent(1), |f| {
                    for segment in diff {
                        let (format, style) = match segment.tag {
                            DiffTag::Insert => (
                                self.style.string.format.insert.clone(),
                                self.style.string.style.insert.clone(),
                            ),
                            DiffTag::Delete => (
                                self.style.string.format.delete.clone(),
                                self.style.string.style.delete.clone(),
                            ),
                            DiffTag::Equal => (
                                self.style.string.format.equal.clone(),
                                self.style.string.style.equal.clone(),
                            ),
                        };

                        let formatted_segment =
                            format.replacen(FORMAT_PLACEHOLDER, &segment.value, 1);

                        f.set_style(style);
                        f.write_str(&formatted_segment);
                    }

                    Ok(())
                })?;

                Ok(())
            }
            DiffKind::Slice | DiffKind::Set | DiffKind::Map => {
                f.indented(style::indent(1), |f| {
                    match Expected::KIND {
                        DiffKind::Slice => f.write_char('['),
                        DiffKind::Set | DiffKind::Map => f.write_char('{'),
                        _ => unreachable!(),
                    };

                    f.write_char('\n');

                    for segment in diff {
                        let (gutter, gutter_style, element_style) = match segment.tag {
                            DiffTag::Insert => (
                                self.style.collection.gutter_char.insert,
                                self.style.collection.gutter_style.insert.clone(),
                                self.style.collection.element_style.insert.clone(),
                            ),
                            DiffTag::Delete => (
                                self.style.collection.gutter_char.delete,
                                self.style.collection.gutter_style.delete.clone(),
                                self.style.collection.element_style.delete.clone(),
                            ),
                            DiffTag::Equal => (
                                self.style.collection.gutter_char.equal,
                                self.style.collection.gutter_style.equal.clone(),
                                self.style.collection.element_style.equal.clone(),
                            ),
                        };

                        f.set_style(gutter_style);
                        f.write_char(' ');
                        f.write_char(gutter);
                        f.reset_style();

                        // Leave room for the gutter char.
                        f.write_str(strings::whitespace(style::indent_len(1) as usize - 2));

                        f.indented_hanging(style::indent(1), |f| {
                            f.set_style(element_style);
                            f.write_str(&segment.value);
                            f.reset_style();

                            Ok(())
                        })?;

                        f.write_char(',');
                        f.write_char('\n');
                    }

                    match Expected::KIND {
                        DiffKind::Slice => f.write_char(']'),
                        DiffKind::Set | DiffKind::Map => f.write_char('}'),
                        _ => unreachable!(),
                    };

                    Ok(())
                })?;

                Ok(())
            }
            DiffKind::Custom(name) => Err(crate::Error::msg(format!(
                "this is not a supported diffable kind: {name}",
            ))),
        }
    }
}

/// Succeeds when the actual value equals the expected value and shows a diff otherwise.
///
/// This matcher is functionally identical to [`equal`], except it shows a diff if the values are
/// not equal. You can use this matcher with any type that implements [`Diffable`], and you can
/// implement [`Diffable`] for your own types.
///
/// Out of the box, you can diff strings, slices, sets, and maps.
///
/// # Examples
///
/// ```
/// use xpct::{expect, eq_diff};
///
/// expect!("diffing strings").to(eq_diff("diffing strings"));
/// ```
///
/// ```
/// use xpct::{expect, eq_diff};
///
/// expect!(["slices", "too"]).to(eq_diff(["slices", "too"]));
/// ```
///
/// # Custom Styling
///
/// This matcher relies on text styling to distinguish added and removed values in string diffs. If
/// the provided text styling for diffs is inaccessible for you, or you prefer to have text styling
/// disabled, you can override the provided styling.
///
/// To do this, write a custom style sheet using [`DiffStyle`] and write your own matcher function
/// that calls [`EqDiffMatcher`].
///
/// This example makes string diffs readable without text styling:
///
/// ```
/// use std::fmt;
///
/// use xpct::core::Matcher;
/// use xpct::format::diff::{DiffFormat, DiffSegmentStyle, DiffStyle};
/// use xpct::matchers::diff::{Diffable, EqDiffMatcher};
///
/// pub fn eq_diff<'a, Actual, Expected>(expected: Expected) -> Matcher<'a, Actual, Actual>
/// where
///     Actual: fmt::Debug + PartialEq<Expected> + Eq + 'a,
///     Expected: Diffable<Actual> + fmt::Debug + 'a,
/// {
///     let mut custom_style = DiffStyle::provided();
///
///     custom_style.string.format = DiffSegmentStyle {
///         insert: String::from("+(%s)"),
///         delete: String::from("-(%s)"),
///         equal: String::from("%s"),
///     };
///
///     Matcher::new(
///         EqDiffMatcher::new(expected),
///         DiffFormat::<Actual, Expected>::new(custom_style),
///     )
/// }
/// ```
///
/// [`equal`]: crate::equal
/// [`Diffable`]: crate::matchers::diff::Diffable
/// [`EqDiffMatcher`]: crate::matchers::diff::EqDiffMatcher
pub fn eq_diff<'a, Actual, Expected>(expected: Expected) -> Matcher<'a, Actual, Actual>
where
    Actual: fmt::Debug + PartialEq<Expected> + Eq + 'a,
    Expected: fmt::Debug + Diffable<Actual> + 'a,
{
    Matcher::new(
        EqDiffMatcher::new(expected),
        DiffFormat::<Actual, Expected>::new(DiffStyle::provided()),
    )
}

#[cfg(test)]
mod tests {
    use super::eq_diff;
    use crate::expect;

    #[test]
    fn succeeds_when_equal() {
        expect!("some string").to(eq_diff("some string"));
        expect!(["some", "slice"]).to(eq_diff(["some", "slice"]));
    }

    #[test]
    fn succeeds_when_not_equal() {
        expect!("some string").to_not(eq_diff("a different string"));
        expect!(["some", "slice"]).to_not(eq_diff(["different", "slice"]));
    }

    #[test]
    #[should_panic]
    fn fails_when_equal() {
        expect!("some string").to_not(eq_diff("some string"));
        expect!(["some", "slice"]).to_not(eq_diff(["some", "slice"]));
    }

    #[test]
    #[should_panic]
    fn fails_when_not_equal() {
        expect!("some string").to(eq_diff("a different string"));
        expect!(["some", "slice"]).to(eq_diff(["different", "slice"]));
    }
}