libtest2_harness/notify/
pretty.rs1use super::Event;
2use super::RunStatus;
3use super::FAILED;
4use super::IGNORED;
5use super::OK;
6
7#[derive(Debug)]
8pub(crate) struct PrettyRunNotifier<W> {
9 writer: W,
10 is_multithreaded: bool,
11 summary: super::Summary,
12 name_width: usize,
13}
14
15impl<W: std::io::Write> PrettyRunNotifier<W> {
16 pub(crate) fn new(writer: W) -> Self {
17 Self {
18 writer,
19 is_multithreaded: false,
20 summary: Default::default(),
21 name_width: 0,
22 }
23 }
24}
25
26impl<W: std::io::Write> super::Notifier for PrettyRunNotifier<W> {
27 fn threaded(&mut self, yes: bool) {
28 self.is_multithreaded = yes;
29 }
30
31 fn notify(&mut self, event: Event) -> std::io::Result<()> {
32 self.summary.notify(event.clone())?;
33 match event {
34 Event::DiscoverStart => {}
35 Event::DiscoverCase { name, run, .. } => {
36 if run {
37 self.name_width = name.len().max(self.name_width);
38 }
39 }
40 Event::DiscoverComplete { .. } => {}
41 Event::SuiteStart => {
42 self.summary.write_start(&mut self.writer)?;
43 }
44 Event::CaseStart { name, .. } => {
45 if !self.is_multithreaded {
46 write!(self.writer, "test {: <1$} ... ", name, self.name_width)?;
47 self.writer.flush()?;
48 }
49 }
50 Event::CaseComplete { name, status, .. } => {
51 let (s, style) = match status {
52 Some(RunStatus::Ignored) => ("ignored", IGNORED),
53 Some(RunStatus::Failed) => ("FAILED", FAILED),
54 None => ("ok", OK),
55 };
56
57 if self.is_multithreaded {
58 write!(self.writer, "test {: <1$} ... ", name, self.name_width)?;
59 }
60 writeln!(self.writer, "{style}{s}{style:#}")?;
61 }
62 Event::SuiteComplete { .. } => {
63 self.summary.write_complete(&mut self.writer)?;
64 }
65 }
66 Ok(())
67 }
68}