9 test_result::TestResult,
10 console::{ConsoleTestState, OutputLocation},
11 bench::fmt_bench_samples,
13 use super::OutputFormatter;
15 pub(crate) struct PrettyFormatter<T> {
16 out: OutputLocation<T>,
18 time_options: Option<time::TestTimeOptions>,
20 /// Number of columns to fill when aligning names
23 is_multithreaded: bool,
26 impl<T: Write> PrettyFormatter<T> {
28 out: OutputLocation<T>,
31 is_multithreaded: bool,
32 time_options: Option<time::TestTimeOptions>,
44 pub fn output_location(&self) -> &OutputLocation<T> {
48 pub fn write_ok(&mut self) -> io::Result<()> {
49 self.write_short_result("ok", term::color::GREEN)
52 pub fn write_failed(&mut self) -> io::Result<()> {
53 self.write_short_result("FAILED", term::color::RED)
56 pub fn write_ignored(&mut self) -> io::Result<()> {
57 self.write_short_result("ignored", term::color::YELLOW)
60 pub fn write_allowed_fail(&mut self) -> io::Result<()> {
61 self.write_short_result("FAILED (allowed)", term::color::YELLOW)
64 pub fn write_time_failed(&mut self) -> io::Result<()> {
65 self.write_short_result("FAILED (time limit exceeded)", term::color::RED)
68 pub fn write_bench(&mut self) -> io::Result<()> {
69 self.write_pretty("bench", term::color::CYAN)
72 pub fn write_short_result(
75 color: term::color::Color,
77 self.write_pretty(result, color)
80 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
82 OutputLocation::Pretty(ref mut term) => {
86 term.write_all(word.as_bytes())?;
92 OutputLocation::Raw(ref mut stdout) => {
93 stdout.write_all(word.as_bytes())?;
99 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
101 self.out.write_all(s.as_bytes())?;
108 exec_time: Option<&time::TestExecTime>
109 ) -> io::Result<()> {
110 if let (Some(opts), Some(time)) = (self.time_options, exec_time) {
111 let time_str = format!(" <{}>", time);
113 let color = if opts.colored {
114 if opts.is_critical(desc, time) {
115 Some(term::color::RED)
116 } else if opts.is_warn(desc, time) {
117 Some(term::color::YELLOW)
126 Some(color) => self.write_pretty(&time_str, color)?,
127 None => self.write_plain(&time_str)?
136 inputs: &Vec<(TestDesc, Vec<u8>)>,
138 ) -> io::Result<()> {
139 let results_out_str = format!("\n{}:\n", results_type);
141 self.write_plain(&results_out_str)?;
143 let mut results = Vec::new();
144 let mut stdouts = String::new();
145 for &(ref f, ref stdout) in inputs {
146 results.push(f.name.to_string());
147 if !stdout.is_empty() {
148 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
149 let output = String::from_utf8_lossy(stdout);
150 stdouts.push_str(&output);
151 stdouts.push_str("\n");
154 if !stdouts.is_empty() {
155 self.write_plain("\n")?;
156 self.write_plain(&stdouts)?;
159 self.write_plain(&results_out_str)?;
161 for name in &results {
162 self.write_plain(&format!(" {}\n", name))?;
167 pub fn write_successes(&mut self, state: &ConsoleTestState) -> io::Result<()> {
168 self.write_results(&state.not_failures, "successes")
171 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
172 self.write_results(&state.failures, "failures")
175 pub fn write_time_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
176 self.write_results(&state.time_failures, "failures (time limit exceeded)")
179 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
180 let name = desc.padded_name(self.max_name_len, desc.name.padding());
181 self.write_plain(&format!("test {} ... ", name))?;
187 impl<T: Write> OutputFormatter for PrettyFormatter<T> {
188 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
189 let noun = if test_count != 1 { "tests" } else { "test" };
190 self.write_plain(&format!("\nrunning {} {}\n", test_count, noun))
193 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
194 // When running tests concurrently, we should not print
195 // the test's name as the result will be mis-aligned.
196 // When running the tests serially, we print the name here so
197 // that the user can see which test hangs.
198 if !self.is_multithreaded {
199 self.write_test_name(desc)?;
209 exec_time: Option<&time::TestExecTime>,
211 _: &ConsoleTestState,
212 ) -> io::Result<()> {
213 if self.is_multithreaded {
214 self.write_test_name(desc)?;
218 TestResult::TrOk => self.write_ok()?,
219 TestResult::TrFailed | TestResult::TrFailedMsg(_) => self.write_failed()?,
220 TestResult::TrIgnored => self.write_ignored()?,
221 TestResult::TrAllowedFail => self.write_allowed_fail()?,
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 if self.is_multithreaded {
235 self.write_test_name(desc)?;
238 self.write_plain(&format!(
239 "test {} has been running for over {} seconds\n",
240 desc.name, time::TEST_WARN_TIMEOUT_S
244 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
245 if state.options.display_output {
246 self.write_successes(state)?;
248 let success = state.failed == 0;
250 if !state.failures.is_empty() {
251 self.write_failures(state)?;
254 if !state.time_failures.is_empty() {
255 self.write_time_failures(state)?;
259 self.write_plain("\ntest result: ")?;
262 // There's no parallelism at this point so it's safe to use color
263 self.write_pretty("ok", term::color::GREEN)?;
265 self.write_pretty("FAILED", term::color::RED)?;
268 let s = if state.allowed_fail > 0 {
270 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
272 state.failed + state.allowed_fail,
280 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
281 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
285 self.write_plain(&s)?;