From 88c16af3a8e2feb6d4b2992df5c060bae4c194d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freitas?= Date: Fri, 23 Aug 2024 23:04:51 +0100 Subject: [PATCH 1/4] Create structure for parser tests --- tests/configs/001.fcp | 5 +++++ tests/configs/001.json | 43 ++++++++++++++++++++++++++++++++++++++++++ tests/configs/test.fpi | 1 + tests/test_parser.py | 23 ++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 tests/configs/001.fcp create mode 100644 tests/configs/001.json create mode 100644 tests/configs/test.fpi create mode 100644 tests/test_parser.py diff --git a/tests/configs/001.fcp b/tests/configs/001.fcp new file mode 100644 index 00000000..d2e9a851 --- /dev/null +++ b/tests/configs/001.fcp @@ -0,0 +1,5 @@ +version: "3" + +struct foo { + bar @ 0: u8; +}; diff --git a/tests/configs/001.json b/tests/configs/001.json new file mode 100644 index 00000000..5112e14e --- /dev/null +++ b/tests/configs/001.json @@ -0,0 +1,43 @@ +{ + "structs": [ + { + "name": "foo", + "signals": [ + { + "name": "bar", + "comment": { + "value": "" + }, + "type": "u8", + "field_id": 0, + "meta": { + "line": 4, + "end_line": 4, + "column": 5, + "end_column": 17, + "start_pos": 31, + "end_pos": 43, + "filename": "001.fcp" + } + } + ], + "meta": { + "line": 3, + "end_line": 5, + "column": 1, + "end_column": 3, + "start_pos": 14, + "end_pos": 46, + "filename": "001.fcp" + }, + "comment": { + "value": "" + } + } + ], + "enums": [], + "devices": [], + "broadcasts": [], + "logs": [], + "version": "3.0" +} diff --git a/tests/configs/test.fpi b/tests/configs/test.fpi new file mode 100644 index 00000000..5db6fe96 --- /dev/null +++ b/tests/configs/test.fpi @@ -0,0 +1 @@ +version: "3" diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 00000000..c0bdda1b --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,23 @@ +import pytest +import os +import json + +from fcp.v2_parser import get_fcp + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + +@pytest.mark.parametrize("fcp,result", [("001.fcp", "001.json")]) +def test_parser(fcp, result): + config_dir = os.path.join(THIS_DIR, "configs") + print("config dir", config_dir) + fpi_config = os.path.join(config_dir, "test.fpi") + fcp_config = os.path.join(config_dir, fcp) + result = os.path.join(config_dir, result) + + fcp_v2, sources = get_fcp(fcp_config, fpi_config).unwrap() + + fcp_json = fcp_v2.unwrap().to_dict() + result = json.loads(open(result).read()) + + + assert fcp_json == result From 639c13298dffa1088540c7449e8bcbd9d931d353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freitas?= Date: Fri, 23 Aug 2024 23:09:57 +0100 Subject: [PATCH 2/4] Correctly set fcp version --- src/fcp/specs/v2.py | 2 +- src/fcp/v2_parser.py | 1 + tests/test_v2.py | 11 ++++++----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/fcp/specs/v2.py b/src/fcp/specs/v2.py index 33b5d57b..872b51ce 100644 --- a/src/fcp/specs/v2.py +++ b/src/fcp/specs/v2.py @@ -22,7 +22,7 @@ class FcpV2(Model): devices: fields.List(device.Device) broadcasts: fields.List(broadcast.Broadcast) logs: fields.List(log.Log) - version: fields.Str(default="1.0") + version: fields.Str() def add_device(self, device): self.devices.append(device) diff --git a/src/fcp/v2_parser.py b/src/fcp/v2_parser.py index 93813127..aaa5f810 100644 --- a/src/fcp/v2_parser.py +++ b/src/fcp/v2_parser.py @@ -578,6 +578,7 @@ def convert(module): structs=module["struct"].values(), enums=module["enum"].values(), logs=module["log"].values(), + version="3.0", ) ) diff --git a/tests/test_v2.py b/tests/test_v2.py index 5b33aadb..6d18ad76 100644 --- a/tests/test_v2.py +++ b/tests/test_v2.py @@ -4,7 +4,8 @@ @pytest.fixture def fcp_v2(): - return FcpV2(devices=[], structs=[], broadcasts=[], enums=[], logs=[]) + return FcpV2(devices=[], structs=[], broadcasts=[], enums=[], logs=[], version="3.0") + @pytest.fixture @@ -15,13 +16,13 @@ def fcp_v2_dict(): "devices": [], "broadcasts": [], "logs": [], - "version": "1.0", + "version": "3.0", } @pytest.fixture def fcp_v2_json(): - return '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "1.0"}' + return '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "3.0"}' def test_fcp_v2_init(fcp_v2): @@ -31,7 +32,7 @@ def test_fcp_v2_init(fcp_v2): def test_fcp_v2_to_json(fcp_v2): assert ( fcp_v2.to_json() - == '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "1.0"}' + == '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "3.0"}' ) @@ -42,7 +43,7 @@ def test_fcp_v2_to_dict(fcp_v2): "devices": [], "broadcasts": [], "logs": [], - "version": "1.0", + "version": "3.0", } From c3354ffce19fb572346d58b5161de87a426ecfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freitas?= Date: Fri, 23 Aug 2024 23:25:22 +0100 Subject: [PATCH 3/4] Basic enum test --- tests/configs/002.fcp | 6 +++++ tests/configs/002.json | 52 ++++++++++++++++++++++++++++++++++++++++++ tests/test_parser.py | 12 +++++----- tests/test_v2.py | 5 ++-- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 tests/configs/002.fcp create mode 100644 tests/configs/002.json diff --git a/tests/configs/002.fcp b/tests/configs/002.fcp new file mode 100644 index 00000000..7ec8be77 --- /dev/null +++ b/tests/configs/002.fcp @@ -0,0 +1,6 @@ +version: "3" + +enum foo { + bar = 0; + baz = 1; +}; diff --git a/tests/configs/002.json b/tests/configs/002.json new file mode 100644 index 00000000..836890cd --- /dev/null +++ b/tests/configs/002.json @@ -0,0 +1,52 @@ +{ + "structs": [], + "enums": [ + { + "name": "foo", + "enumeration": [ + { + "name": "bar", + "value": 0, + "meta": { + "line": 4, + "end_line": 4, + "column": 5, + "end_column": 13, + "start_pos": 29, + "end_pos": 37, + "filename": "002.fcp" + } + }, + { + "name": "baz", + "value": 1, + "meta": { + "line": 5, + "end_line": 5, + "column": 5, + "end_column": 13, + "start_pos": 42, + "end_pos": 50, + "filename": "002.fcp" + } + } + ], + "meta": { + "line": 3, + "end_line": 6, + "column": 1, + "end_column": 3, + "start_pos": 14, + "end_pos": 53, + "filename": "002.fcp" + }, + "comment": { + "value": "" + } + } + ], + "devices": [], + "broadcasts": [], + "logs": [], + "version": "3.0" +} diff --git a/tests/test_parser.py b/tests/test_parser.py index c0bdda1b..decd0150 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -6,18 +6,18 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -@pytest.mark.parametrize("fcp,result", [("001.fcp", "001.json")]) -def test_parser(fcp, result): + +@pytest.mark.parametrize("test_name", ["001", "002"]) +def test_parser(test_name): config_dir = os.path.join(THIS_DIR, "configs") print("config dir", config_dir) fpi_config = os.path.join(config_dir, "test.fpi") - fcp_config = os.path.join(config_dir, fcp) - result = os.path.join(config_dir, result) + fcp_config = os.path.join(config_dir, test_name + ".fcp") + result = os.path.join(config_dir, test_name + ".json") fcp_v2, sources = get_fcp(fcp_config, fpi_config).unwrap() - + fcp_json = fcp_v2.unwrap().to_dict() result = json.loads(open(result).read()) - assert fcp_json == result diff --git a/tests/test_v2.py b/tests/test_v2.py index 6d18ad76..88769280 100644 --- a/tests/test_v2.py +++ b/tests/test_v2.py @@ -4,8 +4,9 @@ @pytest.fixture def fcp_v2(): - return FcpV2(devices=[], structs=[], broadcasts=[], enums=[], logs=[], version="3.0") - + return FcpV2( + devices=[], structs=[], broadcasts=[], enums=[], logs=[], version="3.0" + ) @pytest.fixture From 47b22507f5c0885ca4df7ee3a35999169dfe59ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freitas?= Date: Fri, 23 Aug 2024 23:35:40 +0100 Subject: [PATCH 4/4] Remove usage of dict merge operator for py3.7 compat --- src/fcp/v2_parser.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/fcp/v2_parser.py b/src/fcp/v2_parser.py index aaa5f810..be947f3b 100644 --- a/src/fcp/v2_parser.py +++ b/src/fcp/v2_parser.py @@ -146,7 +146,7 @@ def convert_params(params): values = {} for name, value in params.items(): - values = values | convertion_table[name](value) + values.update(convertion_table[name](value)) return values @@ -566,7 +566,7 @@ def deduplicate(module): def merge(fcp, fpi): fcp = {key: fcp[key] for key in fcp.keys() & {"struct", "enum"}} fpi = {key: fpi[key] for key in fpi.keys() & {"device", "broadcast", "log"}} - fcp = fcp | fpi + fcp.update(fpi) return Ok(fcp) @@ -587,7 +587,7 @@ def convert(module): def get_sources(module): sources = {module.filename: module.source} for mod in module.imports: - sources = sources | get_sources(mod) + sources.update(get_sources(mod)) return sources @@ -625,4 +625,5 @@ def get_fcp(fcp, fpi): fpi_sources = get_sources(fpi) fpi = deduplicate(resolve_imports(fpi).Q()).Q() - return Ok((convert(merge(fcp, fpi).Q()), fcp_sources | fpi_sources)) + fcp_sources.update(fpi_sources) + return Ok((convert(merge(fcp, fpi).Q()), fcp_sources))