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
|
"""Check that a check is working."""
import codecs
import os
from unittest import TestCase
class Check(TestCase):
"""All tests inherit from Check."""
__test__ = False
def setUp(self):
"""Create a placeholder for setup procedure."""
pass
def tearDown(self):
"""Create a placeholder for teardown procedure."""
pass
@property
def this_check(self):
"""Create a placeholder for the specific check."""
raise NotImplementedError
def passes(self, lst):
"""Check if the test runs cleanly on the given text."""
if isinstance(lst, str):
lst = [lst]
errors = []
for text in lst:
errors += self.this_check.check.__wrapped__(text)
return len(errors) == 0
def wpe_too_high(self):
"""Check whether the check is too noisy."""
min_wpe = 50
examples_dir = os.path.join(os.getcwd(), "tests", "corpus")
examples = os.listdir(examples_dir)
for example in examples:
example_path = os.path.join(examples_dir, example)
if ".DS_Store" in example_path:
break
# Compute the number of words per (wpe) error.
with codecs.open(example_path, "r", encoding='utf-8') as f:
text = f.read()
num_errors = len(self.this_check.check.__wrapped__(text))
num_words = len(text)
try:
wpe = 1.0 * num_words / num_errors
except ZeroDivisionError:
wpe = float('Inf')
# Make sure that
assert wpe > min_wpe, \
f"{example} has only {round(wpe, 2)} wpe."
|