Skip to main content

testing_conventions/
violation.rs

1//! The shared `Violation` type emitted by the deterministic test-code lints.
2
3use std::path::PathBuf;
4
5/// A single lint violation found in a test file.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Violation {
8    pub file: PathBuf,
9    /// 1-based line number of the offending construct.
10    pub line: usize,
11    /// Short lint identifier (e.g. `no-monkeypatch`, `no-out-of-module-call`).
12    pub rule: &'static str,
13    pub message: String,
14}
15
16/// A violation rendered for a terminal: `file:line: rule — message`.
17pub fn rendered(violation: &Violation) -> String {
18    format!(
19        "{}:{}: {} — {}",
20        violation.file.display(),
21        violation.line,
22        violation.rule,
23        violation.message
24    )
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn a_violation_renders_its_file_line_rule_and_message() {
33        let rendering = rendered(&Violation {
34            file: PathBuf::from("src/widget.rs"),
35            line: 12,
36            rule: "no-monkeypatch",
37            message: "patches `os.environ`".to_string(),
38        });
39
40        assert_eq!(
41            rendering,
42            "src/widget.rs:12: no-monkeypatch — patches `os.environ`"
43        );
44    }
45}