|
| 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