1 use std::{io, io::prelude::Write};
3 use super::OutputFormatter;
5 bench::fmt_bench_samples,
6 console::{ConsoleTestState, OutputLocation},
7 test_result::TestResult,
12 pub(crate) struct PrettyFormatter<T> {
13 out: OutputLocation<T>,
15 time_options: Option<time::TestTimeOptions>,
17 /// Number of columns to fill when aligning names
20 is_multithreaded: bool,
23 impl<T: Write> PrettyFormatter<T> {
25 out: OutputLocation<T>,
28 is_multithreaded: bool,
29 time_options: Option<time::TestTimeOptions>,
31 PrettyFormatter { out, use_color, max_name_len, is_multithreaded, time_options }
35 pub fn output_location(&self) -> &OutputLocation<T> {
39 pub fn write_ok(&mut self) -> io::Result<()> {
40 self.write_short_result("ok", term::color::GREEN)
43 pub fn write_failed(&mut self) -> io::Result<()> {
44 self.write_short_result("FAILED", term::color::RED)
47 pub fn write_ignored(&mut self) -> io::Result<()> {
48 self.write_short_result("ignored", term::color::YELLOW)
51 pub fn write_allowed_fail(&mut self) -> io::Result<()> {
52 self.write_short_result("FAILED (allowed)", term::color::YELLOW)
55 pub fn write_time_failed(&mut self) -> io::Result<()> {
56 self.write_short_result("FAILED (time limit exceeded)", term::color::RED)
59 pub fn write_bench(&mut self) -> io::Result<()> {
60 self.write_pretty("bench", term::color::CYAN)
63 pub fn write_short_result(
66 color: term::color::Color,
68 self.write_pretty(result, color)
71 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
73 OutputLocation::Pretty(ref mut term) => {
77 term.write_all(word.as_bytes())?;
83 OutputLocation::Raw(ref mut stdout) => {
84 stdout.write_all(word.as_bytes())?;
90 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
92 self.out.write_all(s.as_bytes())?;
99 exec_time: Option<&time::TestExecTime>,
100 ) -> io::Result<()> {
101 if let (Some(opts), Some(time)) = (self.time_options, exec_time) {
102 let time_str = format!(" <{}>", time);
104 let color = if opts.colored {
105 if opts.is_critical(desc, time) {
106 Some(term::color::RED)
107 } else if opts.is_warn(desc, time) {
108 Some(term::color::YELLOW)
117 Some(color) => self.write_pretty(&time_str, color)?,
118 None => self.write_plain(&time_str)?,
127 inputs: &Vec<(TestDesc, Vec<u8>)>,
129 ) -> io::Result<()> {
130 let results_out_str = format!("\n{}:\n", results_type);
132 self.write_plain(&results_out_str)?;
134 let mut results = Vec::new();
135 let mut stdouts = String::new();
136 for &(ref f, ref stdout) in inputs {
137 results.push(f.name.to_string());
138 if !stdout.is_empty() {
139 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
140 let output = String::from_utf8_lossy(stdout);
141 stdouts.push_str(&output);
142 stdouts.push_str("\n");
145 if !stdouts.is_empty() {
146 self.write_plain("\n")?;
147 self.write_plain(&stdouts)?;
150 self.write_plain(&results_out_str)?;
152 for name in &results {
153 self.write_plain(&format!(" {}\n", name))?;
158 pub fn write_successes(&mut self, state: &ConsoleTestState) -> io::Result<()> {
159 self.write_results(&state.not_failures, "successes")
162 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
163 self.write_results(&state.failures, "failures")
166 pub fn write_time_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
167 self.write_results(&state.time_failures, "failures (time limit exceeded)")
170 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
171 let name = desc.padded_name(self.max_name_len, desc.name.padding());
172 self.write_plain(&format!("test {} ... ", name))?;
178 impl<T: Write> OutputFormatter for PrettyFormatter<T> {
179 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
180 let noun = if test_count != 1 { "tests" } else { "test" };
181 self.write_plain(&format!("\nrunning {} {}\n", test_count, noun))
184 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
185 // When running tests concurrently, we should not print
186 // the test's name as the result will be mis-aligned.
187 // When running the tests serially, we print the name here so
188 // that the user can see which test hangs.
189 if !self.is_multithreaded {
190 self.write_test_name(desc)?;
200 exec_time: Option<&time::TestExecTime>,
202 _: &ConsoleTestState,
203 ) -> io::Result<()> {
204 if self.is_multithreaded {
205 self.write_test_name(desc)?;
209 TestResult::TrOk => self.write_ok()?,
210 TestResult::TrFailed | TestResult::TrFailedMsg(_) => self.write_failed()?,
211 TestResult::TrIgnored => self.write_ignored()?,
212 TestResult::TrAllowedFail => self.write_allowed_fail()?,
213 TestResult::TrBench(ref bs) => {
215 self.write_plain(&format!(": {}", fmt_bench_samples(bs)))?;
217 TestResult::TrTimedFail => self.write_time_failed()?,
220 self.write_time(desc, exec_time)?;
221 self.write_plain("\n")
224 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
225 if self.is_multithreaded {
226 self.write_test_name(desc)?;
229 self.write_plain(&format!(
230 "test {} has been running for over {} seconds\n",
232 time::TEST_WARN_TIMEOUT_S
236 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
237 if state.options.display_output {
238 self.write_successes(state)?;
240 let success = state.failed == 0;
242 if !state.failures.is_empty() {
243 self.write_failures(state)?;
246 if !state.time_failures.is_empty() {
247 self.write_time_failures(state)?;
251 self.write_plain("\ntest result: ")?;
254 // There's no parallelism at this point so it's safe to use color
255 self.write_pretty("ok", term::color::GREEN)?;
257 self.write_pretty("FAILED", term::color::RED)?;
260 let s = if state.allowed_fail > 0 {
262 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
264 state.failed + state.allowed_fail,
272 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
273 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
277 self.write_plain(&s)?;