Debian Codegraph @

tests/test_pwd.py

raw symbols SCIP
1 import pytest
2 from typeguard import TypeCheckError
3
4 import inflect
5 from inflect import (
6 BadChunkingOptionError,
7 BadGenderError,
8 BadNumValueError,
9 NumOutOfRangeError,
10 UnknownClassicalModeError,
11 )
12
13 missing = object()
14
15
16 class Test:
17 def test_enclose(self):
18 # def enclose
19 assert inflect.enclose("test") == "(?:test)"
20
21 def test_joinstem(self):
22 # def joinstem
23 assert (
24 inflect.joinstem(-2, ["ephemeris", "iris", ".*itis"])
25 == "(?:ephemer|ir|.*it)"
26 )
27
28 def test_classical(self):
29 # classical dicts
30 assert set(inflect.def_classical.keys()) == set(inflect.all_classical.keys())
31 assert set(inflect.def_classical.keys()) == set(inflect.no_classical.keys())
32
33 # def classical
34 p = inflect.engine()
35 assert p.classical_dict == inflect.def_classical
36
37 p.classical()
38 assert p.classical_dict == inflect.all_classical
39
40 with pytest.raises(TypeError):
41 p.classical(0)
42 with pytest.raises(TypeError):
43 p.classical(1)
44 with pytest.raises(TypeError):
45 p.classical("names")
46 with pytest.raises(TypeError):
47 p.classical("names", "zero")
48 with pytest.raises(TypeError):
49 p.classical("all")
50
51 p.classical(all=False)
52 assert p.classical_dict == inflect.no_classical
53
54 p.classical(names=True, zero=True)
55 mydict = inflect.def_classical.copy()
56 mydict.update(dict(names=1, zero=1))
57 assert p.classical_dict == mydict
58
59 p.classical(all=True)
60 assert p.classical_dict == inflect.all_classical
61
62 p.classical(all=False)
63 p.classical(names=True, zero=True)
64 mydict = inflect.def_classical.copy()
65 mydict.update(dict(names=True, zero=True))
66 assert p.classical_dict == mydict
67
68 p.classical(all=False)
69 p.classical(names=True, zero=False)
70 mydict = inflect.def_classical.copy()
71 mydict.update(dict(names=True, zero=False))
72 assert p.classical_dict == mydict
73
74 with pytest.raises(UnknownClassicalModeError):
75 p.classical(bogus=True)
76
77 def test_num(self):
78 # def num
79 p = inflect.engine()
80 assert p.persistent_count is None
81
82 p.num()
83 assert p.persistent_count is None
84
85 ret = p.num(3)
86 assert p.persistent_count == 3
87 assert ret == "3"
88
89 p.num()
90 ret = p.num("3")
91 assert p.persistent_count == 3
92 assert ret == "3"
93
94 p.num()
95 ret = p.num(count=3, show=1)
96 assert p.persistent_count == 3
97 assert ret == "3"
98
99 p.num()
100 ret = p.num(count=3, show=0)
101 assert p.persistent_count == 3
102 assert ret == ""
103
104 with pytest.raises(BadNumValueError):
105 p.num("text")
106
107 def test_inflect(self):
108 p = inflect.engine()
109 for txt, ans in (
110 ("num(1)", "1"),
111 ("num(1,0)", ""),
112 ("num(1,1)", "1"),
113 ("num(1) ", "1 "),
114 (" num(1) ", " 1 "),
115 ("num(3) num(1)", "3 1"),
116 ):
117 assert p.inflect(txt) == ans, f'p.inflect("{txt}") != "{ans}"'
118
119 for txt, ans in (
120 ("plural('rock')", "rocks"),
121 ("plural('rock') plural('child')", "rocks children"),
122 ("num(2) plural('rock') plural('child')", "2 rocks children"),
123 (
124 "plural('rock') plural_noun('rock') plural_verb('rocks') "
125 "plural_adj('big') a('ant')",
126 "rocks rocks rock big an ant",
127 ),
128 (
129 "an('rock') no('cat') ordinal(3) number_to_words(1234) "
130 "present_participle('runs')",
131 "a rock no cats 3rd one thousand, two hundred and thirty-four running",
132 ),
133 ("a('cat',0) a('cat',1) a('cat',2) a('cat', 2)", "0 cat a cat 2 cat 2 cat"),
134 ):
135 assert p.inflect(txt) == ans, f'p.inflect("{txt}") != "{ans}"'
136
137 def test_user_input_fns(self):
138 p = inflect.engine()
139
140 assert p.pl_sb_user_defined == []
141 p.defnoun("VAX", "VAXen")
142 assert p.plural("VAX") == "VAXEN"
143 assert p.pl_sb_user_defined == ["VAX", "VAXen"]
144
145 assert p.ud_match("word", p.pl_sb_user_defined) is None
146 assert p.ud_match("VAX", p.pl_sb_user_defined) == "VAXen"
147 assert p.ud_match("VVAX", p.pl_sb_user_defined) is None
148
149 p.defnoun("cow", "cows|kine")
150 assert p.plural("cow") == "cows"
151 p.classical()
152 assert p.plural("cow") == "kine"
153
154 assert p.ud_match("cow", p.pl_sb_user_defined) == "cows|kine"
155
156 p.defnoun("(.+i)o", r"$1i")
157 assert p.plural("studio") == "studii"
158 assert p.ud_match("studio", p.pl_sb_user_defined) == "studii"
159
160 p.defnoun("aviatrix", "aviatrices")
161 assert p.plural("aviatrix") == "aviatrices"
162 assert p.ud_match("aviatrix", p.pl_sb_user_defined) == "aviatrices"
163 p.defnoun("aviatrix", "aviatrixes")
164 assert p.plural("aviatrix") == "aviatrixes"
165 assert p.ud_match("aviatrix", p.pl_sb_user_defined) == "aviatrixes"
166 p.defnoun("aviatrix", None)
167 assert p.plural("aviatrix") == "aviatrices"
168 assert p.ud_match("aviatrix", p.pl_sb_user_defined) is None
169
170 p.defnoun("(cat)", r"$1s")
171 assert p.plural("cat") == "cats"
172
173 with pytest.raises(inflect.BadUserDefinedPatternError):
174 p.defnoun("(??", None)
175
176 p.defnoun(None, "any") # check None doesn't crash it
177
178 # defadj
179 p.defadj("hir", "their")
180 assert p.plural("hir") == "their"
181 assert p.ud_match("hir", p.pl_adj_user_defined) == "their"
182
183 # defa defan
184 p.defa("h")
185 assert p.a("h") == "a h"
186 assert p.ud_match("h", p.A_a_user_defined) == "a"
187
188 p.defan("horrendous.*")
189 assert p.a("horrendously") == "an horrendously"
190 assert p.ud_match("horrendously", p.A_a_user_defined) == "an"
191
192 def test_user_input_defverb(self):
193 p = inflect.engine()
194 p.defverb("will", "shall", "will", "will", "will", "will")
195 assert p.ud_match("will", p.pl_v_user_defined) == "will"
196 assert p.plural("will") == "will"
197
198 @pytest.mark.xfail(reason="todo")
199 def test_user_input_defverb_compare(self):
200 p = inflect.engine()
201 p.defverb("will", "shall", "will", "will", "will", "will")
202 assert p.compare("will", "shall") == "s:p"
203 assert p.compare_verbs("will", "shall") == "s:p"
204
205 def test_postprocess(self):
206 p = inflect.engine()
207 for orig, infl, txt in (
208 ("cow", "cows", "cows"),
209 ("I", "we", "we"),
210 ("COW", "cows", "COWS"),
211 ("Cow", "cows", "Cows"),
212 ("cow", "cows|kine", "cows"),
213 ("Entry", "entries", "Entries"),
214 ("can of Coke", "cans of coke", "cans of Coke"),
215 ):
216 assert p.postprocess(orig, infl) == txt
217
218 p.classical()
219 assert p.postprocess("cow", "cows|kine") == "kine"
220
221 def test_partition_word(self):
222 p = inflect.engine()
223 for txt, part in (
224 (" cow ", (" ", "cow", " ")),
225 ("cow", ("", "cow", "")),
226 (" cow", (" ", "cow", "")),
227 ("cow ", ("", "cow", " ")),
228 (" cow ", (" ", "cow", " ")),
229 ("", ("", "", "")),
230 ("bottle of beer", ("", "bottle of beer", "")),
231 # spaces give weird results
232 # (' '),('', ' ', '')),
233 # (' '),(' ', ' ', '')),
234 # (' '),(' ', ' ', '')),
235 ):
236 assert p.partition_word(txt) == part
237
238 def test_pl(self):
239 p = inflect.engine()
240 for fn, sing, plur in (
241 (p.plural, "cow", "cows"),
242 (p.plural, "thought", "thoughts"),
243 (p.plural, "mouse", "mice"),
244 (p.plural, "knife", "knives"),
245 (p.plural, "knifes", "knife"),
246 (p.plural, " cat ", " cats "),
247 (p.plural, "court martial", "courts martial"),
248 (p.plural, "a", "some"),
249 (p.plural, "carmen", "carmina"),
250 (p.plural, "quartz", "quartzes"),
251 (p.plural, "care", "cares"),
252 (p.plural_noun, "cow", "cows"),
253 (p.plural_noun, "thought", "thoughts"),
254 (p.plural_noun, "snooze", "snoozes"),
255 (p.plural_verb, "runs", "run"),
256 (p.plural_verb, "thought", "thought"),
257 (p.plural_verb, "eyes", "eye"),
258 (p.plural_adj, "a", "some"),
259 (p.plural_adj, "this", "these"),
260 (p.plural_adj, "that", "those"),
261 (p.plural_adj, "my", "our"),
262 (p.plural_adj, "cat's", "cats'"),
263 (p.plural_adj, "child's", "children's"),
264 ):
265 assert fn(sing) == plur, (
266 f'{fn.__name__}("{sing}") == "{fn(sing)}" != "{plur}"'
267 )
268
269 for sing, num, plur in (
270 ("cow", 1, "cow"),
271 ("cow", 2, "cows"),
272 ("cow", "one", "cow"),
273 ("cow", "each", "cow"),
274 ("cow", "two", "cows"),
275 ("cow", 0, "cows"),
276 ("cow", "zero", "cows"),
277 ("runs", 0, "run"),
278 ("runs", 1, "runs"),
279 ("am", 0, "are"),
280 ):
281 assert p.plural(sing, num) == plur
282
283 p.classical(zero=True)
284 assert p.plural("cow", 0) == "cow"
285 assert p.plural("cow", "zero") == "cow"
286 assert p.plural("runs", 0) == "runs"
287 assert p.plural("am", 0) == "am"
288 assert p.plural_verb("runs", 1) == "runs"
289
290 assert p.plural("die") == "dice"
291 assert p.plural_noun("die") == "dice"
292
293 with pytest.raises(TypeCheckError):
294 p.plural("")
295 with pytest.raises(TypeCheckError):
296 p.plural_noun("")
297 with pytest.raises(TypeCheckError):
298 p.plural_verb("")
299 with pytest.raises(TypeCheckError):
300 p.plural_adj("")
301
302 def test_sinoun(self):
303 p = inflect.engine()
304 for sing, plur in (
305 ("cat", "cats"),
306 ("die", "dice"),
307 ("goose", "geese"),
308 ):
309 assert p.singular_noun(plur) == sing
310 assert p.inflect("singular_noun('%s')" % plur) == sing
311
312 assert p.singular_noun("cats", count=2) == "cats"
313 assert p.singular_noun("open valves", count=2) == "open valves"
314
315 assert p.singular_noun("zombies") == "zombie"
316
317 assert p.singular_noun("shoes") == "shoe"
318 assert p.singular_noun("dancing shoes") == "dancing shoe"
319
320 assert p.singular_noun("Matisses") == "Matisse"
321 assert p.singular_noun("bouillabaisses") == "bouillabaisse"
322
323 assert p.singular_noun("quartzes") == "quartz"
324
325 assert p.singular_noun("Nietzsches") == "Nietzsche"
326 assert p.singular_noun("aches") == "ache"
327
328 assert p.singular_noun("Clives") == "Clive"
329 assert p.singular_noun("weaves") == "weave"
330
331 assert p.singular_noun("status") is False
332 assert p.singular_noun("hiatus") is False
333
334 def test_gender(self):
335 p = inflect.engine()
336 p.gender("feminine")
337 for sing, plur in (
338 ("she", "they"),
339 ("herself", "themselves"),
340 ("hers", "theirs"),
341 ("to her", "to them"),
342 ("to herself", "to themselves"),
343 ):
344 assert p.singular_noun(plur) == sing, (
345 f"singular_noun({plur}) == {p.singular_noun(plur)} != {sing}"
346 )
347 assert p.inflect("singular_noun('%s')" % plur) == sing
348
349 p.gender("masculine")
350 for sing, plur in (
351 ("he", "they"),
352 ("himself", "themselves"),
353 ("his", "theirs"),
354 ("to him", "to them"),
355 ("to himself", "to themselves"),
356 ):
357 assert p.singular_noun(plur) == sing, (
358 f"singular_noun({plur}) == {p.singular_noun(plur)} != {sing}"
359 )
360 assert p.inflect("singular_noun('%s')" % plur) == sing
361
362 p.gender("gender-neutral")
363 for sing, plur in (
364 ("they", "they"),
365 ("themself", "themselves"),
366 ("theirs", "theirs"),
367 ("to them", "to them"),
368 ("to themself", "to themselves"),
369 ):
370 assert p.singular_noun(plur) == sing, (
371 f"singular_noun({plur}) == {p.singular_noun(plur)} != {sing}"
372 )
373 assert p.inflect("singular_noun('%s')" % plur) == sing
374
375 p.gender("neuter")
376 for sing, plur in (
377 ("it", "they"),
378 ("itself", "themselves"),
379 ("its", "theirs"),
380 ("to it", "to them"),
381 ("to itself", "to themselves"),
382 ):
383 assert p.singular_noun(plur) == sing, (
384 f"singular_noun({plur}) == {p.singular_noun(plur)} != {sing}"
385 )
386 assert p.inflect("singular_noun('%s')" % plur) == sing
387
388 with pytest.raises(BadGenderError):
389 p.gender("male")
390
391 for sing, plur, gen in (
392 ("it", "they", "neuter"),
393 ("she", "they", "feminine"),
394 ("he", "they", "masculine"),
395 ("they", "they", "gender-neutral"),
396 ("she or he", "they", "feminine or masculine"),
397 ("he or she", "they", "masculine or feminine"),
398 ):
399 assert p.singular_noun(plur, gender=gen) == sing
400
401 with pytest.raises(BadGenderError):
402 p.singular_noun("cats", gender="unknown gender")
403
404 @pytest.mark.parametrize(
405 'sing,plur,res',
406 (
407 ("index", "index", "eq"),
408 ("index", "indexes", "s:p"),
409 ("index", "indices", "s:p"),
410 ("indexes", "index", "p:s"),
411 ("indices", "index", "p:s"),
412 ("indices", "indexes", "p:p"),
413 ("indexes", "indices", "p:p"),
414 ("indices", "indices", "eq"),
415 ("inverted index", "inverted indices", "s:p"),
416 ("inverted indices", "inverted index", "p:s"),
417 ("inverted indexes", "inverted indices", "p:p"),
418 ("opuses", "opera", "p:p"),
419 ("opera", "opuses", "p:p"),
420 ("brothers", "brethren", "p:p"),
421 ("cats", "cats", "eq"),
422 ("base", "basis", False),
423 ("syrinx", "syringe", False),
424 ("she", "he", False),
425 ("opus", "operas", False),
426 ("taxi", "taxes", False),
427 ("time", "Times", False),
428 ("time".lower(), "Times".lower(), "s:p"),
429 ("courts martial", "court martial", "p:s"),
430 ("my", "my", "eq"),
431 ("my", "our", "s:p"),
432 ("our", "our", "eq"),
433 pytest.param(
434 "dresses's", "dresses'", "p:p", marks=pytest.mark.xfail(reason="todo")
435 ),
436 pytest.param(
437 "dress's", "dress'", "s:s", marks=pytest.mark.xfail(reason='todo')
438 ),
439 pytest.param(
440 "Jess's", "Jess'", "s:s", marks=pytest.mark.xfail(reason='todo')
441 ),
442 ),
443 )
444 def test_compare_simple(self, sing, plur, res):
445 assert inflect.engine().compare(sing, plur) == res
446
447 @pytest.mark.parametrize(
448 'sing,plur,res',
449 (
450 ("index", "index", "eq"),
451 ("index", "indexes", "s:p"),
452 ("index", "indices", "s:p"),
453 ("indexes", "index", "p:s"),
454 ("indices", "index", "p:s"),
455 ("indices", "indexes", "p:p"),
456 ("indexes", "indices", "p:p"),
457 ("indices", "indices", "eq"),
458 ("inverted index", "inverted indices", "s:p"),
459 ("inverted indices", "inverted index", "p:s"),
460 ("inverted indexes", "inverted indices", "p:p"),
461 ),
462 )
463 def test_compare_nouns(self, sing, plur, res):
464 assert inflect.engine().compare_nouns(sing, plur) == res
465
466 @pytest.mark.parametrize(
467 'sing,plur,res',
468 (
469 ("runs", "runs", "eq"),
470 ("runs", "run", "s:p"),
471 ("run", "run", "eq"),
472 ),
473 )
474 def test_compare_verbs(self, sing, plur, res):
475 assert inflect.engine().compare_verbs(sing, plur) == res
476
477 @pytest.mark.parametrize(
478 'sing,plur,res',
479 (
480 ("my", "my", "eq"),
481 ("my", "our", "s:p"),
482 ("our", "our", "eq"),
483 pytest.param(
484 "dresses's", "dresses'", "p:p", marks=pytest.mark.xfail(reason="todo")
485 ),
486 pytest.param(
487 "dress's", "dress'", "s:s", marks=pytest.mark.xfail(reason='todo')
488 ),
489 pytest.param(
490 "Jess's", "Jess'", "s:s", marks=pytest.mark.xfail(reason='todo')
491 ),
492 ),
493 )
494 def test_compare_adjectives(self, sing, plur, res):
495 assert inflect.engine().compare_adjs(sing, plur) == res
496
497 @pytest.mark.xfail()
498 def test_compare_your_our(self):
499 # multiple adjective plurals not (yet) supported
500 p = inflect.engine()
501 assert p.compare("your", "our") is False
502 p.defadj("my", "our|your") # what's ours is yours
503 assert p.compare("your", "our") == "p:p"
504
505 def test__pl_reg_plurals(self):
506 p = inflect.engine()
507 for pair, stems, end1, end2, ans in (
508 ("indexes|indices", "dummy|ind", "exes", "ices", True),
509 ("indexes|robots", "dummy|ind", "exes", "ices", False),
510 ("beaus|beaux", ".*eau", "s", "x", True),
511 ):
512 assert p._pl_reg_plurals(pair, stems, end1, end2) == ans
513
514 def test__pl_check_plurals_N(self):
515 p = inflect.engine()
516 assert p._pl_check_plurals_N("index", "indices") is False
517 assert p._pl_check_plurals_N("indexes", "indices") is True
518 assert p._pl_check_plurals_N("indices", "indexes") is True
519 assert p._pl_check_plurals_N("stigmata", "stigmas") is True
520 assert p._pl_check_plurals_N("phalanxes", "phalanges") is True
521
522 def test__pl_check_plurals_adj(self):
523 p = inflect.engine()
524 assert p._pl_check_plurals_adj("indexes's", "indices's") is True
525 assert p._pl_check_plurals_adj("indices's", "indexes's") is True
526 assert p._pl_check_plurals_adj("indexes'", "indices's") is True
527 assert p._pl_check_plurals_adj("indexes's", "indices'") is True
528 assert p._pl_check_plurals_adj("indexes's", "indexes's") is False
529 assert p._pl_check_plurals_adj("dogmas's", "dogmata's") is True
530 assert p._pl_check_plurals_adj("dogmas'", "dogmata'") is True
531 assert p._pl_check_plurals_adj("indexes'", "indices'") is True
532
533 def test_count(self):
534 p = inflect.engine()
535 for txt, num in (
536 (1, 1),
537 (2, 2),
538 (0, 2),
539 (87, 2),
540 (-7, 2),
541 ("1", 1),
542 ("2", 2),
543 ("0", 2),
544 ("no", 2),
545 ("zero", 2),
546 ("nil", 2),
547 ("a", 1),
548 ("an", 1),
549 ("one", 1),
550 ("each", 1),
551 ("every", 1),
552 ("this", 1),
553 ("that", 1),
554 ("dummy", 2),
555 ):
556 assert p.get_count(txt) == num
557
558 assert p.get_count() == ""
559 p.num(3)
560 assert p.get_count() == 2
561
562 def test__plnoun(self):
563 p = inflect.engine()
564 for sing, plur in (
565 ("tuna", "tuna"),
566 ("TUNA", "TUNA"),
567 ("swordfish", "swordfish"),
568 ("Governor General", "Governors General"),
569 ("Governor-General", "Governors-General"),
570 ("Major General", "Major Generals"),
571 ("Major-General", "Major-Generals"),
572 ("mother in law", "mothers in law"),
573 ("mother-in-law", "mothers-in-law"),
574 ("about me", "about us"),
575 ("to it", "to them"),
576 ("from it", "from them"),
577 ("with it", "with them"),
578 ("I", "we"),
579 ("you", "you"),
580 ("me", "us"),
581 ("mine", "ours"),
582 ("child", "children"),
583 ("brainchild", "brainchilds"),
584 ("human", "humans"),
585 ("soliloquy", "soliloquies"),
586 ("chairwoman", "chairwomen"),
587 ("goose", "geese"),
588 ("tooth", "teeth"),
589 ("foot", "feet"),
590 ("forceps", "forceps"),
591 ("protozoon", "protozoa"),
592 ("czech", "czechs"),
593 ("codex", "codices"),
594 ("radix", "radices"),
595 ("bacterium", "bacteria"),
596 ("alumnus", "alumni"),
597 ("criterion", "criteria"),
598 ("alumna", "alumnae"),
599 ("bias", "biases"),
600 ("quiz", "quizzes"),
601 ("fox", "foxes"),
602 ("shelf", "shelves"),
603 ("leaf", "leaves"),
604 ("midwife", "midwives"),
605 ("scarf", "scarves"),
606 ("key", "keys"),
607 ("Sally", "Sallys"),
608 ("sally", "sallies"),
609 ("ado", "ados"),
610 ("auto", "autos"),
611 ("alto", "altos"),
612 ("zoo", "zoos"),
613 ("tomato", "tomatoes"),
614 ):
615 assert p._plnoun(sing) == plur, (
616 f'p._plnoun("{sing}") == {p._plnoun(sing)} != "{plur}"'
617 )
618
619 assert p._sinoun(plur) == sing, f'p._sinoun("{plur}") != "{sing}"'
620
621 # words where forming singular is ambiguous or not attempted
622 for sing, plur in (
623 ("son of a gun", "sons of guns"),
624 ("son-of-a-gun", "sons-of-guns"),
625 ("basis", "bases"),
626 ("Jess", "Jesses"),
627 ):
628 assert p._plnoun(sing) == plur, f'p._plnoun("{sing}") != "{plur}"'
629
630 p.num(1)
631 assert p._plnoun("cat") == "cat"
632 p.num(3)
633
634 p.classical(herd=True)
635 assert p._plnoun("swine") == "swine"
636 p.classical(herd=False)
637 assert p._plnoun("swine") == "swines"
638 p.classical(persons=True)
639 assert p._plnoun("chairperson") == "chairpersons"
640 p.classical(persons=False)
641 assert p._plnoun("chairperson") == "chairpeople"
642 p.classical(ancient=True)
643 assert p._plnoun("formula") == "formulae"
644 p.classical(ancient=False)
645 assert p._plnoun("formula") == "formulas"
646
647 p.classical()
648 for sing, plur in (
649 ("matrix", "matrices"),
650 ("gateau", "gateaux"),
651 ("millieu", "millieux"),
652 ("syrinx", "syringes"),
653 ("stamen", "stamina"),
654 ("apex", "apices"),
655 ("appendix", "appendices"),
656 ("maximum", "maxima"),
657 ("focus", "foci"),
658 ("status", "status"),
659 ("aurora", "aurorae"),
660 ("soma", "somata"),
661 ("iris", "irides"),
662 ("solo", "soli"),
663 ("oxymoron", "oxymora"),
664 ("goy", "goyim"),
665 ("afrit", "afriti"),
666 ):
667 assert p._plnoun(sing) == plur
668
669 # p.classical(0)
670 # p.classical('names')
671 # classical now back to the default mode
672
673 @pytest.mark.parametrize(
674 'sing, plur',
675 (
676 pytest.param(
677 'about ME',
678 'about US',
679 marks=pytest.mark.xfail(reason='does not keep case'),
680 ),
681 pytest.param(
682 'YOU',
683 'YOU',
684 marks=pytest.mark.xfail(reason='does not keep case'),
685 ),
686 ),
687 )
688 def test_plnoun_retains_case(self, sing, plur):
689 assert inflect.engine()._plnoun(sing) == plur
690
691 def test_classical_pl(self):
692 p = inflect.engine()
693 p.classical()
694 for sing, plur in (("brother", "brethren"), ("dogma", "dogmata")):
695 assert p.plural(sing) == plur
696
697 def test__pl_special_verb(self):
698 p = inflect.engine()
699 with pytest.raises(TypeCheckError):
700 p._pl_special_verb("")
701 assert p._pl_special_verb("am") == "are"
702 assert p._pl_special_verb("am", 0) == "are"
703 assert p._pl_special_verb("runs", 0) == "run"
704 p.classical(zero=True)
705 assert p._pl_special_verb("am", 0) is False
706 assert p._pl_special_verb("am", 1) == "am"
707 assert p._pl_special_verb("am", 2) == "are"
708 assert p._pl_special_verb("runs", 0) is False
709 assert p._pl_special_verb("am going to") == "are going to"
710 assert p._pl_special_verb("did") == "did"
711 assert p._pl_special_verb("wasn't") == "weren't"
712 assert p._pl_special_verb("shouldn't") == "shouldn't"
713 assert p._pl_special_verb("bias") is False
714 assert p._pl_special_verb("news") is False
715 assert p._pl_special_verb("Jess") is False
716 assert p._pl_special_verb(" ") is False
717 assert p._pl_special_verb("brushes") == "brush"
718 assert p._pl_special_verb("fixes") == "fix"
719 assert p._pl_special_verb("quizzes") == "quiz"
720 assert p._pl_special_verb("fizzes") == "fizz"
721 assert p._pl_special_verb("dresses") == "dress"
722 assert p._pl_special_verb("flies") == "fly"
723 assert p._pl_special_verb("canoes") == "canoe"
724 assert p._pl_special_verb("horseshoes") == "horseshoe"
725 assert p._pl_special_verb("does") == "do"
726 # TODO: what's a real word to test this case?
727 assert p._pl_special_verb("zzzoes") == "zzzo"
728 assert p._pl_special_verb("runs") == "run"
729
730 def test__pl_general_verb(self):
731 p = inflect.engine()
732 assert p._pl_general_verb("acts") == "act"
733 assert p._pl_general_verb("act") == "act"
734 assert p._pl_general_verb("saw") == "saw"
735 assert p._pl_general_verb("runs", 1) == "runs"
736
737 @pytest.mark.parametrize(
738 'adj,plur',
739 (
740 ("a", "some"),
741 ("my", "our"),
742 ("John's", "Johns'"),
743 ("tuna's", "tuna's"),
744 ("TUNA's", "TUNA's"),
745 ("bad", False),
746 ("'", False),
747 pytest.param(
748 "JOHN's",
749 "JOHNS'",
750 marks=pytest.mark.xfail(reason='should this be handled?'),
751 ),
752 pytest.param(
753 "JOHN'S",
754 "JOHNS'",
755 marks=pytest.mark.xfail(reason="can't handle capitals"),
756 ),
757 pytest.param(
758 "TUNA'S",
759 "TUNA'S",
760 marks=pytest.mark.xfail(reason="can't handle capitals"),
761 ),
762 ),
763 )
764 def test__pl_special_adjective(self, adj, plur):
765 p = inflect.engine()
766 assert p._pl_special_adjective(adj) == plur
767
768 @pytest.mark.parametrize(
769 'sing, plur',
770 (
771 ("cat", "a cat"),
772 ("euphemism", "a euphemism"),
773 ("Euler number", "an Euler number"),
774 ("hour", "an hour"),
775 ("houri", "a houri"),
776 ("nth", "an nth"),
777 ("rth", "an rth"),
778 ("sth", "an sth"),
779 ("xth", "an xth"),
780 ("ant", "an ant"),
781 ("book", "a book"),
782 ("RSPCA", "an RSPCA"),
783 ("SONAR", "a SONAR"),
784 ("FJO", "a FJO"),
785 ("FJ", "an FJ"),
786 ("NASA", "a NASA"),
787 ("UN", "a UN"),
788 ("yak", "a yak"),
789 ("yttrium", "an yttrium"),
790 ("a elephant", "an elephant"),
791 ("a giraffe", "a giraffe"),
792 ("an ewe", "a ewe"),
793 ("a orangutan", "an orangutan"),
794 ("R.I.P.", "an R.I.P."),
795 ("C.O.D.", "a C.O.D."),
796 ("e-mail", "an e-mail"),
797 ("X-ray", "an X-ray"),
798 ("T-square", "a T-square"),
799 ("LCD", "an LCD"),
800 ("XML", "an XML"),
801 ("YWCA", "a YWCA"),
802 ("LED", "a LED"),
803 ("OPEC", "an OPEC"),
804 ("FAQ", "a FAQ"),
805 ("UNESCO", "a UNESCO"),
806 ("a", "an a"),
807 ("an", "an an"),
808 ("an ant", "an ant"),
809 ("a cat", "a cat"),
810 ("an cat", "a cat"),
811 ("a ant", "an ant"),
812 ),
813 )
814 def test_a(self, sing, plur):
815 p = inflect.engine()
816 assert p.a(sing) == plur
817
818 def test_a_alt(self):
819 p = inflect.engine()
820 assert p.a("cat", 1) == "a cat"
821 assert p.a("cat", 2) == "2 cat"
822
823 with pytest.raises(TypeCheckError):
824 p.a("")
825
826 def test_a_and_an_same_method(self):
827 assert inflect.engine.a == inflect.engine.an
828 p = inflect.engine()
829 assert p.a == p.an
830
831 def test_no(self):
832 p = inflect.engine()
833 assert p.no("cat") == "no cats"
834 assert p.no("cat", count=3) == "3 cats"
835 assert p.no("cat", count="three") == "three cats"
836 assert p.no("cat", count=1) == "1 cat"
837 assert p.no("cat", count="one") == "one cat"
838 assert p.no("mouse") == "no mice"
839 p.num(3)
840 assert p.no("cat") == "3 cats"
841
842 @pytest.mark.parametrize(
843 'sing, plur',
844 (
845 ("runs", "running"),
846 ("dies", "dying"),
847 ("glues", "gluing"),
848 ("eyes", "eying"),
849 ("skis", "skiing"),
850 ("names", "naming"),
851 ("sees", "seeing"),
852 ("hammers", "hammering"),
853 ("bats", "batting"),
854 ("eats", "eating"),
855 ("loves", "loving"),
856 ("spies", "spying"),
857 ("hoes", "hoeing"),
858 ("alibis", "alibiing"),
859 ("is", "being"),
860 ("are", "being"),
861 ("had", "having"),
862 ("has", "having"),
863 ),
864 )
865 def test_prespart(self, sing, plur):
866 p = inflect.engine()
867 assert p.present_participle(sing) == plur
868
869 @pytest.mark.parametrize(
870 'num, ord',
871 (
872 ("1", "1st"),
873 ("2", "2nd"),
874 ("3", "3rd"),
875 ("4", "4th"),
876 ("10", "10th"),
877 ("28", "28th"),
878 ("100", "100th"),
879 ("101", "101st"),
880 ("1000", "1000th"),
881 ("1001", "1001st"),
882 ("0", "0th"),
883 ("one", "first"),
884 ("two", "second"),
885 ("four", "fourth"),
886 ("twenty", "twentieth"),
887 ("one hundered", "one hunderedth"),
888 ("one hundered and one", "one hundered and first"),
889 ("zero", "zeroth"),
890 ("n", "nth"), # bonus!
891 ),
892 )
893 def test_ordinal(self, num, ord):
894 p = inflect.engine()
895 assert p.ordinal(num) == ord
896
897 def test_millfn(self):
898 p = inflect.engine()
899 millfn = p.millfn
900 assert millfn(1) == " thousand"
901 assert millfn(2) == " million"
902 assert millfn(3) == " billion"
903 assert millfn(0) == " "
904 assert millfn(11) == " decillion"
905 with pytest.raises(NumOutOfRangeError):
906 millfn(12)
907
908 def test_unitfn(self):
909 p = inflect.engine()
910 unitfn = p.unitfn
911 assert unitfn(1, 2) == "one million"
912 assert unitfn(1, 3) == "one billion"
913 assert unitfn(5, 3) == "five billion"
914 assert unitfn(5, 0) == "five "
915 assert unitfn(0, 0) == " "
916
917 def test_tenfn(self):
918 p = inflect.engine()
919 tenfn = p.tenfn
920 assert tenfn(3, 1, 2) == "thirty-one million"
921 assert tenfn(3, 0, 2) == "thirty million"
922 assert tenfn(0, 1, 2) == "one million"
923 assert tenfn(1, 1, 2) == "eleven million"
924 assert tenfn(1, 0, 2) == "ten million"
925 assert tenfn(1, 0, 0) == "ten "
926 assert tenfn(0, 0, 0) == " "
927
928 def test_hundfn(self):
929 p = inflect.engine()
930 hundfn = p.hundfn
931 p._number_args = dict(andword="and")
932 assert hundfn(4, 3, 1, 2) == "four hundred and thirty-one million, "
933 assert hundfn(4, 0, 0, 2) == "four hundred million, "
934 assert hundfn(4, 0, 5, 2) == "four hundred and five million, "
935 assert hundfn(0, 3, 1, 2) == "thirty-one million, "
936 assert hundfn(0, 0, 7, 2) == "seven million, "
937
938 def test_enword(self):
939 p = inflect.engine()
940 enword = p.enword
941 assert enword("5", 1) == "five, "
942 p._number_args = dict(zero="zero", one="one", andword="and")
943 assert enword("0", 1) == " zero, "
944 assert enword("1", 1) == " one, "
945 assert enword("347", 1) == "three, four, seven, "
946
947 assert enword("34", 2) == "thirty-four , "
948 assert enword("347", 2) == "thirty-four , seven, "
949 assert enword("34768", 2) == "thirty-four , seventy-six , eight, "
950 assert enword("1", 2) == "one, "
951
952 assert enword("134", 3) == " one thirty-four , "
953
954 assert enword("0", -1) == "zero"
955 assert enword("1", -1) == "one"
956
957 assert enword("3", -1) == "three , "
958 assert enword("12", -1) == "twelve , "
959 assert enword("123", -1) == "one hundred and twenty-three , "
960 assert enword("1234", -1) == "one thousand, two hundred and thirty-four , "
961 assert (
962 enword("12345", -1) == "twelve thousand, three hundred and forty-five , "
963 )
964 assert (
965 enword("123456", -1)
966 == "one hundred and twenty-three thousand, four hundred and fifty-six , "
967 )
968 assert (
969 enword("1234567", -1)
970 == "one million, two hundred and thirty-four thousand, "
971 "five hundred and sixty-seven , "
972 )
973
974 @pytest.mark.xfail(reason="doesn't use indicated word for 'one'")
975 def test_enword_number_args_override(self):
976 p = inflect.engine()
977 p._number_args["one"] = "single"
978 assert p.enword("1", 2) == "single, "
979
980 def test_numwords(self):
981 p = inflect.engine()
982 numwords = p.number_to_words
983
984 for n, word in (
985 ("1", "one"),
986 ("10", "ten"),
987 ("100", "one hundred"),
988 ("1000", "one thousand"),
989 ("10000", "ten thousand"),
990 ("100000", "one hundred thousand"),
991 ("1000000", "one million"),
992 ("10000000", "ten million"),
993 ("+10", "plus ten"),
994 ("-10", "minus ten"),
995 ("10.", "ten point"),
996 (".10", "point one zero"),
997 ):
998 assert numwords(n) == word
999
1000 for n, word, _wrongword in (
1001 # TODO: should be one point two three
1002 ("1.23", "one point two three", "one point twenty-three"),
1003 ):
1004 assert numwords(n) == word
1005
1006 for n, txt in (
1007 (3, "three bottles of beer on the wall"),
1008 (2, "two bottles of beer on the wall"),
1009 (1, "a solitary bottle of beer on the wall"),
1010 (0, "no more bottles of beer on the wall"),
1011 ):
1012 assert (
1013 "{}{}".format(
1014 numwords(n, one="a solitary", zero="no more"),
1015 p.plural(" bottle of beer on the wall", n),
1016 )
1017 == txt
1018 )
1019
1020 assert numwords(0, one="one", zero="zero") == "zero"
1021
1022 assert numwords("1234") == "one thousand, two hundred and thirty-four"
1023 assert numwords("1234", wantlist=True) == [
1024 "one thousand",
1025 "two hundred and thirty-four",
1026 ]
1027 assert numwords("1234567", wantlist=True) == [
1028 "one million",
1029 "two hundred and thirty-four thousand",
1030 "five hundred and sixty-seven",
1031 ]
1032 assert numwords("+10", wantlist=True) == ["plus", "ten"]
1033 assert numwords("1234", andword="") == "one thousand, two hundred thirty-four"
1034 assert (
1035 numwords("1234", andword="plus")
1036 == "one thousand, two hundred plus thirty-four"
1037 )
1038 assert numwords(p.ordinal("21")) == "twenty-first"
1039 assert numwords("9", threshold=10) == "nine"
1040 assert numwords("10", threshold=10) == "ten"
1041 assert numwords("11", threshold=10) == "11"
1042 assert numwords("1000", threshold=10) == "1,000"
1043 assert numwords("123", threshold=10) == "123"
1044 assert numwords("1234", threshold=10) == "1,234"
1045 assert numwords("1234.5678", threshold=10) == "1,234.5678"
1046 assert numwords("1", decimal=None) == "one"
1047 assert (
1048 numwords("1234.5678", decimal=None)
1049 == "twelve million, three hundred and forty-five "
1050 "thousand, six hundred and seventy-eight"
1051 )
1052
1053 def test_numwords_group_chunking_error(self):
1054 p = inflect.engine()
1055 with pytest.raises(BadChunkingOptionError):
1056 p.number_to_words("1234", group=4)
1057
1058 @pytest.mark.parametrize(
1059 'input,kwargs,expect',
1060 (
1061 ("12345", dict(group=2), "twelve, thirty-four, five"),
1062 ("123456", dict(group=3), "one twenty-three, four fifty-six"),
1063 ("12345", dict(group=1), "one, two, three, four, five"),
1064 (
1065 "1234th",
1066 dict(group=0, andword="and"),
1067 "one thousand, two hundred and thirty-fourth",
1068 ),
1069 (
1070 "1234th",
1071 dict(group=0),
1072 "one thousand, two hundred and thirty-fourth",
1073 ),
1074 ("120", dict(group=2), "twelve, zero"),
1075 ("120", dict(group=2, zero="oh", one="unity"), "twelve, oh"),
1076 (
1077 "555_1202",
1078 dict(group=1, zero="oh"),
1079 "five, five, five, one, two, oh, two",
1080 ),
1081 (
1082 "555_1202",
1083 dict(group=1, one="unity"),
1084 "five, five, five, unity, two, zero, two",
1085 ),
1086 (
1087 "123.456",
1088 dict(group=1, decimal="mark", one="one"),
1089 "one, two, three, mark, four, five, six",
1090 ),
1091 pytest.param(
1092 '12345',
1093 dict(group=3),
1094 'one hundred and twenty-three',
1095 marks=pytest.mark.xfail(reason="'hundred and' missing"),
1096 ),
1097 pytest.param(
1098 '101',
1099 dict(group=2, zero="oh", one="unity"),
1100 "ten, unity",
1101 marks=pytest.mark.xfail(reason="ignoring 'one' param with group=2"),
1102 ),
1103 ),
1104 )
1105 def test_numwords_group(self, input, kwargs, expect):
1106 p = inflect.engine()
1107 assert p.number_to_words(input, **kwargs) == expect
1108
1109 def test_wordlist(self):
1110 p = inflect.engine()
1111 wordlist = p.join
1112 assert wordlist([]) == ""
1113 assert wordlist(("apple",)) == "apple"
1114 assert wordlist(("apple", "banana")) == "apple and banana"
1115 assert wordlist(("apple", "banana", "carrot")) == "apple, banana, and carrot"
1116 assert wordlist(("apple", "1,000", "carrot")) == "apple; 1,000; and carrot"
1117 assert (
1118 wordlist(("apple", "1,000", "carrot"), sep=",")
1119 == "apple, 1,000, and carrot"
1120 )
1121 assert (
1122 wordlist(("apple", "banana", "carrot"), final_sep="")
1123 == "apple, banana and carrot"
1124 )
1125 assert (
1126 wordlist(("apple", "banana", "carrot"), final_sep=";")
1127 == "apple, banana; and carrot"
1128 )
1129 assert (
1130 wordlist(("apple", "banana", "carrot"), conj="or")
1131 == "apple, banana, or carrot"
1132 )
1133
1134 assert wordlist(("apple", "banana"), conj=" or ") == "apple or banana"
1135 assert wordlist(("apple", "banana"), conj="&") == "apple & banana"
1136 assert (
1137 wordlist(("apple", "banana"), conj="&", conj_spaced=False) == "apple&banana"
1138 )
1139 assert (
1140 wordlist(("apple", "banana"), conj="& ", conj_spaced=False)
1141 == "apple& banana"
1142 )
1143
1144 assert (
1145 wordlist(("apple", "banana", "carrot"), conj=" or ")
1146 == "apple, banana, or carrot"
1147 )
1148 assert (
1149 wordlist(("apple", "banana", "carrot"), conj="+")
1150 == "apple, banana, + carrot"
1151 )
1152 assert (
1153 wordlist(("apple", "banana", "carrot"), conj="&")
1154 == "apple, banana, & carrot"
1155 )
1156 assert (
1157 wordlist(("apple", "banana", "carrot"), conj="&", conj_spaced=False)
1158 == "apple, banana,&carrot"
1159 )
1160 assert (
1161 wordlist(("apple", "banana", "carrot"), conj=" &", conj_spaced=False)
1162 == "apple, banana, &carrot"
1163 )
1164
1165 def test_doc_examples(self):
1166 p = inflect.engine()
1167 assert p.plural_noun("I") == "we"
1168 assert p.plural_verb("saw") == "saw"
1169 assert p.plural_adj("my") == "our"
1170 assert p.plural_noun("saw") == "saws"
1171 assert p.plural("was") == "were"
1172 assert p.plural("was", 1) == "was"
1173 assert p.plural_verb("was", 2) == "were"
1174 assert p.plural_verb("was") == "were"
1175 assert p.plural_verb("was", 1) == "was"
1176
1177 for errors, txt in (
1178 (0, "There were no errors"),
1179 (1, "There was 1 error"),
1180 (2, "There were 2 errors"),
1181 ):
1182 assert (
1183 "There {}{}".format(
1184 p.plural_verb("was", errors), p.no(" error", errors)
1185 )
1186 == txt
1187 )
1188
1189 assert (
1190 p.inflect(
1191 "There plural_verb('was',%d) no('error',%d)" % (errors, errors)
1192 )
1193 == txt
1194 )
1195
1196 for num1, num2, txt in ((1, 2, "I saw 2 saws"), (2, 1, "we saw 1 saw")):
1197 assert (
1198 "{}{}{} {}{}".format(
1199 p.num(num1, ""),
1200 p.plural("I"),
1201 p.plural_verb(" saw"),
1202 p.num(num2),
1203 p.plural_noun(" saw"),
1204 )
1205 == txt
1206 )
1207
1208 assert (
1209 p.inflect(
1210 "num(%d, False)plural('I') plural_verb('saw') "
1211 "num(%d) plural_noun('saw')" % (num1, num2)
1212 )
1213 == txt
1214 )
1215
1216 assert p.a("a cat") == "a cat"
1217
1218 for word, txt in (
1219 ("cat", "a cat"),
1220 ("aardvark", "an aardvark"),
1221 ("ewe", "a ewe"),
1222 ("hour", "an hour"),
1223 ):
1224 assert p.a("{} {}".format(p.number_to_words(1, one="a"), word)) == txt
1225
1226 p.num(2)
1227
1228 def test_unknown_method(self):
1229 p = inflect.engine()
1230 with pytest.raises(AttributeError):
1231 p.unknown_method # noqa: B018