diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-19 17:01:36 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-19 17:01:36 +0000 |
| commit | bf7f4d5381203d5c27b2811d62c5b1781533aa2b (patch) | |
| tree | 26903bbf535d83abd7242370d8b6932eb80e3389 /grasp-audit/src/result.rs | |
| parent | fa065ad128882755f2a988d6203b59a2ab5e38ff (diff) | |
fix some clippy fmt warnings
Diffstat (limited to 'grasp-audit/src/result.rs')
| -rw-r--r-- | grasp-audit/src/result.rs | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/grasp-audit/src/result.rs b/grasp-audit/src/result.rs index f591304..de377e5 100644 --- a/grasp-audit/src/result.rs +++ b/grasp-audit/src/result.rs | |||
| @@ -25,7 +25,7 @@ impl TestResult { | |||
| 25 | duration: Duration::default(), | 25 | duration: Duration::default(), |
| 26 | } | 26 | } |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | /// Run a test function and capture the result | 29 | /// Run a test function and capture the result |
| 30 | pub async fn run<F, Fut>(mut self, test_fn: F) -> Self | 30 | pub async fn run<F, Fut>(mut self, test_fn: F) -> Self |
| 31 | where | 31 | where |
| @@ -33,7 +33,7 @@ impl TestResult { | |||
| 33 | Fut: std::future::Future<Output = Result<(), String>>, | 33 | Fut: std::future::Future<Output = Result<(), String>>, |
| 34 | { | 34 | { |
| 35 | let start = Instant::now(); | 35 | let start = Instant::now(); |
| 36 | 36 | ||
| 37 | match test_fn().await { | 37 | match test_fn().await { |
| 38 | Ok(()) => { | 38 | Ok(()) => { |
| 39 | self.passed = true; | 39 | self.passed = true; |
| @@ -43,17 +43,17 @@ impl TestResult { | |||
| 43 | self.error = Some(e); | 43 | self.error = Some(e); |
| 44 | } | 44 | } |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | self.duration = start.elapsed(); | 47 | self.duration = start.elapsed(); |
| 48 | self | 48 | self |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | /// Mark test as passed | 51 | /// Mark test as passed |
| 52 | pub fn pass(mut self) -> Self { | 52 | pub fn pass(mut self) -> Self { |
| 53 | self.passed = true; | 53 | self.passed = true; |
| 54 | self | 54 | self |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | /// Mark test as failed with error | 57 | /// Mark test as failed with error |
| 58 | pub fn fail(mut self, error: impl Into<String>) -> Self { | 58 | pub fn fail(mut self, error: impl Into<String>) -> Self { |
| 59 | self.passed = false; | 59 | self.passed = false; |
| @@ -77,68 +77,69 @@ impl AuditResult { | |||
| 77 | results: Vec::new(), | 77 | results: Vec::new(), |
| 78 | } | 78 | } |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | /// Add a test result | 81 | /// Add a test result |
| 82 | pub fn add(&mut self, result: TestResult) { | 82 | pub fn add(&mut self, result: TestResult) { |
| 83 | self.results.push(result); | 83 | self.results.push(result); |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | /// Merge another audit result | 86 | /// Merge another audit result |
| 87 | pub fn merge(&mut self, other: AuditResult) { | 87 | pub fn merge(&mut self, other: AuditResult) { |
| 88 | self.results.extend(other.results); | 88 | self.results.extend(other.results); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | /// Check if all tests passed | 91 | /// Check if all tests passed |
| 92 | pub fn all_passed(&self) -> bool { | 92 | pub fn all_passed(&self) -> bool { |
| 93 | self.results.iter().all(|r| r.passed) | 93 | self.results.iter().all(|r| r.passed) |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | /// Get count of passed tests | 96 | /// Get count of passed tests |
| 97 | pub fn passed_count(&self) -> usize { | 97 | pub fn passed_count(&self) -> usize { |
| 98 | self.results.iter().filter(|r| r.passed).count() | 98 | self.results.iter().filter(|r| r.passed).count() |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | /// Get count of failed tests | 101 | /// Get count of failed tests |
| 102 | pub fn failed_count(&self) -> usize { | 102 | pub fn failed_count(&self) -> usize { |
| 103 | self.results.iter().filter(|r| !r.passed).count() | 103 | self.results.iter().filter(|r| !r.passed).count() |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | /// Get total count of tests | 106 | /// Get total count of tests |
| 107 | pub fn total_count(&self) -> usize { | 107 | pub fn total_count(&self) -> usize { |
| 108 | self.results.len() | 108 | self.results.len() |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | /// Print a detailed report | 111 | /// Print a detailed report |
| 112 | pub fn print_report(&self) { | 112 | pub fn print_report(&self) { |
| 113 | println!("\n{}", self.spec); | 113 | println!("\n{}", self.spec); |
| 114 | println!("{}", "═".repeat(60)); | 114 | println!("{}", "═".repeat(60)); |
| 115 | println!(); | 115 | println!(); |
| 116 | 116 | ||
| 117 | let passed = self.passed_count(); | 117 | let passed = self.passed_count(); |
| 118 | let total = self.total_count(); | 118 | let total = self.total_count(); |
| 119 | 119 | ||
| 120 | for result in &self.results { | 120 | for result in &self.results { |
| 121 | let status = if result.passed { "✓" } else { "✗" }; | 121 | let status = if result.passed { "✓" } else { "✗" }; |
| 122 | 122 | ||
| 123 | println!("{} {} ({})", status, result.name, result.spec_ref); | 123 | println!("{} {} ({})", status, result.name, result.spec_ref); |
| 124 | println!(" Requirement: {}", result.requirement); | 124 | println!(" Requirement: {}", result.requirement); |
| 125 | 125 | ||
| 126 | if let Some(error) = &result.error { | 126 | if let Some(error) = &result.error { |
| 127 | println!(" Error: {}", error); | 127 | println!(" Error: {}", error); |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | println!(" Duration: {:?}", result.duration); | 130 | println!(" Duration: {:?}", result.duration); |
| 131 | println!(); | 131 | println!(); |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | println!("Results: {}/{} passed ({:.1}%)", | 134 | println!( |
| 135 | passed, | 135 | "Results: {}/{} passed ({:.1}%)", |
| 136 | passed, | ||
| 136 | total, | 137 | total, |
| 137 | (passed as f64 / total as f64) * 100.0 | 138 | (passed as f64 / total as f64) * 100.0 |
| 138 | ); | 139 | ); |
| 139 | println!(); | 140 | println!(); |
| 140 | } | 141 | } |
| 141 | 142 | ||
| 142 | /// Get a summary string | 143 | /// Get a summary string |
| 143 | pub fn summary(&self) -> String { | 144 | pub fn summary(&self) -> String { |
| 144 | format!( | 145 | format!( |
| @@ -153,34 +154,34 @@ impl AuditResult { | |||
| 153 | #[cfg(test)] | 154 | #[cfg(test)] |
| 154 | mod tests { | 155 | mod tests { |
| 155 | use super::*; | 156 | use super::*; |
| 156 | 157 | ||
| 157 | #[tokio::test] | 158 | #[tokio::test] |
| 158 | async fn test_result_pass() { | 159 | async fn test_result_pass() { |
| 159 | let result = TestResult::new("test", "SPEC:1", "Must work") | 160 | let result = TestResult::new("test", "SPEC:1", "Must work") |
| 160 | .run(|| async { Ok(()) }) | 161 | .run(|| async { Ok(()) }) |
| 161 | .await; | 162 | .await; |
| 162 | 163 | ||
| 163 | assert!(result.passed); | 164 | assert!(result.passed); |
| 164 | assert!(result.error.is_none()); | 165 | assert!(result.error.is_none()); |
| 165 | } | 166 | } |
| 166 | 167 | ||
| 167 | #[tokio::test] | 168 | #[tokio::test] |
| 168 | async fn test_result_fail() { | 169 | async fn test_result_fail() { |
| 169 | let result = TestResult::new("test", "SPEC:1", "Must work") | 170 | let result = TestResult::new("test", "SPEC:1", "Must work") |
| 170 | .run(|| async { Err("Failed".to_string()) }) | 171 | .run(|| async { Err("Failed".to_string()) }) |
| 171 | .await; | 172 | .await; |
| 172 | 173 | ||
| 173 | assert!(!result.passed); | 174 | assert!(!result.passed); |
| 174 | assert_eq!(result.error, Some("Failed".to_string())); | 175 | assert_eq!(result.error, Some("Failed".to_string())); |
| 175 | } | 176 | } |
| 176 | 177 | ||
| 177 | #[test] | 178 | #[test] |
| 178 | fn test_audit_result() { | 179 | fn test_audit_result() { |
| 179 | let mut audit = AuditResult::new("Test Spec"); | 180 | let mut audit = AuditResult::new("Test Spec"); |
| 180 | 181 | ||
| 181 | audit.add(TestResult::new("test1", "SPEC:1", "Req1").pass()); | 182 | audit.add(TestResult::new("test1", "SPEC:1", "Req1").pass()); |
| 182 | audit.add(TestResult::new("test2", "SPEC:2", "Req2").fail("Error")); | 183 | audit.add(TestResult::new("test2", "SPEC:2", "Req2").fail("Error")); |
| 183 | 184 | ||
| 184 | assert_eq!(audit.total_count(), 2); | 185 | assert_eq!(audit.total_count(), 2); |
| 185 | assert_eq!(audit.passed_count(), 1); | 186 | assert_eq!(audit.passed_count(), 1); |
| 186 | assert_eq!(audit.failed_count(), 1); | 187 | assert_eq!(audit.failed_count(), 1); |