Skip to content

Commit

Permalink
Merge pull request #18 from JohanMabille/upgrade
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
JohanMabille authored Jun 10, 2024
2 parents 4837447 + bfa906b commit f7e5302
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ message(STATUS "xvega-bindings v${${PROJECT_NAME}_VERSION}")

find_package(xvega REQUIRED)

# nlohmann_json requires libraries that exchange json objects to be linked
# with the same version of nlohmann_json. Therefore this version should be
# the same in all xeus components; to do so, downstream projects should not
# search # directly for nlohmann_json, but rely on find_dependency called by
# find_package(xeus) instead.
set(nlohmann_json_REQUIRED_VERSION 3.11)

if (NOT TARGET nlohmann_json)
find_package(nlohmann_json ${nlohmann_json_REQUIRED_VERSION} REQUIRED)
message(STATUS "Found nlohmann_json ${nlohmann_json_VERSION}")
endif ()

# Build
# =====

Expand Down
1 change: 1 addition & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies:
- ninja
# host Dependencies
- xvega=0.1.0
- nlohmann_json=3.11.3
# Test dependencies
- doctest

55 changes: 28 additions & 27 deletions include/xvega-bindings/xvega_bindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <variant>
#include <vector>
#include <utility>

Expand Down Expand Up @@ -94,7 +95,7 @@ namespace xv_bindings
using range_it_fun = std::function<input_it(T*,
const input_it&,
const input_it&)>;
using parse_function_types = xtl::variant<point_it_fun,
using parse_function_types = std::variant<point_it_fun,
range_it_fun>;

struct command_info {
Expand Down Expand Up @@ -164,13 +165,13 @@ namespace xv_bindings
if (cmd_info.parse_function.index() == 0)
{
/** Calls command functions that receive a point iterator **/
xtl::get<0>(cmd_info.parse_function)(static_cast<T*>(this), it);
std::get<0>(cmd_info.parse_function)(static_cast<T*>(this), it);
it++;
}
else if (cmd_info.parse_function.index() == 1)
{
/** Calls command functions that receive a range iterator **/
it = xtl::get<1>(cmd_info.parse_function)(static_cast<T*>(this),
it = std::get<1>(cmd_info.parse_function)(static_cast<T*>(this),
it,
end);
}
Expand Down Expand Up @@ -202,23 +203,23 @@ namespace xv_bindings
return it;
}

/* Implementation of a visitor for xv::xany which is a xtl::any */
using visitor_map_type = std::unordered_map<std::type_index, std::function<void(xtl::any const&)>>;
/* Implementation of a visitor for xv::xany which is a std::any */
using visitor_map_type = std::unordered_map<std::type_index, std::function<void(std::any const&)>>;

template<typename U, typename F>
inline std::pair<const std::type_index, std::function<void(xtl::any const&)> >
inline std::pair<const std::type_index, std::function<void(std::any const&)> >
to_any_visitor(F const &f)
{
return {
std::type_index(typeid(U)),
[=](xtl::any const &any_type)
[=](std::any const &any_type)
{
f(xtl::any_cast<U const&>(any_type));
f(std::any_cast<U const&>(any_type));
}
};
}

inline void visit_any(const xtl::any& any_type, const visitor_map_type& any_visitor)
inline void visit_any(const std::any& any_type, const visitor_map_type& any_visitor)
{
const auto it = any_visitor.find(std::type_index(any_type.type()));
if (it != any_visitor.cend()) {
Expand Down Expand Up @@ -316,7 +317,7 @@ namespace xv_bindings

struct field_parser : parser_base<field_parser>
{
using xy_variant = xtl::variant<xv::X*, xv::Y*>;
using xy_variant = std::variant<xv::X*, xv::Y*>;
xy_variant enc;

field_parser(xy_variant enc) : enc(enc)
Expand All @@ -331,7 +332,7 @@ namespace xv_bindings

input_it parse_init(const input_it& begin, const input_it&)
{
xtl::visit([&](auto &&x_or_y)
std::visit([&](auto &&x_or_y)
{
x_or_y->field = *begin;
x_or_y->type = "quantitative";
Expand All @@ -342,7 +343,7 @@ namespace xv_bindings

input_it parse_field_bin(const input_it& begin, const input_it& end)
{
bool found = xtl::visit([&](auto &&x_or_y)
bool found = std::visit([&](auto &&x_or_y)
{
return simple_switch(*begin,
{
Expand All @@ -366,7 +367,7 @@ namespace xv_bindings
throw std::runtime_error("Missing or invalid BIN type");
}

xtl::visit([&](auto &&x_or_y)
std::visit([&](auto &&x_or_y)
{
x_or_y->bin().value() = bin;
}, enc);
Expand All @@ -378,7 +379,7 @@ namespace xv_bindings

void parse_field_type(const input_it& it)
{
bool found = xtl::visit([&](auto &&x_or_y)
bool found = std::visit([&](auto &&x_or_y)
{
return simple_switch(*it,
{
Expand All @@ -396,7 +397,7 @@ namespace xv_bindings

input_it parse_field_aggregate(const input_it& begin, const input_it&)
{
bool found = xtl::visit([&](auto &&x_or_y)
bool found = std::visit([&](auto &&x_or_y)
{
return simple_switch(*begin,
{
Expand Down Expand Up @@ -434,7 +435,7 @@ namespace xv_bindings

void parse_field_time_unit(const input_it& it)
{
bool found = xtl::visit([&](auto &&x_or_y)
bool found = std::visit([&](auto &&x_or_y)
{
return simple_switch(*it,
{
Expand Down Expand Up @@ -493,17 +494,17 @@ namespace xv_bindings
void parse_color(const input_it& it)
{
visitor_map_type any_visitor {
to_any_visitor<xtl::xclosure_wrapper<xv::mark_arc&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_area&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_bar&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_circle&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_line&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_point&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_rect&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_rule&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_square&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_tick&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<xtl::xclosure_wrapper<xv::mark_trail&>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_arc>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_area>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_bar>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_circle>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_line>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_point>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_rect>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_rule>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_square>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_tick>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
to_any_visitor<std::reference_wrapper<xv::mark_trail>> ([&](auto mark_generic) { mark_generic.get().color = to_lower(*it); }),
};

visit_any(this->chart.mark().value(), any_visitor);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ find_package(doctest REQUIRED)

set(XVEGA_BINDINGS_TESTS
main.cpp
test_bindings.cpp
)

add_executable(test_xvega_bindings ${XVEGA_BINDINGS_TESTS} ${XVEGA_BINDINGS_HEADERS})
Expand Down
30 changes: 30 additions & 0 deletions test/test_bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/***************************************************************************
* Copyright (c) 2024, QuantStack and xeus-SQLite contributors *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include "doctest/doctest.h"

#include "xvega-bindings/xvega_bindings.hpp"

TEST_SUITE("xvega-bindings")
{
TEST_CASE("sanitize_string_check")
{
std::string code = "\n\n Some inp\nut\n";
std::string sanitized_string;
sanitized_string = xv_bindings::sanitize_string(code);
CHECK_EQ(sanitized_string, " Some input");
}

TEST_CASE("tokenizer_check")
{
std::string code = "%LOAD database.db rw";
std::vector<std::string> tokenized_code;
tokenized_code = xv_bindings::tokenizer(code);
CHECK_EQ(tokenized_code[1], "database.db");
}
}
1 change: 1 addition & 0 deletions xvega-bindingsConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include(CMakeFindDependencyMacro)
# nlohmann_json requires libraries that exchange json objects to be linked
# with the same version of nlohmann_json.
find_dependency(xvega)
find_dependency(nlohmann_json @nlohmann_json_VERSION@ EXACT)

if(NOT TARGET @PROJECT_NAME@)
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
Expand Down

0 comments on commit f7e5302

Please sign in to comment.