libtest2_harness/notify/
terse.rs

1use super::Event;
2use super::RunStatus;
3use super::FAILED;
4use super::IGNORED;
5use super::OK;
6
7#[derive(Debug)]
8pub(crate) struct TerseListNotifier<W> {
9    writer: W,
10    tests: usize,
11}
12
13impl<W: std::io::Write> TerseListNotifier<W> {
14    pub(crate) fn new(writer: W) -> Self {
15        Self { writer, tests: 0 }
16    }
17}
18
19impl<W: std::io::Write> super::Notifier for TerseListNotifier<W> {
20    fn notify(&mut self, event: Event) -> std::io::Result<()> {
21        match event {
22            Event::DiscoverStart => {}
23            Event::DiscoverCase { name, mode, run } => {
24                if run {
25                    let mode = mode.as_str();
26                    writeln!(self.writer, "{name}: {mode}")?;
27                    self.tests += 1;
28                }
29            }
30            Event::DiscoverComplete { .. } => {
31                writeln!(self.writer)?;
32                writeln!(self.writer, "{} tests", self.tests)?;
33                writeln!(self.writer)?;
34            }
35            Event::SuiteStart => {}
36            Event::CaseStart { .. } => {}
37            Event::CaseComplete { .. } => {}
38            Event::SuiteComplete { .. } => {}
39        }
40        Ok(())
41    }
42}
43
44#[derive(Debug)]
45pub(crate) struct TerseRunNotifier<W> {
46    writer: W,
47    summary: super::Summary,
48}
49
50impl<W: std::io::Write> TerseRunNotifier<W> {
51    pub(crate) fn new(writer: W) -> Self {
52        Self {
53            writer,
54            summary: Default::default(),
55        }
56    }
57}
58
59impl<W: std::io::Write> super::Notifier for TerseRunNotifier<W> {
60    fn notify(&mut self, event: Event) -> std::io::Result<()> {
61        self.summary.notify(event.clone())?;
62        match event {
63            Event::DiscoverStart => {}
64            Event::DiscoverCase { .. } => {}
65            Event::DiscoverComplete { .. } => {}
66            Event::SuiteStart => {
67                self.summary.write_start(&mut self.writer)?;
68            }
69            Event::CaseStart { .. } => {}
70            Event::CaseComplete { status, .. } => {
71                let (c, style) = match status {
72                    Some(RunStatus::Ignored) => ('i', IGNORED),
73                    Some(RunStatus::Failed) => ('F', FAILED),
74                    None => ('.', OK),
75                };
76                write!(self.writer, "{style}{c}{style:#}")?;
77                self.writer.flush()?;
78            }
79            Event::SuiteComplete { .. } => {
80                self.summary.write_complete(&mut self.writer)?;
81            }
82        }
83        Ok(())
84    }
85}