Skip to content

Commit

Permalink
Add more tests and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinse committed Nov 9, 2024
1 parent 4d4fc6f commit 496f53d
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/objdictgen/jsonod.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,4 +1453,11 @@ def diff(node1: Node, node2: Node, internal=False) -> TDiffNodes:
entries = diffs.setdefault(strip_brackets(root), [])
entries.append((chtype, change, strip_brackets(path)))

return diffs
# Ensure the Index entries are sorted correctly
def _sort(text):
if text.startswith("Index "):
return f"zz 0x{int(text[6:]):04x}"
return text

# Sort the entries
return {k: diffs[k] for k in sorted(diffs, key=_sort)}
3 changes: 1 addition & 2 deletions src/objdictgen/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,9 @@ def _pprint(text: str, prefix: str = ' '):
for line in pformat(text, width=TERM_COLS).splitlines():
yield prefix + line

for index in sorted(diffs):
for index, entries in diffs.items():
if data or raw or internal:
yield f"{Fore.LIGHTYELLOW_EX}{index}{rst}"
entries = diffs[index]
for chtype, change, path in entries:

# Prepare the path for printing
Expand Down
89 changes: 89 additions & 0 deletions tests/od/diff-a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"$id": "od data",
"$version": "1",
"$description": "Canfestival object dictionary data",
"$tool": "odg 3.2",
"$date": "2022-08-08T21:01:45.713961",
"name": "Diff test A",
"description": "",
"type": "master",
"id": 0,
"profile": "None",
"dictionary": [
{
"index": "0x2000", // 4096
"name": "Device Type",
"struct": "var",
"group": "user",
"mandatory": true,
"sub": [
{
"name": "Device Type",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
}
]
},
{
"index": "0x2018", // 4120
"name": "Identity",
"struct": "record",
"group": "user",
"mandatory": true,
"sub": [
{
"name": "Number of Entries",
"type": "UNSIGNED8", // 5
"access": "ro",
"pdo": false
},
{
"name": "Vendor ID",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
},
{
"name": "Product Code",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
},
{
"name": "Revision Number",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
},
{
"name": "Serial Number",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
}
]
},
{
"index": "0x3000", // 4096
"name": "Device Type",
"struct": "var",
"group": "user",
"mandatory": true,
"sub": [
{
"name": "Device Type",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
}
]
}
]
}
82 changes: 82 additions & 0 deletions tests/od/diff-b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"$id": "od data",
"$version": "1",
"$description": "Canfestival object dictionary data",
"$tool": "odg 3.2",
"$date": "2022-08-08T21:01:45.713961",
"name": "Diff test B",
"description": "",
"type": "master",
"id": 0,
"profile": "None",
"dictionary": [
{
"index": "0x2000", // 4096
"name": "Device Type",
"struct": "var",
"group": "user",
"mandatory": true,
"sub": [
{
"name": "Device Type",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 42
}
]
},
{
"index": "0x2018", // 4120
"name": "Identity",
"struct": "record",
"group": "user",
"mandatory": true,
"sub": [
{
"name": "Number of Entries",
"type": "UNSIGNED8", // 5
"access": "ro",
"pdo": false
},
{
"name": "Vendor ID",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
},
{
"name": "Product Code",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
},
{
"name": "Revision Number",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
}
]
},
{
"index": "0x3001", // 4096
"name": "Device Type",
"struct": "var",
"group": "user",
"mandatory": true,
"sub": [
{
"name": "Device Type",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"value": 0
}
]
}
]
}
35 changes: 34 additions & 1 deletion tests/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pytest

from objdictgen import __main__
from objdictgen.printing import FormatNodeOpts, format_node, format_od_header, format_od_object
from objdictgen.printing import (FormatNodeOpts, format_node, format_od_header,
format_od_object, format_diff_nodes)
from objdictgen.node import Node


Expand Down Expand Up @@ -69,3 +70,35 @@ def test_printing_format_od_object(odpath, file):
assert isinstance(lines, types.GeneratorType)
for line in lines:
assert isinstance(line, str)


@pytest.mark.parametrize("filepair", [
("slave-emcy.json", "slave-heartbeat.json"),
("diff-a.json", "diff-b.json"),
])
def test_printing_diff_nodes(odpath, filepair):

print(filepair)

m1 = Node.LoadFile(odpath / filepair[0])
m2 = Node.LoadFile(odpath / filepair[1])

lines = format_diff_nodes(m1, m2)
assert isinstance(lines, types.GeneratorType)
for line in lines:
assert isinstance(line, str)

lines = format_diff_nodes(m1, m2, data=True, show=True)
assert isinstance(lines, types.GeneratorType)
for line in lines:
assert isinstance(line, str)

lines = format_diff_nodes(m1, m2, raw=True)
assert isinstance(lines, types.GeneratorType)
for line in lines:
assert isinstance(line, str)

lines = format_diff_nodes(m1, m2, internal=True)
assert isinstance(lines, types.GeneratorType)
for line in lines:
assert isinstance(line, str)

0 comments on commit 496f53d

Please sign in to comment.