Merge lp:~danilo/launchpad/bug-531831 into lp:launchpad/db-devel

Proposed by Данило Шеган
Status: Merged
Approved by: Данило Шеган
Approved revision: no longer in the source branch.
Merged at revision: not available
Proposed branch: lp:~danilo/launchpad/bug-531831
Merge into: lp:launchpad/db-devel
Diff against target: 131 lines (+74/-5)
5 files modified
lib/lp/translations/browser/tests/language-views.txt (+1/-1)
lib/lp/translations/stories/standalone/xx-language.txt (+1/-1)
lib/lp/translations/utilities/doc/pluralforms.txt (+1/-1)
lib/lp/translations/utilities/pluralforms.py (+12/-2)
lib/lp/translations/utilities/tests/test_pluralforms.py (+59/-0)
To merge this branch: bzr merge lp:~danilo/launchpad/bug-531831
Reviewer Review Type Date Requested Status
Henning Eggers (community) code Approve
Review via email: mp+20675@code.launchpad.net

Commit message

Show zero among examples of potential candidates for plural form expressions.

Description of the change

= Bug 531831 =

Do not forget about zero when showing a nice set of examples for plural forms.

Also provides more unit testing and extends catching of errors.

bin/test -vvt pluralforms

= Launchpad lint =

Checking for conflicts. and issues in doctests and templates.
Running jslint, xmllint, pyflakes, and pylint.
Using normal rules.

Linting changed files:
  lib/lp/translations/utilities/pluralforms.py
  lib/lp/translations/utilities/doc/pluralforms.txt
  lib/lp/translations/utilities/tests/test_pluralforms.py

To post a comment you must log in.
Revision history for this message
Henning Eggers (henninge) wrote :

Thanks for the thorough fix.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/translations/browser/tests/language-views.txt'
--- lib/lp/translations/browser/tests/language-views.txt 2009-12-03 15:14:55 +0000
+++ lib/lp/translations/browser/tests/language-views.txt 2010-03-05 14:13:43 +0000
@@ -93,7 +93,7 @@
93 ... print form_dict['form'], ':', form_dict['examples']93 ... print form_dict['form'], ':', form_dict['examples']
94 0 : 1, 21, 31, 41, 51, 61...94 0 : 1, 21, 31, 41, 51, 61...
95 1 : 2, 3, 4, 22, 23, 24...95 1 : 2, 3, 4, 22, 23, 24...
96 2 : 5, 6, 7, 8, 9, 10...96 2 : 0, 5, 6, 7, 8, 9...
9797
98View LanguageSet98View LanguageSet
99------------------99------------------
100100
=== modified file 'lib/lp/translations/stories/standalone/xx-language.txt'
--- lib/lp/translations/stories/standalone/xx-language.txt 2009-12-18 20:05:08 +0000
+++ lib/lp/translations/stories/standalone/xx-language.txt 2010-03-05 14:13:43 +0000
@@ -117,7 +117,7 @@
117 Plural forms117 Plural forms
118 Spanish has 2 plural forms:118 Spanish has 2 plural forms:
119 Form 0 for 1.119 Form 0 for 1.
120 Form 1 for 2, 3, 4, 5, 6, 7...120 Form 1 for 0, 2, 3, 4, 5, 6...
121 When ...121 When ...
122122
123 >>> translationteams_portlet = find_portlet(123 >>> translationteams_portlet = find_portlet(
124124
=== modified file 'lib/lp/translations/utilities/doc/pluralforms.txt'
--- lib/lp/translations/utilities/doc/pluralforms.txt 2009-09-16 02:35:32 +0000
+++ lib/lp/translations/utilities/doc/pluralforms.txt 2010-03-05 14:13:43 +0000
@@ -31,4 +31,4 @@
31 ... print form_dict['form'], ":", form_dict['examples']31 ... print form_dict['form'], ":", form_dict['examples']
32 0 : [1, 21, 31, 41, 51, 61]32 0 : [1, 21, 31, 41, 51, 61]
33 1 : [2, 3, 4, 22, 23, 24]33 1 : [2, 3, 4, 22, 23, 24]
34 2 : [5, 6, 7, 8, 9, 10]34 2 : [0, 5, 6, 7, 8, 9]
3535
=== modified file 'lib/lp/translations/utilities/pluralforms.py'
--- lib/lp/translations/utilities/pluralforms.py 2009-09-16 02:35:32 +0000
+++ lib/lp/translations/utilities/pluralforms.py 2010-03-05 14:13:43 +0000
@@ -19,8 +19,13 @@
19 # The max length of the examples list per plural form.19 # The max length of the examples list per plural form.
20 MAX_EXAMPLES = 620 MAX_EXAMPLES = 6
2121
22 for number in range(1, 200):22 for number in range(0, 200):
23 form = expression(number)23 try:
24 form = expression(number)
25 except ZeroDivisionError:
26 raise BadPluralExpression(
27 "Zero division error in the plural form expression.")
28
24 # Create empty list if this form doesn't have one yet29 # Create empty list if this form doesn't have one yet
25 forms.setdefault(form, [])30 forms.setdefault(form, [])
26 # If all the plural forms for this language have examples (max. of 631 # If all the plural forms for this language have examples (max. of 6
@@ -29,6 +34,11 @@
29 continue34 continue
30 forms[form].append(number)35 forms[form].append(number)
3136
37 if pluralforms_count != len(forms):
38 raise BadPluralExpression(
39 "Number of recognized plural forms doesn't match the "
40 "expected number of them.")
41
32 # Each dict has two keys, 'form' and 'examples', that address the form42 # Each dict has two keys, 'form' and 'examples', that address the form
33 # number index and a list of its examples.43 # number index and a list of its examples.
34 return [{'form' : form, 'examples' : examples}44 return [{'form' : form, 'examples' : examples}
3545
=== added file 'lib/lp/translations/utilities/tests/test_pluralforms.py'
--- lib/lp/translations/utilities/tests/test_pluralforms.py 1970-01-01 00:00:00 +0000
+++ lib/lp/translations/utilities/tests/test_pluralforms.py 2010-03-05 14:13:43 +0000
@@ -0,0 +1,59 @@
1# Copyright 2009 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).
3
4import unittest
5
6from lp.translations.utilities.pluralforms import (
7 BadPluralExpression,
8 make_friendly_plural_forms)
9
10class PluralFormsTest(unittest.TestCase):
11 """Test utilities for handling plural forms."""
12
13 def test_make_friendly_plural_form(self):
14 single_form = make_friendly_plural_forms('0', 1)
15 self.assertEqual(single_form,
16 [{'examples': [0, 1, 2, 3, 4, 5], 'form': 0}])
17
18 two_forms = make_friendly_plural_forms('n!=1', 2)
19 self.assertEqual(two_forms,
20 [{'examples': [1], 'form': 0},
21 {'examples': [0, 2, 3, 4, 5, 6], 'form': 1}])
22
23 def test_make_friendly_plural_form_failures(self):
24 # 'To the degree of' is not accepted.
25 self.assertRaises(BadPluralExpression,
26 make_friendly_plural_forms, 'n**2', 1)
27
28 # Expressions longer than 500 characters are not accepted.
29 self.assertRaises(BadPluralExpression,
30 make_friendly_plural_forms, '1'*501, 1)
31
32 # Using arbitrary variable names is not allowed.
33 self.assertRaises(BadPluralExpression,
34 make_friendly_plural_forms, '(a=1)', 1)
35
36 # If number of actual forms doesn't match requested number.
37 self.assertRaises(BadPluralExpression,
38 make_friendly_plural_forms, 'n!=1', 3)
39
40 # Dividing by zero doesn't work.
41 self.assertRaises(BadPluralExpression,
42 make_friendly_plural_forms, '(n/0)', 1)
43
44 def test_make_friendly_plural_form_zero_handling(self):
45 zero_forms = make_friendly_plural_forms('n!=0', 2)
46 self.assertEqual(zero_forms,
47 [{'examples': [0], 'form': 0},
48 {'examples': [1, 2, 3, 4, 5, 6], 'form': 1}])
49
50 # Since 'n' can be zero as well, dividing by it won't work.
51 self.assertRaises(BadPluralExpression,
52 make_friendly_plural_forms, '(1/n)', 1)
53
54
55def test_suite():
56 suite = unittest.TestSuite()
57 suite.addTest(unittest.makeSuite(PluralFormsTest))
58 return suite
59

Subscribers

People subscribed via source and target branches

to status/vote changes: