Skip to content

Commit 4d4fc6f

Browse files
committed
Fixup bugs and add testing
1 parent 410bb90 commit 4d4fc6f

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

src/objdictgen/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
283283
lines = list(format_diff_nodes(od1, od2, data=opts.data, raw=opts.raw,
284284
internal=opts.internal, show=opts.show))
285285

286+
for line in lines:
287+
print(line)
288+
286289
errcode = 1 if lines else 0
287290
if errcode:
288291
print(f"{objdictgen.ODG_PROGRAM}: '{opts.od1}' and '{opts.od2}' differ")

src/objdictgen/jsonod.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ def diff(node1: Node, node2: Node, internal=False) -> TDiffNodes:
14091409

14101410
# Simply diff the python data structure for the nodes
14111411
diff = deepdiff.DeepDiff(node1.__dict__, node2.__dict__, exclude_paths=[
1412-
"root.IndexOrder"
1412+
"IndexOrder"
14131413
], view='tree')
14141414

14151415
else:

src/objdictgen/printing.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ def _pprint(text: str, prefix: str = ' '):
333333
yield prefix + line
334334

335335
for index in sorted(diffs):
336-
yield f"{Fore.LIGHTYELLOW_EX}{index}{rst}"
336+
if data or raw or internal:
337+
yield f"{Fore.LIGHTYELLOW_EX}{index}{rst}"
337338
entries = diffs[index]
338339
for chtype, change, path in entries:
339340

@@ -349,20 +350,24 @@ def _pprint(text: str, prefix: str = ' '):
349350
yield f"<< {ppath}only in {Fore.MAGENTA}LEFT{rst}"
350351
if show:
351352
yield from _pprint(change.t1, " < ")
353+
352354
elif 'added' in chtype:
353355
yield f" >> {ppath}only in {Fore.BLUE}RIGHT{rst}"
354356
if show:
355357
yield from _pprint(change.t2, " > ")
358+
356359
elif 'changed' in chtype:
357360
yield f"<< - >> {ppath}changed value from '{Fore.GREEN}{change.t1}{rst}' to '{Fore.GREEN}{change.t2}{rst}'"
358361
if show:
359362
yield from _pprint(change.t1, " < ")
360363
yield from _pprint(change.t2, " > ")
364+
361365
elif 'type_changes' in chtype:
362366
yield f"<< - >> {ppath}changed type and value from '{Fore.GREEN}{change.t1}{rst}' to '{Fore.GREEN}{change.t2}{rst}'"
363367
if show:
364368
yield from _pprint(change.t1, " < ")
365369
yield from _pprint(change.t2, " > ")
370+
366371
elif 'diff' in chtype:
367372
start = path[0:2]
368373
if start == ' ':
@@ -402,10 +407,10 @@ def text_diff(od1: Node, od2: Node, data_mode: bool=False) -> TDiffNodes:
402407
entry1: TIndexEntry = {}
403408
entry2: TIndexEntry = {}
404409
if index in keys1:
405-
text1 = list(format_od_object(od1, index))
410+
text1 = list(format_od_object(od1, index, unused=True))
406411
entry1 = od1.GetIndexEntry(index)
407412
if index in keys2:
408-
text2 = list(format_od_object(od2, index))
413+
text2 = list(format_od_object(od2, index, unused=True))
409414
entry2 = od2.GetIndexEntry(index)
410415

411416
if data_mode:

tests/test_jsonod.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
""" Test the jsonod module. """
12
import re
23
from pprint import pprint
34
import datetime
45
from freezegun import freeze_time
6+
import pytest
57
from objdictgen import Node
6-
from objdictgen.jsonod import generate_jsonc, generate_node, remove_jsonc
8+
from objdictgen.jsonod import generate_jsonc, generate_node, remove_jsonc, diff
79
from .test_odcompare import shave_equal
810

911

@@ -160,3 +162,21 @@ def test_jsonod_comments(odpath):
160162
if '"$date"' in a or '"$tool"' in a:
161163
continue
162164
assert a == b
165+
166+
167+
@pytest.mark.parametrize("filepair", [
168+
("slave-emcy.json", "slave-heartbeat.json"),
169+
])
170+
def test_jsonod_diff(odpath, filepair):
171+
""" Test the diff function in the jsonod module. """
172+
173+
m1 = Node.LoadFile(odpath / filepair[0])
174+
m2 = Node.LoadFile(odpath / filepair[1])
175+
176+
diffs = diff(m1, m2)
177+
178+
assert set(diffs.keys()) == {"Index 4116", "Index 4119", "Header fields"}
179+
180+
diffs = diff(m1, m2, internal=True)
181+
182+
assert set(diffs.keys()) == {"Dictionary", "Description"}

tests/test_utils.py

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"""Test utils module."""
22

3-
import colorama
3+
from colorama import Fore, Style
44
from objdictgen import utils
55

66

77
def test_utils_remove_color():
88
"""Test remove_color function."""
99
assert utils.remove_color("Hello, World!") == "Hello, World!"
1010

11-
assert utils.remove_color(colorama.Fore.RED + "Hello, World!") == "Hello, World!"
11+
assert utils.remove_color(Fore.RED + "Hello, World!") == "Hello, World!"
1212

13-
assert utils.remove_color(colorama.Fore.RED + "Hello, World!" + colorama.Style.RESET_ALL) == "Hello, World!"
13+
assert utils.remove_color(Fore.RED + "Hello, World!" + Style.RESET_ALL) == "Hello, World!"
1414

1515

1616
def test_utils_strip_brackets():
@@ -77,3 +77,40 @@ def test_utils_copy_in_order():
7777
assert utils.copy_in_order(d, ["b", "d"]) == {"b": 2, "a": 1, "c": 3}
7878

7979
assert utils.copy_in_order(d, []) == d
80+
81+
82+
def test_utils_diff_colored_lines():
83+
"""Test diff_colored_lines function."""
84+
85+
lines1 = ["Hello", "World"]
86+
lines2 = ["Hello", "World!"]
87+
88+
out = list(utils.diff_colored_lines(lines1, lines2))
89+
90+
assert out == [" Hello", "- World", "+ World!" ]
91+
92+
93+
lines1 = [f"{Fore.RED}Hello", f"{Fore.GREEN}World"]
94+
lines2 = ["Hello", "World!"]
95+
96+
out = list(utils.diff_colored_lines(lines1, lines2))
97+
98+
assert out == [f" {Fore.RED}Hello", f"- {Fore.GREEN}World", "+ World!" ]
99+
100+
101+
lines1 = ["Hello", "World!"]
102+
lines2 = [f"{Fore.RED}Hello", f"{Fore.GREEN}World"]
103+
104+
out = list(utils.diff_colored_lines(lines1, lines2))
105+
106+
assert out == [f" {Fore.RED}Hello", "- World!", f"+ {Fore.GREEN}World" ]
107+
108+
109+
def test_utils_diff_highlight_changes():
110+
"""Test highlight_changes function."""
111+
112+
lines = [" Hello", "- World", "+ Friend"]
113+
114+
out = list(utils.highlight_changes(lines))
115+
116+
assert out == [" Hello", "- World", "? ^^ ^", "+ Friend", "? ^ ^^^"]

0 commit comments

Comments
 (0)