Skip to content

Commit

Permalink
add some simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suaviloquence committed Apr 12, 2024
1 parent 03b2494 commit 59a3a7f
Showing 1 changed file with 85 additions and 15 deletions.
100 changes: 85 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,19 @@ mod tests {
use super::*;
use ColorChoice::*;

/// helper struct to implement `Write + 'static` while keeping
/// view access to an underlying buffer
///
/// Uses [`Mutex::try_lock`] so no calls should block even though it is a mutex
#[derive(Debug, Clone, Default)]
struct SharedBuffer(Rc<Mutex<Cursor<Vec<u8>>>>);

impl SharedBuffer {
fn new() -> Self {
Default::default()
}
}

impl Write for SharedBuffer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.try_lock().expect("mutex locked").write(buf)
Expand All @@ -258,6 +268,29 @@ mod tests {
}
}

/// asserts that there must be color/no color, based on the truth of `COLOR`,
/// in the given stream, given a copy of the buffer it links to
fn expect_color<const COLOR: bool>(mut stream: impl Write, buf: SharedBuffer) {
let expected: &[u8] = if COLOR {
b"\x1b[1mcolor!\x1b[0m"
} else {
b"color!"
};

write!(stream, "{}color!{}", Style::new().bold(), Reset).expect("error writing");
let mut grd = buf.0.try_lock().expect("mutex locked");

grd.rewind().expect("error rewinding");
let mut data = Vec::new();
grd.read_to_end(&mut data).expect("error reading");

assert_eq!(
data, expected,
"expected color: {}; found color: {}",
COLOR, !COLOR
);
}

#[test]
fn test_log_level_info() {
let mut config = GlobalConfig::new();
Expand Down Expand Up @@ -300,26 +333,63 @@ mod tests {

#[test]
fn test_set_color_choice() {
for choice in [AlwaysAnsi, Auto, Never] {
fn assert_set_color<const COLOR: bool>(choice: ColorChoice) {
let mut config = GlobalConfig::new();

let out = SharedBuffer::new();
let err = SharedBuffer::new();
config.set_stdout(Box::new(out.clone()));
config.set_stderr(Box::new(err.clone()));

config.set_color_choice(choice);

expect_color::<COLOR>(config.stdout(), out);
expect_color::<COLOR>(config.stderr(), err);
}

assert_set_color::<true>(Always);
assert_set_color::<true>(AlwaysAnsi);
// a SharedBuffer is not a tty, so it should default to no colors.
// TODO: is this test sound?
assert_set_color::<false>(Auto);
assert_set_color::<false>(Never);
}

#[test]
fn test_set_out_color_choice() {
fn assert_set_out_color<const COLOR: bool>(choice: ColorChoice) {
let mut config = GlobalConfig::new();
let buffer = SharedBuffer::default();
config.set_stdout(Box::new(buffer.clone()));

let out = SharedBuffer::new();
config.set_stdout(Box::new(out.clone()));

config.set_color_choice(choice);

write!(
config.stdout(),
"{}are there colors?{}",
Style::new().bold(),
Reset
)
.expect("error writing");
expect_color::<COLOR>(config.stdout(), out);
}

assert_set_out_color::<true>(Always);
assert_set_out_color::<true>(AlwaysAnsi);
assert_set_out_color::<false>(Auto);
assert_set_out_color::<false>(Never);
}

#[test]
fn test_set_err_color_choice() {
fn assert_set_err_color<const COLOR: bool>(choice: ColorChoice) {
let mut config = GlobalConfig::new();

let mut data = Vec::new();
let mut grd = buffer.0.try_lock().expect("mutex locked");
grd.rewind().expect("error rewinding");
grd.read_to_end(&mut data).expect("error reading");
let err = SharedBuffer::new();
config.set_stderr(Box::new(err.clone()));

assert_eq!(data, b"");
config.set_color_choice(choice);

expect_color::<COLOR>(config.stderr(), err);
}

assert_set_err_color::<true>(Always);
assert_set_err_color::<true>(AlwaysAnsi);
assert_set_err_color::<false>(Auto);
assert_set_err_color::<false>(Never);
}
}

0 comments on commit 59a3a7f

Please sign in to comment.