summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2018-02-23 20:45:40 +0330
committerTim Graham <timograham@gmail.com>2018-02-23 14:20:00 -0500
commit5033999153f7303a18d94ea2f08ed78545c0f3df (patch)
treebc761292420fb011b993a541f9f74fc2697ebcc0
parentf82de6bfb1c3dc468f6eb7472b292cc432d00338 (diff)
Fixed #29154 -- Corrected examples in pluralize docstring and added tests.
-rw-r--r--django/template/defaultfilters.py18
-rw-r--r--tests/template_tests/filter_tests/test_pluralize.py23
2 files changed, 32 insertions, 9 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index e32b7f7a5f..2346f50383 100644
--- a/django/template/defaultfilters.py
+++ b/django/template/defaultfilters.py
@@ -854,22 +854,22 @@ def pluralize(value, arg='s'):
Return a plural suffix if the value is not 1. By default, use 's' as the
suffix:
- * If value is 0, vote{{ value|pluralize }} display "0 votes".
- * If value is 1, vote{{ value|pluralize }} display "1 vote".
- * If value is 2, vote{{ value|pluralize }} display "2 votes".
+ * If value is 0, vote{{ value|pluralize }} display "votes".
+ * If value is 1, vote{{ value|pluralize }} display "vote".
+ * If value is 2, vote{{ value|pluralize }} display "votes".
If an argument is provided, use that string instead:
- * If value is 0, class{{ value|pluralize:"es" }} display "0 classes".
- * If value is 1, class{{ value|pluralize:"es" }} display "1 class".
- * If value is 2, class{{ value|pluralize:"es" }} display "2 classes".
+ * If value is 0, class{{ value|pluralize:"es" }} display "classes".
+ * If value is 1, class{{ value|pluralize:"es" }} display "class".
+ * If value is 2, class{{ value|pluralize:"es" }} display "classes".
If the provided argument contains a comma, use the text before the comma
for the singular case and the text after the comma for the plural case:
- * If value is 0, cand{{ value|pluralize:"y,ies" }} display "0 candies".
- * If value is 1, cand{{ value|pluralize:"y,ies" }} display "1 candy".
- * If value is 2, cand{{ value|pluralize:"y,ies" }} display "2 candies".
+ * If value is 0, cand{{ value|pluralize:"y,ies" }} display "candies".
+ * If value is 1, cand{{ value|pluralize:"y,ies" }} display "candy".
+ * If value is 2, cand{{ value|pluralize:"y,ies" }} display "candies".
"""
if ',' not in arg:
arg = ',' + arg
diff --git a/tests/template_tests/filter_tests/test_pluralize.py b/tests/template_tests/filter_tests/test_pluralize.py
index 102987d68d..96c5802296 100644
--- a/tests/template_tests/filter_tests/test_pluralize.py
+++ b/tests/template_tests/filter_tests/test_pluralize.py
@@ -3,6 +3,29 @@ from decimal import Decimal
from django.template.defaultfilters import pluralize
from django.test import SimpleTestCase
+from ..utils import setup
+
+
+class PluralizeTests(SimpleTestCase):
+
+ def check_values(self, *tests):
+ for value, expected in tests:
+ with self.subTest(value=value):
+ output = self.engine.render_to_string('t', {'value': value})
+ self.assertEqual(output, expected)
+
+ @setup({'t': 'vote{{ value|pluralize }}'})
+ def test_no_arguments(self):
+ self.check_values(('0', 'votes'), ('1', 'vote'), ('2', 'votes'))
+
+ @setup({'t': 'class{{ value|pluralize:"es" }}'})
+ def test_suffix(self):
+ self.check_values(('0', 'classes'), ('1', 'class'), ('2', 'classes'))
+
+ @setup({'t': 'cand{{ value|pluralize:"y,ies" }}'})
+ def test_singular_and_plural_suffix(self):
+ self.check_values(('0', 'candies'), ('1', 'candy'), ('2', 'candies'))
+
class FunctionTests(SimpleTestCase):