3 pub(crate) struct PrettyFormatter<T> {
4 out: OutputLocation<T>,
6 time_options: Option<TestTimeOptions>,
8 /// Number of columns to fill when aligning names
11 is_multithreaded: bool,
14 impl<T: Write> PrettyFormatter<T> {
16 out: OutputLocation<T>,
19 is_multithreaded: bool,
20 time_options: Option<TestTimeOptions>,
32 pub fn output_location(&self) -> &OutputLocation<T> {
36 pub fn write_ok(&mut self) -> io::Result<()> {
37 self.write_short_result("ok", term::color::GREEN)
40 pub fn write_failed(&mut self) -> io::Result<()> {
41 self.write_short_result("FAILED", term::color::RED)
44 pub fn write_ignored(&mut self) -> io::Result<()> {
45 self.write_short_result("ignored", term::color::YELLOW)
48 pub fn write_allowed_fail(&mut self) -> io::Result<()> {
49 self.write_short_result("FAILED (allowed)", 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 Pretty(ref mut term) => {
74 term.write_all(word.as_bytes())?;
80 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<&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);
139 stdouts.push_str("\n");
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 self.write_plain(&format!("test {} ... ", name))?;
175 impl<T: Write> OutputFormatter for PrettyFormatter<T> {
176 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
177 let noun = if test_count != 1 { "tests" } else { "test" };
178 self.write_plain(&format!("\nrunning {} {}\n", test_count, noun))
181 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
182 // When running tests concurrently, we should not print
183 // the test's name as the result will be mis-aligned.
184 // When running the tests serially, we print the name here so
185 // that the user can see which test hangs.
186 if !self.is_multithreaded {
187 self.write_test_name(desc)?;
197 exec_time: Option<&TestExecTime>,
199 _: &ConsoleTestState,
200 ) -> io::Result<()> {
201 if self.is_multithreaded {
202 self.write_test_name(desc)?;
206 TrOk => self.write_ok()?,
207 TrFailed | TrFailedMsg(_) => self.write_failed()?,
208 TrIgnored => self.write_ignored()?,
209 TrAllowedFail => self.write_allowed_fail()?,
212 self.write_plain(&format!(": {}", fmt_bench_samples(bs)))?;
214 TrTimedFail => self.write_time_failed()?,
217 self.write_time(desc, exec_time)?;
218 self.write_plain("\n")
221 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
222 if self.is_multithreaded {
223 self.write_test_name(desc)?;
226 self.write_plain(&format!(
227 "test {} has been running for over {} seconds\n",
228 desc.name, TEST_WARN_TIMEOUT_S
232 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
233 if state.options.display_output {
234 self.write_successes(state)?;
236 let success = state.failed == 0;
238 if !state.failures.is_empty() {
239 self.write_failures(state)?;
242 if !state.time_failures.is_empty() {
243 self.write_time_failures(state)?;
247 self.write_plain("\ntest result: ")?;
250 // There's no parallelism at this point so it's safe to use color
251 self.write_pretty("ok", term::color::GREEN)?;
253 self.write_pretty("FAILED", term::color::RED)?;
256 let s = if state.allowed_fail > 0 {
258 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
260 state.failed + state.allowed_fail,
268 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
269 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
273 self.write_plain(&s)?;