Skip to content

Commit f2ce5eb

Browse files
committed
clean up tests in pytest
1 parent 52b337a commit f2ce5eb

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

tests/test_major_functionality.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11

22
def test_langtool_load():
33
import language_tool_python
4-
lang_tool = language_tool_python.LanguageTool("en-US")
5-
matches = lang_tool.check('ain\'t nothin but a thang')
4+
tool = language_tool_python.LanguageTool("en-US")
5+
matches = tool.check('ain\'t nothin but a thang')
66
assert str(matches) == """[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter.', 'replacements': ['Ai'], 'offsetInContext': 0, 'context': "ain't nothin but a thang", 'offset': 0, 'errorLength': 2, 'category': 'CASING', 'ruleIssueType': 'typographical', 'sentence': "ain't nothin but a thang"}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['nothing', 'no thin'], 'offsetInContext': 6, 'context': "ain't nothin but a thang", 'offset': 6, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': "ain't nothin but a thang"}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['than', 'thing', 'hang', 'thank', 'Chang', 'tang', 'thong', 'twang', 'Thant', 'thane', 'Thanh', 'Jhang', 'Shang', 'Zhang'], 'offsetInContext': 19, 'context': "ain't nothin but a thang", 'offset': 19, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': "ain't nothin but a thang"})]"""
7+
tool.close()
78

89

9-
def test_process_starts_and_stops():
10+
def test_process_starts_and_stops_in_context_manager():
1011
import language_tool_python
11-
with language_tool_python.LanguageTool("en-US") as lang_tool:
12-
proc: subprocess.Popen = lang_tool._server
12+
with language_tool_python.LanguageTool("en-US") as tool:
13+
proc: subprocess.Popen = tool._server
1314
# Make sure process is running before killing language tool object.
14-
assert proc.poll() is None, "lang_tool._server not running after creation"
15-
# lang_tool.close() # Explicitly close() object so process stops before garbage collection.
15+
assert proc.poll() is None, "tool._server not running after creation"
1616
# Make sure process stopped after close() was called.
17-
assert proc.poll() is not None, "lang_tool._server should stop running after deletion"
18-
import time; time.sleep(10)
19-
# if poll is None: # p.subprocess is alive
17+
assert proc.poll() is not None, "tool._server should stop running after deletion"
18+
2019

21-
def test_process_starts_and_closes():
20+
def test_process_starts_and_stops_on_close():
2221
import language_tool_python
23-
lang_tool = language_tool_python.LanguageTool("en-US")
24-
proc: subprocess.Popen = lang_tool._server
22+
tool = language_tool_python.LanguageTool("en-US")
23+
proc: subprocess.Popen = tool._server
2524
# Make sure process is running before killing language tool object.
26-
assert proc.poll() is None, "lang_tool._server not running after creation"
27-
lang_tool.close() # Explicitly close() object so process stops before garbage collection.
28-
del lang_tool
25+
assert proc.poll() is None, "tool._server not running after creation"
26+
tool.close() # Explicitly close() object so process stops before garbage collection.
27+
del tool
2928
# Make sure process stopped after close() was called.
30-
assert proc.poll() is not None, "lang_tool._server should stop running after deletion"
29+
assert proc.poll() is not None, "tool._server should stop running after deletion"
3130
# if poll is None: # p.subprocess is alive
3231

3332

3433
# TODO(jxm): Add test to confirm that multiple instances connect to the same process instead of starting new ones.
3534

3635
def test_langtool_languages():
3736
import language_tool_python
38-
lang_tool = language_tool_python.LanguageTool("en-US")
39-
assert lang_tool._get_languages() == {'es-AR', 'ta-IN', 'en-CA', 'da', 'eo', 'pt-AO', 'de', 'gl', 'ru-RU', 'de-DE', 'en', 'br', 'en-ZA', 'pt-MZ', 'ast-ES', 'sk-SK', 'en-AU', 'ta', 'ga', 'be', 'pl', 'tl-PH', 'sl', 'ar', 'es', 'sl-SI', 'en-NZ', 'el', 'el-GR', 'ru', 'zh-CN', 'en-GB', 'be-BY', 'pl-PL', 'km-KH', 'pt', 'uk-UA', 'ca', 'de-DE-x-simple-language', 'ro', 'ca-ES', 'de-CH', 'ja-JP', 'tl', 'pt-PT', 'gl-ES', 'pt-BR', 'km', 'ga-IE', 'ja', 'sv', 'sk', 'en-US', 'de-AT', 'ca-ES-valencia', 'uk', 'it', 'zh', 'br-FR', 'da-DK', 'ast', 'fr', 'fa', 'nl', 'ro-RO', 'nl-BE', 'auto'}
37+
tool = language_tool_python.LanguageTool("en-US")
38+
assert tool._get_languages() == {'es-AR', 'ta-IN', 'en-CA', 'da', 'eo', 'pt-AO', 'de', 'gl', 'ru-RU', 'de-DE', 'en', 'br', 'en-ZA', 'pt-MZ', 'ast-ES', 'sk-SK', 'en-AU', 'ta', 'ga', 'be', 'pl', 'tl-PH', 'sl', 'ar', 'es', 'sl-SI', 'en-NZ', 'el', 'el-GR', 'ru', 'zh-CN', 'en-GB', 'be-BY', 'pl-PL', 'km-KH', 'pt', 'uk-UA', 'ca', 'de-DE-x-simple-language', 'ro', 'ca-ES', 'de-CH', 'ja-JP', 'tl', 'pt-PT', 'gl-ES', 'pt-BR', 'km', 'ga-IE', 'ja', 'sv', 'sk', 'en-US', 'de-AT', 'ca-ES-valencia', 'uk', 'it', 'zh', 'br-FR', 'da-DK', 'ast', 'fr', 'fa', 'nl', 'ro-RO', 'nl-BE', 'auto'}
39+
tool.close()
4040

4141
def test_match():
4242
import language_tool_python
@@ -45,25 +45,28 @@ def test_match():
4545
matches = tool.check(text)
4646
assert len(matches) == 2
4747
assert str(matches[0]) == 'Offset 16, length 1, Rule ID: EN_A_VS_AN\nMessage: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.\nSuggestion: an\nA sentence with a error in the Hitchhiker’s Guide tot he ...\n ^'
48+
tool.close()
4849

4950
def test_uk_typo():
5051
import language_tool_python
51-
language = language_tool_python.LanguageTool("en-UK")
52+
tool = language_tool_python.LanguageTool("en-UK")
5253

5354
sentence1 = "If you think this sentence is fine then, your wrong."
54-
results1 = language.check(sentence1)
55+
results1 = tool.check(sentence1)
5556
assert len(results1) == 1
5657
assert language_tool_python.utils.correct(sentence1, results1) == "If you think this sentence is fine then, you're wrong."
5758

58-
results2 = language.check("You're mum is called Emily, is that right?")
59+
results2 = tool.check("You're mum is called Emily, is that right?")
5960
assert len(results2) == 0
61+
tool.close()
6062

6163
def test_remote_es():
6264
import language_tool_python
6365
tool = language_tool_python.LanguageToolPublicAPI('es')
6466
es_text = 'Escriba un texto aquí. LanguageTool le ayudará a afrentar algunas dificultades propias de la escritura. Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales. También algunos errores de estilo, a grosso modo.'
6567
matches = tool.check(es_text)
6668
assert str(matches) == """[Match({'ruleId': 'AFRENTAR_DIFICULTADES', 'message': 'Confusión entre «afrontar» y «afrentar».', 'replacements': ['afrontar'], 'offsetInContext': 43, 'context': '...n texto aquí. LanguageTool le ayudará a afrentar algunas dificultades propias de la escr...', 'offset': 49, 'errorLength': 8, 'category': 'INCORRECT_EXPRESSIONS', 'ruleIssueType': 'grammar', 'sentence': 'LanguageTool le ayudará a afrentar algunas dificultades propias de la escritura.'}), Match({'ruleId': 'PRON_HABER_PARTICIPIO', 'message': 'El v. ‘haber’ se escribe con hache.', 'replacements': ['ha'], 'offsetInContext': 43, 'context': '...ificultades propias de la escritura. Se a hecho un esfuerzo para detectar errores...', 'offset': 107, 'errorLength': 1, 'category': 'MISSPELLING', 'ruleIssueType': 'misspelling', 'sentence': 'Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales.'}), Match({'ruleId': 'MORFOLOGIK_RULE_ES', 'message': 'Se ha encontrado un posible error ortográfico.', 'replacements': ['ortográficos', 'ortográficas', 'ortográfico', 'orográficos', 'ortografiaos', 'ortografíeos'], 'offsetInContext': 43, 'context': '...rzo para detectar errores tipográficos, ortograficos y incluso gramaticales. También algunos...', 'offset': 163, 'errorLength': 12, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales.'}), Match({'ruleId': 'Y_E_O_U', 'message': 'Cuando precede a palabras que comienzan por ‘i’, la conjunción ‘y’ se transforma en ‘e’.', 'replacements': ['e'], 'offsetInContext': 43, 'context': '...ctar errores tipográficos, ortograficos y incluso gramaticales. También algunos e...', 'offset': 176, 'errorLength': 1, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar', 'sentence': 'Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales.'}), Match({'ruleId': 'GROSSO_MODO', 'message': 'Esta expresión latina se usa sin preposición.', 'replacements': ['grosso modo'], 'offsetInContext': 43, 'context': '...les. También algunos errores de estilo, a grosso modo.', 'offset': 235, 'errorLength': 13, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar', 'sentence': 'También algunos errores de estilo, a grosso modo.'})]"""
69+
tool.close()
6770

6871
def test_correct_en_us():
6972
import language_tool_python
@@ -73,6 +76,7 @@ def test_correct_en_us():
7376
assert len(matches) == 4
7477

7578
assert tool.correct('cz of this brand is awsome,,i love this brand very much') == 'Cz of this brand is awesome,I love this brand very much'
79+
tool.close()
7680

7781
def test_spellcheck_en_gb():
7882
import language_tool_python
@@ -86,6 +90,7 @@ def test_spellcheck_en_gb():
8690
# Correct a sentence without spell-checking
8791
tool.disable_spellchecking()
8892
assert tool.correct(s) == "Wat is wrong with the spll chker"
93+
tool.close()
8994

9095
def test_session_only_new_spellings():
9196
import os

0 commit comments

Comments
 (0)