-
Notifications
You must be signed in to change notification settings - Fork 701
/
Copy pathgtest_json.cpp
144 lines (110 loc) · 3.3 KB
/
gtest_json.cpp
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
#include <gtest/gtest.h>
#include "behaviortree_cpp/blackboard.h"
#include "behaviortree_cpp/json_export.h"
#include "behaviortree_cpp/basic_types.h"
//----------- Custom types ----------
namespace TestTypes {
struct Vector3D {
double x = 0;
double y = 0;
double z = 0;
};
struct Quaternion3D {
double w = 1;
double x = 0;
double y = 0;
double z = 0;
};
struct Pose3D {
Vector3D pos;
Quaternion3D rot;
};
BT_JSON_CONVERTER(Vector3D, v)
{
add_field("x", &v.x);
add_field("y", &v.y);
add_field("z", &v.z);
}
BT_JSON_CONVERTER(Quaternion3D, v)
{
add_field("w", &v.w);
add_field("x", &v.x);
add_field("y", &v.y);
add_field("z", &v.z);
}
BT_JSON_CONVERTER(Pose3D, v)
{
add_field("pos", &v.pos);
add_field("rot", &v.rot);
}
} // namespace TestTypes
//----------- JSON specialization ----------
class JsonTest : public testing::Test {
protected:
JsonTest() {
BT::JsonExporter& exporter = BT::JsonExporter::get();
exporter.addConverter<TestTypes::Pose3D>();
exporter.addConverter<TestTypes::Vector3D>();
exporter.addConverter<TestTypes::Quaternion3D>();
}
};
TEST_F(JsonTest, TwoWaysConversion)
{
BT::JsonExporter& exporter = BT::JsonExporter::get();
TestTypes::Pose3D pose = { {1,2,3},
{4,5,6,7} };
nlohmann::json json;
exporter.toJson(BT::Any(69), json["int"]);
exporter.toJson(BT::Any(3.14), json["real"]);
exporter.toJson(BT::Any(pose), json["pose"]);
std::cout << json.dump(2) << std::endl;
ASSERT_EQ(json["int"], 69);
ASSERT_EQ(json["real"], 3.14);
ASSERT_EQ(json["pose"]["__type"], "Pose3D");
ASSERT_EQ(json["pose"]["pos"]["x"], 1);
ASSERT_EQ(json["pose"]["pos"]["y"], 2);
ASSERT_EQ(json["pose"]["pos"]["z"], 3);
ASSERT_EQ(json["pose"]["rot"]["w"], 4);
ASSERT_EQ(json["pose"]["rot"]["x"], 5);
ASSERT_EQ(json["pose"]["rot"]["y"], 6);
ASSERT_EQ(json["pose"]["rot"]["z"], 7);
// check the two-ways transform, i.e. "from_json"
auto pose2 = exporter.fromJson(json["pose"])->first.cast<TestTypes::Pose3D>();
ASSERT_EQ(pose.pos.x, pose2.pos.x);
ASSERT_EQ(pose.pos.y, pose2.pos.y);
ASSERT_EQ(pose.pos.z, pose2.pos.z);
ASSERT_EQ(pose.rot.w, pose2.rot.w);
ASSERT_EQ(pose.rot.x, pose2.rot.x);
ASSERT_EQ(pose.rot.y, pose2.rot.y);
ASSERT_EQ(pose.rot.z, pose2.rot.z);
auto num = exporter.fromJson(json["int"])->first.cast<int>();
ASSERT_EQ(num, 69);
auto real = exporter.fromJson(json["real"])->first.cast<double>();
ASSERT_EQ(real, 3.14);
}
TEST_F(JsonTest, ConvertFromString)
{
TestTypes::Vector3D vect;
auto const test_json = R"(json:{"x":2.1, "y":4.2, "z":6.3})";
ASSERT_NO_THROW(vect = BT::convertFromString<TestTypes::Vector3D>(test_json));
ASSERT_EQ(vect.x, 2.1);
ASSERT_EQ(vect.y, 4.2);
ASSERT_EQ(vect.z, 6.3);
}
TEST_F(JsonTest, BlackboardInOut)
{
auto bb = BT::Blackboard::create();
bb->set("int", 42);
bb->set("real", 3.14);
bb->set("vect", TestTypes::Vector3D{1.1, 2.2, 3.3});
auto json = ExportBlackboardToJSON(*bb);
std::cout << json.dump(2) << std::endl;
auto bb_out = BT::Blackboard::create();
ImportBlackboardFromJSON(json, *bb_out);
ASSERT_EQ(bb_out->get<int>("int"), 42);
ASSERT_EQ(bb_out->get<double>("real"), 3.14);
auto vect_out = bb_out->get<TestTypes::Vector3D>("vect");
ASSERT_EQ(vect_out.x, 1.1);
ASSERT_EQ(vect_out.y, 2.2);
ASSERT_EQ(vect_out.z, 3.3);
}