3 pub(crate) struct PrettyFormatter<T> {
4 out: OutputLocation<T>,
7 /// Number of columns to fill when aligning names
10 is_multithreaded: bool,
13 impl<T: Write> PrettyFormatter<T> {
15 out: OutputLocation<T>,
18 is_multithreaded: bool,
29 pub fn output_location(&self) -> &OutputLocation<T> {
33 pub fn write_ok(&mut self) -> io::Result<()> {
34 self.write_short_result("ok", term::color::GREEN)
37 pub fn write_failed(&mut self) -> io::Result<()> {
38 self.write_short_result("FAILED", term::color::RED)
41 pub fn write_ignored(&mut self) -> io::Result<()> {
42 self.write_short_result("ignored", term::color::YELLOW)
45 pub fn write_allowed_fail(&mut self) -> io::Result<()> {
46 self.write_short_result("FAILED (allowed)", term::color::YELLOW)
49 pub fn write_bench(&mut self) -> io::Result<()> {
50 self.write_pretty("bench", term::color::CYAN)
53 pub fn write_short_result(
56 color: term::color::Color,
58 self.write_pretty(result, color)?;
59 self.write_plain("\n")
62 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
64 Pretty(ref mut term) => {
68 term.write_all(word.as_bytes())?;
74 Raw(ref mut stdout) => {
75 stdout.write_all(word.as_bytes())?;
81 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
83 self.out.write_all(s.as_bytes())?;
87 pub fn write_successes(&mut self, state: &ConsoleTestState) -> io::Result<()> {
88 self.write_plain("\nsuccesses:\n")?;
89 let mut successes = Vec::new();
90 let mut stdouts = String::new();
91 for &(ref f, ref stdout) in &state.not_failures {
92 successes.push(f.name.to_string());
93 if !stdout.is_empty() {
94 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
95 let output = String::from_utf8_lossy(stdout);
96 stdouts.push_str(&output);
97 stdouts.push_str("\n");
100 if !stdouts.is_empty() {
101 self.write_plain("\n")?;
102 self.write_plain(&stdouts)?;
105 self.write_plain("\nsuccesses:\n")?;
107 for name in &successes {
108 self.write_plain(&format!(" {}\n", name))?;
113 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
114 self.write_plain("\nfailures:\n")?;
115 let mut failures = Vec::new();
116 let mut fail_out = String::new();
117 for &(ref f, ref stdout) in &state.failures {
118 failures.push(f.name.to_string());
119 if !stdout.is_empty() {
120 fail_out.push_str(&format!("---- {} stdout ----\n", f.name));
121 let output = String::from_utf8_lossy(stdout);
122 fail_out.push_str(&output);
123 fail_out.push_str("\n");
126 if !fail_out.is_empty() {
127 self.write_plain("\n")?;
128 self.write_plain(&fail_out)?;
131 self.write_plain("\nfailures:\n")?;
133 for name in &failures {
134 self.write_plain(&format!(" {}\n", name))?;
139 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
140 let name = desc.padded_name(self.max_name_len, desc.name.padding());
141 self.write_plain(&format!("test {} ... ", name))?;
147 impl<T: Write> OutputFormatter for PrettyFormatter<T> {
148 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
149 let noun = if test_count != 1 { "tests" } else { "test" };
150 self.write_plain(&format!("\nrunning {} {}\n", test_count, noun))
153 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
154 // When running tests concurrently, we should not print
155 // the test's name as the result will be mis-aligned.
156 // When running the tests serially, we print the name here so
157 // that the user can see which test hangs.
158 if !self.is_multithreaded {
159 self.write_test_name(desc)?;
165 fn write_result(&mut self, desc: &TestDesc, result: &TestResult, _: &[u8]) -> io::Result<()> {
166 if self.is_multithreaded {
167 self.write_test_name(desc)?;
171 TrOk => self.write_ok(),
172 TrFailed | TrFailedMsg(_) => self.write_failed(),
173 TrIgnored => self.write_ignored(),
174 TrAllowedFail => self.write_allowed_fail(),
177 self.write_plain(&format!(": {}\n", fmt_bench_samples(bs)))
182 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
183 if self.is_multithreaded {
184 self.write_test_name(desc)?;
187 self.write_plain(&format!(
188 "test {} has been running for over {} seconds\n",
189 desc.name, TEST_WARN_TIMEOUT_S
193 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
194 if state.options.display_output {
195 self.write_successes(state)?;
197 let success = state.failed == 0;
199 self.write_failures(state)?;
202 self.write_plain("\ntest result: ")?;
205 // There's no parallelism at this point so it's safe to use color
206 self.write_pretty("ok", term::color::GREEN)?;
208 self.write_pretty("FAILED", term::color::RED)?;
211 let s = if state.allowed_fail > 0 {
213 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
215 state.failed + state.allowed_fail,
223 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
224 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
228 self.write_plain(&s)?;