archunit/files/assertion/
cycle_free.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
use crate::{common::ProjectedCycles, files::CycleViolation, violation::Violation};

/// Converts every projected cycle into machine-readable violation data.
#[must_use]
pub fn gather_cycle_violations(cycles: ProjectedCycles) -> Vec<Violation> {
    cycles
        .into_iter()
        .map(CycleViolation::new)
        .map(Violation::from)
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::{
        common::{Edge, ImportKind, ProjectedEdge},
        violation::ViolationKind,
    };

    use super::gather_cycle_violations;

    fn projected(source: &str, target: &str) -> ProjectedEdge {
        ProjectedEdge::new(
            source,
            target,
            [Edge::new(
                format!("src/{source}.rs"),
                format!("src/{target}.rs"),
                false,
                [ImportKind::Use],
            )],
        )
    }

    #[test]
    fn returns_one_data_violation_per_cycle() {
        let first = vec![projected("a", "b"), projected("b", "a")];
        let second = vec![projected("c", "d"), projected("d", "c")];

        let violations = gather_cycle_violations(vec![first, second]);

        assert_eq!(violations.len(), 2);
        assert!(
            violations
                .iter()
                .all(|violation| violation.kind() == ViolationKind::Cycle)
        );
        assert_eq!(
            violations[0]
                .as_cycle()
                .map(|violation| violation.path.clone()),
            Some(vec!["a".to_owned(), "b".to_owned(), "a".to_owned()])
        );
    }
}