|
| 1 | +/* |
| 2 | + * Copyright 2021 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 | + * \file fable/schema/custom_test.cpp |
| 20 | + * \see fable/schema/custom.hpp |
| 21 | + */ |
| 22 | + |
| 23 | +#include <string> // for string |
| 24 | +#include <vector> // for vector<> |
| 25 | + |
| 26 | +#include <gtest/gtest.h> // for TEST |
| 27 | + |
| 28 | +#include <fable/confable.hpp> // for Confable |
| 29 | +#include <fable/schema.hpp> // for Schema, Variant, Conf, Json |
| 30 | +#include <fable/schema/custom.hpp> // for CustomDeserializer |
| 31 | +#include <fable/utility/gtest.hpp> // for assert_from_conf |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +/** |
| 36 | + * MyCustomStruct models a command that can take either a string which it |
| 37 | + * passes to a shell to run, or an array, which it runs directly. |
| 38 | + * |
| 39 | + * It needs to make use of CustomDeserializer for this to work. |
| 40 | + * We dispense with the error checking that would be required in production |
| 41 | + * code, since this is just a unit test. |
| 42 | + */ |
| 43 | +struct MyCustomStruct : public fable::Confable { |
| 44 | + std::string executable; |
| 45 | + std::vector<std::string> args; |
| 46 | + |
| 47 | + CONFABLE_SCHEMA(MyCustomStruct) { |
| 48 | + // clang-format off |
| 49 | + using namespace fable; |
| 50 | + using namespace fable::schema; // NOLINT(build/namespaces) |
| 51 | + return Struct{ |
| 52 | + {"command", Variant{ |
| 53 | + "system command to execute", |
| 54 | + // Variant #1: system command to pass to a shell |
| 55 | + { |
| 56 | + CustomDeserializer( |
| 57 | + make_prototype<std::string>(), |
| 58 | + [this](CustomDeserializer*, const Conf& c) { |
| 59 | + this->executable = "/bin/bash"; |
| 60 | + this->args = {"-c", c.get<std::string>()}; |
| 61 | + } |
| 62 | + ), |
| 63 | + // Variant #2: an array with the first element the command to run |
| 64 | + // and the rest of the items the arguments to the command. |
| 65 | + CustomDeserializer( |
| 66 | + make_prototype<std::vector<std::string>>().min_items(1), |
| 67 | + [this](CustomDeserializer*, const Conf& c) { |
| 68 | + auto args = c.get<std::vector<std::string>>(); |
| 69 | + this->executable = args[0]; |
| 70 | + for (size_t i = 1; i < args.size(); ++i) { |
| 71 | + this->args.push_back(args[i]); |
| 72 | + } |
| 73 | + } |
| 74 | + ), |
| 75 | + }, |
| 76 | + }.require()} |
| 77 | + }; |
| 78 | + // clang-format on |
| 79 | + } |
| 80 | + |
| 81 | + void to_json(fable::Json& j) const override { |
| 82 | + j = fable::Json{ |
| 83 | + {"executable", executable}, |
| 84 | + {"args", args}, |
| 85 | + }; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +} // anonymous namespace |
| 90 | + |
| 91 | +TEST(fable_schema_custom, schema) { |
| 92 | + MyCustomStruct tmp; |
| 93 | + fable::assert_schema_eq(tmp, R"({ |
| 94 | + "type": "object", |
| 95 | + "properties": { |
| 96 | + "command": { |
| 97 | + "description": "system command to execute", |
| 98 | + "anyOf": [ |
| 99 | + { |
| 100 | + "type": "string" |
| 101 | + }, |
| 102 | + { |
| 103 | + "type": "array", |
| 104 | + "items": { |
| 105 | + "type": "string" |
| 106 | + }, |
| 107 | + "minItems": 1 |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + }, |
| 112 | + "required": ["command"], |
| 113 | + "additionalProperties": false |
| 114 | + })"); |
| 115 | +} |
| 116 | + |
| 117 | +TEST(fable_schema_custom, from_conf) { |
| 118 | + MyCustomStruct tmp; |
| 119 | + |
| 120 | + fable::assert_from_conf(tmp, R"({ |
| 121 | + "command": "echo 'Hello World'" |
| 122 | + })"); |
| 123 | + ASSERT_EQ(tmp.executable, "/bin/bash"); |
| 124 | + |
| 125 | + fable::assert_from_conf(tmp, R"({ |
| 126 | + "command": ["echo", "Hello World!"] |
| 127 | + })"); |
| 128 | + ASSERT_EQ(tmp.executable, "echo"); |
| 129 | + |
| 130 | + fable::assert_invalidate(tmp, R"({ |
| 131 | + "command": [] |
| 132 | + })"); |
| 133 | + ASSERT_EQ(tmp.executable, "echo"); |
| 134 | +} |
| 135 | + |
| 136 | +TEST(fable_schema_custom, to_json) { |
| 137 | + MyCustomStruct tmp; |
| 138 | + tmp.executable = "echo"; |
| 139 | + tmp.args = {"Hello World"}; |
| 140 | + fable::assert_to_json(tmp, R"({ |
| 141 | + "executable": "echo", |
| 142 | + "args": ["Hello World"] |
| 143 | + })"); |
| 144 | +} |
0 commit comments