Skip to content

Commit 808ead9

Browse files
committed
fable: Fix critical error using FromConfable as prototype schema
Resolves #186.
1 parent 475edfc commit 808ead9

File tree

4 files changed

+320
-1
lines changed

4 files changed

+320
-1
lines changed

fable/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ if(BUILD_TESTING)
7575
src/fable/schema/factory_test.cpp
7676
src/fable/schema/factory_advanced_test.cpp
7777
src/fable/schema/ignore_test.cpp
78+
src/fable/schema/map_nested_test.cpp
79+
src/fable/schema/map_test.cpp
7880
src/fable/schema/number_test.cpp
7981
src/fable/schema/optional_test.cpp
8082
src/fable/schema/string_test.cpp

fable/include/fable/schema/confable.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ class FromConfable : public Base<FromConfable<T>> {
9393
return tmp;
9494
}
9595

96-
void reset_ptr() override { ptr_ = nullptr; }
96+
void reset_ptr() override {
97+
ptr_ = nullptr;
98+
schema_.reset_ptr();
99+
}
97100

98101
private:
99102
Box schema_;
+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#include <string>
20+
#include <map>
21+
#include <vector>
22+
23+
#include <gtest/gtest.h>
24+
25+
#include <fable/confable.hpp>
26+
#include <fable/schema.hpp>
27+
#include <fable/utility/gtest.hpp>
28+
29+
namespace {
30+
31+
template <typename T>
32+
struct Nested : public fable::Confable {
33+
T value{}; // NOLINT
34+
35+
public:
36+
CONFABLE_SCHEMA(Nested<T>) {
37+
return fable::Schema{
38+
{ "v", fable::Schema(&value, "nested value") },
39+
};
40+
}
41+
};
42+
43+
template <typename T>
44+
struct MapOfSomething : public fable::Confable {
45+
std::map<std::string, T> values; // NOLINT
46+
47+
public:
48+
CONFABLE_SCHEMA(MapOfSomething<T>) {
49+
return fable::Schema{
50+
{"values", fable::Schema(&values, "")},
51+
};
52+
}
53+
};
54+
55+
}
56+
57+
TEST(fable_schema_map, validate_nested_1x) {
58+
Nested<double> wrapper;
59+
60+
fable::assert_validate(wrapper, R"({
61+
"v": 1.0
62+
})");
63+
}
64+
65+
TEST(fable_schema_map, validate_nested_2x) {
66+
Nested<Nested<double>> wrapper;
67+
68+
fable::assert_validate(wrapper, R"({
69+
"v": { "v": 1.0 }
70+
})");
71+
}
72+
73+
TEST(fable_schema_map, validate_nested_3x) {
74+
Nested<Nested<Nested<double>>> wrapper;
75+
76+
fable::assert_validate(wrapper, R"({
77+
"v": { "v": { "v": 1.0 } }
78+
})");
79+
}
80+
81+
TEST(fable_schema_map, validate_nested_4x) {
82+
Nested<Nested<Nested<Nested<double>>>> wrapper;
83+
84+
fable::assert_validate(wrapper, R"({
85+
"v": { "v": { "v": { "v": 1.0 } } }
86+
})");
87+
}
88+
89+
TEST(fable_schema_map, validate_nested_8x) {
90+
Nested<Nested<Nested<Nested<Nested<Nested<Nested<Nested<double>>>>>>>> wrapper;
91+
92+
fable::assert_validate(wrapper, R"({
93+
"v": { "v": { "v": { "v": { "v": { "v": { "v": { "v": 1.0 } } } } } } }
94+
})");
95+
}
96+
97+
TEST(fable_schema_map, validate_map_of_nested_2x) {
98+
MapOfSomething<Nested<Nested<double>>> wrapper;
99+
100+
fable::assert_validate(wrapper, R"({
101+
"values": {
102+
"a": {
103+
"v": { "v": 1.0 }
104+
}
105+
}
106+
})");
107+
}
108+
109+
TEST(fable_schema_map, validate_map_of_nested_3x) {
110+
MapOfSomething<Nested<Nested<Nested<double>>>> wrapper;
111+
112+
fable::assert_validate(wrapper, R"({
113+
"values": {
114+
"a": {
115+
"v": { "v": { "v": 1.0 } }
116+
}
117+
}
118+
})");
119+
}
120+
121+
TEST(fable_schema_map, validate_map_of_nested_4x) {
122+
MapOfSomething<Nested<Nested<Nested<Nested<double>>>>> wrapper;
123+
124+
fable::assert_validate(wrapper, R"({
125+
"values": {
126+
"a": {
127+
"v": { "v": { "v": { "v": 1.0 } } }
128+
}
129+
}
130+
})");
131+
}
132+
133+
TEST(fable_schema_map, validate_map_of_double) {
134+
MapOfSomething<double> wrapper;
135+
136+
fable::assert_validate(wrapper, R"({
137+
"values": {
138+
"a": 1.0,
139+
"b": 2.0
140+
}
141+
})");
142+
}

fable/src/fable/schema/map_test.cpp

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#include <string>
20+
#include <map>
21+
#include <vector>
22+
23+
#include <gtest/gtest.h>
24+
25+
#include <fable/confable.hpp>
26+
#include <fable/schema.hpp>
27+
#include <fable/utility/gtest.hpp>
28+
29+
namespace {
30+
31+
struct Vec3d: public fable::Confable {
32+
double x{0.0}; // NOLINT
33+
double y{0.0}; // NOLINT
34+
double z{0.0}; // NOLINT
35+
36+
public:
37+
CONFABLE_SCHEMA(Vec3d) {
38+
return fable::Schema{
39+
{"x", fable::Schema(&x, "Object position x axis")},
40+
{"y", fable::Schema(&y, "Object position y axis")},
41+
{"z", fable::Schema(&z, "Object position z axis")},
42+
};
43+
}
44+
};
45+
46+
struct Object : public fable::Confable {
47+
double velocity{0.0}; // NOLINT
48+
Vec3d position; // NOLINT
49+
50+
public:
51+
CONFABLE_SCHEMA(Object) {
52+
return fable::Schema{
53+
{"velocity", fable::Schema(&velocity, "Object longitudinal velocity")},
54+
{"position", fable::Schema(&position, "Object position coordinates (x,y,z)")},
55+
};
56+
}
57+
};
58+
59+
struct ObjectContainer : public fable::Confable {
60+
std::vector<Object> objects; // NOLINT
61+
62+
public:
63+
CONFABLE_SCHEMA(ObjectContainer) {
64+
return fable::Schema{
65+
{"objects", fable::Schema(&objects, "")},
66+
};
67+
}
68+
};
69+
70+
struct NamedObject : public fable::Confable {
71+
Object named_object; // NOLINT
72+
73+
public:
74+
CONFABLE_SCHEMA(NamedObject) {
75+
return fable::Schema{
76+
{"named_object", fable::Schema(&named_object, "")},
77+
};
78+
}
79+
};
80+
81+
struct NestedNamedObject : public fable::Confable {
82+
NamedObject ego_sensor_mockup; // NOLINT
83+
84+
public:
85+
CONFABLE_SCHEMA(NestedNamedObject) {
86+
return fable::Schema{
87+
{"ego_sensor_mockup", fable::Schema(&ego_sensor_mockup, "Ego sensor mockup configuration")},
88+
};
89+
}
90+
};
91+
92+
template <typename T>
93+
struct MapOfSomething : public fable::Confable {
94+
std::map<std::string, T> values; // NOLINT
95+
96+
public:
97+
CONFABLE_SCHEMA(MapOfSomething<T>) {
98+
return fable::Schema{
99+
{"values", fable::Schema(&values, "")},
100+
};
101+
}
102+
};
103+
104+
} // anonymous namespace
105+
106+
TEST(fable_schema_map, validate_map_of_vec3d) {
107+
MapOfSomething<Vec3d> wrapper;
108+
109+
fable::assert_validate(wrapper, R"({
110+
"values": {
111+
"a": { "x": 1.0, "y": 2.0, "z": 3.0 },
112+
"b": { "x": 0.0, "y": 0.0, "z": 0.0 }
113+
}
114+
})");
115+
}
116+
117+
TEST(fable_schema_map, validate_map_of_object) {
118+
MapOfSomething<Object> wrapper;
119+
120+
fable::assert_validate(wrapper, R"({
121+
"values": {
122+
"a": { "position": { "x": 1.0, "y": 2.0, "z": 3.0 }, "velocity": 0.0 },
123+
"b": { "position": { "x": 0.0, "y": 0.0, "z": 0.0 } },
124+
"c": { },
125+
"d": { "velocity": 1.0 }
126+
}
127+
})");
128+
}
129+
130+
TEST(fable_schema_map, validate_map_of_objectcontainer) {
131+
MapOfSomething<ObjectContainer> wrapper;
132+
133+
fable::assert_validate(wrapper, R"({
134+
"values": {
135+
"a": { "objects": [{ "position": { "x": 1.0, "y": 2.0, "z": 3.0 }, "velocity": 0.0 }] },
136+
"b": { "objects": [{ "position": { "x": 0.0, "y": 0.0, "z": 0.0 } }] },
137+
"c": { "objects": [{ }] },
138+
"d": { "objects": [{ "velocity": 1.0 }] }
139+
}
140+
})");
141+
}
142+
143+
TEST(fable_schema_map, validate_map_of_namedobject) {
144+
MapOfSomething<NamedObject> wrapper;
145+
146+
fable::assert_validate(wrapper, R"({
147+
"values": {
148+
"a": { "named_object": { } }
149+
}
150+
})");
151+
}
152+
153+
TEST(fable_schema_map, validate_map_of_fromconfable) {
154+
MapOfSomething<NestedNamedObject> minimator_config;
155+
156+
fable::assert_validate(minimator_config, R"({
157+
"values": {
158+
"ego1": {
159+
"ego_sensor_mockup": {
160+
"named_object": {
161+
"velocity": 0.0,
162+
"position": {
163+
"x": 0.0,
164+
"y": 0.0,
165+
"z": 0.0
166+
}
167+
}
168+
}
169+
}
170+
}
171+
})");
172+
}

0 commit comments

Comments
 (0)