From 9fd493b3d52a15a3f249069e2f33c97cfa692326 Mon Sep 17 00:00:00 2001 From: Thomas Debrunner Date: Tue, 12 Mar 2024 15:52:04 +0100 Subject: [PATCH] MessageSet: Added more tests and fixed comments from new enum encoding --- .github/workflows/test.yml | 2 +- gcovr.cfg | 1 + include/mav/MessageSet.h | 17 +++++------ tests/MessageSet.cpp | 62 +++++++++++++++++++++++++++++++++++--- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 15323eb..64e1560 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,7 +35,7 @@ jobs: run: ./build/tests/tests - name: Generate coverage report - run: gcovr --sonarqube -o coverage.xml build + run: gcovr --exclude-throw-branches --exclude-unreachable-branches --sonarqube -o coverage.xml build - name: Run sonar-scanner env: diff --git a/gcovr.cfg b/gcovr.cfg index 4e242a4..3920172 100644 --- a/gcovr.cfg +++ b/gcovr.cfg @@ -1,4 +1,5 @@ exclude-throw-branches = yes +exclude-unreachable-branches = yes filter = include/mav exclude = include/mav/rapidxml/* exclude = include/mav/picosha2/* \ No newline at end of file diff --git a/include/mav/MessageSet.h b/include/mav/MessageSet.h index 99499c7..b1a9612 100644 --- a/include/mav/MessageSet.h +++ b/include/mav/MessageSet.h @@ -37,7 +37,6 @@ #include #include #include -#include #include #include "MessageDefinition.h" @@ -93,23 +92,23 @@ namespace mav { try { uint64_t res = std::stoul(str, &pos, base); if (pos != str.size()) { - throw ParseError("Could not parse " + str + " as a number"); + throw ParseError(StringFormat() << "Could not parse " << str << " as a number" << StringFormat::end); } return res; } catch (std::exception &e) { - throw ParseError("Could not parse " + str + " as a number (stoul failed): " + e.what()); + throw ParseError(StringFormat() << "Could not parse " << str << " as a number (stoul failed): " << e.what() << StringFormat::end); } } static uint64_t _parseEnumValue(const std::string &str) { // Check for binary format: 0b or 0B if (str.size() >= 2 && (str.substr(0, 2) == "0b" || str.substr(0, 2) == "0B")) { - return std::bitset<64>(str.substr(2)).to_ullong(); + return _strict_stoul(str.substr(2), 2); } // Check for hexadecimal format: 0x or 0X if (str.size() >= 2 && (str.substr(0, 2) == "0x" || str.substr(0, 2) == "0X")) { - return _strict_stoul(str, 16); + return _strict_stoul(str.substr(2), 16); } // Check for exponential format: 2** @@ -131,7 +130,7 @@ namespace mav { } - static bool _isPrefix(std::string_view prefix, std::string_view full) { + static bool _isPrefix(std::string_view prefix, std::string_view full) noexcept { return prefix == full.substr(0, prefix.size()); } @@ -168,7 +167,7 @@ namespace mav { } else if (_isPrefix("double", field_type_string)) { return {FieldType::BaseType::DOUBLE, size}; } - throw ParseError("Unknown field type: " + field_type_string); + throw ParseError(StringFormat() << "Unknown field type: " << field_type_string << StringFormat::end); } public: @@ -189,7 +188,7 @@ namespace mav { auto doc = std::make_shared>(); try { doc->parse<0>(file->data()); - } catch (rapidxml::parse_error &e) { + } catch (const rapidxml::parse_error &e) { throw ParseError(e.what()); } return {file, doc, ""}; @@ -198,7 +197,7 @@ namespace mav { void parse(std::map &out_enum, std::map> &out_messages, - std::map> &out_message_ids) { + std::map> &out_message_ids) const { auto root_node = _document->first_node("mavlink"); if (!root_node) { diff --git a/tests/MessageSet.cpp b/tests/MessageSet.cpp index 49ec056..9690d3c 100644 --- a/tests/MessageSet.cpp +++ b/tests/MessageSet.cpp @@ -35,6 +35,19 @@ TEST_CASE("Message set creation") { CHECK_THROWS_AS(message_set.addFromXMLString(""), mav::ParseError); } + SUBCASE("Can not add message with unknown field type") { + CHECK_THROWS_AS(message_set.addFromXMLString(R""""( + + + + Field + + + +)""""), mav::ParseError); + + } + SUBCASE("Can add valid, partial XML") { // This is a valid XML file, but it does not contain any messages or enums message_set.addFromXMLString(""); @@ -158,10 +171,10 @@ TEST_CASE("Enum value encoding") { - + - + @@ -194,7 +207,7 @@ TEST_CASE("Enum value encoding") { CHECK_THROWS_AS(message_set.addFromXMLString(empty_value), mav::ParseError); } - SUBCASE("Enum with invalid value") { + SUBCASE("Enum with invalid value 1") { std::string invalid_value = R""""( @@ -209,6 +222,35 @@ TEST_CASE("Enum value encoding") { CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError); } + SUBCASE("Enum with invalid value 2") { + std::string invalid_value = R""""( + + + + + + + +)""""; + + MessageSet message_set; + CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError); + } + + SUBCASE("Enum with invalid value 3") { + std::string invalid_value = R""""( + + + + + + + +)""""; + + MessageSet message_set; + CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError); + } SUBCASE("Enum with overflow value") { std::string invalid_value = R""""( @@ -225,8 +267,20 @@ TEST_CASE("Enum value encoding") { CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError); } + SUBCASE("Enum with non-base-2 exponential value") { + std::string invalid_value = R""""( + + + + + + + +)""""; - + MessageSet message_set; + CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError); + } }