Skip to content

Commit

Permalink
add unit tests and render test for tab handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-laplante committed Mar 31, 2022
1 parent 3c9e38e commit f3b64b7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/style.rs
Expand Up @@ -770,4 +770,66 @@ mod tests {
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "fghijklmno");
}

#[test]
fn tab_expansion_basics() {
const WIDTH: u16 = 20;
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

let mut style = ProgressStyle::default_spinner();
style.message = "a\ttest".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "⠁ a test");

style.tab_width = 8;
buf.clear();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "⠁ a test");
}

#[test]
fn tab_expansion_wide_msg() {
const WIDTH: u16 = 20;
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

let mut style = ProgressStyle::with_template("{wide_msg}").unwrap();
style.message = "abc\tdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "abc defghijklmnop");

buf.clear();
let mut style = ProgressStyle::with_template("{wide_msg:>}").unwrap();
style.message = "abc\tdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], " defghijklmnopqrst");

buf.clear();
let mut style = ProgressStyle::with_template("{wide_msg:^}").unwrap();
style.message = "abc\tdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "c defghijklmnopqr");
}

#[test]
fn tab_expansion_multiple() {
const WIDTH: u16 = 20;
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

let mut style = ProgressStyle::with_template("\top: {wide_msg}").unwrap();
style.message = "abc\tdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], " op: abc defgh");

buf.clear();
let mut style = ProgressStyle::with_template("{msg}:\t{wide_bar}").unwrap();
style.message = "\tOP".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], " OP: ░░░░░░░░░");
}
}
35 changes: 35 additions & 0 deletions tests/render.rs
Expand Up @@ -270,3 +270,38 @@ fn ticker_drop() {
" doing stuff 0\n doing stuff 1\n doing stuff 2\n doing stuff 3\n doing stuff 4"
);
}

#[test]
fn multi_with_tabs() {
let in_mem = InMemoryTerm::new(10, 80);
let mp =
MultiProgress::with_draw_target(ProgressDrawTarget::term_like(Box::new(in_mem.clone())));

let overall_progress = mp.add(ProgressBar::new(10).with_message("overall"));

let spinner = mp.insert_before(
&overall_progress,
ProgressBar::new_spinner()
.with_finish(ProgressFinish::AndLeave)
.with_message("spinning"),
);
spinner.tick();

assert_eq!(in_mem.contents(), "⠁ spinning");
assert_eq!(in_mem.cursor_pos(), (0, 80));

let msg1 = mp.insert_after(
&spinner,
ProgressBar::new_spinner()
.with_finish(ProgressFinish::AndLeave)
.with_message("\tmessage!"),
);

msg1.tick();
assert_eq!(in_mem.contents(), "⠁ spinning\n⠁ message!");
// If tabs are being handled correctly, the cursor should be at the end of the last line.
assert_eq!(in_mem.cursor_pos(), (1, 80));

overall_progress.inc(1);
assert_eq!(in_mem.contents(), "⠁ spinning\n⠁ message!\n███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/10");
}

0 comments on commit f3b64b7

Please sign in to comment.