From 8586ebdc45c9c9a9ad9002d8305ad216c4a97548 Mon Sep 17 00:00:00 2001 From: Patrick Crumley Date: Wed, 2 Oct 2024 20:05:30 -0700 Subject: [PATCH] get the automated generation of test cases working again --- HOWTO.md | 12 +++++++++--- generator/json2test.py | 13 ++++++++++++- generator/sbpg/test_structs.py | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/HOWTO.md b/HOWTO.md index f205c6f4a..69400b7bf 100644 --- a/HOWTO.md +++ b/HOWTO.md @@ -87,7 +87,9 @@ python missing.py --host [HOST] --port [PORT] ### New Messages The `json2test` script can be used to automatically generate tests for newly -defined messages. +defined messages. The json2test script uses [uv](https://docs.astral.sh/uv/) to +manage its dependencies, which can be installed following the instructions on that +website To use `json2test` a JSON file should be hand written with example contents of a message. For example, to generate tests for the `MSG_HEARTBEAT` message (which @@ -105,13 +107,17 @@ form: And then generate a test for using `json2test` with: ```shell -PYTHONPATH="python/" python generator/json2test.py --input heartbeat.json --output spec/tests/yaml/swiftnav/sbp/system/test_MsgHeartbeat.yaml +uv run json2test.py --input heartbeat.json --output spec/tests/yaml/swiftnav/sbp/system/test_MsgHeartbeat.yaml ``` +*NOTE* because the json2test file imports the local version of sbp & the build +system of it all is a little slapdash, this command will ONLY work if called from +inside of `libsbp/generator/` directory. + Usage for `json2test` ```shell -python json2test --input [PATH_TO_JSON_IN] --output [PATH_TO_YAML_OUT] +uv run json2test --input [PATH_TO_JSON_IN] --output [PATH_TO_YAML_OUT] ``` * The `msg_type` can also be provided through a CLI parameter, with `--msg-id diff --git a/generator/json2test.py b/generator/json2test.py index 83d01c61b..6b0d06126 100644 --- a/generator/json2test.py +++ b/generator/json2test.py @@ -1,4 +1,4 @@ -# Copyright (C) 2022 Swift Navigation Inc. +# Copyright (C) 2024 Swift Navigation Inc. # Contact: https://support.swiftnav.com # # This source is subject to the license found in the file 'LICENSE' which must @@ -7,6 +7,17 @@ # THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. +# +# /// script +# requires-python = ">=3.12" +# dependencies = [ +# "jinja2 ~= 3.1", +# "pytest ~= 8.3", +# "pyyaml ~= 6.0", +# "sbp @ file:///${PROJECT_ROOT}/../python", +# ] +# /// + """ Generate test case from json input """ diff --git a/generator/sbpg/test_structs.py b/generator/sbpg/test_structs.py index 4a280518d..dfc6463bc 100644 --- a/generator/sbpg/test_structs.py +++ b/generator/sbpg/test_structs.py @@ -99,7 +99,7 @@ def __init__(self, raw_packet, msg_type, raw_json, msg, sbp, test_msg_data=None) self.payload = base64.standard_b64decode(self.sbp['payload']) self.payload_as_byte_array = list(bytearray(self.payload)) self.payload_len_for_encoding_buf = len(self.payload) if len(self.payload) > 0 else 1 - msg_name = msg['name'] + msg_name = msg.__class__.__name__ for i in range(0, len(ACRONYMS)): msg_name = re.sub(ACRONYMS[i], LOWER_ACRONYMS[i], msg_name) self.enum_value = "Sbp" + msg_name