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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::{
types::{Rule, TestDefinition, TestResult},
DisplayLevel::{self, *},
Phonet,
};
use Reason::*;
use ValidStatus::*;
pub struct PhonetResults {
pub list: Vec<TestResult>,
pub fail_count: u32,
}
impl PhonetResults {
pub fn run(scheme: &Phonet) -> PhonetResults {
if scheme.tests.is_empty() {
return PhonetResults {
list: Vec::new(),
fail_count: 0,
};
}
let mut list = vec![];
let mut fail_count = 0;
let mut max_word_len = 0;
for test in &scheme.tests {
match test {
TestDefinition::Note(note) => list.push(TestResult::Note(note.to_string())),
TestDefinition::Test { intent, word } => {
let validity = validate_test(word, &scheme.rules);
let pass = !(validity.is_valid() ^ intent);
let reason = if !pass {
Reason::from(validity, &scheme.reasons)
} else {
Passed
};
if !pass {
fail_count += 1;
}
if word.len() > max_word_len {
max_word_len = word.len();
}
list.push(TestResult::Test {
intent: *intent,
word: word.to_string(),
pass,
reason,
});
}
}
}
PhonetResults { list, fail_count }
}
fn max_word_len(&self, display_level: DisplayLevel) -> usize {
self
.list
.iter()
.map(|x| match x {
TestResult::Test { word, pass, .. } => match display_level {
ShowAll => word.len(),
NotesAndFails | JustFails if !pass => word.len(),
_ => 0,
},
_ => 0,
})
.max()
.unwrap_or(10)
}
pub fn display(&self, display_level: DisplayLevel, no_color: bool) {
if self.list.is_empty() {
if no_color {
println!("No tests ran.");
} else {
println!("\x1b[33mNo tests ran.\x1b[0m");
}
return;
}
let max_word_len = self.max_word_len(display_level);
for item in &self.list {
match item {
TestResult::Note(note) => match display_level {
ShowAll | NotesAndFails => {
if no_color {
println!("{note}")
} else {
println!("\x1b[34m{note}\x1b[0m")
}
}
_ => (),
},
TestResult::Test {
intent,
word,
pass,
reason,
} => {
if match display_level {
ShowAll => false,
NotesAndFails | JustFails if !pass => false,
_ => true,
} {
continue;
}
let reason = match &reason {
Passed => "",
ShouldBeInvalid => {
if no_color {
"Valid, but should be invalid"
} else {
"\x1b[33mValid, but should be invalid\x1b[0m"
}
}
NoReasonGiven => "No reason given",
Custom(reason) => reason,
};
if no_color {
println!(
" {intent} {word}{space} {result} {reason}",
intent = if *intent { "✔" } else { "✗" },
space = " ".repeat(max_word_len - word.chars().count()),
result = if *pass { "pass" } else { "FAIL" },
);
} else {
println!(
" \x1b[{intent}\x1b[0m {word}{space} \x1b[1;{result} \x1b[0;3;1m{reason}\x1b[0m",
intent = if *intent { "36m✔" } else { "35m✗" },
space = " ".repeat(max_word_len - word.chars().count()),
result = if *pass { "32mpass" } else { "31mFAIL" },
);
}
}
}
}
if self.fail_count == 0 {
if no_color {
println!("All tests pass!");
} else {
println!("\x1b[32;1;3mAll tests pass!\x1b[0m");
}
} else {
if no_color {
println!(
"{fails} test{s} failed!",
fails = self.fail_count,
s = if self.fail_count == 1 { "" } else { "s" },
);
} else {
println!(
"\x1b[31;1;3m{fails} test{s} failed!\x1b[0m",
fails = self.fail_count,
s = if self.fail_count == 1 { "" } else { "s" },
);
}
}
}
}
pub enum Reason {
Passed,
NoReasonGiven,
ShouldBeInvalid,
Custom(String),
}
impl Reason {
fn from(validity: ValidStatus, reasons: &[String]) -> Self {
match validity {
Valid => ShouldBeInvalid,
Invalid(reason) => match reason {
None => NoReasonGiven,
Some(reason) => match reasons.get(reason) {
Some(x) => Reason::Custom(x.to_string()),
None => NoReasonGiven,
},
},
}
}
}
pub enum ValidStatus {
Valid,
Invalid(Option<usize>),
}
impl ValidStatus {
pub fn is_valid(&self) -> bool {
if let Valid = self {
return true;
}
false
}
}
pub fn validate_test(word: &str, rules: &Vec<Rule>) -> ValidStatus {
for Rule {
intent,
pattern,
reason_ref,
} in rules
{
if intent
^ pattern
.is_match(word)
.expect("Failed checking regex match. This error should NEVER APPEAR!")
{
return Invalid(*reason_ref);
}
}
Valid
}