|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "checker/internal/field_path.h" |
| 16 | + |
| 17 | +#include "absl/strings/str_cat.h" |
| 18 | +#include "internal/testing.h" |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +using ::testing::ElementsAre; |
| 23 | + |
| 24 | +TEST(FieldPathTest, EmptyPathReturnsEmptyString) { |
| 25 | + FieldPath field_path(""); |
| 26 | + EXPECT_EQ(field_path.GetPath(), ""); |
| 27 | + EXPECT_THAT(field_path.GetFieldSelection(), ElementsAre("")); |
| 28 | + EXPECT_EQ(field_path.GetFieldName(), ""); |
| 29 | +} |
| 30 | + |
| 31 | +TEST(FieldPathTest, DelimiterPathReturnsEmptyStrings) { |
| 32 | + FieldPath field_path("."); |
| 33 | + EXPECT_EQ(field_path.GetPath(), "."); |
| 34 | + EXPECT_THAT(field_path.GetFieldSelection(), ElementsAre("", "")); |
| 35 | + EXPECT_EQ(field_path.GetFieldName(), ""); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(FieldPathTest, FieldPathReturnsFields) { |
| 39 | + FieldPath field_path("resource.name.other_field"); |
| 40 | + EXPECT_EQ(field_path.GetPath(), "resource.name.other_field"); |
| 41 | + EXPECT_THAT(field_path.GetFieldSelection(), |
| 42 | + ElementsAre("resource", "name", "other_field")); |
| 43 | + EXPECT_EQ(field_path.GetFieldName(), "resource"); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(FieldPathTest, AbslStringifyPrintsFieldSelection) { |
| 47 | + FieldPath field_path("resource.name"); |
| 48 | + EXPECT_EQ(absl::StrCat(field_path), |
| 49 | + "FieldPath { field path: 'resource.name', field selection: " |
| 50 | + "{'resource', 'name'} }"); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(FieldPathTest, EqualsComparesFieldSelectionAndReturnsTrue) { |
| 54 | + FieldPath field_path_1("resource.name"); |
| 55 | + FieldPath field_path_2("resource.name"); |
| 56 | + EXPECT_TRUE(field_path_1 == field_path_2); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(FieldPathTest, EqualsComparesFieldSelectionAndReturnsFalse) { |
| 60 | + FieldPath field_path_1("resource.name"); |
| 61 | + FieldPath field_path_2("resource.type"); |
| 62 | + EXPECT_FALSE(field_path_1 == field_path_2); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(FieldPathTest, LessThanComparesFieldSelectionAndReturnsTrue) { |
| 66 | + FieldPath field_path_1("resource.name"); |
| 67 | + FieldPath field_path_2("resource.type"); |
| 68 | + EXPECT_TRUE(field_path_1 < field_path_2); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(FieldPathTest, LessThanComparesFieldSelectionAndReturnsFalse) { |
| 72 | + FieldPath field_path_1("resource.name"); |
| 73 | + FieldPath field_path_2("resource.name"); |
| 74 | + EXPECT_FALSE(field_path_1 < field_path_2); |
| 75 | +} |
| 76 | + |
| 77 | +} // namespace |
0 commit comments