-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
yaml_doxygen_test.cc
259 lines (220 loc) · 6.61 KB
/
yaml_doxygen_test.cc
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <fstream>
#include <sstream>
#include <gtest/gtest.h>
#include "drake/common/find_resource.h"
#include "drake/common/temp_directory.h"
#include "drake/common/yaml/yaml_io.h"
// This is a unit test that exactly replicates the examples in yaml_doxygen.h,
// to ensure that they remain correct. Any changes to this file or the doxygen
// file must be mirrored into the other file.
namespace drake {
namespace yaml {
namespace test {
namespace {
// (This is a test helper, not part of the Doxygen header.)
std::string GetTempFilename() {
return temp_directory() + "/filename.yaml";
}
// Write data to a scratch file and then return the temp's filename.
// (This is a test helper, not part of the Doxygen header.)
std::string WriteTemp(const std::string& data) {
const std::string filename = GetTempFilename();
std::ofstream output(filename);
output << data;
return filename;
}
// This is an example from the Doxygen header.
struct MyData {
template <typename Archive>
void Serialize(Archive* a) {
a->Visit(DRAKE_NVP(foo));
a->Visit(DRAKE_NVP(bar));
}
double foo{0.0};
std::vector<double> bar;
};
GTEST_TEST(YamlDoxygenTest, ExamplesLoading) {
// This data is an example from the Doxygen header.
const std::string input = R"""(
foo: 1.0
bar: [2.0, 3.0]
)""";
auto input_filename = WriteTemp(input);
std::stringstream std_cout;
// This code is an example from the Doxygen header.
const MyData data = LoadYamlFile<MyData>(input_filename);
std_cout << fmt::format("foo = {:.1f}\n", data.foo);
std_cout << fmt::format("bar = {:.1f}\n", fmt::join(data.bar, ", "));
const std::string expected_output = R"""(
foo = 1.0
bar = 2.0, 3.0
)""";
EXPECT_EQ(std_cout.str(), expected_output.substr(1));
}
GTEST_TEST(YamlDoxygenTest, ExamplesSaving) {
const std::string output_filename = GetTempFilename();
// This code is an example from the Doxygen header.
MyData data{4.0, {5.0, 6.0}};
SaveYamlFile(output_filename, data);
// This data is an example from the Doxygen header.
const std::string output = ReadFileOrThrow(output_filename);
const std::string expected_output = R"""(
foo: 4.0
bar: [5.0, 6.0]
)""";
EXPECT_EQ(output, expected_output.substr(1));
}
// This is an example from the Doxygen header.
struct MoreData {
template <typename Archive>
void Serialize(Archive* a) {
a->Visit(DRAKE_NVP(baz));
a->Visit(DRAKE_NVP(quux));
}
std::string baz;
std::map<std::string, MyData> quux;
};
// This is an example from the Doxygen header.
MyData LoadMyData(const std::string& filename) {
return LoadYamlFile<MyData>(filename);
}
GTEST_TEST(YamlDoxygenTest, ReadingYamlFiles) {
// This data is an example from the Doxygen header.
const std::string input = R"""(
foo: 1.0
bar: [2.0, 3.0]
)""";
auto input_filename = WriteTemp(input);
std::stringstream std_cout;
// This code is an example from the Doxygen header.
const MyData data = LoadMyData(input_filename);
std_cout << fmt::format("foo = {:.1f}\n", data.foo);
std_cout << fmt::format("bar = {:.1f}\n", fmt::join(data.bar, ", "));
// This data is an example from the Doxygen header.
const std::string expected_output = R"""(
foo = 1.0
bar = 2.0, 3.0
)""";
EXPECT_EQ(std_cout.str(), expected_output.substr(1));
}
// This is an example from the Doxygen header.
MyData LoadMyData2(const std::string& filename) {
return LoadYamlFile<MyData>(filename, "data_2");
}
GTEST_TEST(YamlDoxygenTest, TopLevelChild) {
// This data is an example from the Doxygen header.
const std::string input = R"""(
data_1:
foo: 1.0
bar: [2.0, 3.0]
data_2:
foo: 4.0
bar: [5.0, 6.0]
)""";
auto input_filename = WriteTemp(input);
std::stringstream std_cout;
const MyData data = LoadMyData2(input_filename);
std_cout << fmt::format("foo = {:.1f}\n", data.foo);
std_cout << fmt::format("bar = {:.1f}\n", fmt::join(data.bar, ", "));
// This data is an example from the Doxygen header.
const std::string expected_output = R"""(
foo = 4.0
bar = 5.0, 6.0
)""";
EXPECT_EQ(std_cout.str(), expected_output.substr(1));
}
GTEST_TEST(YamlDoxygenTest, MergeKeys) {
// This data is an example from the Doxygen header.
const std::string input = R"""(
_template: &common_foo
foo: 1.0
data_1:
<< : *common_foo
bar: [2.0, 3.0]
data_2:
<< : *common_foo
bar: [5.0, 6.0]
)""";
auto input_filename = WriteTemp(input);
const MyData data = LoadMyData2(input_filename);
EXPECT_EQ(data.foo, 1.0);
EXPECT_EQ(data.bar, std::vector({5.0, 6.0}));
}
GTEST_TEST(YamlDoxygenTest, WritingYamlFiles) {
std::stringstream std_cout;
// This code is an example from the Doxygen header.
MyData data{1.0, {2.0, 3.0}};
std_cout << SaveYamlString(data, "root");
// This data is an example from the Doxygen header.
const std::string expected_output = R"""(
root:
foo: 1.0
bar: [2.0, 3.0]
)""";
EXPECT_EQ(std_cout.str(), expected_output.substr(1));
}
// This is an example from the Doxygen header.
std::map<std::string, MyData> LoadAllMyData(const std::string& filename) {
return LoadYamlFile<std::map<std::string, MyData>>(filename);
}
GTEST_TEST(YamlDoxygenTest, DocumentRoot) {
// This data is an example from the Doxygen header.
const std::string input = R"""(
data_1:
foo: 1.0
bar: [2.0, 3.0]
data_2:
foo: 4.0
bar: [5.0, 6.0]
)""";
auto input_filename = WriteTemp(input);
auto data = LoadAllMyData(input_filename);
EXPECT_EQ(data.size(), 2);
EXPECT_EQ(data.at("data_2").foo, 4.0);
}
// This is an example from the Doxygen header.
struct Foo {
template <typename Archive>
void Serialize(Archive* a) {
a->Visit(DRAKE_NVP(data));
}
std::string data;
};
// This is an example from the Doxygen header.
struct Bar {
template <typename Archive>
void Serialize(Archive* a) {
a->Visit(DRAKE_NVP(value));
}
std::variant<std::string, double, Foo> value;
};
GTEST_TEST(YamlDoxygenTest, NullableTypes) {
// This data is an example from the Doxygen header.
const std::string input = R"""(
bar:
value: hello
bar2:
value: !!str hello
bar3:
value: !!float 1.0
bar4:
value: !Foo
data: hello
)""";
auto bar = LoadYamlString<Bar>(input, "bar");
ASSERT_EQ(bar.value.index(), 0);
EXPECT_EQ(std::get<std::string>(bar.value), "hello");
auto bar2 = LoadYamlString<Bar>(input, "bar2");
ASSERT_EQ(bar2.value.index(), 0);
EXPECT_EQ(std::get<std::string>(bar2.value), "hello");
auto bar3 = LoadYamlString<Bar>(input, "bar3");
ASSERT_EQ(bar3.value.index(), 1);
EXPECT_EQ(std::get<double>(bar3.value), 1.0);
auto bar4 = LoadYamlString<Bar>(input, "bar4");
ASSERT_EQ(bar4.value.index(), 2);
EXPECT_EQ(std::get<Foo>(bar4.value).data, "hello");
}
} // namespace
} // namespace test
} // namespace yaml
} // namespace drake