archunit/common/assertion/
empty_test.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
use crate::common::Filter;

use super::EmptyTestViolation;

/// Produces the defensive violation for a terminal that selected no subjects.
///
/// This assertion deliberately judges the subject selection rather than derived evidence such as
/// dependency edges or cycles. A selected item with no relationships is still a non-empty test.
#[must_use]
pub fn gather_empty_test_violations<T>(
    selected_items: &[T],
    subject: impl Into<String>,
    selectors: &[Filter],
    is_negated: bool,
    allow_empty_tests: bool,
) -> Vec<EmptyTestViolation> {
    if !selected_items.is_empty() || allow_empty_tests {
        return Vec::new();
    }

    vec![EmptyTestViolation::new_with_mood(
        subject,
        selectors.iter().cloned(),
        is_negated,
    )]
}

#[cfg(test)]
mod tests {
    use super::gather_empty_test_violations;
    use crate::common::RegexFactory;

    #[test]
    fn empty_selection_produces_one_data_violation_with_selector_order_and_mood() {
        let factory = RegexFactory::default();
        let filters = [
            factory
                .folder_matcher("src/apis/**")
                .expect("fixture folder selector should compile"),
            factory
                .filename_matcher("*_handler.rs")
                .expect("fixture name selector should compile"),
        ];

        let violations =
            gather_empty_test_violations(&Vec::<String>::new(), "files", &filters, true, false);

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].subject, "files");
        assert_eq!(violations[0].selectors[0].pattern().source(), "src/apis/**");
        assert_eq!(
            violations[0].selectors[1].pattern().source(),
            "*_handler.rs"
        );
        assert!(violations[0].is_negated);
    }

    #[test]
    fn selected_subject_without_derived_evidence_is_not_empty() {
        let violations = gather_empty_test_violations(&[()], "files", &[], false, false);

        assert!(violations.is_empty());
    }

    #[test]
    fn explicit_option_allows_an_empty_selection() {
        let violations = gather_empty_test_violations(&Vec::<()>::new(), "files", &[], false, true);

        assert!(violations.is_empty());
    }
}