From a04ef1b70178921d0ad260349b2304695e04737a Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:08:42 +0000 Subject: [PATCH] tests: refactor all draw tests --- src/ui/draw_blocks.rs | 1204 ++++++++++++++++++++--------------------- 1 file changed, 589 insertions(+), 615 deletions(-) diff --git a/src/ui/draw_blocks.rs b/src/ui/draw_blocks.rs index 83d3a67..6d9419a 100644 --- a/src/ui/draw_blocks.rs +++ b/src/ui/draw_blocks.rs @@ -1821,13 +1821,10 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); assert_eq!(result_cell.fg, Color::Reset); } } @@ -1844,13 +1841,11 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); if BORDER_CHARS.contains(&result_cell.symbol()) { assert_eq!(result_cell.fg, Color::LightCyan); } @@ -1885,20 +1880,13 @@ mod tests { }) .unwrap(); - let test = |terminal: &Terminal, expected: [&str; 6]| { - let result = &terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.fg, Color::Reset); - } + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.fg, Color::Reset); } - }; - - test(&setup.terminal, expected); + } // animation moved by one frame setup.gui_state.lock().next_loading(uuid); @@ -1921,7 +1909,13 @@ mod tests { }) .unwrap(); - test(&setup.terminal, expected); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.fg, Color::Reset); + } + } } #[test] @@ -1932,28 +1926,6 @@ mod tests { insert_logs(&setup); - let test = |terminal: &Terminal, - expected: [&str; 6], - range: RangeInclusive| { - let result = &terminal.backend().buffer().content; - - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.fg, Color::Reset); - - if range.contains(&index) { - assert_eq!(result_cell.modifier, Modifier::BOLD); - } else { - assert!(result_cell.modifier.is_empty()); - } - } - } - }; - let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); setup .terminal @@ -1969,11 +1941,24 @@ mod tests { "│ │", "╰───────────────────────╯", ]; - test(&setup.terminal, expected, 76..=98); + + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.fg, Color::Reset); + + if row_index == 3 && (1..=23).contains(&result_cell_index) { + assert_eq!(result_cell.modifier, Modifier::BOLD); + } else { + assert!(result_cell.modifier.is_empty()); + } + } + } // Change selected log line setup.app_data.lock().log_previous(); - let _fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); + _ = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); setup .terminal @@ -1990,7 +1975,19 @@ mod tests { "│ │", "╰───────────────────────╯", ]; - test(&setup.terminal, expected, 51..=73); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.fg, Color::Reset); + + if row_index == 2 && (1..=23).contains(&result_cell_index) { + assert_eq!(result_cell.modifier, Modifier::BOLD); + } else { + assert!(result_cell.modifier.is_empty()); + } + } + } } #[test] @@ -2022,14 +2019,10 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); } } } @@ -2038,28 +2031,8 @@ mod tests { // Charts panel // // ************ // - const EXPECTED: [&str; 10] = [ - "╭───────────── cpu 03.00% ─────────────╮╭────────── memory 30.00 kB ───────────╮", - "│10.00%│ • ││100.00 kB│ •• │", - "│ │ •• ││ │ •• │", - "│ │ ••• ││ │ • • │", - "│ │ • • ││ │ • • │", - "│ │ • •• ││ │•• •• │", - "│ │• • ││ │• • │", - "│ │• • ││ │• • │", - "│ │ ││ │ │", - "╰──────────────────────────────────────╯╰──────────────────────────────────────╯", - ]; - const MEMORY_INDEX: [usize; 16] = [ - 134, 135, 214, 215, 293, 295, 372, 375, 451, 452, 455, 456, 531, 535, 611, 615, - ]; - - const CPU_INDEX: [usize; 15] = [ - 92, 171, 172, 250, 251, 252, 330, 332, 409, 413, 414, 488, 493, 568, 573, - ]; - #[allow(clippy::cast_precision_loss)] - // Add fixed data to the cpu & mem vecdeques, that match the above data + // Add fixed data to the cpu & mem vecdeques fn insert_chart_data(setup: &TuiTestSetup) { for i in 1..=10 { setup.app_data.lock().update_stats_by_id( @@ -2082,8 +2055,62 @@ mod tests { ); } } + + /// CPU and Memroy charts used in multiple tests, based on data from above insert_chart_data() + const EXPECTED: [&str; 10] = [ + "╭───────────── cpu 03.00% ─────────────╮╭────────── memory 30.00 kB ───────────╮", + "│10.00%│ • ││100.00 kB│ •• │", + "│ │ •• ││ │ •• │", + "│ │ ••• ││ │ • • │", + "│ │ • • ││ │ • • │", + "│ │ • •• ││ │•• •• │", + "│ │• • ││ │• • │", + "│ │• • ││ │• • │", + "│ │ ││ │ │", + "╰──────────────────────────────────────╯╰──────────────────────────────────────╯", + ]; + + // co-ordinates of the dots from the cpu chart + const CPU_XY: [(usize, usize); 15] = [ + (1, 12), + (2, 11), + (2, 12), + (3, 10), + (3, 11), + (3, 12), + (4, 10), + (4, 12), + (5, 9), + (5, 13), + (5, 14), + (6, 8), + (6, 13), + (7, 8), + (7, 13), + ]; + + // co-ordinates of the dots from the memory chart + const MEM_XY: [(usize, usize); 16] = [ + (1, 54), + (1, 55), + (2, 54), + (2, 55), + (3, 53), + (3, 55), + (4, 52), + (4, 55), + (5, 51), + (5, 52), + (5, 55), + (5, 56), + (6, 51), + (6, 55), + (7, 51), + (7, 55), + ]; + #[test] - /// When status is Running, but not data, charts drawn without dots etc + /// When status is Running, but not data, charts drawn without dots etc, colours correct fn test_draw_blocks_charts_running_none() { let (w, h) = (80, 10); let mut setup = test_setup(w, h, true, true); @@ -2108,26 +2135,20 @@ mod tests { "╰──────────────────────────────────────╯╰──────────────────────────────────────╯", ]; - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - - match index { - // chart tiles - cpu 03.00% && memory 30.00 kB - are green - 14..=25 | 52..=67 => { + match (row_index, result_cell_index) { + (0, 14..=25 | 52..=67) => { assert_eq!(result_cell.fg, Color::Green); assert_eq!(result_cell.modifier, Modifier::BOLD); } - // Cpu & Memory max are orange and bold - 81..=86 | 121..=127 => { + (1, 1..=6 | 41..=47) => { assert_eq!(result_cell.fg, ORANGE); assert_eq!(result_cell.modifier, Modifier::BOLD); } - // All others _ => { assert_eq!(result_cell.fg, Color::Reset); assert!(result_cell.modifier.is_empty()); @@ -2152,35 +2173,28 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in EXPECTED.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&EXPECTED, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - match index { - // chart tiles - cpu 03.00% && memory 30.00 kB - are green - 14..=25 | 51..=67 => { + match (row_index, result_cell_index) { + (0, 14..=25 | 51..=67) => { assert_eq!(result_cell.fg, Color::Green); assert_eq!(result_cell.modifier, Modifier::BOLD); } - // Cpu & Memory max are orange and bold - 81..=86 | 121..=129 => { + (1, 1..=6 | 41..=49) => { assert_eq!(result_cell.fg, ORANGE); assert_eq!(result_cell.modifier, Modifier::BOLD); } - // cpu dots are magenta - _x if CPU_INDEX.contains(&index) => { + xy if CPU_XY.contains(&xy) => { assert_eq!(result_cell.fg, Color::Magenta); assert!(result_cell.modifier.is_empty()); } - // memory dots are cyan - _x if MEMORY_INDEX.contains(&index) => { + xy if MEM_XY.contains(&xy) => { assert_eq!(result_cell.fg, Color::Cyan); assert!(result_cell.modifier.is_empty()); } - // All others _ => { assert_eq!(result_cell.fg, Color::Reset); assert!(result_cell.modifier.is_empty()); @@ -2206,29 +2220,24 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in EXPECTED.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&EXPECTED, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - match index { - // Titles and y axis are yellow - 14..=25 | 51..=67 | 81..=86 | 121..=129 => { + match (row_index, result_cell_index) { + (0, 14..=25 | 51..=67) | (1, 1..=6 | 41..=49) => { assert_eq!(result_cell.fg, Color::Yellow); assert_eq!(result_cell.modifier, Modifier::BOLD); } - _x if CPU_INDEX.contains(&index) => { + xy if CPU_XY.contains(&xy) => { assert_eq!(result_cell.fg, Color::Magenta); assert!(result_cell.modifier.is_empty()); } - // memory dots are cyan - _x if MEMORY_INDEX.contains(&index) => { + xy if MEM_XY.contains(&xy) => { assert_eq!(result_cell.fg, Color::Cyan); assert!(result_cell.modifier.is_empty()); } - // All others _ => { assert_eq!(result_cell.fg, Color::Reset); assert!(result_cell.modifier.is_empty()); @@ -2253,30 +2262,24 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in EXPECTED.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&EXPECTED, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - match index { - // Titles and y axis are red - 14..=25 | 51..=67 | 81..=86 | 121..=129 => { + match (row_index, result_cell_index) { + (0, 14..=25 | 51..=67) | (1, 1..=6 | 41..=49) => { assert_eq!(result_cell.fg, Color::Red); assert_eq!(result_cell.modifier, Modifier::BOLD); } - // cpu dots are magenta - _x if CPU_INDEX.contains(&index) => { + xy if CPU_XY.contains(&xy) => { assert_eq!(result_cell.fg, Color::Magenta); assert!(result_cell.modifier.is_empty()); } - // memory dots are cyan - _x if MEMORY_INDEX.contains(&index) => { + xy if MEM_XY.contains(&xy) => { assert_eq!(result_cell.fg, Color::Cyan); assert!(result_cell.modifier.is_empty()); } - // All others _ => { assert_eq!(result_cell.fg, Color::Reset); assert!(result_cell.modifier.is_empty()); @@ -2299,7 +2302,7 @@ mod tests { let mut fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); - let expected = " ( h ) show help "; + let expected = [" ( h ) show help "]; setup .terminal @@ -2308,17 +2311,17 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (index, expected_char) in expected.chars().enumerate() { - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!(result_cell.fg, Color::White); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::White); + } } fd.help_visible = true; - let expected = " ( h ) exit help "; + let expected = [" ( h ) exit help "]; setup .terminal .draw(|f| { @@ -2326,13 +2329,13 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (index, expected_char) in expected.chars().enumerate() { - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!(result_cell.fg, Color::Black); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } } } @@ -2343,7 +2346,7 @@ mod tests { let mut setup = test_setup(w, h, true, true); let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); - let expected = " name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "; + let expected = [" name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "]; setup .terminal .draw(|f| { @@ -2351,19 +2354,19 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (index, expected_char) in expected.chars().enumerate() { - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!( - result_cell.fg, - match index { - (2..=122) => Color::Black, - _ => Color::White, - } - ); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!( + result_cell.fg, + match result_cell_index { + (2..=122) => Color::Black, + _ => Color::White, + } + ); + } } } @@ -2375,7 +2378,7 @@ mod tests { let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let expected = - " name state status cpu ( h ) show help "; + [" name state status cpu ( h ) show help "]; setup .terminal .draw(|f| { @@ -2383,19 +2386,19 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (index, expected_char) in expected.chars().enumerate() { - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!( - result_cell.fg, - match index { - (2..=62) => Color::Black, - _ => Color::White, - } - ); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!( + result_cell.fg, + match result_cell_index { + (2..=62) => Color::Black, + _ => Color::White, + } + ); + } } } @@ -2405,68 +2408,72 @@ mod tests { let (w, h) = (140, 1); let mut setup = test_setup(w, h, true, true); let mut fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); - let mut test = |expected: &str, range: RangeInclusive, x: (Header, SortedOrder)| { - fd.sorted_by = Some(x); - setup - .terminal - .draw(|f| { - super::heading_bar(setup.area, f, &fd, &setup.gui_state); - }) - .unwrap(); + let mut test = + |expected: &[&str], range: RangeInclusive, x: (Header, SortedOrder)| { + fd.sorted_by = Some(x); - let result = &setup.terminal.backend().buffer().content; - for (index, expected_char) in expected.chars().enumerate() { - let result_cell = &result[index]; - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!( - result_cell.fg, - match index { - 0 | 1 => Color::White, - // given range | help section - x if range.contains(&x) || (123..=139).contains(&x) => Color::White, - _ => Color::Black, + setup + .terminal + .draw(|f| { + super::heading_bar(setup.area, f, &fd, &setup.gui_state); + }) + .unwrap(); + + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!( + result_cell.fg, + match result_cell_index { + 0 | 1 => Color::White, + // given range | help section + x if range.contains(&x) || (123..=139).contains(&x) => Color::White, + _ => Color::Black, + } + ); } - ); - } - }; + } + }; // Name - test(" ▲ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 1..=14, (Header::Name, SortedOrder::Asc)); - test(" ▼ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 1..=14, (Header::Name, SortedOrder::Desc)); + test(&[" ▲ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 1..=14, (Header::Name, SortedOrder::Asc)); + test(&[" ▼ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 1..=14, (Header::Name, SortedOrder::Desc)); // state - test(" name ▲ state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 15..=26, (Header::State, SortedOrder::Asc)); - test(" name ▼ state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 15..=26, (Header::State, SortedOrder::Desc)); + test(&[" name ▲ state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 15..=26, (Header::State, SortedOrder::Asc)); + test(&[" name ▼ state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 15..=26, (Header::State, SortedOrder::Desc)); // status - test(" name state ▲ status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 27..=47, (Header::Status, SortedOrder::Asc)); - test(" name state ▼ status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 27..=47, (Header::Status, SortedOrder::Desc)); + test(&[" name state ▲ status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 27..=47, (Header::Status, SortedOrder::Asc)); + test(&[" name state ▼ status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 27..=47, (Header::Status, SortedOrder::Desc)); // cpu - test(" name state status ▲ cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 48..=57, (Header::Cpu, SortedOrder::Asc)); - test(" name state status ▼ cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", 48..=57, (Header::Cpu, SortedOrder::Desc)); + test(&[" name state status ▲ cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 48..=57, (Header::Cpu, SortedOrder::Asc)); + test(&[" name state status ▼ cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "], 48..=57, (Header::Cpu, SortedOrder::Desc)); // mem - test(" name state status cpu ▲ memory/limit id image ↓ rx ↑ tx ( h ) show help ", 58..=77, (Header::Memory, SortedOrder::Asc)); - test(" name state status cpu ▼ memory/limit id image ↓ rx ↑ tx ( h ) show help ", 58..=77, (Header::Memory, SortedOrder::Desc)); + test(&[" name state status cpu ▲ memory/limit id image ↓ rx ↑ tx ( h ) show help "], 58..=77, (Header::Memory, SortedOrder::Asc)); + test(&[" name state status cpu ▼ memory/limit id image ↓ rx ↑ tx ( h ) show help "], 58..=77, (Header::Memory, SortedOrder::Desc)); // id - test(" name state status cpu memory/limit ▲ id image ↓ rx ↑ tx ( h ) show help ", 78..=88, (Header::Id, SortedOrder::Asc)); - test(" name state status cpu memory/limit ▼ id image ↓ rx ↑ tx ( h ) show help ", 78..=88, (Header::Id, SortedOrder::Desc)); + test(&[" name state status cpu memory/limit ▲ id image ↓ rx ↑ tx ( h ) show help "], 78..=88, (Header::Id, SortedOrder::Asc)); + test(&[" name state status cpu memory/limit ▼ id image ↓ rx ↑ tx ( h ) show help "], 78..=88, (Header::Id, SortedOrder::Desc)); // image - test(" name state status cpu memory/limit id ▲ image ↓ rx ↑ tx ( h ) show help ", 89..=98, (Header::Image, SortedOrder::Asc)); - test(" name state status cpu memory/limit id ▼ image ↓ rx ↑ tx ( h ) show help ", 89..=98, (Header::Image, SortedOrder::Desc)); + test(&[" name state status cpu memory/limit id ▲ image ↓ rx ↑ tx ( h ) show help "], 89..=98, (Header::Image, SortedOrder::Asc)); + test(&[" name state status cpu memory/limit id ▼ image ↓ rx ↑ tx ( h ) show help "], 89..=98, (Header::Image, SortedOrder::Desc)); // rx - test(" name state status cpu memory/limit id image ▲ ↓ rx ↑ tx ( h ) show help ", 99..=108, (Header::Rx, SortedOrder::Asc)); - test(" name state status cpu memory/limit id image ▼ ↓ rx ↑ tx ( h ) show help ", 99..=108, (Header::Rx, SortedOrder::Desc)); + test(&[" name state status cpu memory/limit id image ▲ ↓ rx ↑ tx ( h ) show help "], 99..=108, (Header::Rx, SortedOrder::Asc)); + test(&[" name state status cpu memory/limit id image ▼ ↓ rx ↑ tx ( h ) show help "], 99..=108, (Header::Rx, SortedOrder::Desc)); // tx - test(" name state status cpu memory/limit id image ↓ rx ▲ ↑ tx ( h ) show help ", 109..=118, (Header::Tx, SortedOrder::Asc)); - test(" name state status cpu memory/limit id image ↓ rx ▼ ↑ tx ( h ) show help ", 109..=118, (Header::Tx, SortedOrder::Desc)); + test(&[" name state status cpu memory/limit id image ↓ rx ▲ ↑ tx ( h ) show help "], 109..=118, (Header::Tx, SortedOrder::Asc)); + test(&[" name state status cpu memory/limit id image ↓ rx ▼ ↑ tx ( h ) show help "], 109..=118, (Header::Tx, SortedOrder::Desc)); } #[test] @@ -2485,21 +2492,21 @@ mod tests { }) .unwrap(); - let expected = " ⠙ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "; + let expected = [" ⠙ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "]; - let result = &setup.terminal.backend().buffer().content; - for (index, expected_char) in expected.chars().enumerate() { - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!( - result_cell.fg, - match index { - (2..=122) => Color::Black, - _ => Color::White, - } - ); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!( + result_cell.fg, + match result_cell_index { + (2..=122) => Color::Black, + _ => Color::White, + } + ); + } } } @@ -2519,87 +2526,83 @@ mod tests { super::help_box(f); }) .unwrap(); + let version_row = format!(" ╭ {VERSION} ────────────────────────────────────────────────────────────────────────────╮ "); let expected = [ - " ".to_owned(), - format!(" ╭ {VERSION} ────────────────────────────────────────────────────────────────────────────╮ "), - " │ │ ".to_owned(), - " │ 88 │ ".to_owned(), - " │ 88 │ ".to_owned(), - " │ 88 │ ".to_owned(), - " │ ,adPPYba, 8b, ,d8 88 ,d8 ,adPPYba, 8b,dPPYba, │ ".to_owned(), - r#" │ a8" "8a `Y8, ,8P' 88 ,a8" a8P_____88 88P' "Y8 │ "#.to_owned(), - r#" │ 8b d8 )888( 8888[ 8PP""""""" 88 │ "#.to_owned(), - r#" │ "8a, ,a8" ,d8" "8b, 88`"Yba, "8b, ,aa 88 │ "#.to_owned(), - r#" │ `"YbbdP"' 8P' `Y8 88 `Y8a `"Ybbd8"' 88 │ "#.to_owned(), - " │ │ ".to_owned(), - " │ A simple tui to view & control docker containers │ ".to_owned(), - " │ │ ".to_owned(), - " │ ( tab ) or ( shift+tab ) change panels │ ".to_owned(), - " │ ( ↑ ↓ ) or ( j k ) or ( PgUp PgDown ) or ( Home End ) change selected line │ ".to_owned(), - " │ ( enter ) send docker container command │ ".to_owned(), - " │ ( e ) exec into a container │ ".to_owned(), - " │ ( h ) toggle this help information │ ".to_owned(), - " │ ( s ) save logs to file │ ".to_owned(), - " │ ( m ) toggle mouse capture - if disabled, text on screen can be selected & copied │ ".to_owned(), - " │ ( F1 ) or ( / ) toggle filter mode │ ".to_owned(), - " │ ( 0 ) stop sort │ ".to_owned(), - " │ ( 1 - 9 ) sort by header - or click header │ ".to_owned(), - " │ ( esc ) close dialog │ ".to_owned(), - " │ ( q ) quit at any time │ ".to_owned(), - " │ │ ".to_owned(), - " │ currently an early work in progress, all and any input appreciated │ ".to_owned(), - " │ https://github.com/mrjackwills/oxker │ ".to_owned(), - " │ │ ".to_owned(), - " │ │ ".to_owned(), - " ╰───────────────────────────────────────────────────────────────────────────────────╯ ".to_owned(), - " ".to_owned(), + " ", + version_row.as_str(), + " │ │ ", + " │ 88 │ ", + " │ 88 │ ", + " │ 88 │ ", + " │ ,adPPYba, 8b, ,d8 88 ,d8 ,adPPYba, 8b,dPPYba, │ ", + r#" │ a8" "8a `Y8, ,8P' 88 ,a8" a8P_____88 88P' "Y8 │ "#, + r#" │ 8b d8 )888( 8888[ 8PP""""""" 88 │ "#, + r#" │ "8a, ,a8" ,d8" "8b, 88`"Yba, "8b, ,aa 88 │ "#, + r#" │ `"YbbdP"' 8P' `Y8 88 `Y8a `"Ybbd8"' 88 │ "#, + " │ │ ", + " │ A simple tui to view & control docker containers │ ", + " │ │ ", + " │ ( tab ) or ( shift+tab ) change panels │ ", + " │ ( ↑ ↓ ) or ( j k ) or ( PgUp PgDown ) or ( Home End ) change selected line │ ", + " │ ( enter ) send docker container command │ ", + " │ ( e ) exec into a container │ ", + " │ ( h ) toggle this help information │ ", + " │ ( s ) save logs to file │ ", + " │ ( m ) toggle mouse capture - if disabled, text on screen can be selected & copied │ ", + " │ ( F1 ) or ( / ) toggle filter mode │ ", + " │ ( 0 ) stop sort │ ", + " │ ( 1 - 9 ) sort by header - or click header │ ", + " │ ( esc ) close dialog │ ", + " │ ( q ) quit at any time │ ", + " │ │ ", + " │ currently an early work in progress, all and any input appreciated │ ", + " │ https://github.com/mrjackwills/oxker │ ", + " │ │ ", + " │ │ ", + " ╰───────────────────────────────────────────────────────────────────────────────────╯ ", + " " ]; - for (row_index, row) in expected.iter().enumerate() { - let mut bracket_key = vec![]; - let mut push_bracket_key = false; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - let result = &setup.terminal.backend().buffer().content; - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - let result_str = result_cell.symbol(); - - assert_eq!(result_cell.symbol(), expected_char.to_string()); - - // First and last row, and first char and last char in each row, is empty - if row_index == 0 - || row_index == usize::from(h - 1) - || char_index == 0 - || char_index == usize::from(w - 1) - { - assert_eq!(result_cell.fg, Color::Reset); - assert_eq!(result_cell.bg, Color::Reset); - // Borders - } else if BORDER_CHARS.contains(&result_str) { - assert_eq!(result_cell.fg, Color::Black); - assert_eq!(result_cell.bg, Color::Magenta); - // everything else has a magenta background - } else { - assert_eq!(result_cell.bg, Color::Magenta); - } - - // check that ( [key] ) is white - if result_str == "(" { - push_bracket_key = true; - bracket_key.push(result_cell); - } - if push_bracket_key { - bracket_key.push(result_cell); - if result_str == ")" { - push_bracket_key = false; - for i in &bracket_key { - assert_eq!(i.fg, Color::White); - } - bracket_key.clear(); + match (row_index, result_cell_index) { + // first & last row, and first & last char on each row, is reset/reset, making sure that the help info is centered in the given area + (0 | 32, _) | (0..=33, 0 | 86) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + // border is black on magenta + (1 | 31, _) | (1..=31, 1 | 85) => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + // oxker logo && description + (2..=10, 2..=85) | (12, 19..=66) + // button in the brackets + | (14, 2..=10 | 13..=27) + | (15, 2..=10 | 13..=21 | 24..=40 | 43..=56) + | (16 | 23, 2..=12) + | (17..=20 | 22 | 25, 2..=8) + | (21, 2..=9 | 12..=18) + | (24, 2..=10) => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::White); + } + // The URL is white and underlined + (28, 25..=60) => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::White); + assert_eq!(result_cell.modifier, Modifier::UNDERLINED); + } + // The rest is black on magenta + _ => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); } } - // TODO should really be testing every color of every str here } } } @@ -2634,38 +2637,25 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - - if row_index == 0 - || row_index == usize::from(h - 1) - || char_index < 8 - || char_index > usize::from(w - 9) - { - assert_eq!(result_cell.fg, Color::Reset); - assert_eq!(result_cell.bg, Color::Reset); - } else { - assert_eq!(result_cell.bg, Color::White); - } - - // Borders are black - if BORDER_CHARS.contains(&result_cell.symbol()) { - assert_eq!(result_cell.fg, Color::Black); - // Container name is red - } else if row_index == 3 && (57..=67).contains(&char_index) { - assert_eq!(result_cell.fg, Color::Red); - // All other text is black - } else if !row_index == 0 - && !row_index == usize::from(h - 1) - && !char_index < 8 - && !char_index > usize::from(w - 9) - { - assert_eq!(result_cell.fg, Color::Black); + match (row_index, result_cell_index) { + (0 | 9, _) | (1..=8, 0..=7 | 74..=81) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + (3, 57..=67) => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Red); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Black); + } } } } @@ -2699,37 +2689,25 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - assert_eq!(result_cell.symbol(), expected_char.to_string()); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - if row_index == 0 - || row_index == usize::from(h - 1) - || char_index < 8 - || char_index > usize::from(w - 9) - { - assert_eq!(result_cell.fg, Color::Reset); - assert_eq!(result_cell.bg, Color::Reset); - } else { - assert_eq!(result_cell.bg, Color::White); - } - - // Borders are black - if BORDER_CHARS.contains(&result_cell.symbol()) { - assert_eq!(result_cell.fg, Color::Black); - // Container name is red - } else if row_index == 3 && (57..=82).contains(&char_index) { - assert_eq!(result_cell.fg, Color::Red); - // All other text is black - } else if !row_index == 0 - && !row_index == usize::from(h - 1) - && !char_index < 8 - && !char_index > usize::from(w - 9) - { - assert_eq!(result_cell.fg, Color::Black); + match (row_index, result_cell_index) { + (0 | 9, _) | (1..=8, 0..=7 | 98..=106) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + (3, 57..=91) => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Red); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Black); + } } } } @@ -2764,22 +2742,17 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(expected_char.to_string(), result_cell.symbol()); - let (fg, bg) = if row_index >= 6 && char_index >= 32 { - (Color::White, Color::Blue) - } else { - (Color::Reset, Color::Reset) + let (bg, fg) = match (row_index, result_cell_index) { + (6..=8, 32..=44) => (Color::Blue, Color::White), + _ => (Color::Reset, Color::Reset), }; - - assert_eq!(result_cell.fg, fg); assert_eq!(result_cell.bg, bg); + assert_eq!(result_cell.fg, fg); } } } @@ -2794,11 +2767,47 @@ mod tests { let (w, h) = (140, 1); let mut setup = test_setup(w, h, true, true); - setup.app_data.lock().filter_term_push('c'); setup .gui_state .lock() .status_push(crate::ui::Status::Filter); + setup + .terminal + .draw(|f| { + super::filter_bar(setup.area, f, &setup.app_data); + }) + .unwrap(); + + let expected = [ + " Enter done Esc clear filter: " + ]; + + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=6 | 13..=17 => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + 7..=12 | 18..=24 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); + } + 25..=32 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + } + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + } + } + + setup.app_data.lock().filter_term_push('c'); setup .terminal @@ -2808,32 +2817,31 @@ mod tests { .unwrap(); let expected = [ - " Enter done Esc clear filter: c " - ]; - let result = &setup.terminal.backend().buffer().content; + " Enter done Esc clear filter: c " + ]; - for (row_index, row) in result.chunks(usize::from(w)).enumerate() { + for (row_index, result_row) in get_result(&setup, w) { let expected_row = expected_to_vec(&expected, row_index); - for (cell_index, cell) in row.iter().enumerate() { - match cell_index { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + match result_cell_index { 0..=6 | 13..=17 => { - assert_eq!(cell.bg, Color::Magenta); - assert_eq!(cell.fg, Color::Black); + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); } 7..=12 | 18..=24 | 33 => { - assert_eq!(cell.bg, Color::Reset); - assert_eq!(cell.fg, Color::Gray); + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); } 25..=32 => { - assert_eq!(cell.bg, Color::Reset); - assert_eq!(cell.fg, Color::Magenta); + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); } _ => { - assert_eq!(cell.bg, Color::Reset); - assert_eq!(cell.fg, Color::Reset); + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); } } - assert_eq!(cell.symbol(), expected_row[cell_index]); } } } @@ -2855,39 +2863,33 @@ mod tests { }) .unwrap(); - let expected = vec![ - " ".to_owned(), - " ╭───────────────── Error ──────────────────╮ ".to_owned(), - " │ │ ".to_owned(), - " │ Unable to access docker daemon │ ".to_owned(), - " │ │ ".to_owned(), - format!(" │ oxker::v{VERSION} closing in 04 seconds │ "), - " │ │ ".to_owned(), - " ╰──────────────────────────────────────────╯ ".to_owned(), - " ".to_owned(), + let version_row = format!(" │ oxker::v{VERSION} closing in 04 seconds │ "); + let expected = [ + " ", + " ╭───────────────── Error ──────────────────╮ ", + " │ │ ", + " │ Unable to access docker daemon │ ", + " │ │ ", + version_row.as_str(), + " │ │ ", + " ╰──────────────────────────────────────────╯ ", + " ", ]; - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - - if (1..=usize::from(h) - 2).contains(&row_index) - && (1..=usize::from(w) - 2).contains(&char_index) - { - assert_eq!(result_cell.bg, Color::Red); - } - if result_cell - .symbol() - .chars() - .next() - .unwrap() - .is_alphanumeric() - { - assert_eq!(result_cell.fg, Color::White); + match (row_index, result_cell_index) { + (0 | 8, _) | (1..=7, 0 | 45) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + _ => { + assert_eq!(result_cell.bg, Color::Red); + assert_eq!(result_cell.fg, Color::White); + } } } } @@ -2919,27 +2921,20 @@ mod tests { " ", ]; - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); - assert_eq!(result_cell.symbol(), expected_char.to_string()); - if (1..=usize::from(h) - 2).contains(&row_index) - && (1..=usize::from(w) - 2).contains(&char_index) - { - assert_eq!(result_cell.bg, Color::Red); - } - if result_cell - .symbol() - .chars() - .next() - .unwrap() - .is_alphanumeric() - || ["(", ")"].contains(&result_cell.symbol()) - { - assert_eq!(result_cell.fg, Color::White); + match (row_index, result_cell_index) { + (0 | 9, _) | (1..=8, 0 | 38) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + _ => { + assert_eq!(result_cell.bg, Color::Red); + assert_eq!(result_cell.fg, Color::White); + } } } } @@ -2971,23 +2966,31 @@ mod tests { "╰────────────────────────────╯", ]; - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(expected_char.to_string(), result_cell.symbol()); - if row_index == 0 && !BORDER_CHARS.contains(&result_cell.symbol()) { - assert_eq!(result_cell.fg, Color::Green); - assert_eq!(result_cell.modifier, Modifier::BOLD); - } else { - assert_eq!(result_cell.fg, Color::Reset); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + match (row_index, result_cell_index) { + (0, 11..=17) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Green); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + (1, 11..=18) => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + assert!(result_cell.modifier.is_empty()); + } } } } - // when state is "State::Running | State::Paused | State::Restarting, won't show "no ports" + // When state is "State::Running | State::Paused | State::Restarting, won't show "no ports" setup.app_data.lock().containers.items[0].state = State::Dead; let max_lens = setup.app_data.lock().get_longest_port(); setup @@ -3008,18 +3011,17 @@ mod tests { "╰────────────────────────────╯", ]; - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(expected_char.to_string(), result_cell.symbol()); - if row_index == 0 && !BORDER_CHARS.contains(&result_cell.symbol()) { + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Reset); + if let (0, 11..=17) = (row_index, result_cell_index) { assert_eq!(result_cell.fg, Color::Red); assert_eq!(result_cell.modifier, Modifier::BOLD); } else { assert_eq!(result_cell.fg, Color::Reset); + assert!(result_cell.modifier.is_empty()); } } } @@ -3065,31 +3067,30 @@ mod tests { "╰──────────────────────────────╯", ]; - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Reset); - assert_eq!(expected_char.to_string(), result_cell.symbol()); + match (row_index, result_cell_index) { + (0, 12..=18) => { + assert_eq!(result_cell.fg, Color::Green); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + (1, 1..=28) => { + assert_eq!(result_cell.fg, Color::Yellow); + assert!(result_cell.modifier.is_empty()); + } + (2..=4, 1..=28) => { + assert_eq!(result_cell.fg, Color::White); + assert!(result_cell.modifier.is_empty()); + } - let result_cell_as_char = result_cell - .symbol() - .chars() - .next() - .unwrap() - .is_ascii_alphanumeric(); - if row_index == 0 && result_cell_as_char { - assert_eq!(result_cell.fg, Color::Green); - } - if row_index == 1 && result_cell_as_char { - assert_eq!(result_cell.fg, Color::Yellow); - } - if (2..=3).contains(&row_index) && result_cell_as_char { - assert_eq!(result_cell.fg, Color::White); - } - if row_index == 4 && result_cell_as_char { - assert_eq!(result_cell.fg, Color::White); + _ => { + assert_eq!(result_cell.fg, Color::Reset); + assert!(result_cell.modifier.is_empty()); + } } } } @@ -3102,6 +3103,36 @@ mod tests { let mut setup = test_setup(w, h, true, true); let max_lens = setup.app_data.lock().get_longest_port(); + setup + .terminal + .draw(|f| { + super::ports(f, setup.area, &setup.app_data, max_lens); + }) + .unwrap(); + + let expected = [ + "╭─────────── ports ────────────╮", + "│ ip private public │", + "│ 8001 │", + "│ │", + "│ │", + "│ │", + "│ │", + "╰──────────────────────────────╯", + ]; + + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Reset); + if let (0, 12..=18) = (row_index, result_cell_index) { + assert_eq!(result_cell.fg, Color::Green); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + } + } + setup.app_data.lock().containers.items[0].state = State::Paused; setup .terminal @@ -3110,39 +3141,19 @@ mod tests { }) .unwrap(); - let expected = [ - "╭─────────── ports ────────────╮", - "│ ip private public │", - "│ 8001 │", - "│ │", - "│ │", - "│ │", - "│ │", - "╰──────────────────────────────╯", - ]; - - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(expected_char.to_string(), result_cell.symbol()); - - if row_index == 0 - && result_cell - .symbol() - .chars() - .next() - .unwrap() - .is_ascii_alphanumeric() - { + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Reset); + if let (0, 12..=18) = (row_index, result_cell_index) { assert_eq!(result_cell.fg, Color::Yellow); + assert_eq!(result_cell.modifier, Modifier::BOLD); } } } - setup.app_data.lock().containers.items[0].state = State::Dead; + setup.app_data.lock().containers.items[0].state = State::Exited; setup .terminal .draw(|f| { @@ -3150,35 +3161,14 @@ mod tests { }) .unwrap(); - // This is wrong - why? - let expected = [ - "╭─────────── ports ────────────╮", - "│ ip private public │", - "│ 8001 │", - "│ │", - "│ │", - "│ │", - "│ │", - "╰──────────────────────────────╯", - ]; - - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(expected_char.to_string(), result_cell.symbol()); - - if row_index == 0 - && result_cell - .symbol() - .chars() - .next() - .unwrap() - .is_ascii_alphanumeric() - { + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); + assert_eq!(result_cell.bg, Color::Reset); + if let (0, 12..=18) = (row_index, result_cell_index) { assert_eq!(result_cell.fg, Color::Red); + assert_eq!(result_cell.modifier, Modifier::BOLD); } } } @@ -3242,13 +3232,10 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string(),); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); } } } @@ -3309,15 +3296,10 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - - for (row_index, row) in result.chunks(usize::from(w)).enumerate() { - let expected_row = expected[row_index] - .chars() - .map(|i| i.to_string()) - .collect::>(); - for (cell_index, cell) in row.iter().enumerate() { - assert_eq!(cell.symbol(), expected_row[cell_index]); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); } } @@ -3330,37 +3312,37 @@ mod tests { setup.app_data.lock().filter_term_push('1'); let expected = [ - " name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", - "╭ Containers 1/1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮╭──────────────╮", - "│⚪ container_1 ✓ running Up 1 hour 03.00% 30.00 kB / 30.00 kB 1 image_1 0.00 kB 0.00 kB ││▶ pause │", - "│ ││ restart │", - "│ ││ stop │", - "│ ││ delete │", - "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯╰──────────────╯", - "╭ Logs 3/3 - container_1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮", - "│ line 1 │", - "│ line 2 │", - "│▶ line 3 │", - "│ │", - "│ │", - "│ │", - "│ │", - "│ │", - "│ │", - "│ │", - "│ │", - "│ │", - "│ │", - "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", - "╭───────────────────────── cpu 03.00% ──────────────────────────╮╭─────────────────────── memory 30.00 kB ───────────────────────╮╭────────── ports ───────────╮", - "│10.00%│ ••• ││100.00 kB│ •• ││ ip private public│", - "│ │ •• • ││ │ •• • ││ 8001 │", - "│ │ ••• • • ││ │ ••• • • ││ │", - "│ │• •• ││ │• •• ││ │", - "│ │ ││ │ ││ │", - "╰───────────────────────────────────────────────────────────────╯╰───────────────────────────────────────────────────────────────╯╰────────────────────────────╯", - " Enter done Esc clear filter: r_1 " - ]; + " name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help ", + "╭ Containers 1/1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮╭──────────────╮", + "│⚪ container_1 ✓ running Up 1 hour 03.00% 30.00 kB / 30.00 kB 1 image_1 0.00 kB 0.00 kB ││▶ pause │", + "│ ││ restart │", + "│ ││ stop │", + "│ ││ delete │", + "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯╰──────────────╯", + "╭ Logs 3/3 - container_1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮", + "│ line 1 │", + "│ line 2 │", + "│▶ line 3 │", + "│ │", + "│ │", + "│ │", + "│ │", + "│ │", + "│ │", + "│ │", + "│ │", + "│ │", + "│ │", + "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", + "╭───────────────────────── cpu 03.00% ──────────────────────────╮╭─────────────────────── memory 30.00 kB ───────────────────────╮╭────────── ports ───────────╮", + "│10.00%│ ••• ││100.00 kB│ •• ││ ip private public│", + "│ │ •• • ││ │ •• • ││ 8001 │", + "│ │ ••• • • ││ │ ••• • • ││ │", + "│ │• •• ││ │• •• ││ │", + "│ │ ││ │ ││ │", + "╰───────────────────────────────────────────────────────────────╯╰───────────────────────────────────────────────────────────────╯╰────────────────────────────╯", + " Enter done Esc clear filter: r_1 " + ]; setup .terminal .draw(|f| { @@ -3368,15 +3350,10 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - - for (row_index, row) in result.chunks(usize::from(w)).enumerate() { - let expected_row = expected[row_index] - .chars() - .map(|i| i.to_string()) - .collect::>(); - for (cell_index, cell) in row.iter().enumerate() { - assert_eq!(cell.symbol(), expected_row[cell_index]); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); } } } @@ -3441,13 +3418,10 @@ mod tests { }) .unwrap(); - let result = &setup.terminal.backend().buffer().content; - for (row_index, row) in expected.iter().enumerate() { - for (char_index, expected_char) in row.chars().enumerate() { - let index = row_index * usize::from(w) + char_index; - let result_cell = &result[index]; - - assert_eq!(result_cell.symbol(), expected_char.to_string(),); + for (row_index, result_row) in get_result(&setup, w) { + let expected_row = expected_to_vec(&expected, row_index); + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + assert_eq!(result_cell.symbol(), expected_row[result_cell_index]); } } }