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) -> io::Result<()> {
49 self.write_short_result("ignored", term::color::YELLOW)
52 pub fn write_time_failed(&mut self) -> io::Result<()> {
53 self.write_short_result("FAILED (time limit exceeded)", term::color::RED)
56 pub fn write_bench(&mut self) -> io::Result<()> {
57 self.write_pretty("bench", term::color::CYAN)
60 pub fn write_short_result(
63 color: term::color::Color,
65 self.write_pretty(result, color)
68 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
70 OutputLocation::Pretty(ref mut term) => {
74 term.write_all(word.as_bytes())?;
80 OutputLocation::Raw(ref mut stdout) => {
81 stdout.write_all(word.as_bytes())?;
87 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
89 self.out.write_all(s.as_bytes())?;
96 exec_time: Option<&time::TestExecTime>,
98 if let (Some(opts), Some(time)) = (self.time_options, exec_time) {
99 let time_str = format!(" <{}>", time);
101 let color = if opts.colored {
102 if opts.is_critical(desc, time) {
103 Some(term::color::RED)
104 } else if opts.is_warn(desc, time) {
105 Some(term::color::YELLOW)
114 Some(color) => self.write_pretty(&time_str, color)?,
115 None => self.write_plain(&time_str)?,
124 inputs: &Vec<(TestDesc, Vec<u8>)>,
126 ) -> io::Result<()> {
127 let results_out_str = format!("\n{}:\n", results_type);
129 self.write_plain(&results_out_str)?;
131 let mut results = Vec::new();
132 let mut stdouts = String::new();
133 for &(ref f, ref stdout) in inputs {
134 results.push(f.name.to_string());
135 if !stdout.is_empty() {
136 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
137 let output = String::from_utf8_lossy(stdout);
138 stdouts.push_str(&output);
142 if !stdouts.is_empty() {
143 self.write_plain("\n")?;
144 self.write_plain(&stdouts)?;
147 self.write_plain(&results_out_str)?;
149 for name in &results {
150 self.write_plain(&format!(" {}\n", name))?;
155 pub fn write_successes(&mut self, state: &ConsoleTestState) -> io::Result<()> {
156 self.write_results(&state.not_failures, "successes")
159 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
160 self.write_results(&state.failures, "failures")
163 pub fn write_time_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
164 self.write_results(&state.time_failures, "failures (time limit exceeded)")
167 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
168 let name = desc.padded_name(self.max_name_len, desc.name.padding());
169 if let Some(test_mode) = desc.test_mode() {
170 self.write_plain(&format!("test {} - {} ... ", name, test_mode))?;
172 self.write_plain(&format!("test {} ... ", name))?;
179 impl<T: Write> OutputFormatter for PrettyFormatter<T> {
180 fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> {
181 let noun = if test_count != 1 { "tests" } else { "test" };
182 let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed {
183 format!(" (shuffle seed: {})", shuffle_seed)
187 self.write_plain(&format!("\nrunning {} {}{}\n", test_count, noun, shuffle_seed_msg))
190 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
191 // When running tests concurrently, we should not print
192 // the test's name as the result will be mis-aligned.
193 // When running the tests serially, we print the name here so
194 // that the user can see which test hangs.
195 if !self.is_multithreaded {
196 self.write_test_name(desc)?;
206 exec_time: Option<&time::TestExecTime>,
208 _: &ConsoleTestState,
209 ) -> io::Result<()> {
210 if self.is_multithreaded {
211 self.write_test_name(desc)?;
215 TestResult::TrOk => self.write_ok()?,
216 TestResult::TrFailed | TestResult::TrFailedMsg(_) => self.write_failed()?,
217 TestResult::TrIgnored => self.write_ignored()?,
218 TestResult::TrBench(ref bs) => {
220 self.write_plain(&format!(": {}", fmt_bench_samples(bs)))?;
222 TestResult::TrTimedFail => self.write_time_failed()?,
225 self.write_time(desc, exec_time)?;
226 self.write_plain("\n")
229 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
230 self.write_plain(&format!(
231 "test {} has been running for over {} seconds\n",
233 time::TEST_WARN_TIMEOUT_S
237 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
238 if state.options.display_output {
239 self.write_successes(state)?;
241 let success = state.failed == 0;
243 if !state.failures.is_empty() {
244 self.write_failures(state)?;
247 if !state.time_failures.is_empty() {
248 self.write_time_failures(state)?;
252 self.write_plain("\ntest result: ")?;
255 // There's no parallelism at this point so it's safe to use color
256 self.write_pretty("ok", term::color::GREEN)?;
258 self.write_pretty("FAILED", term::color::RED)?;
262 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out",
263 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
266 self.write_plain(&s)?;
268 if let Some(ref exec_time) = state.exec_time {
269 let time_str = format!("; finished in {}", exec_time);
270 self.write_plain(&time_str)?;
273 self.write_plain("\n\n")?;