Skip to content

Commit

Permalink
MessageSet: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasDebrunner committed Mar 14, 2024
1 parent a6d7af9 commit f4fae05
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions gcovr.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exclude-throw-branches = yes
exclude-unreachable-branches = yes
filter = include/mav
exclude = include/mav/rapidxml/*
exclude = include/mav/picosha2/*
10 changes: 5 additions & 5 deletions include/mav/MessageSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -197,7 +197,7 @@ namespace mav {

void parse(std::map<std::string, uint64_t> &out_enum,
std::map<std::string, std::shared_ptr<const MessageDefinition>> &out_messages,
std::map<int, std::shared_ptr<const MessageDefinition>> &out_message_ids) {
std::map<int, std::shared_ptr<const MessageDefinition>> &out_message_ids) const {

auto root_node = _document->first_node("mavlink");
if (!root_node) {
Expand Down
58 changes: 56 additions & 2 deletions tests/MessageSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ TEST_CASE("Message set creation") {
CHECK_THROWS_AS(message_set.addFromXMLString("<mavlink>"), mav::ParseError);
}

SUBCASE("Can not add message with unknown field type") {
CHECK_THROWS_AS(message_set.addFromXMLString(R""""(
<mavlink>
<messages>
<message id="15321" name="ONLY_MESSAGE">
<field type="unknown" name="field">Field</field>
</message>
</messages>
</mavlink>
)""""), 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("<mavlink></mavlink>");
Expand Down Expand Up @@ -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""""(
<mavlink>
<enums>
Expand All @@ -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""""(
<mavlink>
<enums>
<enum name="MY_ENUM_INVALID_VALUE">
<entry value="thisiswrong" name="INVALID" />
</enum>
</enums>
</mavlink>
)"""";

MessageSet message_set;
CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError);
}

SUBCASE("Enum with invalid value 3") {
std::string invalid_value = R""""(
<mavlink>
<enums>
<enum name="MY_ENUM_INVALID_VALUE">
<entry value="128thereismorecontent" name="INVALID" />
</enum>
</enums>
</mavlink>
)"""";

MessageSet message_set;
CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError);
}

SUBCASE("Enum with overflow value") {
std::string invalid_value = R""""(
Expand All @@ -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""""(
<mavlink>
<enums>
<enum name="MY_ENUM_INVALID_VALUE">
<entry value="3**3" name="INVALID" />
</enum>
</enums>
</mavlink>
)"""";


MessageSet message_set;
CHECK_THROWS_AS(message_set.addFromXMLString(invalid_value), mav::ParseError);
}

}

0 comments on commit f4fae05

Please sign in to comment.