behave 0.9.1

BDD testing framework with expressive expect! matchers and a zero-keyword DSL.
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
//! Match error type for failed expectations.

use std::fmt;

/// Error returned when an expectation matcher fails.
///
/// Contains structured information about what was expected vs what was found.
/// The [`Display`](fmt::Display) output uses a three-line format:
///
/// ```text
/// expect!(expression)
///   actual: <what was found>
/// expected: <what was expected>
/// ```
///
/// When negated, the expected line reads `expected: not <description>`.
///
/// When the `color` feature is enabled, actual values appear in red and
/// expected values in green. Multiline values get a line-by-line diff
/// with `+`/`-` markers. The `NO_COLOR` environment variable disables
/// ANSI codes while preserving the diff format.
///
/// # Examples
///
/// ```
/// use behave::MatchError;
///
/// let err = MatchError::new(
///     "score".to_string(),
///     "to equal 100".to_string(),
///     "42".to_string(),
///     false,
/// );
/// let msg = err.to_string();
/// assert!(msg.contains("expect!(score)"));
/// assert!(msg.contains("42"));
/// assert!(msg.contains("to equal 100"));
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct MatchError {
    /// The stringified expression that was tested.
    pub expression: String,
    /// What the matcher expected to find.
    pub expected: String,
    /// What the matcher actually found.
    pub actual: String,
    /// Whether the expectation was negated with `.negate()`.
    pub negated: bool,
    /// Source file where the expectation was created.
    pub file: Option<&'static str>,
    /// Source line where the expectation was created.
    pub line: Option<u32>,
}

impl MatchError {
    /// Creates a new match error with the given details.
    ///
    /// Values for `actual` and `expected` are automatically truncated
    /// when they exceed the internal 10 KB limit.
    ///
    /// # Examples
    ///
    /// ```
    /// use behave::MatchError;
    ///
    /// let err = MatchError::new(
    ///     "x".to_string(),
    ///     "true".to_string(),
    ///     "false".to_string(),
    ///     false,
    /// );
    /// assert_eq!(err.expression, "x");
    /// ```
    #[allow(clippy::needless_pass_by_value)]
    pub fn new(expression: String, expected: String, actual: String, negated: bool) -> Self {
        Self {
            expression,
            expected: truncate_value(&expected, TRUNCATE_MAX),
            actual: truncate_value(&actual, TRUNCATE_MAX),
            negated,
            file: None,
            line: None,
        }
    }

    /// Returns a new error with source location attached.
    ///
    /// # Examples
    ///
    /// ```
    /// use behave::MatchError;
    ///
    /// let err = MatchError::new(
    ///     "x".to_string(),
    ///     "true".to_string(),
    ///     "false".to_string(),
    ///     false,
    /// ).with_location(Some("tests/my_test.rs"), Some(42));
    /// assert_eq!(err.file, Some("tests/my_test.rs"));
    /// ```
    #[must_use]
    pub const fn with_location(mut self, file: Option<&'static str>, line: Option<u32>) -> Self {
        self.file = file;
        self.line = line;
        self
    }
}

impl fmt::Display for MatchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        #[cfg(feature = "color")]
        {
            fmt_enhanced(f, self)
        }
        #[cfg(not(feature = "color"))]
        {
            fmt_plain(f, self)
        }
    }
}

/// Default truncation limit in bytes (10 KB).
const TRUNCATE_MAX: usize = 10_240;

/// Truncates a string to at most `max` bytes, appending a suffix with
/// the original size when truncation occurs.
///
/// Uses [`str::is_char_boundary`] to avoid splitting a multi-byte UTF-8
/// character.
fn truncate_value(s: &str, max: usize) -> String {
    if s.len() <= max {
        return s.to_string();
    }
    let total = s.len();
    let mut end = max;
    while !s.is_char_boundary(end) {
        end -= 1;
    }
    format!(
        "{} [truncated at {}KB, total {total} bytes]",
        &s[..end],
        max / 1024
    )
}

#[cfg(not(feature = "color"))]
fn fmt_plain(f: &mut fmt::Formatter<'_>, err: &MatchError) -> fmt::Result {
    let negation = if err.negated { "not " } else { "" };
    write!(
        f,
        "expect!({})\n  actual: {}\nexpected: {}{}",
        err.expression, err.actual, negation, err.expected
    )?;
    fmt_location(f, err)
}

fn fmt_location(f: &mut fmt::Formatter<'_>, err: &MatchError) -> fmt::Result {
    if let (Some(file), Some(line)) = (err.file, err.line) {
        write!(f, "\n      at: {file}:{line}")?;
    }
    Ok(())
}

// --- color feature: structured diff with optional ANSI codes ---

#[cfg(feature = "color")]
mod ansi {
    pub(super) const RED: &str = "\x1b[31m";
    pub(super) const GREEN: &str = "\x1b[32m";
    pub(super) const RESET: &str = "\x1b[0m";
}

/// Returns `(red, green, reset)` ANSI codes, or empty strings when
/// color is disabled.
#[cfg(feature = "color")]
const fn color_codes(colorize: bool) -> (&'static str, &'static str, &'static str) {
    if colorize {
        (ansi::RED, ansi::GREEN, ansi::RESET)
    } else {
        ("", "", "")
    }
}

/// Checks the `NO_COLOR` environment variable per <https://no-color.org/>.
///
/// Returns `true` when ANSI codes should be emitted.
#[cfg(feature = "color")]
fn should_colorize() -> bool {
    std::env::var("NO_COLOR").map_or(true, |v| v.is_empty())
}

#[cfg(feature = "color")]
fn is_multiline(actual: &str, expected: &str) -> bool {
    actual.contains('\n') || expected.contains('\n')
}

#[cfg(feature = "color")]
fn fmt_enhanced(f: &mut fmt::Formatter<'_>, err: &MatchError) -> fmt::Result {
    let colorize = should_colorize();
    if err.negated || !is_multiline(&err.actual, &err.expected) {
        fmt_single_line(f, err, &err.actual, &err.expected, colorize)?;
    } else {
        fmt_diff_header(f, err, colorize)?;
        fmt_diff_body(f, &err.actual, &err.expected, colorize)?;
    }
    fmt_location(f, err)
}

#[cfg(feature = "color")]
fn fmt_single_line(
    f: &mut fmt::Formatter<'_>,
    err: &MatchError,
    actual: &str,
    expected: &str,
    colorize: bool,
) -> fmt::Result {
    let (red, green, reset) = color_codes(colorize);
    let negation = if err.negated { "not " } else { "" };
    write!(
        f,
        "expect!({})\n  actual: {red}{}{reset}\nexpected: {}{green}{}{reset}",
        err.expression, actual, negation, expected,
    )
}

#[cfg(feature = "color")]
fn fmt_diff_header(f: &mut fmt::Formatter<'_>, err: &MatchError, colorize: bool) -> fmt::Result {
    let (red, green, reset) = color_codes(colorize);
    writeln!(f, "expect!({})", err.expression)?;
    writeln!(f, "{red}--- actual{reset}")?;
    write!(f, "{green}+++ expected{reset}")
}

#[cfg(feature = "color")]
fn fmt_diff_body(
    f: &mut fmt::Formatter<'_>,
    actual: &str,
    expected: &str,
    colorize: bool,
) -> fmt::Result {
    let (red, green, reset) = color_codes(colorize);
    let diff = similar::TextDiff::from_lines(actual, expected);
    for change in diff.iter_all_changes() {
        let is_changed = match change.tag() {
            similar::ChangeTag::Delete => {
                write!(f, "\n{red}-")?;
                true
            }
            similar::ChangeTag::Insert => {
                write!(f, "\n{green}+")?;
                true
            }
            similar::ChangeTag::Equal => {
                write!(f, "\n ")?;
                false
            }
        };
        write!(f, "{}", change.value().trim_end_matches('\n'))?;
        if is_changed {
            write!(f, "{reset}")?;
        }
        if change.missing_newline() {
            write!(f, "\n\\ No newline at end")?;
        }
    }
    Ok(())
}

impl std::error::Error for MatchError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_stores_fields() {
        let err = MatchError::new(
            "x + 1".to_string(),
            "42".to_string(),
            "99".to_string(),
            false,
        );
        assert_eq!(err.expression, "x + 1");
        assert_eq!(err.expected, "42");
        assert_eq!(err.actual, "99");
        assert!(!err.negated);
    }

    #[cfg(not(feature = "color"))]
    #[test]
    fn display_normal() {
        let err = MatchError::new("val".to_string(), "42".to_string(), "99".to_string(), false);
        let msg = err.to_string();
        assert!(msg.contains("expect!(val)"));
        assert!(msg.contains("actual: 99"));
        assert!(msg.contains("expected: 42"));
        assert!(!msg.contains("not"));
    }

    #[cfg(not(feature = "color"))]
    #[test]
    fn display_negated() {
        let err = MatchError::new("val".to_string(), "42".to_string(), "42".to_string(), true);
        let msg = err.to_string();
        assert!(msg.contains("expected: not 42"));
    }

    #[test]
    fn error_source_is_none() {
        let err = MatchError::new("x".to_string(), "a".to_string(), "b".to_string(), false);
        assert!(std::error::Error::source(&err).is_none());
    }

    // --- truncate_value ---

    #[test]
    fn truncate_under_limit() {
        let s = "short";
        assert_eq!(truncate_value(s, 100), "short");
    }

    #[test]
    fn truncate_at_limit() {
        let s = "exact";
        assert_eq!(truncate_value(s, 5), "exact");
    }

    #[test]
    fn truncate_over_limit() {
        let s = "hello world";
        let result = truncate_value(s, 5);
        assert!(result.starts_with("hello"));
        assert!(result.contains("[truncated at 0KB, total 11 bytes]"));
    }

    #[test]
    fn truncate_multibyte_boundary() {
        // Each emoji is 4 bytes. With a limit of 5, we can't fit a full
        // second emoji, so truncation should back up to byte 4.
        let s = "\u{1F600}\u{1F601}\u{1F602}"; // 12 bytes total
        let result = truncate_value(s, 5);
        assert!(result.starts_with('\u{1F600}'));
        assert!(result.contains("[truncated at 0KB, total 12 bytes]"));
    }

    #[test]
    fn truncate_suffix_derives_from_limit() {
        let s = "a".repeat(TRUNCATE_MAX + 100);
        let result = truncate_value(&s, TRUNCATE_MAX);
        assert!(result.contains("[truncated at 10KB,"));
    }

    #[test]
    fn new_truncates_large_values() {
        let long = "x".repeat(TRUNCATE_MAX + 500);
        let err = MatchError::new("e".to_string(), long.clone(), long, false);
        assert!(err.actual.len() < TRUNCATE_MAX + 100);
        assert!(err.expected.len() < TRUNCATE_MAX + 100);
        assert!(err.actual.contains("[truncated"));
        assert!(err.expected.contains("[truncated"));
    }

    #[test]
    fn with_location_sets_fields() {
        let err = MatchError::new("x".to_string(), "a".to_string(), "b".to_string(), false)
            .with_location(Some("test.rs"), Some(42));
        assert_eq!(err.file, Some("test.rs"));
        assert_eq!(err.line, Some(42));
    }

    #[cfg(not(feature = "color"))]
    #[test]
    fn display_shows_location() {
        let err = MatchError::new("x".to_string(), "a".to_string(), "b".to_string(), false)
            .with_location(Some("test.rs"), Some(42));
        let msg = err.to_string();
        assert!(msg.contains("at: test.rs:42"));
    }

    #[cfg(feature = "color")]
    mod color_tests {
        use super::*;

        // Note: these tests verify ANSI output and assume NO_COLOR is not set.
        // Run with `NO_COLOR=` (unset) to ensure they pass.

        #[test]
        fn single_line_has_ansi_codes() {
            let err = MatchError::new("val".to_string(), "42".to_string(), "99".to_string(), false);
            let msg = err.to_string();
            // Structural content always present
            assert!(msg.contains("expect!(val)"));
            assert!(msg.contains("actual:"));
            assert!(msg.contains("99"));
            assert!(msg.contains("expected:"));
            assert!(msg.contains("42"));
            // ANSI codes present when NO_COLOR is not set
            if should_colorize() {
                assert!(msg.contains("\x1b[31m99\x1b[0m"));
                assert!(msg.contains("\x1b[32m42\x1b[0m"));
            }
        }

        #[test]
        fn negated_uses_single_line_format() {
            let err = MatchError::new("val".to_string(), "42".to_string(), "42".to_string(), true);
            let msg = err.to_string();
            assert!(msg.contains("expected: not"));
            if should_colorize() {
                assert!(msg.contains("\x1b[31m"));
            }
        }

        #[test]
        fn multiline_shows_diff_markers() {
            let err = MatchError::new(
                "text".to_string(),
                "line1\nline2\n".to_string(),
                "line1\nchanged\n".to_string(),
                false,
            );
            let msg = err.to_string();
            // Structural diff markers always present
            assert!(msg.contains("--- actual"));
            assert!(msg.contains("+++ expected"));
            assert!(msg.contains("-changed"));
            assert!(msg.contains("+line2"));
            assert!(msg.contains(" line1"));
        }

        #[test]
        fn multiline_equal_lines_have_no_color() {
            let err = MatchError::new(
                "text".to_string(),
                "same\ndiff_expected\n".to_string(),
                "same\ndiff_actual\n".to_string(),
                false,
            );
            let msg = err.to_string();
            // Equal lines should appear with space prefix, no ANSI
            assert!(msg.contains("\n same"));
        }
    }
}