| 1 | from unittest import TestCase |
| 2 | from pgzero import spellcheck |
| 3 | |
| 4 | |
| 5 | class SuggestionTest(TestCase): |
| 6 | HOOKS = spellcheck.HOOKS |
| 7 | |
| 8 | def assert_suggestions(self, w, candidates, expected): |
| 9 | suggestions = spellcheck.suggest(w, candidates) |
| 10 | self.assertEqual(suggestions, expected) |
| 11 | |
| 12 | def assert_best_suggestion(self, w, expected): |
| 13 | suggestions = spellcheck.suggest(w, self.HOOKS) |
| 14 | self.assertEqual(suggestions[0], expected) |
| 15 | |
| 16 | def test_distance_zero(self): |
| 17 | self.assertEqual( |
| 18 | spellcheck.distance('on_mouse_down', 'on_mouse_down'), |
| 19 | 0 |
| 20 | ) |
| 21 | |
| 22 | def test_misspelled(self): |
| 23 | """With only one suggestion, return it.""" |
| 24 | self.assert_suggestions('drow', ['draw'], ['draw']) |
| 25 | |
| 26 | def test_not_misspelled(self): |
| 27 | """With only completely different words, there are no candidates.""" |
| 28 | self.assert_suggestions('fire_laser', ['draw', 'update'], []) |
| 29 | |
| 30 | def test_misspelled_on_mouse_down(self): |
| 31 | self.assert_best_suggestion('onmousedown', 'on_mouse_down') |
| 32 | |
| 33 | def test_misspelled_on_mouse_down2(self): |
| 34 | self.assert_best_suggestion('onMouseDown', 'on_mouse_down') |
| 35 | |
| 36 | def test_misspelled_on_mouse_down3(self): |
| 37 | self.assert_best_suggestion('on_muose_Down', 'on_mouse_down') |
| 38 | |
| 39 | def test_misspelled_on_mouse_down4(self): |
| 40 | self.assert_best_suggestion('on_mouse_don', 'on_mouse_down') |
| 41 | |
| 42 | |
| 43 | class LoggingSpellCheckResult: |
| 44 | def __init__(self): |
| 45 | self.warnings = [] |
| 46 | self.errors = [] |
| 47 | |
| 48 | def warn(self, msg, found, suggestion): |
| 49 | self.warnings.append((found, suggestion)) |
| 50 | |
| 51 | def error(self, msg, found, suggestion): |
| 52 | self.errors.append((found, suggestion)) |
| 53 | |
| 54 | def has_error(self, found, suggestion): |
| 55 | return (found, suggestion) in self.errors |
| 56 | |
| 57 | def has_warning(self, found, suggestion): |
| 58 | return (found, suggestion) in self.warnings |
| 59 | |
| 60 | def get_report(self): |
| 61 | lines = [] |
| 62 | for type in ('warnings', 'errors'): |
| 63 | msgs = getattr(self, type) |
| 64 | if msgs: |
| 65 | lines += [ |
| 66 | 'Got %s:' % type, |
| 67 | ] |
| 68 | lines += [' %s -> %s' % m for m in msgs] |
| 69 | else: |
| 70 | lines += ['No %s emitted' % type] |
| 71 | return '\n'.join(lines) |
| 72 | |
| 73 | |
| 74 | class SpellCheckerTest(TestCase): |
| 75 | def setUp(self): |
| 76 | self.result = LoggingSpellCheckResult() |
| 77 | |
| 78 | def assert_has_warning(self, found, suggestion): |
| 79 | if self.result.has_warning(found, suggestion): |
| 80 | return |
| 81 | |
| 82 | raise AssertionError( |
| 83 | 'Expected warning (%s -> %s)\n' % (found, suggestion) + |
| 84 | self.result.get_report() |
| 85 | ) |
| 86 | |
| 87 | def assert_has_error(self, found, suggestion): |
| 88 | if self.result.has_error(found, suggestion): |
| 89 | return |
| 90 | |
| 91 | raise AssertionError( |
| 92 | 'Expected error (%s -> %s)\n' % (found, suggestion) + |
| 93 | self.result.get_report() |
| 94 | ) |
| 95 | |
| 96 | def spellcheck(self, namespace): |
| 97 | spellcheck.spellcheck(namespace, self.result) |
| 98 | |
| 99 | def test_misspelled_mousedown(self): |
| 100 | self.spellcheck({ |
| 101 | 'on_moose_down': lambda: None, |
| 102 | }) |
| 103 | self.assert_has_warning('on_moose_down', 'on_mouse_down') |
| 104 | |
| 105 | def test_misspelled_width_uppercase(self): |
| 106 | self.spellcheck({ |
| 107 | 'WIDHT': 640, |
| 108 | }) |
| 109 | self.assert_has_warning('WIDHT', 'WIDTH') |
| 110 | |
| 111 | def test_misspelled_width_lowercase(self): |
| 112 | self.spellcheck({ |
| 113 | 'widht': 640, |
| 114 | }) |
| 115 | self.assert_has_warning('widht', 'WIDTH') |
| 116 | |
| 117 | def test_misspelled_width_mixed_lower_and_upper_case(self): |
| 118 | self.spellcheck({ |
| 119 | 'WIDht': 640, |
| 120 | }) |
| 121 | self.assert_has_warning('WIDht', 'WIDTH') |
| 122 | |
| 123 | def test_misspelled_param(self): |
| 124 | self.spellcheck({ |
| 125 | 'on_mouse_down': lambda buton: None, |
| 126 | }) |
| 127 | self.assert_has_error('buton', 'button') |