|
| 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 | + * \file fable/schema/string_test.cpp |
| 20 | + * \see fable/schema/string.hpp |
| 21 | + */ |
| 22 | + |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include <fable/confable.hpp> // for Confable |
| 26 | +#include <fable/environment.hpp> // for Environment |
| 27 | +#include <fable/schema/string.hpp> // for String, ... |
| 28 | +#include <fable/utility/gtest.hpp> // for assert_to_json, ... |
| 29 | + |
| 30 | +#define TO_CONF(x) fable::Conf{fable::Json(x)} |
| 31 | + |
| 32 | +TEST(fable_schema_string, plain) { |
| 33 | + std::string t; |
| 34 | + auto s = fable::schema::make_schema(&t, "plain"); |
| 35 | + |
| 36 | + fable::assert_to_json(s, TO_CONF("")); |
| 37 | + fable::assert_from_eq_to(s, TO_CONF("hello string")); |
| 38 | + fable::assert_schema_eq(s, R"({ |
| 39 | + "type": "string", |
| 40 | + "description": "plain" |
| 41 | + })"); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(fable_schema_string, not_empty) { |
| 45 | + std::string t; |
| 46 | + auto s = fable::schema::make_schema(&t, "not empty").not_empty(); |
| 47 | + |
| 48 | + fable::assert_invalidate(s, TO_CONF("")); |
| 49 | + fable::assert_from_eq_to(s, TO_CONF("not empty")); |
| 50 | + fable::assert_schema_eq(s, R"({ |
| 51 | + "type": "string", |
| 52 | + "description": "not empty", |
| 53 | + "minLength": 1 |
| 54 | + })"); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(fable_schema_string, min_max_length) { |
| 58 | + std::string t; |
| 59 | + auto s = fable::schema::make_schema(&t, "min 4, max 8").min_length(4).max_length(8); |
| 60 | + |
| 61 | + for (const auto& x : {"", "a", "asdfasdfx"}) { |
| 62 | + fable::assert_invalidate(s, TO_CONF(x)); |
| 63 | + } |
| 64 | + for (const auto& x : {"asdf", "xxxxxx", "abcdefgh"}) { |
| 65 | + fable::assert_from_eq_to(s, TO_CONF(x)); |
| 66 | + } |
| 67 | + fable::assert_schema_eq(s, R"({ |
| 68 | + "type": "string", |
| 69 | + "description": "min 4, max 8", |
| 70 | + "minLength": 4, |
| 71 | + "maxLength": 8 |
| 72 | + })"); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(fable_schema_string, pattern) { |
| 76 | + std::string t; |
| 77 | + auto s = fable::schema::make_schema(&t, "c_identifier").c_identifier(); |
| 78 | + |
| 79 | + for (const auto& x : {"0_", "0", "not identifier", "", "a-b"}) { |
| 80 | + fable::assert_invalidate(s, TO_CONF(x)); |
| 81 | + } |
| 82 | + for (const auto& x : {"asdf", "_8", "_", "c_identier", "somethingElse"}) { |
| 83 | + fable::assert_from_eq_to(s, TO_CONF(x)); |
| 84 | + } |
| 85 | + fable::assert_schema_eq(s, R"({ |
| 86 | + "type": "string", |
| 87 | + "description": "c_identifier", |
| 88 | + "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$" |
| 89 | + })"); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(fable_schema_string, interpolate) { |
| 93 | + std::string t; |
| 94 | + fable::Environment env{ |
| 95 | + {"TEST", "true"}, |
| 96 | + {"NAME", "world"} |
| 97 | + }; |
| 98 | + auto s = fable::schema::make_schema(&t, "interpolate").interpolate(true).environment(&env); |
| 99 | + |
| 100 | + EXPECT_ANY_THROW(assert_from_conf(s, TO_CONF("${}"))); |
| 101 | + EXPECT_ANY_THROW(assert_from_conf(s, TO_CONF("${__UNLIKELY}"))); |
| 102 | + |
| 103 | + fable::assert_from_conf(s, TO_CONF("${__UNLIKELY-default}")); |
| 104 | + fable::assert_to_json(s, fable::Json("default")); |
| 105 | + ASSERT_EQ(t, "default"); |
| 106 | + |
| 107 | + fable::assert_from_conf(s, TO_CONF("${TEST-false}")); |
| 108 | + fable::assert_to_json(s, fable::Json("true")); |
| 109 | + ASSERT_EQ(t, "true"); |
| 110 | + |
| 111 | + fable::assert_from_conf(s, TO_CONF("hello ${NAME}")); |
| 112 | + fable::assert_to_json(s, fable::Json("hello world")); |
| 113 | + ASSERT_EQ(t, "hello world"); |
| 114 | + |
| 115 | + fable::assert_schema_eq(s, R"({ |
| 116 | + "type": "string", |
| 117 | + "description": "interpolate" |
| 118 | + })"); |
| 119 | +} |
| 120 | + |
| 121 | +TEST(fable_schema_string, enum) { |
| 122 | + std::string t; |
| 123 | + auto s = fable::schema::make_schema(&t, "enum").enum_of({"true", "false"}); |
| 124 | + |
| 125 | + for (const auto& x : {"", "asdf", "False"}) { |
| 126 | + fable::assert_invalidate(s, TO_CONF(x)); |
| 127 | + } |
| 128 | + for (const auto& x : {"true", "false"}) { |
| 129 | + fable::assert_from_eq_to(s, TO_CONF(x)); |
| 130 | + } |
| 131 | + fable::assert_schema_eq(s, R"({ |
| 132 | + "type": "string", |
| 133 | + "description": "enum", |
| 134 | + "enum": [ "true", "false" ] |
| 135 | + })"); |
| 136 | +} |
0 commit comments