forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplural_tests.py
executable file
·68 lines (53 loc) · 2.35 KB
/
plural_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""Test plural module."""
#
# (C) Pywikibot team, 2015-2022
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import unittest
from contextlib import suppress
from pywikibot import plural
from tests.aspects import MetaTestCaseClass, TestCase
class MetaPluralRulesTest(MetaTestCaseClass):
"""Metaclass to test each plural rule in separate tests."""
def __new__(cls, name, bases, dct):
"""Create a new test case which tests all plural rules."""
def create_test(rule):
def test_static_rule(self):
"""Test a rule which is just one integer."""
self.assertEqual(rule['nplurals'], 1)
self.assertEqual(rule['plural'], 0)
def test_callable_rule(self):
"""Test a rule which is callable."""
# in theory a static rule could be also callable
self.assertGreater(rule['nplurals'], 0)
num_plurals = set()
for num in range(self.max_num + 1):
index = rule['plural'](num)
self.assertLess(index, rule['nplurals'],
msg='Plural for {} created an index {} '
'(greater than {})'
.format(num, index, rule['nplurals']))
num_plurals.add(index)
self.assertCountEqual(num_plurals,
list(range(rule['nplurals'])))
# Don't already fail on creation
if callable(rule.get('plural')):
return test_callable_rule
return test_static_rule
for lang, rule in plural.plural_rules.items():
cls.add_method(dct, f"test_{lang.replace('-', '_')}",
create_test(rule),
doc_suffix=f'for "{lang}"')
return super().__new__(cls, name, bases, dct)
class TestPluralRules(TestCase, metaclass=MetaPluralRulesTest):
"""Test the consistency of the plural rules."""
net = False
# for callable plural rules it'll test up until this number, this number
# must cause to create all plurals in all dynamic languages
max_num = 1000
if __name__ == '__main__': # pragma: no cover
with suppress(SystemExit):
unittest.main()