Skip to content

Commit

Permalink
get the automated generation of test cases working again
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrumley committed Oct 3, 2024
1 parent 4ca4b5c commit 8586ebd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
12 changes: 9 additions & 3 deletions HOWTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion generator/json2test.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
"""
Expand Down
2 changes: 1 addition & 1 deletion generator/sbpg/test_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8586ebd

Please sign in to comment.