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
|
from unittest import TestCase
from pgzero import spellcheck
class SuggestionTest(TestCase):
HOOKS = spellcheck.HOOKS
def assert_suggestions(self, w, candidates, expected):
suggestions = spellcheck.suggest(w, candidates)
self.assertEqual(suggestions, expected)
def assert_best_suggestion(self, w, expected):
suggestions = spellcheck.suggest(w, self.HOOKS)
self.assertEqual(suggestions[0], expected)
def test_distance_zero(self):
self.assertEqual(
spellcheck.distance('on_mouse_down', 'on_mouse_down'),
0
)
def test_misspelled(self):
"""With only one suggestion, return it."""
self.assert_suggestions('drow', ['draw'], ['draw'])
def test_not_misspelled(self):
"""With only completely different words, there are no candidates."""
self.assert_suggestions('fire_laser', ['draw', 'update'], [])
def test_misspelled_on_mouse_down(self):
self.assert_best_suggestion('onmousedown', 'on_mouse_down')
def test_misspelled_on_mouse_down2(self):
self.assert_best_suggestion('onMouseDown', 'on_mouse_down')
def test_misspelled_on_mouse_down3(self):
self.assert_best_suggestion('on_muose_Down', 'on_mouse_down')
def test_misspelled_on_mouse_down4(self):
self.assert_best_suggestion('on_mouse_don', 'on_mouse_down')
class LoggingSpellCheckResult:
def __init__(self):
self.warnings = []
self.errors = []
def warn(self, msg, found, suggestion):
self.warnings.append((found, suggestion))
def error(self, msg, found, suggestion):
self.errors.append((found, suggestion))
def has_error(self, found, suggestion):
return (found, suggestion) in self.errors
def has_warning(self, found, suggestion):
return (found, suggestion) in self.warnings
def get_report(self):
lines = []
for type in ('warnings', 'errors'):
msgs = getattr(self, type)
if msgs:
lines += [
'Got %s:' % type,
]
lines += [' %s -> %s' % m for m in msgs]
else:
lines += ['No %s emitted' % type]
return '\n'.join(lines)
class SpellCheckerTest(TestCase):
def setUp(self):
self.result = LoggingSpellCheckResult()
def assert_has_warning(self, found, suggestion):
if self.result.has_warning(found, suggestion):
return
raise AssertionError(
'Expected warning (%s -> %s)\n' % (found, suggestion) +
self.result.get_report()
)
def assert_has_error(self, found, suggestion):
if self.result.has_error(found, suggestion):
return
raise AssertionError(
'Expected error (%s -> %s)\n' % (found, suggestion) +
self.result.get_report()
)
def spellcheck(self, namespace):
spellcheck.spellcheck(namespace, self.result)
def test_misspelled_mousedown(self):
self.spellcheck({
'on_moose_down': lambda: None,
})
self.assert_has_warning('on_moose_down', 'on_mouse_down')
def test_misspelled_width_uppercase(self):
self.spellcheck({
'WIDHT': 640,
})
self.assert_has_warning('WIDHT', 'WIDTH')
def test_misspelled_width_lowercase(self):
self.spellcheck({
'widht': 640,
})
self.assert_has_warning('widht', 'WIDTH')
def test_misspelled_width_mixed_lower_and_upper_case(self):
self.spellcheck({
'WIDht': 640,
})
self.assert_has_warning('WIDht', 'WIDTH')
def test_misspelled_param(self):
self.spellcheck({
'on_mouse_down': lambda buton: None,
})
self.assert_has_error('buton', 'button')
|