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
1=== modified file 'lib/lp/translations/browser/tests/language-views.txt'
2--- lib/lp/translations/browser/tests/language-views.txt 2009-12-03 15:14:55 +0000
3+++ lib/lp/translations/browser/tests/language-views.txt 2010-03-05 14:13:43 +0000
4@@ -93,7 +93,7 @@
5 ... print form_dict['form'], ':', form_dict['examples']
6 0 : 1, 21, 31, 41, 51, 61...
7 1 : 2, 3, 4, 22, 23, 24...
8- 2 : 5, 6, 7, 8, 9, 10...
9+ 2 : 0, 5, 6, 7, 8, 9...
10
11 View LanguageSet
12 ------------------
13
14=== modified file 'lib/lp/translations/stories/standalone/xx-language.txt'
15--- lib/lp/translations/stories/standalone/xx-language.txt 2009-12-18 20:05:08 +0000
16+++ lib/lp/translations/stories/standalone/xx-language.txt 2010-03-05 14:13:43 +0000
17@@ -117,7 +117,7 @@
18 Plural forms
19 Spanish has 2 plural forms:
20 Form 0 for 1.
21- Form 1 for 2, 3, 4, 5, 6, 7...
22+ Form 1 for 0, 2, 3, 4, 5, 6...
23 When ...
24
25 >>> translationteams_portlet = find_portlet(
26
27=== modified file 'lib/lp/translations/utilities/doc/pluralforms.txt'
28--- lib/lp/translations/utilities/doc/pluralforms.txt 2009-09-16 02:35:32 +0000
29+++ lib/lp/translations/utilities/doc/pluralforms.txt 2010-03-05 14:13:43 +0000
30@@ -31,4 +31,4 @@
31 ... print form_dict['form'], ":", form_dict['examples']
32 0 : [1, 21, 31, 41, 51, 61]
33 1 : [2, 3, 4, 22, 23, 24]
34- 2 : [5, 6, 7, 8, 9, 10]
35+ 2 : [0, 5, 6, 7, 8, 9]
36
37=== modified file 'lib/lp/translations/utilities/pluralforms.py'
38--- lib/lp/translations/utilities/pluralforms.py 2009-09-16 02:35:32 +0000
39+++ lib/lp/translations/utilities/pluralforms.py 2010-03-05 14:13:43 +0000
40@@ -19,8 +19,13 @@
41 # The max length of the examples list per plural form.
42 MAX_EXAMPLES = 6
43
44- for number in range(1, 200):
45- form = expression(number)
46+ for number in range(0, 200):
47+ try:
48+ form = expression(number)
49+ except ZeroDivisionError:
50+ raise BadPluralExpression(
51+ "Zero division error in the plural form expression.")
52+
53 # Create empty list if this form doesn't have one yet
54 forms.setdefault(form, [])
55 # If all the plural forms for this language have examples (max. of 6
56@@ -29,6 +34,11 @@
57 continue
58 forms[form].append(number)
59
60+ if pluralforms_count != len(forms):
61+ raise BadPluralExpression(
62+ "Number of recognized plural forms doesn't match the "
63+ "expected number of them.")
64+
65 # Each dict has two keys, 'form' and 'examples', that address the form
66 # number index and a list of its examples.
67 return [{'form' : form, 'examples' : examples}
68
69=== added file 'lib/lp/translations/utilities/tests/test_pluralforms.py'
70--- lib/lp/translations/utilities/tests/test_pluralforms.py 1970-01-01 00:00:00 +0000
71+++ lib/lp/translations/utilities/tests/test_pluralforms.py 2010-03-05 14:13:43 +0000
72@@ -0,0 +1,59 @@
73+# Copyright 2009 Canonical Ltd. This software is licensed under the
74+# GNU Affero General Public License version 3 (see the file LICENSE).
75+
76+import unittest
77+
78+from lp.translations.utilities.pluralforms import (
79+ BadPluralExpression,
80+ make_friendly_plural_forms)
81+
82+class PluralFormsTest(unittest.TestCase):
83+ """Test utilities for handling plural forms."""
84+
85+ def test_make_friendly_plural_form(self):
86+ single_form = make_friendly_plural_forms('0', 1)
87+ self.assertEqual(single_form,
88+ [{'examples': [0, 1, 2, 3, 4, 5], 'form': 0}])
89+
90+ two_forms = make_friendly_plural_forms('n!=1', 2)
91+ self.assertEqual(two_forms,
92+ [{'examples': [1], 'form': 0},
93+ {'examples': [0, 2, 3, 4, 5, 6], 'form': 1}])
94+
95+ def test_make_friendly_plural_form_failures(self):
96+ # 'To the degree of' is not accepted.
97+ self.assertRaises(BadPluralExpression,
98+ make_friendly_plural_forms, 'n**2', 1)
99+
100+ # Expressions longer than 500 characters are not accepted.
101+ self.assertRaises(BadPluralExpression,
102+ make_friendly_plural_forms, '1'*501, 1)
103+
104+ # Using arbitrary variable names is not allowed.
105+ self.assertRaises(BadPluralExpression,
106+ make_friendly_plural_forms, '(a=1)', 1)
107+
108+ # If number of actual forms doesn't match requested number.
109+ self.assertRaises(BadPluralExpression,
110+ make_friendly_plural_forms, 'n!=1', 3)
111+
112+ # Dividing by zero doesn't work.
113+ self.assertRaises(BadPluralExpression,
114+ make_friendly_plural_forms, '(n/0)', 1)
115+
116+ def test_make_friendly_plural_form_zero_handling(self):
117+ zero_forms = make_friendly_plural_forms('n!=0', 2)
118+ self.assertEqual(zero_forms,
119+ [{'examples': [0], 'form': 0},
120+ {'examples': [1, 2, 3, 4, 5, 6], 'form': 1}])
121+
122+ # Since 'n' can be zero as well, dividing by it won't work.
123+ self.assertRaises(BadPluralExpression,
124+ make_friendly_plural_forms, '(1/n)', 1)
125+
126+
127+def test_suite():
128+ suite = unittest.TestSuite()
129+ suite.addTest(unittest.makeSuite(PluralFormsTest))
130+ return suite
131+

Subscribers

People subscribed via source and target branches

to status/vote changes: