Skip to content

Commit

Permalink
Merge pull request LaboratoryOfPlasmaPhysics#11 from Dekken/contains
Browse files Browse the repository at this point in the history
refactor dict::contains to not throw but only return bool
  • Loading branch information
PhilipDeegan authored Jul 9, 2020
2 parents b5cc203 + c7b7283 commit 5b4be6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ endif ()
if (BUILD_DEMO)
add_subdirectory(examples)
endif ()


message("cppdict CMAKE OPTION LIST ")
message("Build doc : " ${BUILD_DOC})
message("Build demo : " ${BUILD_DEMO})
message("Build bench : " ${BUILD_BENCH})
message("Build test : " ${BUILD_TEST})
9 changes: 1 addition & 8 deletions include/dict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,7 @@ struct Dict

bool contains(std::string key)
{
if (std::holds_alternative<node_t>(data))
return std::get<node_t>(data).count(key) > 0;

#ifndef NDEBUG
std::cout << __FILE__ << " " << __LINE__ << " " << currentKey << std::endl;
#endif

throw std::runtime_error("cppdict: contains() has no a map");
return isNode() and std::get<node_t>(data).count(key);
}

std::size_t size() const noexcept
Expand Down
12 changes: 12 additions & 0 deletions test/basic_dict_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ TEST_CASE("Deals with both references and values", "[simple cppdict::Dict<int, d
}

}


TEST_CASE("Contains expects true or false", "[simple cppdict::Dict<int, double, std::string>]")
{
Dict dict;
dict["this"]["is"]["the"]["source"] = 3.14;

REQUIRE(dict["this"].contains("is"));
REQUIRE(!dict.contains("is"));
REQUIRE(dict["this"]["is"]["the"].contains("source"));
REQUIRE(!dict["this"]["is"]["the"]["source"].contains("nothing"));
}

0 comments on commit 5b4be6c

Please sign in to comment.