1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#define CAF_SUITE config_value_reader
#include "caf/config_value_reader.hpp"
#include "core-test.hpp"
#include "inspector-tests.hpp"
#include "caf/config_value.hpp"
#include "caf/config_value_writer.hpp"
#include "caf/inspector_access.hpp"
using namespace caf;
using namespace std::literals::string_literals;
namespace {
using i64 = int64_t;
constexpr i64 operator""_i64(unsigned long long int x) {
return static_cast<int64_t>(x);
}
using i64_list = std::vector<i64>;
struct fixture {
config_value x;
template <class T>
void deserialize(const config_value& src, T& value) {
config_value_reader reader{&src};
if (!detail::load(reader, value))
CAF_FAIL("deserialization failed: " << reader.get_error());
}
template <class T>
void deserialize(const settings& src, T& value) {
deserialize(config_value{src}, value);
}
template <class T>
void deserialize(T& value) {
return deserialize(x, value);
}
template <class T>
optional<T> get(const settings& cfg, string_view key) {
if (auto ptr = get_if<T>(&cfg, key))
return *ptr;
return none;
}
template <class T>
optional<T> get(string_view key) {
if (auto* xs = get_if<settings>(&x))
return get<T>(*xs, key);
else
CAF_FAIL("fixture does not contain a dictionary");
}
};
} // namespace
BEGIN_FIXTURE_SCOPE(fixture)
CAF_TEST(readers deserialize builtin types from config values) {
std::string value;
auto& xs = x.as_dictionary();
put(xs, "foo", "bar");
deserialize(xs["foo"], value);
CHECK_EQ(value, "bar");
}
CAF_TEST(readers deserialize simple objects from configs) {
auto& xs = x.as_dictionary();
put(xs, "foo", "hello");
put(xs, "bar", "world");
foobar fb;
deserialize(fb);
CHECK_EQ(fb.foo(), "hello"s);
CHECK_EQ(fb.bar(), "world"s);
}
CAF_TEST(readers deserialize complex objects from configs) {
MESSAGE("fill a dictionary with data for a 'basics' object");
auto& xs = x.as_dictionary();
put(xs, "v1", settings{});
put(xs, "v2", 42_i64);
put(xs, "v3", i64_list({1, 2, 3, 4}));
settings msg1;
put(msg1, "content", 2.0);
put(msg1, "@content-type", "double");
settings msg2;
put(msg2, "content", "foobar"s);
put(msg2, "@content-type", "std::string");
put(xs, "v4", make_config_value_list(msg1, msg2));
put(xs, "v5", i64_list({10, 20}));
config_value::list v6;
v6.emplace_back(i64{123});
v6.emplace_back(msg1);
put(xs, "v6", v6);
put(xs, "v7.one", i64{1});
put(xs, "v7.two", i64{2});
put(xs, "v7.three", i64{3});
put(xs, "v8", i64_list());
MESSAGE("deserialize and verify the 'basics' object");
basics obj;
deserialize(obj);
CHECK_EQ(obj.v2, 42);
CHECK_EQ(obj.v3[0], 1);
CHECK_EQ(obj.v3[1], 2);
CHECK_EQ(obj.v3[2], 3);
CHECK_EQ(obj.v3[3], 4);
CHECK_EQ(obj.v4[0], dummy_message{{2.0}});
CHECK_EQ(obj.v4[1], dummy_message{{"foobar"s}});
CHECK_EQ(obj.v5[0], i64{10});
CHECK_EQ(obj.v5[1], i64{20});
CHECK_EQ(obj.v6, std::make_tuple(int32_t{123}, dummy_message{{2.0}}));
CHECK_EQ(obj.v7["one"], 1);
CHECK_EQ(obj.v7["two"], 2);
CHECK_EQ(obj.v7["three"], 3);
}
CAF_TEST(readers deserialize objects from the output of writers) {
MESSAGE("serialize the 'line' object");
{
line l{{10, 20, 30}, {70, 60, 50}};
config_value tmp;
config_value_writer writer{&tmp};
if (!detail::save(writer, l))
CAF_FAIL("failed two write to settings: " << writer.get_error());
if (!holds_alternative<settings>(tmp))
CAF_FAIL("writer failed to produce a dictionary");
x.as_dictionary() = std::move(caf::get<settings>(tmp));
}
MESSAGE("serialize and verify the 'line' object");
{
line l{{0, 0, 0}, {0, 0, 0}};
deserialize(l);
CHECK_EQ(l.p1.x, 10);
CHECK_EQ(l.p1.y, 20);
CHECK_EQ(l.p1.z, 30);
CHECK_EQ(l.p2.x, 70);
CHECK_EQ(l.p2.y, 60);
CHECK_EQ(l.p2.z, 50);
}
}
END_FIXTURE_SCOPE()
|