File: test_inflections.py

package info (click to toggle)
python-inflect 7.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 496 kB
  • sloc: python: 4,807; makefile: 3
file content (268 lines) | stat: -rw-r--r-- 8,740 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os

import pytest

import inflect


def is_eq(p, a, b):
    return (
        p.compare(a, b)
        or p.plnounequal(a, b)
        or p.plverbequal(a, b)
        or p.pladjequal(a, b)
    )


def test_many():  # noqa: C901
    p = inflect.engine()

    data = get_data()

    for line in data:
        if "TODO:" in line:
            continue
        try:
            singular, rest = line.split("->", 1)
        except ValueError:
            continue
        singular = singular.strip()
        rest = rest.strip()
        try:
            plural, comment = rest.split("#", 1)
        except ValueError:
            plural = rest.strip()
            comment = ""
        try:
            mod_plural, class_plural = plural.split("|", 1)
            mod_plural = mod_plural.strip()
            class_plural = class_plural.strip()
        except ValueError:
            mod_plural = class_plural = plural.strip()
        if "verb" in comment.lower():
            is_nv = "_V"
        elif "noun" in comment.lower():
            is_nv = "_N"
        else:
            is_nv = ""

        p.classical(all=0, names=0)
        mod_PL_V = p.plural_verb(singular)
        mod_PL_N = p.plural_noun(singular)
        mod_PL = p.plural(singular)
        if is_nv == "_V":
            mod_PL_val = mod_PL_V
        elif is_nv == "_N":
            mod_PL_val = mod_PL_N
        else:
            mod_PL_val = mod_PL

        p.classical(all=1)
        class_PL_V = p.plural_verb(singular)
        class_PL_N = p.plural_noun(singular)
        class_PL = p.plural(singular)
        if is_nv == "_V":
            class_PL_val = class_PL_V
        elif is_nv == "_N":
            class_PL_val = class_PL_N
        else:
            class_PL_val = class_PL

        check_all(
            p, is_nv, singular, mod_PL_val, class_PL_val, mod_plural, class_plural
        )


def check_all(p, is_nv, singular, mod_PL_val, class_PL_val, mod_plural, class_plural):
    assert mod_plural == mod_PL_val
    assert class_plural == class_PL_val
    assert is_eq(p, singular, mod_plural) in ("s:p", "p:s", "eq")
    assert is_eq(p, mod_plural, singular) in ("p:s", "s:p", "eq")
    assert is_eq(p, singular, class_plural) in ("s:p", "p:s", "eq")
    assert is_eq(p, class_plural, singular) in ("p:s", "s:p", "eq")
    assert singular != ""
    expected = mod_PL_val if class_PL_val else f"{mod_PL_val}|{class_PL_val}"
    assert mod_PL_val == expected

    if is_nv != "_V":
        assert p.singular_noun(mod_plural, 1) == singular

        assert p.singular_noun(class_plural, 1) == singular


def test_def():
    p = inflect.engine()

    p.defnoun("kin", "kine")
    p.defnoun("(.*)x", "$1xen")

    p.defverb("foobar", "feebar", "foobar", "feebar", "foobars", "feebar")

    p.defadj("red", "red|gules")

    assert p.no("kin", 0) == "no kine"
    assert p.no("kin", 1) == "1 kin"
    assert p.no("kin", 2) == "2 kine"

    assert p.no("regex", 0) == "no regexen"

    assert p.plural("foobar", 2) == "feebar"
    assert p.plural("foobars", 2) == "feebar"

    assert p.plural("red", 0) == "red"
    assert p.plural("red", 1) == "red"
    assert p.plural("red", 2) == "red"
    p.classical(all=True)
    assert p.plural("red", 0) == "red"
    assert p.plural("red", 1) == "red"
    assert p.plural("red", 2) == "gules"


def test_ordinal():
    p = inflect.engine()
    assert p.ordinal(0) == "0th"
    assert p.ordinal(1) == p.ordinal("1") == "1st"
    assert p.ordinal(2) == "2nd"
    assert p.ordinal(3) == "3rd"
    assert p.ordinal(4) == "4th"
    assert p.ordinal(5) == "5th"
    assert p.ordinal(6) == "6th"
    assert p.ordinal(7) == "7th"
    assert p.ordinal(8) == "8th"
    assert p.ordinal(9) == "9th"
    assert p.ordinal(10) == "10th"
    assert p.ordinal(11) == "11th"
    assert p.ordinal(12) == "12th"
    assert p.ordinal(13) == "13th"
    assert p.ordinal(14) == "14th"
    assert p.ordinal(15) == "15th"
    assert p.ordinal(16) == "16th"
    assert p.ordinal(17) == "17th"
    assert p.ordinal(18) == "18th"
    assert p.ordinal(19) == "19th"
    assert p.ordinal(20) == "20th"
    assert p.ordinal(21) == "21st"
    assert p.ordinal(22) == "22nd"
    assert p.ordinal(23) == "23rd"
    assert p.ordinal(24) == "24th"
    assert p.ordinal(100) == "100th"
    assert p.ordinal(101) == "101st"
    assert p.ordinal(102) == "102nd"
    assert p.ordinal(103) == "103rd"
    assert p.ordinal(104) == "104th"

    assert p.ordinal(1.1) == p.ordinal("1.1") == "1.1st"
    assert p.ordinal(1.2) == "1.2nd"
    assert p.ordinal(5.502) == "5.502nd"

    assert p.ordinal("zero") == "zeroth"
    assert p.ordinal("one") == "first"
    assert p.ordinal("two") == "second"
    assert p.ordinal("three") == "third"
    assert p.ordinal("four") == "fourth"
    assert p.ordinal("five") == "fifth"
    assert p.ordinal("six") == "sixth"
    assert p.ordinal("seven") == "seventh"
    assert p.ordinal("eight") == "eighth"
    assert p.ordinal("nine") == "ninth"
    assert p.ordinal("ten") == "tenth"
    assert p.ordinal("eleven") == "eleventh"
    assert p.ordinal("twelve") == "twelfth"
    assert p.ordinal("thirteen") == "thirteenth"
    assert p.ordinal("fourteen") == "fourteenth"
    assert p.ordinal("fifteen") == "fifteenth"
    assert p.ordinal("sixteen") == "sixteenth"
    assert p.ordinal("seventeen") == "seventeenth"
    assert p.ordinal("eighteen") == "eighteenth"
    assert p.ordinal("nineteen") == "nineteenth"
    assert p.ordinal("twenty") == "twentieth"
    assert p.ordinal("twenty-one") == "twenty-first"
    assert p.ordinal("twenty-two") == "twenty-second"
    assert p.ordinal("twenty-three") == "twenty-third"
    assert p.ordinal("twenty-four") == "twenty-fourth"
    assert p.ordinal("one hundred") == "one hundredth"
    assert p.ordinal("one hundred and one") == "one hundred and first"
    assert p.ordinal("one hundred and two") == "one hundred and second"
    assert p.ordinal("one hundred and three") == "one hundred and third"
    assert p.ordinal("one hundred and four") == "one hundred and fourth"


def test_decimal_ordinals():
    """
    Capture expectation around ordinals for decimals.

    This expectation is held loosely. Another expectation may be
    considered if appropriate.
    """

    p = inflect.engine()
    assert p.ordinal("1.23") == "1.23rd"
    assert p.ordinal("7.09") == "7.09th"


def test_prespart():
    p = inflect.engine()
    assert p.present_participle("sees") == "seeing"
    assert p.present_participle("eats") == "eating"
    assert p.present_participle("bats") == "batting"
    assert p.present_participle("hates") == "hating"
    assert p.present_participle("spies") == "spying"
    assert p.present_participle("skis") == "skiing"


def test_inflect_on_tuples():
    p = inflect.engine()
    assert p.inflect("plural('egg', ('a', 'b', 'c'))") == "eggs"
    assert p.inflect("plural('egg', ['a', 'b', 'c'])") == "eggs"
    assert p.inflect("plural_noun('egg', ('a', 'b', 'c'))") == "eggs"
    assert p.inflect("plural_adj('a', ('a', 'b', 'c'))") == "some"
    assert p.inflect("plural_verb('was', ('a', 'b', 'c'))") == "were"
    assert p.inflect("singular_noun('eggs', ('a', 'b', 'c'))") == "eggs"
    assert p.inflect("an('error', ('a', 'b', 'c'))") == "('a', 'b', 'c') error"
    assert p.inflect("This is not a function(name)") == "This is not a function(name)"


def test_inflect_on_builtin_constants():
    p = inflect.engine()
    assert (
        p.inflect("Plural of False is plural('False')") == "Plural of False is Falses"
    )
    assert p.inflect("num(%d, False) plural('False')" % 10) == " Falses"

    assert p.inflect("plural('True')") == "Trues"
    assert p.inflect("num(%d, True) plural('False')" % 10) == "10 Falses"
    assert p.inflect("num(%d, %r) plural('False')" % (10, True)) == "10 Falses"

    assert p.inflect("plural('None')") == "Nones"
    assert p.inflect("num(%d, %r) plural('True')" % (10, None)) == "10 Trues"


def test_inflect_keyword_args():
    p = inflect.engine()
    assert (
        p.inflect("number_to_words(1234, andword='')")
        == "one thousand, two hundred thirty-four"
    )

    assert (
        p.inflect("number_to_words(1234, andword='plus')")
        == "one thousand, two hundred plus thirty-four"
    )

    assert (
        p.inflect("number_to_words('555_1202', group=1, zero='oh')")
        == "five, five, five, one, two, oh, two"
    )


def test_NameError_in_strings():
    with pytest.raises(NameError):
        p = inflect.engine()
        assert p.inflect("plural('two')") == "twoes"
        p.inflect("plural(two)")


def get_data():
    filename = os.path.join(os.path.dirname(__file__), "inflections.txt")
    with open(filename, encoding='utf-8') as strm:
        return list(map(str.strip, strm))