mirror of
https://github.com/codespell-project/codespell.git
synced 2025-05-21 01:17:01 +08:00
* Add context options: -A/-B/-C; Fixes #287 * Fix indentation of line continuation. * Fix 'only before context' case. * Add some tests for the new context options. * Refactor context code (pass context tuple, instead of before/after variables) * Get rid of 'remove temp file code' in context test. * Fix reintroduced error (regarding 'only before context').
This commit is contained in:

committed by
Eric Larson

parent
c3e589d066
commit
e74f433bb8
@ -302,6 +302,65 @@ def test_case_handling(tmpdir, capsys):
|
||||
os.remove(f.name)
|
||||
|
||||
|
||||
def test_context(tmpdir, capsys):
|
||||
"""Test context options."""
|
||||
d = str(tmpdir)
|
||||
with open(op.join(d, 'context.txt'), 'w') as f:
|
||||
f.write('line 1\nline 2\nline 3 abandonned\nline 4\nline 5')
|
||||
|
||||
# symmetric context, fully within file
|
||||
cs.main('-C', '1', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert len(lines) == 5
|
||||
assert lines[0] == ': line 2'
|
||||
assert lines[1] == '> line 3 abandonned'
|
||||
assert lines[2] == ': line 4'
|
||||
|
||||
# requested context is bigger than the file
|
||||
cs.main('-C', '10', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert len(lines) == 7
|
||||
assert lines[0] == ': line 1'
|
||||
assert lines[1] == ': line 2'
|
||||
assert lines[2] == '> line 3 abandonned'
|
||||
assert lines[3] == ': line 4'
|
||||
assert lines[4] == ': line 5'
|
||||
|
||||
# only before context
|
||||
cs.main('-B', '2', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert len(lines) == 5
|
||||
assert lines[0] == ': line 1'
|
||||
assert lines[1] == ': line 2'
|
||||
assert lines[2] == '> line 3 abandonned'
|
||||
|
||||
# only after context
|
||||
cs.main('-A', '1', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert len(lines) == 4
|
||||
assert lines[0] == '> line 3 abandonned'
|
||||
assert lines[1] == ': line 4'
|
||||
|
||||
# asymmetric context
|
||||
cs.main('-B', '2', '-A', '1', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert len(lines) == 6
|
||||
assert lines[0] == ': line 1'
|
||||
assert lines[1] == ': line 2'
|
||||
assert lines[2] == '> line 3 abandonned'
|
||||
assert lines[3] == ': line 4'
|
||||
|
||||
# both '-C' and '-A' on the command line
|
||||
cs.main('-C', '2', '-A', '1', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert 'ERROR' in lines[0]
|
||||
|
||||
# both '-C' and '-B' on the command line
|
||||
cs.main('-C', '2', '-B', '1', d)
|
||||
lines = capsys.readouterr()[0].split('\n')
|
||||
assert 'ERROR' in lines[0]
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def FakeStdin(text):
|
||||
if sys.version[0] == '2':
|
||||
|
Reference in New Issue
Block a user