Skip to content

Commit

Permalink
fetch catch
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Feb 6, 2024
1 parent de74200 commit 892c6e2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 18,011 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build*
CMakeLists.txt.user
tpp/
56 changes: 33 additions & 23 deletions include/dict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,25 @@ struct Dict
}


bool isLeaf() const noexcept { return !isNode() && !isEmpty(); }
bool isLeaf() const noexcept
{
return !isNode() && !isEmpty();
}

bool isNode() const noexcept { return std::holds_alternative<node_t>(data); }
bool isNode() const noexcept
{
return std::holds_alternative<node_t>(data);
}

bool isEmpty() const noexcept { return std::holds_alternative<empty_leaf_t>(data); }
bool isEmpty() const noexcept
{
return std::holds_alternative<empty_leaf_t>(data);
}

bool isValue() const noexcept { return !isNode() and !isEmpty(); }
bool isValue() const noexcept
{
return !isNode() and !isEmpty();
}

template<typename T, typename U = std::enable_if_t<is_any_of<T, Types...>()>>
Dict& operator=(const T& value)
Expand Down Expand Up @@ -338,27 +350,27 @@ auto& get(std::vector<std::string> keys, size_t iKey, Dict<Types...>& currentNod
}




template<typename T, template<typename... Types> class Dict, typename... Types,
typename Check = std::enable_if_t<is_any_of<T, Types...>()>>
void add(std::string path, T&& value, Dict<Types...>& dict)
auto _split_string(std::string const& path, char delimiter = '/')
{
std::vector<std::string> keys;
std::string key;
std::istringstream tokenStream{path};
while (std::getline(tokenStream, key, '/'))
{
while (std::getline(tokenStream, key, delimiter))
keys.push_back(key);
}
return keys;
}


template<typename T, template<typename... Types> class Dict, typename... Types,
typename Check = std::enable_if_t<is_any_of<T, Types...>()>>
void add(std::string path, T&& value, Dict<Types...>& dict)
{
auto keys = _split_string(path);
auto&& node = get(keys, 0ul, dict);
node = std::forward<T>(value);
}




template<typename Paths, typename... Types>
std::optional<Dict<Types...>> _traverse_to_node(Dict<Types...> const& dict, Paths const& paths,
std::size_t idx = 0)
Expand All @@ -373,30 +385,29 @@ std::optional<Dict<Types...>> _traverse_to_node(Dict<Types...> const& dict, Path
return std::nullopt;
}


template<typename... Types>
std::optional<Dict<Types...>> traverse_to_node(Dict<Types...> const& dict, std::string const& path,
char delimiter = '/')
{
std::string tmp = "";
std::vector<std::string> paths;
std::istringstream iss(path);
while (std::getline(iss, tmp, delimiter))
paths.push_back(tmp);
auto paths = _split_string(path);
return _traverse_to_node(dict, paths);
}


template<typename T, typename... Types>
T const& get(Dict<Types...> const& dict, std::string const& path, char delimiter = '/')
T const& at(Dict<Types...> const& dict, std::string const& path, char delimiter = '/')
{
auto leaf = traverse_to_node(dict, path, delimiter);
if (leaf)
return leaf->template to<T>();
throw std::runtime_error("cppdict: contains no path " + path);
}


template<typename T, typename... Types>
T get(Dict<Types...> const& dict, std::string const& path, T const default_value,
char delimiter = '/')
T at(Dict<Types...> const& dict, std::string const& path, T const default_value,
char delimiter = '/')
{
auto leaf = traverse_to_node(dict, path, delimiter);
if (leaf)
Expand All @@ -405,6 +416,5 @@ T get(Dict<Types...> const& dict, std::string const& path, T const default_value
}



} // namespace cppdict
#endif
12 changes: 12 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@

include(FetchContent)
set(FETCHCONTENT_BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tpp/catch2)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2
GIT_TAG devel
)
FetchContent_MakeAvailable(Catch2)

add_executable(basic_dict_ops basic_dict_ops.cpp)
target_include_directories(basic_dict_ops PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/../include)
target_compile_definitions(basic_dict_ops PRIVATE CATCH_CONFIG_NO_POSIX_SIGNALS)
target_link_libraries(basic_dict_ops PRIVATE Catch2::Catch2WithMain)
add_test(test_basic_dict_ops basic_dict_ops)

add_executable(stl_compatibility stl_compatibility.cpp)
target_include_directories(stl_compatibility PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/../include)
target_compile_definitions(stl_compatibility PRIVATE CATCH_CONFIG_NO_POSIX_SIGNALS)
target_link_libraries(stl_compatibility PRIVATE Catch2::Catch2WithMain)
add_test(test_stl_compatibility stl_compatibility)
18 changes: 8 additions & 10 deletions test/basic_dict_ops.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#define CATCH_CONFIG_MAIN
#if __has_include(<catch2/catch.hpp>)
#include <catch2/catch.hpp>
#else
#include <catch.hpp>
#endif
// #define CATCH_CONFIG_MAIN

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>

#include "dict.hpp"
using Dict = cppdict::Dict<int, double, std::string>;
Expand Down Expand Up @@ -105,17 +103,17 @@ TEST_CASE("Dict const accessor", "[simple cppdict::Dict<int>]")
REQUIRE_THROWS_WITH(dictRef["one"]["two"].to<double>(), "cppdict: invalid key: one");
}

TEST_CASE("Can get key by delimiter if exists", "or raises if missing")
TEST_CASE("Can at key by delimiter if exists", "or raises if missing")
{
Dict dict;
dict["this"]["is"]["pi"] = 3.14;
REQUIRE_THROWS_WITH(cppdict::get<double>(dict, "this/is/e"),
REQUIRE_THROWS_WITH(cppdict::at<double>(dict, "this/is/e"),
"cppdict: contains no path this/is/e");
}

TEST_CASE("Can get key by delimiter if exists", "or default if missing")
TEST_CASE("Can at key by delimiter if exists", "or default if missing")
{
Dict dict;
dict["this"]["is"]["pi"] = 3.14;
REQUIRE(cppdict::get(dict, "this/is/e", 2.71) == 2.71);
REQUIRE(cppdict::at(dict, "this/is/e", 2.71) == 2.71);
}
Loading

0 comments on commit 892c6e2

Please sign in to comment.