|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/util/property_util.h" |
| 21 | + |
| 22 | +#include <optional> |
| 23 | +#include <string> |
| 24 | +#include <string_view> |
| 25 | +#include <unordered_map> |
| 26 | + |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +namespace iceberg { |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +constexpr std::string_view kKey = "test.enabled"; |
| 34 | + |
| 35 | +std::unordered_map<std::string, std::string> Properties(std::string_view value) { |
| 36 | + return {{std::string(kKey), std::string(value)}}; |
| 37 | +} |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +TEST(PropertyUtilTest, BooleanDefaultsWhenPropertyIsAbsent) { |
| 42 | + const std::unordered_map<std::string, std::string> empty; |
| 43 | + EXPECT_TRUE(PropertyUtil::PropertyAsBoolean(empty, kKey, true)); |
| 44 | + EXPECT_FALSE(PropertyUtil::PropertyAsBoolean(empty, kKey, false)); |
| 45 | + EXPECT_EQ(PropertyUtil::PropertyAsOptionalBoolean(empty, kKey), std::nullopt); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(PropertyUtilTest, BooleanIgnoresCase) { |
| 49 | + for (const auto* value : {"true", "TRUE", "TrUe"}) { |
| 50 | + EXPECT_TRUE(PropertyUtil::PropertyAsBoolean(Properties(value), kKey, false)) << value; |
| 51 | + EXPECT_EQ(PropertyUtil::PropertyAsOptionalBoolean(Properties(value), kKey), true) |
| 52 | + << value; |
| 53 | + } |
| 54 | + for (const auto* value : {"false", "FALSE", "FaLsE"}) { |
| 55 | + EXPECT_FALSE(PropertyUtil::PropertyAsBoolean(Properties(value), kKey, true)) << value; |
| 56 | + EXPECT_EQ(PropertyUtil::PropertyAsOptionalBoolean(Properties(value), kKey), false) |
| 57 | + << value; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +TEST(PropertyUtilTest, NonBooleanValueReadsAsFalse) { |
| 62 | + // Matches Java's PropertyUtil.propertyAsBoolean: a value that does not spell a |
| 63 | + // boolean is not rejected, and the default does not apply either, since the |
| 64 | + // property is set. |
| 65 | + for (const auto* value : {"", " true", "yes", "1", "ture"}) { |
| 66 | + EXPECT_FALSE(PropertyUtil::PropertyAsBoolean(Properties(value), kKey, true)) << value; |
| 67 | + EXPECT_EQ(PropertyUtil::PropertyAsOptionalBoolean(Properties(value), kKey), false) |
| 68 | + << value; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace iceberg |
0 commit comments