Skip to content

Commit

Permalink
Add test with bad JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Aug 18, 2024
1 parent 0b21e74 commit fcf4a97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion g2o/core/io/io_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::optional<AbstractGraph> IoJson::load(std::istream& input) {
input >> json;
return json::fromJson(json);
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());
G2O_ERROR("Exception while loading: {}", e.what());
}
return std::nullopt;
}
Expand Down
18 changes: 18 additions & 0 deletions unit_test/general/graph_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include "gtest/gtest.h"
#include "unit_test/test_helper/allocate_optimizer.h"

#ifdef G2O_HAVE_JSON
#include "g2o/core/io/io_json.h"
#endif

using namespace testing; // NOLINT

MATCHER(ParamEqual, "") {
Expand Down Expand Up @@ -317,6 +321,20 @@ INSTANTIATE_TEST_SUITE_P(AbstractGraph, AbstractGraphIO, kFileformatsToTest);
INSTANTIATE_TEST_SUITE_P(OptimizableGraphGraph, OptimizableGraphIO,
kFileformatsToTest);

TEST(OptimizableGraphIO, IllegalJson) {
#ifdef G2O_HAVE_JSON
std::istringstream input(R"({"name": "Joe", "age": 42})");
g2o::IoJson json;
#ifdef NDEBUG
EXPECT_FALSE(json.load(input).has_value());
#else
EXPECT_DEBUG_DEATH(json.load(input), "file json.hpp");
#endif
#else
SUCCEED();
#endif
}

TEST(OptimizableGraphIO, FileFilter) {
EXPECT_THAT(g2o::io::getFileFilter(false), Not(IsEmpty()));
EXPECT_THAT(g2o::io::getFileFilter(true), Not(IsEmpty()));
Expand Down

0 comments on commit fcf4a97

Please sign in to comment.