From ce0c3c3d17a2d1230c4ec073dc346d0c893eff78 Mon Sep 17 00:00:00 2001 From: Carl Georg Biermann Date: Thu, 7 Oct 2021 14:19:56 +0200 Subject: [PATCH] Bonded interactions refactoring: add unittest for call_method() --- src/script_interface/tests/ObjectMap_test.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/script_interface/tests/ObjectMap_test.cpp b/src/script_interface/tests/ObjectMap_test.cpp index d4d88923bd6..77ec565211b 100644 --- a/src/script_interface/tests/ObjectMap_test.cpp +++ b/src/script_interface/tests/ObjectMap_test.cpp @@ -25,6 +25,7 @@ #include "script_interface/LocalContext.hpp" #include "script_interface/ObjectMap.hpp" +#include "script_interface/get_value.hpp" #include "core/communication.hpp" @@ -125,6 +126,50 @@ BOOST_AUTO_TEST_CASE(serialization) { BOOST_CHECK(key == 3); } +BOOST_AUTO_TEST_CASE(calling_methods) { + // Check the different call_method functions + auto e = std::make_shared(); + auto f = std::make_shared(); + VariantMap params{}; + ObjectMapImpl map; + BOOST_CHECK(boost::get(map.call_method("empty", params))); + + // insert an element with key + int first_key = 3; + params["key"] = make_variant(first_key); + params["object"] = make_variant(e); + map.call_method("insert", params); + // insert an element without key + params.clear(); + params["object"] = make_variant(f); + auto const second_key = boost::get(map.call_method("insert", params)); + + // Check the returned map + auto const map_ret = boost::get>( + map.call_method("get_map", params)); + BOOST_CHECK(e == boost::get(map_ret.at(first_key))); + BOOST_CHECK(f == boost::get(map_ret.at(second_key))); + BOOST_REQUIRE_EQUAL(map_ret.size(), 2); + + // Check contents of the internal map + BOOST_CHECK(map.elements().at(first_key) == e); + BOOST_CHECK(map.elements().at(second_key) == f); + params.clear(); + BOOST_CHECK(!boost::get(map.call_method("empty", params))); + BOOST_REQUIRE_EQUAL(boost::get(map.call_method("size", params)), 2); + + // Erase an element + params["key"] = second_key; + map.call_method("erase", params); + BOOST_CHECK_THROW(map.elements().at(second_key), std::out_of_range); + params.clear(); + BOOST_REQUIRE_EQUAL(boost::get(map.call_method("size", params)), 1); + + // Clear map + map.call_method("clear", params); + BOOST_CHECK(boost::get(map.call_method("empty", params))); +} + int main(int argc, char **argv) { auto mpi_env = std::make_shared(argc, argv); Communication::init(mpi_env);