|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: optional_utils unit tests |
| 4 | +
|
| 5 | + Author: Diffblue Ltd. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <testing-utils/catch.hpp> |
| 10 | + |
| 11 | +#include <util/optional_utils.h> |
| 12 | + |
| 13 | +#include <map> |
| 14 | +#include <string> |
| 15 | +#include <unordered_map> |
| 16 | + |
| 17 | +namespace |
| 18 | +{ |
| 19 | +template <typename map_like_collectiont> |
| 20 | +void do_optional_lookup_test(map_like_collectiont &map) |
| 21 | +{ |
| 22 | + map.insert({"hello", "world"}); |
| 23 | + map.insert({"pwd", "/home"}); |
| 24 | + auto const hello_result = optional_lookup(map, "hello"); |
| 25 | + REQUIRE(hello_result.has_value()); |
| 26 | + REQUIRE(hello_result.value() == "world"); |
| 27 | + auto const pwd_result = optional_lookup(map, "pwd"); |
| 28 | + REQUIRE(pwd_result.has_value()); |
| 29 | + REQUIRE(pwd_result.value() == "/home"); |
| 30 | + REQUIRE_FALSE(optional_lookup(map, "does not exit").has_value()); |
| 31 | +} |
| 32 | +} // namespace |
| 33 | + |
| 34 | +TEST_CASE("Using optional_lookup with std::map") |
| 35 | +{ |
| 36 | + auto map = std::map<std::string, std::string>{}; |
| 37 | + do_optional_lookup_test(map); |
| 38 | +} |
| 39 | + |
| 40 | +TEST_CASE("Using optional_lookup with std::unordered_map") |
| 41 | +{ |
| 42 | + auto map = std::unordered_map<std::string, std::string>{}; |
| 43 | + do_optional_lookup_test(map); |
| 44 | +} |
0 commit comments