diff options
Diffstat (limited to 'tests/client_ui.rs')
| -rw-r--r-- | tests/client_ui.rs | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/tests/client_ui.rs b/tests/client_ui.rs new file mode 100644 index 0000000..78faed0 --- /dev/null +++ b/tests/client_ui.rs | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | use zagan_bet::client::ui::{ | ||
| 2 | command_names, completion_lines, help_lines, input_cursor_column, lines_to_move_up, | ||
| 3 | parse_bet_arg, record_history, | ||
| 4 | }; | ||
| 5 | |||
| 6 | #[test] | ||
| 7 | fn help_has_no_source_indentation() { | ||
| 8 | for line in help_lines() { | ||
| 9 | assert!( | ||
| 10 | !line.starts_with(" "), | ||
| 11 | "help line kept its source indentation: {line:?}" | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | #[test] | ||
| 17 | fn help_documents_every_command() { | ||
| 18 | let help = help_lines().join("\n"); | ||
| 19 | for command in command_names() { | ||
| 20 | assert!(help.contains(command), "help text is missing '{command}'"); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | #[test] | ||
| 25 | fn completion_knows_every_command() { | ||
| 26 | let commands = command_names(); | ||
| 27 | for expected in [ | ||
| 28 | "connect", | ||
| 29 | "games", | ||
| 30 | "play", | ||
| 31 | "hosts", | ||
| 32 | "send", | ||
| 33 | "logout", | ||
| 34 | "quit", | ||
| 35 | ] { | ||
| 36 | assert!( | ||
| 37 | commands.contains(&expected), | ||
| 38 | "tab completion is missing '{expected}'" | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | #[test] | ||
| 44 | fn history_records_a_command_once() { | ||
| 45 | let mut history = Vec::new(); | ||
| 46 | record_history(&mut history, "info"); | ||
| 47 | record_history(&mut history, "info"); | ||
| 48 | |||
| 49 | assert_eq!(history, vec!["info"]); | ||
| 50 | } | ||
| 51 | |||
| 52 | #[test] | ||
| 53 | fn history_keeps_distinct_commands_in_order() { | ||
| 54 | let mut history = Vec::new(); | ||
| 55 | record_history(&mut history, "info"); | ||
| 56 | record_history(&mut history, "ping"); | ||
| 57 | record_history(&mut history, "info"); | ||
| 58 | |||
| 59 | assert_eq!(history, vec!["info", "ping", "info"]); | ||
| 60 | } | ||
| 61 | |||
| 62 | #[test] | ||
| 63 | fn blank_commands_are_not_recorded() { | ||
| 64 | let mut history = Vec::new(); | ||
| 65 | record_history(&mut history, ""); | ||
| 66 | record_history(&mut history, " "); | ||
| 67 | |||
| 68 | assert!(history.is_empty()); | ||
| 69 | } | ||
| 70 | |||
| 71 | #[test] | ||
| 72 | fn bet_accepts_ten_or_more() { | ||
| 73 | assert_eq!(parse_bet_arg("10"), Ok(10)); | ||
| 74 | assert_eq!(parse_bet_arg("50"), Ok(50)); | ||
| 75 | assert_eq!(parse_bet_arg(" 250 "), Ok(250)); | ||
| 76 | } | ||
| 77 | |||
| 78 | #[test] | ||
| 79 | fn bet_rejects_less_than_ten() { | ||
| 80 | assert!(parse_bet_arg("9").is_err()); | ||
| 81 | assert!(parse_bet_arg("0").is_err()); | ||
| 82 | } | ||
| 83 | |||
| 84 | #[test] | ||
| 85 | fn bet_rejects_non_numbers() { | ||
| 86 | assert!(parse_bet_arg("abc").is_err()); | ||
| 87 | assert!(parse_bet_arg("-50").is_err()); | ||
| 88 | assert!(parse_bet_arg("").is_err()); | ||
| 89 | } | ||
| 90 | |||
| 91 | #[test] | ||
| 92 | fn redraw_moves_up_by_line_count_minus_one() { | ||
| 93 | assert_eq!(lines_to_move_up(0), 0); | ||
| 94 | assert_eq!(lines_to_move_up(1), 0); | ||
| 95 | assert_eq!(lines_to_move_up(7), 6); | ||
| 96 | assert_eq!(lines_to_move_up(70_000), u16::MAX); | ||
| 97 | } | ||
| 98 | |||
| 99 | #[test] | ||
| 100 | fn input_cursor_is_after_the_removed_character() { | ||
| 101 | assert_eq!(input_cursor_column(10, 8), 18); | ||
| 102 | assert_eq!(input_cursor_column(10, 0), 10); | ||
| 103 | assert_eq!(input_cursor_column(2, 5), 7); | ||
| 104 | assert_eq!(input_cursor_column(70_000, 70_000), u16::MAX); | ||
| 105 | } | ||
| 106 | |||
| 107 | #[test] | ||
| 108 | fn completions_wrap_within_the_requested_width() { | ||
| 109 | let commands = command_names(); | ||
| 110 | let lines = completion_lines(&commands, 40); | ||
| 111 | |||
| 112 | assert_eq!(lines.first().map(String::as_str), Some("Possible completions:")); | ||
| 113 | for line in &lines[1..] { | ||
| 114 | assert!( | ||
| 115 | line.len() <= 40, | ||
| 116 | "completion line is too wide ({} > 40): {line:?}", | ||
| 117 | line.len() | ||
| 118 | ); | ||
| 119 | } | ||
| 120 | |||
| 121 | let listed: Vec<&str> = lines[1..] | ||
| 122 | .iter() | ||
| 123 | .flat_map(|line| line.split_whitespace()) | ||
| 124 | .collect(); | ||
| 125 | for command in commands { | ||
| 126 | assert!( | ||
| 127 | listed.contains(&command), | ||
| 128 | "completion list is missing '{command}'" | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | } | ||
