Skip to content

Commit a3ac553

Browse files
committed
Hide implementation details of OutputLocation<T> from PrettyFormatter.
1 parent c7b05b7 commit a3ac553

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

library/test/src/console.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ use super::{
1919
types::{NamePadding, TestDesc, TestDescAndFn},
2020
};
2121

22+
pub trait Output {
23+
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()>;
24+
fn write_plain(&mut self, word: &str) -> io::Result<()>;
25+
}
26+
2227
/// Generic wrapper over stdout.
2328
pub enum OutputLocation<T> {
2429
Pretty(Box<term::StdoutTerminal>),
@@ -41,8 +46,8 @@ impl<T: Write> Write for OutputLocation<T> {
4146
}
4247
}
4348

44-
impl<T: Write> OutputLocation<T> {
45-
pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
49+
impl<T: Write> Output for OutputLocation<T> {
50+
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
4651
match self {
4752
OutputLocation::Pretty(ref mut term) => {
4853
term.fg(color)?;
@@ -57,7 +62,7 @@ impl<T: Write> OutputLocation<T> {
5762
self.flush()
5863
}
5964

60-
pub fn write_plain(&mut self, word: &str) -> io::Result<()> {
65+
fn write_plain(&mut self, word: &str) -> io::Result<()> {
6166
self.write_all(word.as_bytes())?;
6267
self.flush()
6368
}
@@ -193,14 +198,14 @@ impl ConsoleTestState {
193198

194199
// List the tests to console, and optionally to logfile. Filters are honored.
195200
pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<()> {
196-
let output = match term::stdout() {
201+
let mut output = match term::stdout() {
197202
None => OutputLocation::Raw(io::stdout().lock()),
198203
Some(t) => OutputLocation::Pretty(t),
199204
};
200205

201206
let mut out: Box<dyn OutputFormatter> = match opts.format {
202207
OutputFormat::Pretty | OutputFormat::Junit => {
203-
Box::new(PrettyFormatter::new(output, false, 0, false, None))
208+
Box::new(PrettyFormatter::new(&mut output, false, 0, false, None))
204209
}
205210
OutputFormat::Terse => Box::new(TerseFormatter::new(output, false, 0, false)),
206211
OutputFormat::Json => Box::new(JsonFormatter::new(output)),
@@ -306,7 +311,7 @@ fn on_test_event(
306311
/// A simple console test runner.
307312
/// Runs provided tests reporting process and results to the stdout.
308313
pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<bool> {
309-
let output = match term::stdout() {
314+
let mut output = match term::stdout() {
310315
None => OutputLocation::Raw(io::stdout()),
311316
Some(t) => OutputLocation::Pretty(t),
312317
};
@@ -321,7 +326,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
321326

322327
let mut out: Box<dyn OutputFormatter> = match opts.format {
323328
OutputFormat::Pretty => Box::new(PrettyFormatter::new(
324-
output,
329+
&mut output,
325330
opts.use_color(),
326331
max_name_len,
327332
is_multithreaded,

library/test/src/formatters/pretty.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use std::{io, io::prelude::Write};
1+
use std::io;
22

33
use super::OutputFormatter;
44
use crate::{
55
bench::fmt_bench_samples,
6-
console::{ConsoleTestDiscoveryState, ConsoleTestState, OutputLocation},
6+
console::{ConsoleTestDiscoveryState, ConsoleTestState, Output},
77
term,
88
test_result::TestResult,
99
time,
1010
types::TestDesc,
1111
};
1212

13-
pub(crate) struct PrettyFormatter<T> {
14-
out: OutputLocation<T>,
13+
pub(crate) struct PrettyFormatter<'a> {
14+
out: &'a mut dyn Output,
1515
use_color: bool,
1616
time_options: Option<time::TestTimeOptions>,
1717

@@ -21,9 +21,9 @@ pub(crate) struct PrettyFormatter<T> {
2121
is_multithreaded: bool,
2222
}
2323

24-
impl<T: Write> PrettyFormatter<T> {
24+
impl<'a> PrettyFormatter<'a> {
2525
pub fn new(
26-
out: OutputLocation<T>,
26+
out: &'a mut dyn Output,
2727
use_color: bool,
2828
max_name_len: usize,
2929
is_multithreaded: bool,
@@ -159,7 +159,7 @@ impl<T: Write> PrettyFormatter<T> {
159159
}
160160
}
161161

162-
impl<T: Write> OutputFormatter for PrettyFormatter<T> {
162+
impl OutputFormatter for PrettyFormatter<'_> {
163163
fn write_discovery_start(&mut self) -> io::Result<()> {
164164
Ok(())
165165
}

library/test/src/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,8 @@ fn should_sort_failures_before_printing_them() {
894894
};
895895

896896
let mut output = Vec::new();
897-
let mut out = PrettyFormatter::new(OutputLocation::Raw(&mut output), false, 10, false, None);
897+
let mut raw = OutputLocation::Raw(&mut output);
898+
let mut out = PrettyFormatter::new(&mut raw, false, 10, false, None);
898899

899900
let st = console::ConsoleTestState {
900901
log_out: None,

0 commit comments

Comments
 (0)