From 3d5dbb394b373c970f0f44011d06f3ba9b1d96fc Mon Sep 17 00:00:00 2001 From: Raphael Druon Date: Mon, 24 Apr 2023 03:33:29 -0600 Subject: [PATCH] Add render tests --- tests/render.rs | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/tests/render.rs b/tests/render.rs index 76be91d1..08784ec4 100644 --- a/tests/render.rs +++ b/tests/render.rs @@ -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() + ); +}