Skip to content

Commit

Permalink
Add render tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RDruon authored and djc committed May 9, 2023
1 parent 66a90a6 commit 3d5dbb3
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions tests/render.rs
Expand Up @@ -916,3 +916,131 @@ fn multi_progress_bottom_alignment() {
in_mem.write_line("anchor").unwrap();
assert_eq!(in_mem.contents(), "\n\nanchor");
}

#[test]
fn progress_bar_terminal_wrap() {
use std::cmp::min;
let in_mem = InMemoryTerm::new(10, 20);

let mut downloaded = 0;
let total_size = 231231231;

let pb = ProgressBar::with_draw_target(
None,
ProgressDrawTarget::term_like(Box::new(in_mem.clone())),
);
pb.set_style(ProgressStyle::default_bar()
.template("{msg:>12.cyan.bold} {spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes}").unwrap()
.progress_chars("#>-"));

pb.set_message("Downloading");
assert_eq!(
in_mem.contents(),
r#" Downloading ⠁ [00:0
0:00] [-------------
--------------------
-------] 0B/0B"#
);

let new = min(downloaded + 223211, total_size);
downloaded = new;
pb.set_position(new);
assert_eq!(
in_mem.contents(),
r#" Downloading ⠁ [00:0
0:00] [-------------
--------------------
-------] 217.98 KiB/
217.98 KiB"#
);

let new = min(downloaded + 223211, total_size);
pb.set_position(new);
assert_eq!(
in_mem.contents(),
r#" Downloading ⠉ [00:0
0:00] [-------------
--------------------
-------] 435.96 KiB/
435.96 KiB"#
);

pb.set_style(
ProgressStyle::default_bar()
.template("{msg:>12.green.bold} downloading {total_bytes:.green} in {elapsed:.green}")
.unwrap(),
);
pb.finish_with_message("Finished");
assert_eq!(
in_mem.contents(),
r#" Finished downloa
ding 435.96 KiB in 0
s"#
);

println!("{:?}", in_mem.contents())
}

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

let pb1 = mp.add(ProgressBar::new(10));
let pb2 = mp.add(ProgressBar::new(5));
let pb3 = mp.add(ProgressBar::new(100));

assert_eq!(in_mem.contents(), "");

pb1.inc(2);
mp.println("message printed that is longer than terminal width :)")
.unwrap();
assert_eq!(
in_mem.contents(),
r#"message printed that is longer than terminal wid
th :)
████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 2/10"#
);

mp.println("another great message!").unwrap();
assert_eq!(
in_mem.contents(),
r#"message printed that is longer than terminal wid
th :)
another great message!
████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 2/10"#
);

pb2.inc(1);
pb3.tick();
mp.println("one last message but this one is also longer than terminal width")
.unwrap();

assert_eq!(
in_mem.contents(),
r#"message printed that is longer than terminal wid
th :)
another great message!
one last message but this one is also longer tha
n terminal width
████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 2/10
████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/5
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/100"#
.trim()
);

drop(pb1);
drop(pb2);
drop(pb3);

assert_eq!(
in_mem.contents(),
r#"message printed that is longer than terminal wid
th :)
another great message!
one last message but this one is also longer tha
n terminal width"#
.trim()
);
}

0 comments on commit 3d5dbb3

Please sign in to comment.