archunit/testing/
test_result_options.rs

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
use super::ColorChoice;

/// Presentation and expectation options for [`crate::ResultFactory`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct TestResultOptions {
    color: ColorChoice,
    expected_to_pass: bool,
}

impl TestResultOptions {
    /// Creates auto-colored output that expects the architecture rule to pass.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            color: ColorChoice::Auto,
            expected_to_pass: true,
        }
    }

    /// Returns the configured ANSI color policy.
    #[must_use]
    pub const fn color(self) -> ColorChoice {
        self.color
    }

    /// Returns whether an empty violation list is expected.
    #[must_use]
    pub const fn expects_to_pass(self) -> bool {
        self.expected_to_pass
    }

    /// Selects the ANSI color policy.
    #[must_use]
    pub const fn with_color(mut self, color: ColorChoice) -> Self {
        self.color = color;
        self
    }

    /// Controls whether the architecture rule is expected to have no violations.
    #[must_use]
    pub const fn with_expected_to_pass(mut self, expected: bool) -> Self {
        self.expected_to_pass = expected;
        self
    }
}

impl Default for TestResultOptions {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::TestResultOptions;
    use crate::testing::ColorChoice;

    #[test]
    fn defaults_to_auto_color_and_a_passing_expectation() {
        let options = TestResultOptions::default();

        assert_eq!(options.color(), ColorChoice::Auto);
        assert!(options.expects_to_pass());
    }

    #[test]
    fn consuming_builders_are_branchable_values() {
        let base = TestResultOptions::new().with_color(ColorChoice::Never);
        let inverted = base.with_expected_to_pass(false);

        assert!(base.expects_to_pass());
        assert!(!inverted.expects_to_pass());
        assert_eq!(inverted.color(), ColorChoice::Never);
    }
}