Skip to content

Commit b28837a

Browse files
committed
Add test case to test diffing function
1 parent 780f202 commit b28837a

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import print_function
22

33
import argparse
4+
import difflib
45
import io
56
import json
67
import sys
7-
import difflib
88
from collections import OrderedDict
99
from typing import List
1010
from typing import Mapping
@@ -56,7 +56,7 @@ def parse_topkeys(s): # type: (str) -> List[str]
5656
return s.split(',')
5757

5858

59-
def get_diff(source, target):
59+
def get_diff(source, target): # type: (List[str], List[str]) -> str
6060
source_lines = ''.join(source).split('\n')
6161
target_lines = ''.join(target).split('\n')
6262
d = difflib.Differ()
@@ -132,7 +132,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
132132
print('File {} is not pretty-formatted'.format(json_file))
133133

134134
if args.show_expected:
135-
print(get_diff(contents, pretty_contents))
135+
print(get_diff(contents, list(pretty_contents)))
136136

137137
if args.autofix:
138138
_autofix(json_file, pretty_contents)

tests/pretty_format_json_test.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from six import PY2
55

6+
from pre_commit_hooks.pretty_format_json import get_diff
67
from pre_commit_hooks.pretty_format_json import main
78
from pre_commit_hooks.pretty_format_json import parse_num_to_int
89
from testing.util import get_resource_path
@@ -105,3 +106,78 @@ def test_top_sorted_get_pretty_format():
105106
def test_badfile_main():
106107
ret = main([get_resource_path('ok_yaml.yaml')])
107108
assert ret == 1
109+
110+
111+
def test_diffing_output():
112+
table_tests = [
113+
{
114+
'name': 'diff_test_1',
115+
'source': """
116+
{
117+
"key1": "val1",
118+
"key2": 2,
119+
"array_key": [1, 2, 3],
120+
"object":{
121+
"bool_key": true
122+
}
123+
}
124+
""",
125+
'target': """
126+
{
127+
"array_key": [
128+
1,
129+
2,
130+
3
131+
],
132+
"key1": "val1",
133+
"key2": 2,
134+
"object": {
135+
"bool_key": true
136+
}
137+
}
138+
""",
139+
'expected': """
140+
{
141+
+ "array_key": [
142+
+ 1,
143+
+ 2,
144+
+ 3
145+
+ ],
146+
- "key1": "val1",
147+
? --
148+
149+
+ "key1": "val1",
150+
- "key2": 2,
151+
? -- --
152+
153+
+ "key2": 2,
154+
- "array_key": [1, 2, 3],
155+
- "object":{
156+
? ------
157+
158+
+ "object": {
159+
? +
160+
161+
- "bool_key": true
162+
? ----
163+
164+
+ "bool_key": true
165+
- }
166+
+ }
167+
}
168+
""",
169+
},
170+
{
171+
'name': 'diff_test_2',
172+
'source': '',
173+
'target': '',
174+
'expected': '',
175+
},
176+
177+
]
178+
for test in table_tests:
179+
s = list(test['source'])
180+
t = list(test['target'])
181+
expected = test['expected'].strip()
182+
actual = get_diff(s, t).strip()
183+
assert actual == expected

0 commit comments

Comments
 (0)