From 3734067833644a2f6ae9ef5771ad0de5f1d49b0b Mon Sep 17 00:00:00 2001 From: Thomas Debrunner Date: Thu, 14 Mar 2024 10:18:18 +0100 Subject: [PATCH] MessageSet: add more tests --- .github/workflows/test.yml | 2 +- gcovr.cfg | 1 + include/mav/MessageSet.h | 10 +++---- tests/MessageSet.cpp | 58 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 8 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 ff34ebf..b1a9612 100644 --- a/include/mav/MessageSet.h +++ b/include/mav/MessageSet.h @@ -92,11 +92,11 @@ 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); } } @@ -130,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()); } @@ -167,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: @@ -197,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 6a1874b..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(""); @@ -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); + } }