| 1 |
import os |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import inflect |
| 6 |
|
| 7 |
|
| 8 |
def is_eq(p, a, b): |
| 9 |
return ( |
| 10 |
p.compare(a, b) |
| 11 |
or p.plnounequal(a, b) |
| 12 |
or p.plverbequal(a, b) |
| 13 |
or p.pladjequal(a, b) |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
def test_many(): |
| 18 |
p = inflect.engine() |
| 19 |
|
| 20 |
data = get_data() |
| 21 |
|
| 22 |
for line in data: |
| 23 |
if "TODO:" in line: |
| 24 |
continue |
| 25 |
try: |
| 26 |
singular, rest = line.split("->", 1) |
| 27 |
except ValueError: |
| 28 |
continue |
| 29 |
singular = singular.strip() |
| 30 |
rest = rest.strip() |
| 31 |
try: |
| 32 |
plural, comment = rest.split("#", 1) |
| 33 |
except ValueError: |
| 34 |
plural = rest.strip() |
| 35 |
comment = "" |
| 36 |
try: |
| 37 |
mod_plural, class_plural = plural.split("|", 1) |
| 38 |
mod_plural = mod_plural.strip() |
| 39 |
class_plural = class_plural.strip() |
| 40 |
except ValueError: |
| 41 |
mod_plural = class_plural = plural.strip() |
| 42 |
if "verb" in comment.lower(): |
| 43 |
is_nv = "_V" |
| 44 |
elif "noun" in comment.lower(): |
| 45 |
is_nv = "_N" |
| 46 |
else: |
| 47 |
is_nv = "" |
| 48 |
|
| 49 |
p.classical(all=0, names=0) |
| 50 |
mod_PL_V = p.plural_verb(singular) |
| 51 |
mod_PL_N = p.plural_noun(singular) |
| 52 |
mod_PL = p.plural(singular) |
| 53 |
if is_nv == "_V": |
| 54 |
mod_PL_val = mod_PL_V |
| 55 |
elif is_nv == "_N": |
| 56 |
mod_PL_val = mod_PL_N |
| 57 |
else: |
| 58 |
mod_PL_val = mod_PL |
| 59 |
|
| 60 |
p.classical(all=1) |
| 61 |
class_PL_V = p.plural_verb(singular) |
| 62 |
class_PL_N = p.plural_noun(singular) |
| 63 |
class_PL = p.plural(singular) |
| 64 |
if is_nv == "_V": |
| 65 |
class_PL_val = class_PL_V |
| 66 |
elif is_nv == "_N": |
| 67 |
class_PL_val = class_PL_N |
| 68 |
else: |
| 69 |
class_PL_val = class_PL |
| 70 |
|
| 71 |
check_all( |
| 72 |
p, is_nv, singular, mod_PL_val, class_PL_val, mod_plural, class_plural |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
def check_all(p, is_nv, singular, mod_PL_val, class_PL_val, mod_plural, class_plural): |
| 77 |
assert mod_plural == mod_PL_val |
| 78 |
assert class_plural == class_PL_val |
| 79 |
assert is_eq(p, singular, mod_plural) in ("s:p", "p:s", "eq") |
| 80 |
assert is_eq(p, mod_plural, singular) in ("p:s", "s:p", "eq") |
| 81 |
assert is_eq(p, singular, class_plural) in ("s:p", "p:s", "eq") |
| 82 |
assert is_eq(p, class_plural, singular) in ("p:s", "s:p", "eq") |
| 83 |
assert singular != "" |
| 84 |
expected = mod_PL_val if class_PL_val else f"{mod_PL_val}|{class_PL_val}" |
| 85 |
assert mod_PL_val == expected |
| 86 |
|
| 87 |
if is_nv != "_V": |
| 88 |
assert p.singular_noun(mod_plural, 1) == singular |
| 89 |
|
| 90 |
assert p.singular_noun(class_plural, 1) == singular |
| 91 |
|
| 92 |
|
| 93 |
def test_def(): |
| 94 |
p = inflect.engine() |
| 95 |
|
| 96 |
p.defnoun("kin", "kine") |
| 97 |
p.defnoun("(.*)x", "$1xen") |
| 98 |
|
| 99 |
p.defverb("foobar", "feebar", "foobar", "feebar", "foobars", "feebar") |
| 100 |
|
| 101 |
p.defadj("red", "red|gules") |
| 102 |
|
| 103 |
assert p.no("kin", 0) == "no kine" |
| 104 |
assert p.no("kin", 1) == "1 kin" |
| 105 |
assert p.no("kin", 2) == "2 kine" |
| 106 |
|
| 107 |
assert p.no("regex", 0) == "no regexen" |
| 108 |
|
| 109 |
assert p.plural("foobar", 2) == "feebar" |
| 110 |
assert p.plural("foobars", 2) == "feebar" |
| 111 |
|
| 112 |
assert p.plural("red", 0) == "red" |
| 113 |
assert p.plural("red", 1) == "red" |
| 114 |
assert p.plural("red", 2) == "red" |
| 115 |
p.classical(all=True) |
| 116 |
assert p.plural("red", 0) == "red" |
| 117 |
assert p.plural("red", 1) == "red" |
| 118 |
assert p.plural("red", 2) == "gules" |
| 119 |
|
| 120 |
|
| 121 |
def test_ordinal(): |
| 122 |
p = inflect.engine() |
| 123 |
assert p.ordinal(0) == "0th" |
| 124 |
assert p.ordinal(1) == p.ordinal("1") == "1st" |
| 125 |
assert p.ordinal(2) == "2nd" |
| 126 |
assert p.ordinal(3) == "3rd" |
| 127 |
assert p.ordinal(4) == "4th" |
| 128 |
assert p.ordinal(5) == "5th" |
| 129 |
assert p.ordinal(6) == "6th" |
| 130 |
assert p.ordinal(7) == "7th" |
| 131 |
assert p.ordinal(8) == "8th" |
| 132 |
assert p.ordinal(9) == "9th" |
| 133 |
assert p.ordinal(10) == "10th" |
| 134 |
assert p.ordinal(11) == "11th" |
| 135 |
assert p.ordinal(12) == "12th" |
| 136 |
assert p.ordinal(13) == "13th" |
| 137 |
assert p.ordinal(14) == "14th" |
| 138 |
assert p.ordinal(15) == "15th" |
| 139 |
assert p.ordinal(16) == "16th" |
| 140 |
assert p.ordinal(17) == "17th" |
| 141 |
assert p.ordinal(18) == "18th" |
| 142 |
assert p.ordinal(19) == "19th" |
| 143 |
assert p.ordinal(20) == "20th" |
| 144 |
assert p.ordinal(21) == "21st" |
| 145 |
assert p.ordinal(22) == "22nd" |
| 146 |
assert p.ordinal(23) == "23rd" |
| 147 |
assert p.ordinal(24) == "24th" |
| 148 |
assert p.ordinal(100) == "100th" |
| 149 |
assert p.ordinal(101) == "101st" |
| 150 |
assert p.ordinal(102) == "102nd" |
| 151 |
assert p.ordinal(103) == "103rd" |
| 152 |
assert p.ordinal(104) == "104th" |
| 153 |
|
| 154 |
assert p.ordinal(1.1) == p.ordinal("1.1") == "1.1st" |
| 155 |
assert p.ordinal(1.2) == "1.2nd" |
| 156 |
assert p.ordinal(5.502) == "5.502nd" |
| 157 |
|
| 158 |
assert p.ordinal("zero") == "zeroth" |
| 159 |
assert p.ordinal("one") == "first" |
| 160 |
assert p.ordinal("two") == "second" |
| 161 |
assert p.ordinal("three") == "third" |
| 162 |
assert p.ordinal("four") == "fourth" |
| 163 |
assert p.ordinal("five") == "fifth" |
| 164 |
assert p.ordinal("six") == "sixth" |
| 165 |
assert p.ordinal("seven") == "seventh" |
| 166 |
assert p.ordinal("eight") == "eighth" |
| 167 |
assert p.ordinal("nine") == "ninth" |
| 168 |
assert p.ordinal("ten") == "tenth" |
| 169 |
assert p.ordinal("eleven") == "eleventh" |
| 170 |
assert p.ordinal("twelve") == "twelfth" |
| 171 |
assert p.ordinal("thirteen") == "thirteenth" |
| 172 |
assert p.ordinal("fourteen") == "fourteenth" |
| 173 |
assert p.ordinal("fifteen") == "fifteenth" |
| 174 |
assert p.ordinal("sixteen") == "sixteenth" |
| 175 |
assert p.ordinal("seventeen") == "seventeenth" |
| 176 |
assert p.ordinal("eighteen") == "eighteenth" |
| 177 |
assert p.ordinal("nineteen") == "nineteenth" |
| 178 |
assert p.ordinal("twenty") == "twentieth" |
| 179 |
assert p.ordinal("twenty-one") == "twenty-first" |
| 180 |
assert p.ordinal("twenty-two") == "twenty-second" |
| 181 |
assert p.ordinal("twenty-three") == "twenty-third" |
| 182 |
assert p.ordinal("twenty-four") == "twenty-fourth" |
| 183 |
assert p.ordinal("one hundred") == "one hundredth" |
| 184 |
assert p.ordinal("one hundred and one") == "one hundred and first" |
| 185 |
assert p.ordinal("one hundred and two") == "one hundred and second" |
| 186 |
assert p.ordinal("one hundred and three") == "one hundred and third" |
| 187 |
assert p.ordinal("one hundred and four") == "one hundred and fourth" |
| 188 |
|
| 189 |
|
| 190 |
def test_decimal_ordinals(): |
| 191 |
""" |
| 192 |
Capture expectation around ordinals for decimals. |
| 193 |
|
| 194 |
This expectation is held loosely. Another expectation may be |
| 195 |
considered if appropriate. |
| 196 |
""" |
| 197 |
|
| 198 |
p = inflect.engine() |
| 199 |
assert p.ordinal("1.23") == "1.23rd" |
| 200 |
assert p.ordinal("7.09") == "7.09th" |
| 201 |
|
| 202 |
|
| 203 |
def test_prespart(): |
| 204 |
p = inflect.engine() |
| 205 |
assert p.present_participle("sees") == "seeing" |
| 206 |
assert p.present_participle("eats") == "eating" |
| 207 |
assert p.present_participle("bats") == "batting" |
| 208 |
assert p.present_participle("hates") == "hating" |
| 209 |
assert p.present_participle("spies") == "spying" |
| 210 |
assert p.present_participle("skis") == "skiing" |
| 211 |
|
| 212 |
|
| 213 |
def test_inflect_on_tuples(): |
| 214 |
p = inflect.engine() |
| 215 |
assert p.inflect("plural('egg', ('a', 'b', 'c'))") == "eggs" |
| 216 |
assert p.inflect("plural('egg', ['a', 'b', 'c'])") == "eggs" |
| 217 |
assert p.inflect("plural_noun('egg', ('a', 'b', 'c'))") == "eggs" |
| 218 |
assert p.inflect("plural_adj('a', ('a', 'b', 'c'))") == "some" |
| 219 |
assert p.inflect("plural_verb('was', ('a', 'b', 'c'))") == "were" |
| 220 |
assert p.inflect("singular_noun('eggs', ('a', 'b', 'c'))") == "eggs" |
| 221 |
assert p.inflect("an('error', ('a', 'b', 'c'))") == "('a', 'b', 'c') error" |
| 222 |
assert p.inflect("This is not a function(name)") == "This is not a function(name)" |
| 223 |
|
| 224 |
|
| 225 |
def test_inflect_on_builtin_constants(): |
| 226 |
p = inflect.engine() |
| 227 |
assert ( |
| 228 |
p.inflect("Plural of False is plural('False')") == "Plural of False is Falses" |
| 229 |
) |
| 230 |
assert p.inflect("num(%d, False) plural('False')" % 10) == " Falses" |
| 231 |
|
| 232 |
assert p.inflect("plural('True')") == "Trues" |
| 233 |
assert p.inflect("num(%d, True) plural('False')" % 10) == "10 Falses" |
| 234 |
assert p.inflect("num(%d, %r) plural('False')" % (10, True)) == "10 Falses" |
| 235 |
|
| 236 |
assert p.inflect("plural('None')") == "Nones" |
| 237 |
assert p.inflect("num(%d, %r) plural('True')" % (10, None)) == "10 Trues" |
| 238 |
|
| 239 |
|
| 240 |
def test_inflect_keyword_args(): |
| 241 |
p = inflect.engine() |
| 242 |
assert ( |
| 243 |
p.inflect("number_to_words(1234, andword='')") |
| 244 |
== "one thousand, two hundred thirty-four" |
| 245 |
) |
| 246 |
|
| 247 |
assert ( |
| 248 |
p.inflect("number_to_words(1234, andword='plus')") |
| 249 |
== "one thousand, two hundred plus thirty-four" |
| 250 |
) |
| 251 |
|
| 252 |
assert ( |
| 253 |
p.inflect("number_to_words('555_1202', group=1, zero='oh')") |
| 254 |
== "five, five, five, one, two, oh, two" |
| 255 |
) |
| 256 |
|
| 257 |
|
| 258 |
def test_NameError_in_strings(): |
| 259 |
with pytest.raises(NameError): |
| 260 |
p = inflect.engine() |
| 261 |
assert p.inflect("plural('two')") == "twoes" |
| 262 |
p.inflect("plural(two)") |
| 263 |
|
| 264 |
|
| 265 |
def get_data(): |
| 266 |
filename = os.path.join(os.path.dirname(__file__), "inflections.txt") |
| 267 |
with open(filename, encoding='utf-8') as strm: |
| 268 |
return list(map(str.strip, strm)) |