Skip to content

Commit 04e55e4

Browse files
author
Ed Page
committed
fix(dict): Correctly connect dict with varcon
We had a bug where `finallizes` with EnGb would not correct to `finalises`
1 parent 77cfccb commit 04e55e4

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

src/dict.rs

+77-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ impl BuiltIn {
3535

3636
let word = word_token.token();
3737
let mut corrections = if let Some(correction) = self.correct_with_dict(word) {
38-
self.correct_with_vars(word)
39-
.unwrap_or_else(|| Status::Corrections(vec![Cow::Borrowed(correction)]))
38+
match self.correct_with_vars(correction) {
39+
Some(Status::Valid) => Status::Corrections(vec![Cow::Borrowed(correction)]),
40+
Some(correction @ Status::Corrections(_)) => correction,
41+
Some(Status::Invalid) => {
42+
unreachable!("correct_with_vars should always have valid suggestions")
43+
}
44+
None => Status::Corrections(vec![Cow::Borrowed(correction)]),
45+
}
4046
} else {
4147
self.correct_with_vars(word)?
4248
};
@@ -244,6 +250,75 @@ impl<'i, 'w, D: typos::Dictionary> typos::Dictionary for Override<'i, 'w, D> {
244250
mod test {
245251
use super::*;
246252

253+
#[cfg(feature = "dict")]
254+
#[test]
255+
fn test_dict_correct() {
256+
let dict = BuiltIn::new(crate::config::Locale::default());
257+
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
258+
"finallizes",
259+
typos::tokens::Case::Lower,
260+
0,
261+
));
262+
assert_eq!(
263+
correction,
264+
Some(Status::Corrections(vec!["finalizes".into()]))
265+
);
266+
}
267+
268+
#[cfg(feature = "vars")]
269+
#[test]
270+
fn test_varcon_no_locale() {
271+
let dict = BuiltIn::new(crate::config::Locale::En);
272+
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
273+
"finalizes",
274+
typos::tokens::Case::Lower,
275+
0,
276+
));
277+
assert_eq!(correction, Some(Status::Valid));
278+
}
279+
280+
#[cfg(feature = "vars")]
281+
#[test]
282+
fn test_varcon_same_locale() {
283+
let dict = BuiltIn::new(crate::config::Locale::EnUs);
284+
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
285+
"finalizes",
286+
typos::tokens::Case::Lower,
287+
0,
288+
));
289+
assert_eq!(correction, Some(Status::Valid));
290+
}
291+
292+
#[cfg(feature = "vars")]
293+
#[test]
294+
fn test_varcon_different_locale() {
295+
let dict = BuiltIn::new(crate::config::Locale::EnGb);
296+
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
297+
"finalizes",
298+
typos::tokens::Case::Lower,
299+
0,
300+
));
301+
assert_eq!(
302+
correction,
303+
Some(Status::Corrections(vec!["finalises".into()]))
304+
);
305+
}
306+
307+
#[cfg(all(feature = "dict", feature = "vars"))]
308+
#[test]
309+
fn test_dict_to_varcon() {
310+
let dict = BuiltIn::new(crate::config::Locale::EnGb);
311+
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
312+
"finallizes",
313+
typos::tokens::Case::Lower,
314+
0,
315+
));
316+
assert_eq!(
317+
correction,
318+
Some(Status::Corrections(vec!["finalises".into()]))
319+
);
320+
}
321+
247322
#[test]
248323
fn test_case_correct() {
249324
let cases = [

0 commit comments

Comments
 (0)