Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: implement `assert_any_suggestion_result` as in #950 (#1033)
Andrew Dunbar5967d707bc24
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 104a78299..0e0d4381d 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -184,6 +184,13 @@ mod tests {);}+ /// Runs a provided linter on text, applies the first suggestion from each lint+ /// and asserts whether the result is equal to a given value.+ #[track_caller]+ pub fn assert_suggestion_result(text: &str, linter: impl Linter, expected_result: &str) {+ assert_nth_suggestion_result(text, linter, expected_result, 0);+ }+/// Runs a provided linter on text, applies the nth suggestion from each lint/// and asserts whether the result is equal to a given value.///@@ -195,6 +202,52 @@ mod tests {expected_result: &str,n: usize,) {+ let transformed_str = transform_nth_str(text, &mut linter, n);++ if transformed_str.as_str() != expected_result {+ panic!(+ "Expected \"{transformed_str}\" to be \"{expected_result}\" after applying the computed suggestions."+ );+ }++ // Applying the suggestions should fix all the lints.+ assert_lint_count(&transformed_str, linter, 0);+ }++ pub fn assert_top3_suggestion_result(+ text: &str,+ mut linter: impl Linter,+ expected_result: &str,+ ) {+ let zeroth = transform_nth_str(text, &mut linter, 0);+ let first = transform_nth_str(text, &mut linter, 1);+ let second = transform_nth_str(text, &mut linter, 2);++ match (+ zeroth.as_str() == expected_result,+ first.as_str() == expected_result,+ second.as_str() == expected_result,+ ) {+ (true, false, false) => assert_lint_count(&zeroth, linter, 0),+ (false, true, false) => assert_lint_count(&first, linter, 0),+ (false, false, true) => assert_lint_count(&second, linter, 0),+ (false, false, false) => panic!(+ "None of the top 3 suggestions produced the expected result:\n\+ Expected: \"{expected_result}\"\n\+ Got:\n\+ [0]: \"{zeroth}\"\n\+ [1]: \"{first}\"\n\+ [2]: \"{second}\""+ ),+ // I think it's not possible for more than one suggestion to be correct+ (true, true, false) => unreachable!(),+ (true, false, true) => unreachable!(),+ (false, true, true) => unreachable!(),+ (true, true, true) => unreachable!(),+ }+ }++ fn transform_nth_str(text: &str, linter: &mut impl Linter, n: usize) -> String {let mut text_chars: Vec<char> = text.chars().collect();let mut iter_count = 0;@@ -229,22 +282,6 @@ mod tests {eprintln!("Corrected {} times.", iter_count);- let transformed_str: String = text_chars.iter().collect();-- if transformed_str.as_str() != expected_result {- panic!(- "Expected \"{transformed_str}\" to be \"{expected_result}\" after applying the computed suggestions."- );- }-- // Applying the suggestions should fix all the lints.- assert_lint_count(&transformed_str, linter, 0);- }-- /// Runs a provided linter on text, applies the first suggestion from each lint- /// and asserts whether the result is equal to a given value.- #[track_caller]- pub fn assert_suggestion_result(text: &str, linter: impl Linter, expected_result: &str) {- assert_nth_suggestion_result(text, linter, expected_result, 0);+ text_chars.iter().collect()}}diff --git a/harper-core/src/linting/possessive_your.rs b/harper-core/src/linting/possessive_your.rsindex b89ec0258..0eadf08c5 100644--- a/harper-core/src/linting/possessive_your.rs+++ b/harper-core/src/linting/possessive_your.rs@@ -40,6 +40,7 @@ impl PatternLinter for PossessiveYour {lint_kind: LintKind::WordChoice,suggestions: vec![Suggestion::replace_with_match_case("your".chars().collect(), orig_chars),+ Suggestion::replace_with_match_case("you're a".chars().collect(), orig_chars),Suggestion::replace_with_match_case("you're an".chars().collect(), orig_chars),],message: "The possessive version of this word is more common in this context."@@ -55,7 +56,9 @@ impl PatternLinter for PossessiveYour {#[cfg(test)]mod tests {- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};+ use crate::linting::tests::{+ assert_lint_count, assert_suggestion_result, assert_top3_suggestion_result,+ };use super::PossessiveYour;@@ -85,4 +88,31 @@ mod tests {0,);}++ #[test]+ fn test_top3_suggestion_your() {+ assert_top3_suggestion_result(+ "You combination of artist and teacher.",+ PossessiveYour::default(),+ "Your combination of artist and teacher.",+ );+ }++ #[test]+ fn test_top3_suggestion_youre_a() {+ assert_top3_suggestion_result(+ "You combination of artist and teacher.",+ PossessiveYour::default(),+ "You're a combination of artist and teacher.",+ );+ }++ // #[test]+ // fn test_top3_suggestion_multiple() {+ // assert_top3_suggestion_result(+ // "You knowledge. You imagination. You icosahedron",+ // PossessiveYour::default(),+ // "Your knowledge. Your imagination. You're an icosahedron",+ // );+ // }}diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rsindex 594c4d822..22c32e815 100644--- a/harper-core/src/linting/spell_check.rs+++ b/harper-core/src/linting/spell_check.rs@@ -131,11 +131,15 @@ impl<T: Dictionary> Linter for SpellCheck<T> {mod tests {use crate::{Dialect, FstDictionary,- linting::tests::{assert_lint_count, assert_suggestion_result},+ linting::tests::{+ assert_lint_count, assert_suggestion_result, assert_top3_suggestion_result,+ },};use super::SpellCheck;+ // Capitalization tests+#[test]fn america_capitalized() {assert_suggestion_result(@@ -145,6 +149,8 @@ mod tests {);}+ // Dialect tests+#[test]fn harper_automattic_capitalized() {assert_lint_count(@@ -288,4 +294,16 @@ mod tests {0,);}++ // Unit tests for specific spell check corrections++ #[test]+ fn corrects_abandonedware_1131_1166() {+ // assert_suggestion_result(+ assert_top3_suggestion_result(+ "Abandonedware is abandoned. Do not bother submitting issues about the empty page bug. Author moved to greener pastures",+ SpellCheck::new(FstDictionary::curated(), Dialect::American),+ "Abandonware is abandoned. Do not bother submitting issues about the empty page bug. Author moved to greener pastures",+ );+ }}