From 39b166243a2cbe2909578ff9ef1a9379325f70fb Mon Sep 17 00:00:00 2001 From: Nytelife26 Date: Tue, 28 May 2024 11:37:55 +0100 Subject: [PATCH] test: add tests for reverse_existence_check --- proselint/checks/restricted/elementary.py | 12 ++++++ proselint/checks/restricted/top1000.py | 12 ++++++ tests/test_restricted_elementary.py | 27 ------------- tests/test_restricted_top1000.py | 27 ------------- tests/test_reverse_existence_check.py | 49 +++++++++++++++++++++++ 5 files changed, 73 insertions(+), 54 deletions(-) delete mode 100644 tests/test_restricted_elementary.py delete mode 100644 tests/test_restricted_top1000.py create mode 100644 tests/test_reverse_existence_check.py diff --git a/proselint/checks/restricted/elementary.py b/proselint/checks/restricted/elementary.py index 85a4f1ce6..6b5de5fab 100644 --- a/proselint/checks/restricted/elementary.py +++ b/proselint/checks/restricted/elementary.py @@ -24,6 +24,18 @@ import proselint from proselint.checks import CheckResult, reverse_existence_check +examples_pass = [ + "A boy and his goat went to a farm.", + "I am tired.", + "Your body is made of water.", +] + +examples_fail = [ + "Cells make up your body.", + "I love clowns.", + "I hate cells and clowns.", +] + _CSV_PATH = "checks/restricted/elementary.csv" with files(proselint).joinpath(_CSV_PATH).open("r") as data: ELEMENTARY_WORDS = data.read().split() diff --git a/proselint/checks/restricted/top1000.py b/proselint/checks/restricted/top1000.py index 68993d9f5..a9222d7d2 100644 --- a/proselint/checks/restricted/top1000.py +++ b/proselint/checks/restricted/top1000.py @@ -24,6 +24,18 @@ import proselint from proselint.checks import CheckResult, reverse_existence_check +examples_pass = [ + "I am blonde.", + "I'm gonna listen to music tonight.", + "I will go to sleep because I have school.", +] + +examples_fail = [ + "I am tired.", + "I hate broccoli.", + "I am tired and hate broccoli.", +] + _CSV_PATH = "checks/restricted/top1000.csv" with files(proselint).joinpath(_CSV_PATH).open("r") as data: diff --git a/tests/test_restricted_elementary.py b/tests/test_restricted_elementary.py deleted file mode 100644 index a6289ca3c..000000000 --- a/tests/test_restricted_elementary.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Tests reverse existence for elementary and check.""" - -from proselint.checks.restricted import elementary as chk - -from .check import Check - - -class TestCheck(Check): - """The test class for restricted.elementary.""" - - __test__ = True - - @property - def this_check(self): - """Boilerplate.""" - return chk - - def test_smoke(self): - """Basic smoke test for restricted.elementary.""" - assert self.passes("""A boy and his goat went to a farm.""") - assert self.passes("""I am tired.""") - assert self.passes("""Water make up your body.""") - assert self.passes("""""") - - assert not self.passes(""" Cells make up your body.""") - assert not self.passes("I love clowns.") - assert not self.passes(""" I hate cells and clowns.""") diff --git a/tests/test_restricted_top1000.py b/tests/test_restricted_top1000.py deleted file mode 100644 index 3ca2dc0e9..000000000 --- a/tests/test_restricted_top1000.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Tests reverse existence for top1000 and check.""" - -from proselint.checks.restricted import top1000 as chk - -from .check import Check - - -class TestCheck(Check): - """The test class for restricted.top1000.""" - - __test__ = True - - @property - def this_check(self): - """Boilerplate.""" - return chk - - def test_smoke(self): - """Basic smoke test for restricted.top1000.""" - assert self.passes("""I am blonde.""") - assert self.passes("""I'm gonna listen to music tonight.""") - assert self.passes("""I will go to sleep because I have school.""") - assert self.passes("""""") - - assert not self.passes("""I am tired.""") - assert not self.passes("""I hate broccoli.""") - assert not self.passes("""I am tired and hate broccoli.""") diff --git a/tests/test_reverse_existence_check.py b/tests/test_reverse_existence_check.py new file mode 100644 index 000000000..5de76363d --- /dev/null +++ b/tests/test_reverse_existence_check.py @@ -0,0 +1,49 @@ +"""Test the reverse_existence_check function from proselint.checks""" +from proselint.checks import reverse_existence_check + +def test_reverse_existence_check(): + """Basic smoke test to determine that reverse_existence_check is working.""" + allowed = ["abc", "cab"] + err = "error message" + msg = "something else exists" + assert reverse_existence_check("abc cab abc abc", allowed, err, msg) == [] + assert reverse_existence_check("easy breezy", allowed, err, msg) != [] + assert err in reverse_existence_check("easy breezy", allowed, err, msg)[0] + assert msg in reverse_existence_check("easy breezy", allowed, err, msg)[0] + +def test_reverse_existence_check_multiple_matches(): + """Test that multiple matches are found correctly.""" + allowed = ["abc", "cab"] + err = "error message" + msg = "something exists" + assert len(reverse_existence_check("abc and abc are easy like cab", allowed, err, msg)) == 4 + assert len(reverse_existence_check("ABC and abc are easy like cab CAB", allowed, err, msg, ignore_case=True)) == 4 + assert len(reverse_existence_check("ABC and abc are easy like cab CAB", allowed, err, msg, ignore_case=False)) == 6 + assert reverse_existence_check("abc cab abc cab", allowed, err, msg) == [] + +def test_reverse_existence_check_special_chars(): + """Test that matches with ' and - are found correctly.""" + allowed = ["abc", "cab"] + err = "error message" + msg = "something exists" + assert len(reverse_existence_check("what's your go-to?", allowed, err, msg)) == 3 + allowed = ["what's"] + assert len(reverse_existence_check("what's your go-to?", allowed, err, msg)) == 2 + allowed = ["what's", "go-to"] + assert len(reverse_existence_check("what's your go-to?", allowed, err, msg)) == 1 + +def test_reverse_existence_check_no_digits(): + """Test that matches with digits are excluded correctly.""" + allowed = ["abc", "cab"] + err = "error message" + msg = "something exists" + assert len(reverse_existence_check("abc cab 123 a5bc noway no7way", allowed, err, msg)) == 1 + assert reverse_existence_check("abc cab 123 a5bc no7way", allowed, err, msg) == [] + +def test_reverse_existence_check_3_chars(): + """Test that matches are exclusively >=3 characters long.""" + allowed = ["abc", "cab"] + err = "error message" + msg = "something exists" + assert len(reverse_existence_check("i am not me", allowed, err, msg)) == 1 + assert reverse_existence_check("i am me", allowed, err, msg) == []