From 76fe3370d6ffb3cde7dfdcf9491032ac9170794a 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 --- gcovr.cfg | 1 + include/mav/MessageSet.h | 10 ++++----- tests/MessageSet.cpp | 45 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 7 deletions(-) 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..48e89ad 100644 --- a/tests/MessageSet.cpp +++ b/tests/MessageSet.cpp @@ -194,7 +194,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 +209,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 +254,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); + } }