From 496f53d83c0ab9b6cd15bc48554109f004fc07fd Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Sat, 9 Nov 2024 01:06:53 +0100 Subject: [PATCH] Add more tests and minor fixes --- src/objdictgen/jsonod.py | 9 +++- src/objdictgen/printing.py | 3 +- tests/od/diff-a.json | 89 ++++++++++++++++++++++++++++++++++++++ tests/od/diff-b.json | 82 +++++++++++++++++++++++++++++++++++ tests/test_printing.py | 35 ++++++++++++++- 5 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 tests/od/diff-a.json create mode 100644 tests/od/diff-b.json diff --git a/src/objdictgen/jsonod.py b/src/objdictgen/jsonod.py index e511ed1..07b3dea 100644 --- a/src/objdictgen/jsonod.py +++ b/src/objdictgen/jsonod.py @@ -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)} diff --git a/src/objdictgen/printing.py b/src/objdictgen/printing.py index 7945511..10b6a26 100644 --- a/src/objdictgen/printing.py +++ b/src/objdictgen/printing.py @@ -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 diff --git a/tests/od/diff-a.json b/tests/od/diff-a.json new file mode 100644 index 0000000..d8b1a11 --- /dev/null +++ b/tests/od/diff-a.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/od/diff-b.json b/tests/od/diff-b.json new file mode 100644 index 0000000..7f86c8a --- /dev/null +++ b/tests/od/diff-b.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/test_printing.py b/tests/test_printing.py index 549c893..3ff63a4 100644 --- a/tests/test_printing.py +++ b/tests/test_printing.py @@ -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 @@ -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)