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