| @@ -118,11 +118,19 @@ fn run_tests(compiled: &Compiled, options: &TestOptions, timings: &mut Timings) |
| 118 | 118 | } |
| 119 | 119 | None => vec![None; tests.len()], |
| 120 | 120 | }; |
| 121 | + // The interpreter cannot call C (ADR 0005): a `@test native` test is |
| 122 | + // skipped, never run, cached or counted as passed (ADR 0061, |
| 123 | + // Revision 1). Natively, it runs like any other. |
| 124 | + let skipped: Vec<bool> = tests |
| 125 | + .iter() |
| 126 | + .map(|(_, t)| t.native_only && !options.native) |
| 127 | + .collect(); |
| 121 | 128 | let cached: Vec<bool> = names |
| 122 | 129 | .iter() |
| 123 | 130 | .zip(&keys) |
| 124 | | − .map(|(name, key)| match (&cache, key) { |
| 125 | | − (Some(cache), Some(key)) => cache.passed(name, *key), |
| 131 | + .zip(&skipped) |
| 132 | + .map(|((name, key), skipped)| match (&cache, key) { |
| 133 | + (Some(cache), Some(key)) if !skipped => cache.passed(name, *key), |
| 126 | 134 | _ => false, |
| 127 | 135 | }) |
| 128 | 136 | .collect(); |
| @@ -131,18 +139,16 @@ fn run_tests(compiled: &Compiled, options: &TestOptions, timings: &mut Timings) |
| 131 | 139 | Ok(exe) => Some(Runner::Native(exe)), |
| 132 | 140 | Err(code) => return code, |
| 133 | 141 | } |
| 134 | | − } else if cached.iter().all(|&c| c) { |
| 142 | + } else if (0..tests.len()).all(|i| cached[i] || skipped[i]) { |
| 135 | 143 | None |
| 136 | 144 | } else { |
| 137 | 145 | // Unoptimised by default: the optimiser took most of a run's time, |
| 138 | 146 | // and what it does is checked where it matters, by native tests |
| 139 | 147 | // and `--opt` (issue gabla108/louise#316, ADR 0043 Revision 1). The program is |
| 140 | 148 | // prepared once for every test's entry, and shared. |
| 141 | | − let live: Vec<FnId> = entries |
| 142 | | − .iter() |
| 143 | | − .zip(&cached) |
| 144 | | − .filter(|(_, cached)| !**cached) |
| 145 | | − .filter_map(|(e, _)| e.clone().ok()) |
| 149 | + let live: Vec<FnId> = (0..tests.len()) |
| 150 | + .filter(|&i| !cached[i] && !skipped[i]) |
| 151 | + .filter_map(|i| entries[i].clone().ok()) |
| 146 | 152 | .collect(); |
| 147 | 153 | let mut program = program.clone(); |
| 148 | 154 | let optimise = if options.opt { |
| @@ -165,19 +171,26 @@ fn run_tests(compiled: &Compiled, options: &TestOptions, timings: &mut Timings) |
| 165 | 171 | |index| { |
| 166 | 172 | let (_, t) = &tests[index]; |
| 167 | 173 | match (&entries[index], &runner) { |
| 168 | | − _ if cached[index] => Report::cached(), |
| 174 | + // A test that cannot run fails, even where it would be skipped. |
| 175 | + (Err(reason), _) => Report::failed(reason.clone()), |
| 176 | + _ if skipped[index] => Report::of(Outcome::Skipped), |
| 177 | + _ if cached[index] => Report::of(Outcome::Cached), |
| 169 | 178 | (Ok(entry), Some(runner)) => { |
| 170 | 179 | runner.run(index, *entry, jobs, t.should_panic.as_deref(), record) |
| 171 | 180 | } |
| 172 | | − (Ok(_), None) => unreachable!("a runner when a test is not cached"), |
| 173 | | − (Err(reason), _) => Report::failed(reason.clone()), |
| 181 | + (Ok(_), None) => unreachable!("a runner when a test is neither cached nor skipped"), |
| 174 | 182 | } |
| 175 | 183 | }, |
| 176 | 184 | |index, mut report| { |
| 177 | | − if let (Some(cache), Some(key), false) = (&mut cache, keys[index], cached[index]) { |
| 178 | | − match report.verdict { |
| 179 | | − Ok(()) => cache.passes(&names[index], key, std::mem::take(&mut report.inputs)), |
| 180 | | − Err(_) => cache.fails(&names[index]), |
| 185 | + if let (Some(cache), Some(key)) = (&mut cache, keys[index]) { |
| 186 | + match report.outcome { |
| 187 | + Outcome::Passed => { |
| 188 | + cache.passes(&names[index], key, std::mem::take(&mut report.inputs)) |
| 189 | + } |
| 190 | + Outcome::Failed(_) => cache.fails(&names[index]), |
| 191 | + // A kept pass is already kept; a skip is not a pass, and |
| 192 | + // kept as one, would later print as `ok (cached)`. |
| 193 | + Outcome::Cached | Outcome::Skipped => {} |
| 181 | 194 | } |
| 182 | 195 | } |
| 183 | 196 | printer.done(index, report, |i| names[i].clone()) |
| @@ -187,9 +200,8 @@ fn run_tests(compiled: &Compiled, options: &TestOptions, timings: &mut Timings) |
| 187 | 200 | if let Some(cache) = cache { |
| 188 | 201 | cache.save(); |
| 189 | 202 | } |
| 190 | | − let Printer { passed, failed, .. } = printer; |
| 191 | | − println!("\n{passed} passed, {failed} failed"); |
| 192 | | − if failed > 0 { |
| 203 | + println!("\n{}", printer.summary()); |
| 204 | + if printer.failed > 0 { |
| 193 | 205 | ExitCode::FAILURE |
| 194 | 206 | } else { |
| 195 | 207 | ExitCode::SUCCESS |
| @@ -239,42 +251,50 @@ impl Runner { |
| 239 | 251 | Report { |
| 240 | 252 | stdout, |
| 241 | 253 | stderr, |
| 242 | | − verdict: ended.verdict(should_panic), |
| 243 | | − cached: false, |
| 254 | + outcome: match ended.verdict(should_panic) { |
| 255 | + Ok(()) => Outcome::Passed, |
| 256 | + Err(reason) => Outcome::Failed(reason), |
| 257 | + }, |
| 244 | 258 | inputs: inputs.map(|i| i.take()).unwrap_or_default(), |
| 245 | 259 | } |
| 246 | 260 | } |
| 247 | 261 | } |
| 248 | 262 | |
| 249 | | −/// A test's output and verdict. |
| 263 | +/// How a test came out. |
| 264 | +#[derive(Clone, PartialEq, Eq, Debug)] |
| 265 | +enum Outcome { |
| 266 | + /// It ran and passed. |
| 267 | + Passed, |
| 268 | + /// It passed in an earlier run, and its inputs have not changed since. |
| 269 | + Cached, |
| 270 | + /// It failed, for this reason. |
| 271 | + Failed(String), |
| 272 | + /// A `@test native` test in the interpreter, which cannot run it. |
| 273 | + Skipped, |
| 274 | +} |
| 275 | + |
| 276 | +/// A test's output and outcome. |
| 250 | 277 | struct Report { |
| 251 | 278 | stdout: Vec<u8>, |
| 252 | 279 | stderr: Vec<u8>, |
| 253 | | − verdict: Result<(), String>, |
| 254 | | − /// Whether the verdict is a kept one, not this run's. |
| 255 | | − cached: bool, |
| 280 | + outcome: Outcome, |
| 256 | 281 | /// What the run read from outside the program, if it was noted. |
| 257 | 282 | inputs: Vec<louise_interp::Input>, |
| 258 | 283 | } |
| 259 | 284 | |
| 260 | 285 | impl Report { |
| 261 | | − fn failed(reason: String) -> Report { |
| 286 | + /// A report of a test that did not run (or could not). |
| 287 | + fn of(outcome: Outcome) -> Report { |
| 262 | 288 | Report { |
| 263 | 289 | stdout: Vec::new(), |
| 264 | 290 | stderr: Vec::new(), |
| 265 | | − verdict: Err(reason), |
| 266 | | − cached: false, |
| 291 | + outcome, |
| 267 | 292 | inputs: Vec::new(), |
| 268 | 293 | } |
| 269 | 294 | } |
| 270 | 295 | |
| 271 | | − /// A pass kept from an earlier run. |
| 272 | | − fn cached() -> Report { |
| 273 | | − Report { |
| 274 | | − cached: true, |
| 275 | | − verdict: Ok(()), |
| 276 | | − ..Report::failed(String::new()) |
| 277 | | − } |
| 296 | + fn failed(reason: String) -> Report { |
| 297 | + Report::of(Outcome::Failed(reason)) |
| 278 | 298 | } |
| 279 | 299 | } |
| 280 | 300 | |
| @@ -286,6 +306,7 @@ struct Printer { |
| 286 | 306 | next: usize, |
| 287 | 307 | passed: usize, |
| 288 | 308 | failed: usize, |
| 309 | + skipped: usize, |
| 289 | 310 | } |
| 290 | 311 | |
| 291 | 312 | impl Printer { |
| @@ -295,21 +316,50 @@ impl Printer { |
| 295 | 316 | while let Some(report) = self.waiting.remove(&self.next) { |
| 296 | 317 | let _ = io::stdout().write_all(&report.stdout); |
| 297 | 318 | let _ = io::stderr().write_all(&report.stderr); |
| 298 | | − let name = name(self.next); |
| 299 | | − match report.verdict { |
| 300 | | − Ok(()) => { |
| 301 | | − self.passed += 1; |
| 302 | | − let cached = if report.cached { " (cached)" } else { "" }; |
| 303 | | − println!("test {name} ... ok{cached}"); |
| 304 | | − } |
| 305 | | − Err(reason) => { |
| 306 | | − self.failed += 1; |
| 307 | | − println!("test {name} ... FAILED: {reason}"); |
| 308 | | − } |
| 309 | | − } |
| 319 | + let line = self.count(&name(self.next), report.outcome); |
| 320 | + println!("{line}"); |
| 310 | 321 | self.next += 1; |
| 311 | 322 | } |
| 312 | 323 | } |
| 324 | + |
| 325 | + /// Count a test's outcome, and say it: `test m.t ... ok`. |
| 326 | + fn count(&mut self, name: &str, outcome: Outcome) -> String { |
| 327 | + let result = match outcome { |
| 328 | + Outcome::Passed | Outcome::Cached => { |
| 329 | + self.passed += 1; |
| 330 | + let cached = if outcome == Outcome::Cached { |
| 331 | + " (cached)" |
| 332 | + } else { |
| 333 | + "" |
| 334 | + }; |
| 335 | + format!("ok{cached}") |
| 336 | + } |
| 337 | + Outcome::Failed(reason) => { |
| 338 | + self.failed += 1; |
| 339 | + format!("FAILED: {reason}") |
| 340 | + } |
| 341 | + Outcome::Skipped => { |
| 342 | + self.skipped += 1; |
| 343 | + "skipped (native only)".to_owned() |
| 344 | + } |
| 345 | + }; |
| 346 | + format!("test {name} ... {result}") |
| 347 | + } |
| 348 | + |
| 349 | + /// The run's last line: `3 passed, 0 failed`, and the skipped tests, |
| 350 | + /// if any (they are neither). |
| 351 | + fn summary(&self) -> String { |
| 352 | + let Printer { |
| 353 | + passed, |
| 354 | + failed, |
| 355 | + skipped, |
| 356 | + .. |
| 357 | + } = self; |
| 358 | + match skipped { |
| 359 | + 0 => format!("{passed} passed, {failed} failed"), |
| 360 | + _ => format!("{passed} passed, {failed} failed, {skipped} skipped (native only)"), |
| 361 | + } |
| 362 | + } |
| 313 | 363 | } |
| 314 | 364 | |
| 315 | 365 | /// A test's output, written by the interpreter's thread. |
| @@ -529,7 +579,7 @@ fn panic_message(stderr: &str) -> Option<String> { |
| 529 | 579 | |
| 530 | 580 | #[cfg(test)] |
| 531 | 581 | mod tests { |
| 532 | | − use super::{Ended, panic_message}; |
| 582 | + use super::{Ended, Outcome, Printer, panic_message}; |
| 533 | 583 | |
| 534 | 584 | /// How a native test ended, as its verdict, and the stderr it shows. |
| 535 | 585 | #[cfg(unix)] |
| @@ -609,4 +659,28 @@ mod tests { |
| 609 | 659 | Err("the test process failed (expected a panic containing \"\")".into()) |
| 610 | 660 | ); |
| 611 | 661 | } |
| 662 | + |
| 663 | + #[test] |
| 664 | + fn a_skipped_test_is_said_and_counted_apart() { |
| 665 | + let mut printer = Printer::default(); |
| 666 | + assert_eq!(printer.count("m.a", Outcome::Passed), "test m.a ... ok"); |
| 667 | + assert_eq!( |
| 668 | + printer.count("m.b", Outcome::Cached), |
| 669 | + "test m.b ... ok (cached)" |
| 670 | + ); |
| 671 | + // Nothing skipped: the summary is what it always was. |
| 672 | + assert_eq!(printer.summary(), "2 passed, 0 failed"); |
| 673 | + assert_eq!( |
| 674 | + printer.count("m.c", Outcome::Skipped), |
| 675 | + "test m.c ... skipped (native only)" |
| 676 | + ); |
| 677 | + assert_eq!( |
| 678 | + printer.count("m.d", Outcome::Failed("panic: no".into())), |
| 679 | + "test m.d ... FAILED: panic: no" |
| 680 | + ); |
| 681 | + assert_eq!( |
| 682 | + printer.summary(), |
| 683 | + "2 passed, 1 failed, 1 skipped (native only)" |
| 684 | + ); |
| 685 | + } |
| 612 | 686 | } |