1- import unittest
1+ import sys
22
33import pytest
4- import tomllib
54
65from babel import numbers , rbnf
76from babel .localedata import locale_identifiers
87
98soft_hyphen = '\xad '
109
1110
12- class TestRuleEngine (unittest .TestCase ):
11+ def test_basic ():
12+ x = rbnf .RuleBasedNumberFormat .negotiate ('hu_HU' )
13+ assert str (x ._locale ) == 'hu'
14+ assert 'spellout-numbering' in x .available_rulesets
15+
16+
17+ def test_negotiation ():
18+ for lid in locale_identifiers ():
19+ try :
20+ loc = rbnf .RuleBasedNumberFormat .negotiate (lid )._locale
21+ except rbnf .RulesetNotFound :
22+ # generate warning if necessary
23+ continue
24+ # test groups
25+ for k in loc ._data ['rbnf_rules' ]:
26+ assert k in rbnf .RuleBasedNumberFormat .group_types
27+
28+
29+ def test_tokenization ():
30+ x = list (rbnf .tokenize ("text[opt];" ))
31+ res = [
32+ rbnf .TokenInfo (type = 1 , reference = 'text' , optional = False ),
33+ rbnf .TokenInfo (type = 1 , reference = 'opt' , optional = True ),
34+ ]
35+ assert x == res
36+
37+
38+ def test_xml_parsing ():
1339 """
14- Test everything related to the rules engine
40+ all the rules should be able to go through the parser and tokenizer
41+ made up some rules and run the tokenizer on them
42+
43+ TODO
44+ read data from all the locales that have rbnf_rules defined
45+ all the raw rules should be in a specific structure based
46+ on the XML specification
1547 """
48+ assert True
1649
17- def test_basic (self ):
18- x = rbnf .RuleBasedNumberFormat .negotiate ('hu_HU' )
19- assert str (x ._locale ) == 'hu'
20- assert 'spellout-numbering' in x .available_rulesets
21-
22- def test_negotiation (self ):
23- for lid in locale_identifiers ():
24- try :
25- loc = rbnf .RuleBasedNumberFormat .negotiate (lid )._locale
26- except rbnf .RulesetNotFound :
27- # generate warning if necessary
28- continue
29- # test groups
30- for k in loc ._data ['rbnf_rules' ]:
31- assert k in rbnf .RuleBasedNumberFormat .group_types
32-
33- def test_tokenization (self ):
34-
35- x = list (rbnf .tokenize ("text[opt];" ))
36- res = [
37- rbnf .TokenInfo (type = 1 , reference = 'text' , optional = False ),
38- rbnf .TokenInfo (type = 1 , reference = 'opt' , optional = True ),
39- ]
40- assert x == res
41-
42- def test_xml_parsing (self ):
43- """
44- all the rules should be able to go through the parser and tokenizer
45- made up some rules and run the tokenizer on them
46-
47- TODO
48- read data from all the locales that have rbnf_rules defined
49- all the raw rules should be in a specific structure based
50- on the XML specification
51- """
52- assert True
53-
54- def test_compute_divisor (self ):
55- for rule , divisor in (
56- (1001 , 1000 ),
57- (1_000_001 , 1_000_000 ),
58- (1_000_000_001 , 1_000_000_000 ),
59- (1_000_000_000_000_000_001 , 1_000_000_000_000_000_000 ),
60- (1001 , 1000 ),
61- (1_000_000 , 1_000_000 ),
62- (1_000_000_000 , 1_000_000_000 ),
63- (1_000_000_000_000_000_000 , 1_000_000_000_000_000_000 ),
64- ):
65- assert rbnf .compute_divisor (rule , 10 ) == divisor
50+
51+ def test_compute_divisor ():
52+ for rule , divisor in (
53+ (1001 , 1000 ),
54+ (1_000_001 , 1_000_000 ),
55+ (1_000_000_001 , 1_000_000_000 ),
56+ (1_000_000_000_000_000_001 , 1_000_000_000_000_000_000 ),
57+ (1001 , 1000 ),
58+ (1_000_000 , 1_000_000 ),
59+ (1_000_000_000 , 1_000_000_000 ),
60+ (1_000_000_000_000_000_000 , 1_000_000_000_000_000_000 ),
61+ ):
62+ assert rbnf .compute_divisor (rule , 10 ) == divisor
6663
6764
6865@pytest .mark .all_rbnf_locales
@@ -77,7 +74,9 @@ def test_spelling_smoke(locale, ruleset):
7774
7875
7976@pytest .mark .all_rbnf_locales
77+ @pytest .mark .skipif (sys .version_info < (3 , 11 ), reason = "requires python3.11 or higher for tomllib" )
8078def test_spelling_smoke_toml (locale ):
79+ import tomllib
8180
8281 speller = rbnf .RuleBasedNumberFormat .negotiate (locale )
8382
@@ -94,30 +93,3 @@ def test_spelling_smoke_toml(locale):
9493 print (f"RBNFError for { locale } in { ruleset } spelling { number } : { e } " )
9594 continue
9695 assert result == expected , f"{ locale } { ruleset } { number } : expected { expected } , got { result } "
97-
98-
99- # def test_hu_HU_error():
100- # with pytest.raises(exceptions.TooBigToSpell) as excinfo:
101- # _spell(10**66, ordinal=True)
102-
103- # with pytest.raises(exceptions.PrecisionError) as excinfo:
104- # _spell(.4326752, locale='hu_HU', precision=7)
105-
106- # with pytest.raises(exceptions.PrecisionError) as excinfo:
107- # _spell(.4326752)
108-
109- # with pytest.raises(exceptions.NoFractionOrdinalsAllowed) as excinfo:
110- # _spell('1999.23862', ordinal=True)
111-
112- # def test_en_GB_error():
113- # with pytest.raises(exceptions.TooBigToSpell) as excinfo:
114- # _spell(10**24, ordinal=True, locale='en_GB')
115-
116- # with pytest.raises(exceptions.PrecisionError) as excinfo:
117- # _spell(.4326752, locale='en_GB', precision=4)
118-
119- # with pytest.raises(exceptions.PrecisionError) as excinfo:
120- # _spell(.4326752, locale='en_GB')
121-
122- # with pytest.raises(exceptions.NoFractionOrdinalsAllowed) as excinfo:
123- # _spell('1999.23', ordinal=True, locale='en_GB')
0 commit comments