1 use std::{io, io::prelude::Write};
3 use super::OutputFormatter;
5 bench::fmt_bench_samples,
6 console::{ConsoleTestState, OutputLocation},
8 test_result::TestResult,
13 pub(crate) struct PrettyFormatter<T> {
14 out: OutputLocation<T>,
16 time_options: Option<time::TestTimeOptions>,
18 /// Number of columns to fill when aligning names
21 is_multithreaded: bool,
24 impl<T: Write> PrettyFormatter<T> {
26 out: OutputLocation<T>,
29 is_multithreaded: bool,
30 time_options: Option<time::TestTimeOptions>,
32 PrettyFormatter { out, use_color, max_name_len, is_multithreaded, time_options }
36 pub fn output_location(&self) -> &OutputLocation<T> {
40 pub fn write_ok(&mut self) -> io::Result<()> {
41 self.write_short_result("ok", term::color::GREEN)
44 pub fn write_failed(&mut self) -> io::Result<()> {
45 self.write_short_result("FAILED", term::color::RED)
48 pub fn write_ignored(&mut self, message: Option<&'static str>) -> io::Result<()> {
49 if let Some(message) = message {
50 self.write_short_result(&format!("ignored, {}", message), term::color::YELLOW)
52 self.write_short_result("ignored", term::color::YELLOW)
56 pub fn write_time_failed(&mut self) -> io::Result<()> {
57 self.write_short_result("FAILED (time limit exceeded)", term::color::RED)
60 pub fn write_bench(&mut self) -> io::Result<()> {
61 self.write_pretty("bench", term::color::CYAN)
64 pub fn write_short_result(
67 color: term::color::Color,
69 self.write_pretty(result, color)
72 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
74 OutputLocation::Pretty(ref mut term) => {
78 term.write_all(word.as_bytes())?;
84 OutputLocation::Raw(ref mut stdout) => {
85 stdout.write_all(word.as_bytes())?;
91 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
93 self.out.write_all(s.as_bytes())?;
100 exec_time: Option<&time::TestExecTime>,
101 ) -> io::Result<()> {
102 if let (Some(opts), Some(time)) = (self.time_options, exec_time) {
103 let time_str = format!(" <{time}>");
105 let color = if self.use_color {
106 if opts.is_critical(desc, time) {
107 Some(term::color::RED)
108 } else if opts.is_warn(desc, time) {
109 Some(term::color::YELLOW)
118 Some(color) => self.write_pretty(&time_str, color)?,
119 None => self.write_plain(&time_str)?,
128 inputs: &Vec<(TestDesc, Vec<u8>)>,
130 ) -> io::Result<()> {
131 let results_out_str = format!("\n{results_type}:\n");
133 self.write_plain(&results_out_str)?;
135 let mut results = Vec::new();
136 let mut stdouts = String::new();
137 for &(ref f, ref stdout) in inputs {
138 results.push(f.name.to_string());
139 if !stdout.is_empty() {
140 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
141 let output = String::from_utf8_lossy(stdout);
142 stdouts.push_str(&output);
146 if !stdouts.is_empty() {
147 self.write_plain("\n")?;
148 self.write_plain(&stdouts)?;
151 self.write_plain(&results_out_str)?;
153 for name in &results {
154 self.write_plain(&format!(" {name}\n"))?;
159 pub fn write_successes(&mut self, state: &ConsoleTestState) -> io::Result<()> {
160 self.write_results(&state.not_failures, "successes")
163 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
164 self.write_results(&state.failures, "failures")
167 pub fn write_time_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
168 self.write_results(&state.time_failures, "failures (time limit exceeded)")
171 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
172 let name = desc.padded_name(self.max_name_len, desc.name.padding());
173 if let Some(test_mode) = desc.test_mode() {
174 self.write_plain(&format!("test {name} - {test_mode} ... "))?;
176 self.write_plain(&format!("test {name} ... "))?;
183 impl<T: Write> OutputFormatter for PrettyFormatter<T> {
184 fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> {
185 let noun = if test_count != 1 { "tests" } else { "test" };
186 let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed {
187 format!(" (shuffle seed: {shuffle_seed})")
191 self.write_plain(&format!("\nrunning {test_count} {noun}{shuffle_seed_msg}\n"))
194 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
195 // When running tests concurrently, we should not print
196 // the test's name as the result will be mis-aligned.
197 // When running the tests serially, we print the name here so
198 // that the user can see which test hangs.
199 if !self.is_multithreaded {
200 self.write_test_name(desc)?;
210 exec_time: Option<&time::TestExecTime>,
212 _: &ConsoleTestState,
213 ) -> io::Result<()> {
214 if self.is_multithreaded {
215 self.write_test_name(desc)?;
219 TestResult::TrOk => self.write_ok()?,
220 TestResult::TrFailed | TestResult::TrFailedMsg(_) => self.write_failed()?,
221 TestResult::TrIgnored => self.write_ignored(desc.ignore_message)?,
222 TestResult::TrBench(ref bs) => {
224 self.write_plain(&format!(": {}", fmt_bench_samples(bs)))?;
226 TestResult::TrTimedFail => self.write_time_failed()?,
229 self.write_time(desc, exec_time)?;
230 self.write_plain("\n")
233 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
234 self.write_plain(&format!(
235 "test {} has been running for over {} seconds\n",
237 time::TEST_WARN_TIMEOUT_S
241 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
242 if state.options.display_output {
243 self.write_successes(state)?;
245 let success = state.failed == 0;
247 if !state.failures.is_empty() {
248 self.write_failures(state)?;
251 if !state.time_failures.is_empty() {
252 self.write_time_failures(state)?;
256 self.write_plain("\ntest result: ")?;
259 // There's no parallelism at this point so it's safe to use color
260 self.write_pretty("ok", term::color::GREEN)?;
262 self.write_pretty("FAILED", term::color::RED)?;
266 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out",
267 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
270 self.write_plain(&s)?;
272 if let Some(ref exec_time) = state.exec_time {
273 let time_str = format!("; finished in {exec_time}");
274 self.write_plain(&time_str)?;
277 self.write_plain("\n\n")?;