Skip to content

Commit

Permalink
diff tool: multiline diffs supported
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Aug 29, 2019
1 parent d129935 commit dd7c628
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nested_diff/diff_tool.py
Expand Up @@ -40,6 +40,7 @@ def diff(self, a, b):
"""
return nested_diff.diff(
a, b,
multiline_diff_context=self.args.text_ctx,
A=self.args.A,
N=self.args.N,
O=self.args.O,
Expand All @@ -58,6 +59,15 @@ def get_argparser(self, description=None):
parser.add_argument('file1', type=argparse.FileType())
parser.add_argument('file2', type=argparse.FileType())

parser.add_argument(
'--text-ctx',
default=3,
metavar='NUM',
type=int,
help='Amount of context lines for multiline strings diffs; ' +
'negative value will disable multiline diffs, default is 3'
)

parser.add_argument(
'--ifmt',
type=str,
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/diff_tool/shared.multiline.a.json
@@ -0,0 +1,3 @@
{
"text": "Common line,\nanother common line.\nAnd one more common line.\nStill a common line, but last, next will be different.\nFirst string for a.\nSecond striing for a.\nThird string, belongs to file a.\nAnd fourth. Again to a.\nWow! Common strings began! =)\nAnother common line.\nDifferent line. AAaaaaa!!\nAaaaa!!\nA!\n"
}
3 changes: 3 additions & 0 deletions tests/cli/diff_tool/shared.multiline.b.json
@@ -0,0 +1,3 @@
{
"text": "Common line,\nanother common line.\nAnd one more common line.\nStill a common line, but last, next will be different.\nFirst string for b.\nSecond striing for b.\nThird string, belongs to file b.\nAnd fourth. Again to b.\nWow! Common strings began! =)\nAnother common line.\nDifferent line. BBbbbbb!!\nBbbbb!!\nB!\n"
}
47 changes: 47 additions & 0 deletions tests/cli/diff_tool/test_diff_tool.py
Expand Up @@ -102,6 +102,53 @@ def test_ini_ifmt(capsys, expected, fullname, PY2):
assert expected == captured.out


def test_multiline_default(capsys, expected, fullname):
DiffApp(args=(
fullname('multiline.a.json', shared=True),
fullname('multiline.b.json', shared=True),
)).run()

captured = capsys.readouterr()
assert '' == captured.err
assert expected == captured.out


def test_multiline_default_term(capsys, expected, fullname):
DiffApp(args=(
fullname('multiline.a.json', shared=True),
fullname('multiline.b.json', shared=True),
'--ofmt', 'term',
)).run()

captured = capsys.readouterr()
assert '' == captured.err
assert expected == captured.out


def test_multiline_context_0(capsys, expected, fullname):
DiffApp(args=(
fullname('multiline.a.json', shared=True),
fullname('multiline.b.json', shared=True),
'--text-ctx', '0'
)).run()

captured = capsys.readouterr()
assert '' == captured.err
assert expected == captured.out


def test_multiline_disabled(capsys, expected, fullname):
DiffApp(args=(
fullname('multiline.a.json', shared=True),
fullname('multiline.b.json', shared=True),
'--text-ctx', '-1'
)).run()

captured = capsys.readouterr()
assert '' == captured.err
assert expected == captured.out


def test_text_ofmt(capsys, expected, fullname):
DiffApp(args=(
fullname('lists.a.json', shared=True),
Expand Down
17 changes: 17 additions & 0 deletions tests/cli/diff_tool/test_diff_tool.test_multiline_context_0.exp
@@ -0,0 +1,17 @@
{'text'}
@@ -5,4 +5,4 @@
- First string for a.
- Second striing for a.
- Third string, belongs to file a.
- And fourth. Again to a.
+ First string for b.
+ Second striing for b.
+ Third string, belongs to file b.
+ And fourth. Again to b.
@@ -11,3 +11,3 @@
- Different line. AAaaaaa!!
- Aaaaa!!
- A!
+ Different line. BBbbbbb!!
+ Bbbbb!!
+ B!
22 changes: 22 additions & 0 deletions tests/cli/diff_tool/test_diff_tool.test_multiline_default.exp
@@ -0,0 +1,22 @@
{'text'}
@@ -2,13 +2,13 @@
another common line.
And one more common line.
Still a common line, but last, next will be different.
- First string for a.
- Second striing for a.
- Third string, belongs to file a.
- And fourth. Again to a.
+ First string for b.
+ Second striing for b.
+ Third string, belongs to file b.
+ And fourth. Again to b.
Wow! Common strings began! =)
Another common line.
- Different line. AAaaaaa!!
- Aaaaa!!
- A!
+ Different line. BBbbbbb!!
+ Bbbbb!!
+ B!

22 changes: 22 additions & 0 deletions tests/cli/diff_tool/test_diff_tool.test_multiline_default_term.exp
@@ -0,0 +1,22 @@
{'text'}
 @@ -2,13 +2,13 @@
another common line.
And one more common line.
Still a common line, but last, next will be different.
- First string for a.
- Second striing for a.
- Third string, belongs to file a.
- And fourth. Again to a.
+ First string for b.
+ Second striing for b.
+ Third string, belongs to file b.
+ And fourth. Again to b.
Wow! Common strings began! =)
Another common line.
- Different line. AAaaaaa!!
- Aaaaa!!
- A!
+ Different line. BBbbbbb!!
+ Bbbbb!!
+ B!

@@ -0,0 +1,3 @@
{'text'}
- 'Common line,\nanother common line.\nAnd one more common line.\nStill a common line, but last, next will be different.\nFirst string for a.\nSecond striing for a.\nThird string, belongs to file a.\nAnd fourth. Again to a.\nWow! Common strings began! =)\nAnother common line.\nDifferent line. AAaaaaa!!\nAaaaa!!\nA!\n'
+ 'Common line,\nanother common line.\nAnd one more common line.\nStill a common line, but last, next will be different.\nFirst string for b.\nSecond striing for b.\nThird string, belongs to file b.\nAnd fourth. Again to b.\nWow! Common strings began! =)\nAnother common line.\nDifferent line. BBbbbbb!!\nBbbbb!!\nB!\n'

0 comments on commit dd7c628

Please sign in to comment.