Skip to content

Commit

Permalink
shell: added tests for #98
Browse files Browse the repository at this point in the history
  • Loading branch information
matze-dd authored Nov 6, 2020
1 parent 670d147 commit c04514e
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 3 deletions.
41 changes: 40 additions & 1 deletion tests/test_shell_cmd/run_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

tmp_dir = 'tests/test_shell_cmd/tmp/'
lt_exec = tmp_dir + 'lt_exec.sh'
lt_in = tmp_dir + 'lt_in.txt'
lt_out = tmp_dir + 'lt_out.txt'
shell_in = tmp_dir + 'shell_in.tex'
msg_header = '=== ' + shell_in + ' ===\n'
Expand All @@ -17,6 +18,7 @@
#
def run_shell(options, latex, encoding, lt_json_out):

os.system('mkdir ' + tmp_dir)
with open(lt_exec, mode='w') as f:
f.write('#!/bin/bash\n')
f.write('cat ' + lt_out + '\n')
Expand All @@ -32,6 +34,43 @@ def run_shell(options, latex, encoding, lt_json_out):
+ ' ' + options + ' ' + shell_in)
out = subprocess.run(cmd.split(), stdout=subprocess.PIPE)

os.system('rm ' + lt_exec + ' ' + shell_in + ' ' + lt_out)
os.system('rm -r ' + tmp_dir)
return out.stdout.decode()

json_ok = r"""
{"software":{"name":"LanguageTool","version":"4.7","buildDate":"2019-09-28 10:09","apiVersion":1,"premium":false,"premiumHint":"You might be missing errors only the Premium version can find. Contact us at support<at>languagetoolplus.com.","status":""},"warnings":{"incompleteResults":false},"language":{"name":"English (GB)","code":"en-GB","detectedLanguage":{"name":"English (GB)","code":"en-GB","confidence":1.0}},"matches":[]}
"""

# run yalafi.shell, using an LT emulation called with --lt-command,
# return input to LT as string
# - otpions: additional options passed to yalafi.shell
# - latex: LaTeX text, is written to a temp file
# - encoding: encoding of temp LaTeX file
#
def get_lt_in(options, latex, encoding):

os.system('mkdir ' + tmp_dir)
with open(lt_exec, mode='w') as f:
f.write('#!/bin/bash\n')
f.write('echo >> ' + lt_in + '\n')
f.write('echo $* >> ' + lt_in + '\n')
f.write('cat >> ' + lt_in + '\n')
f.write('echo >> ' + lt_in + '\n')
f.write('cat ' + lt_out + '\n')
os.system('chmod +x ' + lt_exec)

with open(lt_out, mode='w') as f:
f.write(json_ok)

with open(shell_in, mode='w', encoding=encoding) as f:
f.write(latex)

os.system('python -m yalafi.shell --lt-command ' + lt_exec
+ ' ' + options + ' ' + shell_in)

with open(lt_in) as f:
txt = f.read()
os.system('rm -r ' + tmp_dir)

return txt

119 changes: 119 additions & 0 deletions tests/test_shell_cmd/test_lt_multilang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#
# test options and input to LanguageTool that are sent by yalafi.shell
# - here: for multi-language support
#

from tests.test_shell_cmd import run_shell

# without multi-lang
#
latex_1 = r"""
A \foreignlanguage{german}{B} C
"""
lt_in_1 = """
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
A B C
"""
def test_1():
lt_in = run_shell.get_lt_in('', latex_1, 'utf-8')
assert lt_in == lt_in_1

# with multi-lang
#
latex_2 = r"""
A \foreignlanguage{german}{B} C
"""
lt_in_2 = """
--json --encoding utf-8 --language de-DE --disable WHITESPACE_RULE -
B
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
A L-L-L C
"""
def test_2():
lt_in = run_shell.get_lt_in('--multi-language', latex_2, 'utf-8')
assert lt_in == lt_in_2

# with multi-lang, but --ml-continue-threshold 0
#
latex_3 = r"""
A \foreignlanguage{german}{B} C
"""
lt_in_3 = """
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
A
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
C
--json --encoding utf-8 --language de-DE --disable WHITESPACE_RULE -
B
"""
def test_3():
lt_in = run_shell.get_lt_in('--multi-language --ml-continue-threshold 0',
latex_3, 'utf-8')
assert lt_in == lt_in_3

# with multi-lang and --ml-disable
#
latex_4 = r"""
A \foreignlanguage{german}{B} C
"""
lt_in_4 = """
--json --encoding utf-8 --language de-DE --disable WHITESPACE_RULE,x,y -
B
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
A L-L-L C
"""
def test_4():
lt_in = run_shell.get_lt_in('--multi-language --ml-disable x,y',
latex_4, 'utf-8')
assert lt_in == lt_in_4

# with multi-lang and --ml-disable, but --ml-rule-thresh 0
#
latex_5 = r"""
A \foreignlanguage{german}{B} C
"""
lt_in_5 = """
--json --encoding utf-8 --language de-DE --disable WHITESPACE_RULE -
B
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
A L-L-L C
"""
def test_5():
lt_in = run_shell.get_lt_in('--multi-language --ml-disable x,y'
+ ' --ml-rule-threshold 0', latex_5, 'utf-8')
assert lt_in == lt_in_5

# with multi-lang and --ml-disablecategories
#
latex_6 = r"""
A \foreignlanguage{german}{B} C
"""
lt_in_6 = """
--json --encoding utf-8 --language de-DE --disable WHITESPACE_RULE --disablecategories x,y -
B
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
A L-L-L C
"""
def test_6():
lt_in = run_shell.get_lt_in('--multi-language --ml-disablecategories x,y',
latex_6, 'utf-8')
assert lt_in == lt_in_6

67 changes: 67 additions & 0 deletions tests/test_shell_cmd/test_lt_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# test options and input to LanguageTool that are sent by yalafi.shell
# - here: option passing
#

import pytest
from tests.test_shell_cmd import run_shell

latex_in = r"""
$x$ $y$ $z$
"""

data_test_options = [
('', """
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE -
C-C-C D-D-D E-E-E
"""),
('--disable ""', """
--json --encoding utf-8 --language en-GB -
C-C-C D-D-D E-E-E
"""),
('--disable x,y', """
--json --encoding utf-8 --language en-GB --disable x,y -
C-C-C D-D-D E-E-E
"""),
('--enable x,y --disable ""', """
--json --encoding utf-8 --language en-GB --enable x,y -
C-C-C D-D-D E-E-E
"""),
('--disablecategories x,y', """
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE --disablecategories x,y -
C-C-C D-D-D E-E-E
"""),
('--enablecategories x,y', """
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE --enablecategories x,y -
C-C-C D-D-D E-E-E
"""),
('--language ru-RU', """
--json --encoding utf-8 --language ru-RU --disable WHITESPACE_RULE -
В-В-В Г-Г-Г Д-Д-Д
"""),
('--lt-options "~--xx 1 --yy 2"', """
--json --encoding utf-8 --language en-GB --disable WHITESPACE_RULE --xx 1 --yy 2 -
C-C-C D-D-D E-E-E
"""),
]

@pytest.mark.parametrize('options,lt_in_expected', data_test_options)
def test_macros_latex(options, lt_in_expected):
lt_in = run_shell.get_lt_in(options, latex_in, 'utf-8')
assert lt_in == lt_in_expected
19 changes: 17 additions & 2 deletions tests/test_shell_cmd/test_shell_checks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#
# test yalafi.shell with options --single-letters and
# --equation-puncluation
# - test yalafi.shell with options --single-letters
# and --equation-punctuation
#
# - test option --list-unknown
#

from tests.test_shell_cmd import run_shell
Expand Down Expand Up @@ -47,3 +49,16 @@ def test_shell_equation():
latex, encoding, json)
assert out == run_shell.msg_header + msg_equation


latex_unkn = r"""
A \xxxxxx
\begin{yyyyyyyy}
B
"""
msg_unkn = r"""\xxxxxx
yyyyyyyy
"""
def test_shell_unkn():
out = run_shell.run_shell('--list-unknown', latex_unkn, encoding, json)
assert out == run_shell.msg_header + msg_unkn

0 comments on commit c04514e

Please sign in to comment.