Skip to content

Commit 631ba50

Browse files
author
spirit
committed
Test get_country_code().
1 parent e55dca6 commit 631ba50

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎language_tool/country_codes.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,12 @@ def get_country_code(country_name):
207207
if country_name in COUNTRY_CODES.values():
208208
return country_name
209209

210-
codes = difflib.get_close_matches(
211-
country_name, COUNTRY_CODES.keys(), 1)
212-
if codes:
213-
return COUNTRY_CODES[codes[0]]
210+
matches = difflib.get_close_matches(country_name, COUNTRY_CODES, 1)
211+
if matches:
212+
return COUNTRY_CODES[matches[0]]
214213

215-
codes = difflib.get_close_matches(
216-
country_name, LANGUAGE_TO_COUNTRY.keys(), 1)
217-
if codes:
218-
return LANGUAGE_TO_COUNTRY[codes[0]]
214+
matches = difflib.get_close_matches(country_name, LANGUAGE_TO_COUNTRY, 1)
215+
if matches:
216+
return LANGUAGE_TO_COUNTRY[matches[0]]
219217

220218
raise KeyError(country_name)

‎test.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import warnings
77

88
import language_tool
9+
from language_tool.country_codes import get_country_code
910

1011

1112
class TestLanguageTool(unittest.TestCase):
@@ -51,6 +52,30 @@ class TestLanguageTool(unittest.TestCase):
5152
],
5253
}
5354

55+
country_tests = {
56+
"Australian": "AU",
57+
"Canadian": "CA",
58+
"GB": "GB",
59+
"New Zealand": "NZ",
60+
"South African": "ZA",
61+
"US": "US",
62+
"Austria": "AT",
63+
"Germany": "DE",
64+
"Swiss": "CH",
65+
"Brazil": "BR",
66+
"Portugal": "PT",
67+
68+
"Australia": "AU",
69+
"Canada": "CA",
70+
"United Kingdom": "GB",
71+
"South Africa": "ZA",
72+
"United States": "US",
73+
"Switzerland": "CH",
74+
75+
"Austrian": "AT",
76+
"Brazilian": "BR",
77+
}
78+
5479
def test_samples(self):
5580
languages = language_tool.get_languages()
5681
for language, tests in self.tests.items():
@@ -84,6 +109,10 @@ def test_version(self):
84109
version = language_tool.get_version()
85110
self.assertTrue(version)
86111

112+
def test_country_codes(self):
113+
for country, code in self.country_tests.items():
114+
self.assertEqual(get_country_code(country), code)
115+
87116

88117
if __name__ == "__main__":
89118
unittest.main()

0 commit comments

Comments
 (0)