Skip to content

Commit fc6c737

Browse files
committed
Hide implementation details of OutputLocation<T> from TerseFormatter.
1 parent 1f676f9 commit fc6c737

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

library/test/src/console.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
207207
OutputFormat::Pretty | OutputFormat::Junit => {
208208
Box::new(PrettyFormatter::new(&mut output, false, 0, false, None))
209209
}
210-
OutputFormat::Terse => Box::new(TerseFormatter::new(output, false, 0, false)),
210+
OutputFormat::Terse => Box::new(TerseFormatter::new(&mut output, false, 0, false)),
211211
OutputFormat::Json => Box::new(JsonFormatter::new(&mut output)),
212212
};
213213
let mut st = ConsoleTestDiscoveryState::new(opts)?;
@@ -332,9 +332,12 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
332332
is_multithreaded,
333333
opts.time_options,
334334
)),
335-
OutputFormat::Terse => {
336-
Box::new(TerseFormatter::new(output, opts.use_color(), max_name_len, is_multithreaded))
337-
}
335+
OutputFormat::Terse => Box::new(TerseFormatter::new(
336+
&mut output,
337+
opts.use_color(),
338+
max_name_len,
339+
is_multithreaded,
340+
)),
338341
OutputFormat::Json => Box::new(JsonFormatter::new(&mut output)),
339342
OutputFormat::Junit => Box::new(JunitFormatter::new(&mut output)),
340343
};

library/test/src/formatters/terse.rs

+11-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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,
@@ -15,8 +15,8 @@ use crate::{
1515
// result chars leaves 12 chars for a progress count like " 11704/12853".
1616
const QUIET_MODE_MAX_COLUMN: usize = 88;
1717

18-
pub(crate) struct TerseFormatter<T> {
19-
out: OutputLocation<T>,
18+
pub(crate) struct TerseFormatter<'a> {
19+
out: &'a mut dyn Output,
2020
use_color: bool,
2121
is_multithreaded: bool,
2222
/// Number of columns to fill when aligning names
@@ -27,9 +27,9 @@ pub(crate) struct TerseFormatter<T> {
2727
total_test_count: usize,
2828
}
2929

30-
impl<T: Write> TerseFormatter<T> {
30+
impl<'a> TerseFormatter<'a> {
3131
pub fn new(
32-
out: OutputLocation<T>,
32+
out: &'a mut dyn Output,
3333
use_color: bool,
3434
max_name_len: usize,
3535
is_multithreaded: bool,
@@ -98,29 +98,13 @@ impl<T: Write> TerseFormatter<T> {
9898
Ok(())
9999
}
100100

101-
pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
102-
match self.out {
103-
OutputLocation::Pretty(ref mut term) => {
104-
if self.use_color {
105-
term.fg(color)?;
106-
}
107-
term.write_all(word.as_bytes())?;
108-
if self.use_color {
109-
term.reset()?;
110-
}
111-
term.flush()
112-
}
113-
OutputLocation::Raw(ref mut stdout) => {
114-
stdout.write_all(word.as_bytes())?;
115-
stdout.flush()
116-
}
117-
}
101+
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
102+
if self.use_color { self.out.write_pretty(word, color) } else { self.out.write_plain(word) }
118103
}
119104

120-
pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
105+
fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
121106
let s = s.as_ref();
122-
self.out.write_all(s.as_bytes())?;
123-
self.out.flush()
107+
self.out.write_plain(s)
124108
}
125109

126110
pub fn write_outputs(&mut self, state: &ConsoleTestState) -> io::Result<()> {
@@ -187,7 +171,7 @@ impl<T: Write> TerseFormatter<T> {
187171
}
188172
}
189173

190-
impl<T: Write> OutputFormatter for TerseFormatter<T> {
174+
impl OutputFormatter for TerseFormatter<'_> {
191175
fn write_discovery_start(&mut self) -> io::Result<()> {
192176
Ok(())
193177
}

0 commit comments

Comments
 (0)