Skip to content

Commit 496f53d

Browse files
committed
Add more tests and minor fixes
1 parent 4d4fc6f commit 496f53d

File tree

5 files changed

+214
-4
lines changed

5 files changed

+214
-4
lines changed

src/objdictgen/jsonod.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1453,4 +1453,11 @@ def diff(node1: Node, node2: Node, internal=False) -> TDiffNodes:
14531453
entries = diffs.setdefault(strip_brackets(root), [])
14541454
entries.append((chtype, change, strip_brackets(path)))
14551455

1456-
return diffs
1456+
# Ensure the Index entries are sorted correctly
1457+
def _sort(text):
1458+
if text.startswith("Index "):
1459+
return f"zz 0x{int(text[6:]):04x}"
1460+
return text
1461+
1462+
# Sort the entries
1463+
return {k: diffs[k] for k in sorted(diffs, key=_sort)}

src/objdictgen/printing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,9 @@ def _pprint(text: str, prefix: str = ' '):
332332
for line in pformat(text, width=TERM_COLS).splitlines():
333333
yield prefix + line
334334

335-
for index in sorted(diffs):
335+
for index, entries in diffs.items():
336336
if data or raw or internal:
337337
yield f"{Fore.LIGHTYELLOW_EX}{index}{rst}"
338-
entries = diffs[index]
339338
for chtype, change, path in entries:
340339

341340
# Prepare the path for printing

tests/od/diff-a.json

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"$id": "od data",
3+
"$version": "1",
4+
"$description": "Canfestival object dictionary data",
5+
"$tool": "odg 3.2",
6+
"$date": "2022-08-08T21:01:45.713961",
7+
"name": "Diff test A",
8+
"description": "",
9+
"type": "master",
10+
"id": 0,
11+
"profile": "None",
12+
"dictionary": [
13+
{
14+
"index": "0x2000", // 4096
15+
"name": "Device Type",
16+
"struct": "var",
17+
"group": "user",
18+
"mandatory": true,
19+
"sub": [
20+
{
21+
"name": "Device Type",
22+
"type": "UNSIGNED32", // 7
23+
"access": "ro",
24+
"pdo": false,
25+
"value": 0
26+
}
27+
]
28+
},
29+
{
30+
"index": "0x2018", // 4120
31+
"name": "Identity",
32+
"struct": "record",
33+
"group": "user",
34+
"mandatory": true,
35+
"sub": [
36+
{
37+
"name": "Number of Entries",
38+
"type": "UNSIGNED8", // 5
39+
"access": "ro",
40+
"pdo": false
41+
},
42+
{
43+
"name": "Vendor ID",
44+
"type": "UNSIGNED32", // 7
45+
"access": "ro",
46+
"pdo": false,
47+
"value": 0
48+
},
49+
{
50+
"name": "Product Code",
51+
"type": "UNSIGNED32", // 7
52+
"access": "ro",
53+
"pdo": false,
54+
"value": 0
55+
},
56+
{
57+
"name": "Revision Number",
58+
"type": "UNSIGNED32", // 7
59+
"access": "ro",
60+
"pdo": false,
61+
"value": 0
62+
},
63+
{
64+
"name": "Serial Number",
65+
"type": "UNSIGNED32", // 7
66+
"access": "ro",
67+
"pdo": false,
68+
"value": 0
69+
}
70+
]
71+
},
72+
{
73+
"index": "0x3000", // 4096
74+
"name": "Device Type",
75+
"struct": "var",
76+
"group": "user",
77+
"mandatory": true,
78+
"sub": [
79+
{
80+
"name": "Device Type",
81+
"type": "UNSIGNED32", // 7
82+
"access": "ro",
83+
"pdo": false,
84+
"value": 0
85+
}
86+
]
87+
}
88+
]
89+
}

tests/od/diff-b.json

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"$id": "od data",
3+
"$version": "1",
4+
"$description": "Canfestival object dictionary data",
5+
"$tool": "odg 3.2",
6+
"$date": "2022-08-08T21:01:45.713961",
7+
"name": "Diff test B",
8+
"description": "",
9+
"type": "master",
10+
"id": 0,
11+
"profile": "None",
12+
"dictionary": [
13+
{
14+
"index": "0x2000", // 4096
15+
"name": "Device Type",
16+
"struct": "var",
17+
"group": "user",
18+
"mandatory": true,
19+
"sub": [
20+
{
21+
"name": "Device Type",
22+
"type": "UNSIGNED32", // 7
23+
"access": "ro",
24+
"pdo": false,
25+
"value": 42
26+
}
27+
]
28+
},
29+
{
30+
"index": "0x2018", // 4120
31+
"name": "Identity",
32+
"struct": "record",
33+
"group": "user",
34+
"mandatory": true,
35+
"sub": [
36+
{
37+
"name": "Number of Entries",
38+
"type": "UNSIGNED8", // 5
39+
"access": "ro",
40+
"pdo": false
41+
},
42+
{
43+
"name": "Vendor ID",
44+
"type": "UNSIGNED32", // 7
45+
"access": "ro",
46+
"pdo": false,
47+
"value": 0
48+
},
49+
{
50+
"name": "Product Code",
51+
"type": "UNSIGNED32", // 7
52+
"access": "ro",
53+
"pdo": false,
54+
"value": 0
55+
},
56+
{
57+
"name": "Revision Number",
58+
"type": "UNSIGNED32", // 7
59+
"access": "ro",
60+
"pdo": false,
61+
"value": 0
62+
}
63+
]
64+
},
65+
{
66+
"index": "0x3001", // 4096
67+
"name": "Device Type",
68+
"struct": "var",
69+
"group": "user",
70+
"mandatory": true,
71+
"sub": [
72+
{
73+
"name": "Device Type",
74+
"type": "UNSIGNED32", // 7
75+
"access": "ro",
76+
"pdo": false,
77+
"value": 0
78+
}
79+
]
80+
}
81+
]
82+
}

tests/test_printing.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import pytest
44

55
from objdictgen import __main__
6-
from objdictgen.printing import FormatNodeOpts, format_node, format_od_header, format_od_object
6+
from objdictgen.printing import (FormatNodeOpts, format_node, format_od_header,
7+
format_od_object, format_diff_nodes)
78
from objdictgen.node import Node
89

910

@@ -69,3 +70,35 @@ def test_printing_format_od_object(odpath, file):
6970
assert isinstance(lines, types.GeneratorType)
7071
for line in lines:
7172
assert isinstance(line, str)
73+
74+
75+
@pytest.mark.parametrize("filepair", [
76+
("slave-emcy.json", "slave-heartbeat.json"),
77+
("diff-a.json", "diff-b.json"),
78+
])
79+
def test_printing_diff_nodes(odpath, filepair):
80+
81+
print(filepair)
82+
83+
m1 = Node.LoadFile(odpath / filepair[0])
84+
m2 = Node.LoadFile(odpath / filepair[1])
85+
86+
lines = format_diff_nodes(m1, m2)
87+
assert isinstance(lines, types.GeneratorType)
88+
for line in lines:
89+
assert isinstance(line, str)
90+
91+
lines = format_diff_nodes(m1, m2, data=True, show=True)
92+
assert isinstance(lines, types.GeneratorType)
93+
for line in lines:
94+
assert isinstance(line, str)
95+
96+
lines = format_diff_nodes(m1, m2, raw=True)
97+
assert isinstance(lines, types.GeneratorType)
98+
for line in lines:
99+
assert isinstance(line, str)
100+
101+
lines = format_diff_nodes(m1, m2, internal=True)
102+
assert isinstance(lines, types.GeneratorType)
103+
for line in lines:
104+
assert isinstance(line, str)

0 commit comments

Comments
 (0)