Add missing test; refactor last bit

This commit is contained in:
Sergey Golitsynskiy
2020-06-23 03:32:34 -04:00
parent fd2759a635
commit 9fdea64f57
2 changed files with 12 additions and 8 deletions
+5 -8
View File
@@ -83,17 +83,11 @@ class Inflector(object):
def pluralize(self, word):
'''Pluralizes nouns.'''
return self._handle_nonchanging(word) or \
self._handle_irregular(word) or \
self._apply_rule(self.PLURALIZE_RULES, word) or \
word
return self._transform(self.PLURALIZE_RULES, word)
def singularize(self, word):
'''Singularizes nouns.'''
return self._handle_nonchanging(word) or \
self._handle_irregular(word, pluralize=False) or \
self._apply_rule(self.SINGULARIZE_RULES, word) or \
word
return self._transform(self.SINGULARIZE_RULES, word, pluralize=False)
def cond_plural(self, number_of_records, word):
'''Returns the plural form of a word if first parameter is greater than 1'''
@@ -101,6 +95,9 @@ class Inflector(object):
return self.pluralize(word)
return word
def _transform(self, rules, word, pluralize=True):
return self._handle_nonchanging(word) or self._handle_irregular(word, pluralize=pluralize) or self._apply_rule(rules, word) or word
def _handle_nonchanging(self, word):
if word.lower() in self.NONCHANGING_WORDS:
return word
+7
View File
@@ -86,3 +86,10 @@ def test_pluralize_rules(test_data, inflector):
@pytest.mark.parametrize('test_data', SINGULAR_TO_PLURAL.items())
def test_singularize_rules(test_data, inflector):
assert test_data[0] == inflector.singularize(test_data[1])
def test_cond_plural(inflector):
assert 'edge' == inflector.cond_plural(1, 'edge')
assert 'edges' == inflector.cond_plural(-1, 'edge')
assert 'edges' == inflector.cond_plural(0, 'edge')
assert 'edges' == inflector.cond_plural(2, 'edge')