|
| 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 | +#include "common/ast/navigable_ast_internal.h" |
| 15 | + |
| 16 | +#include <iterator> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "absl/base/casts.h" |
| 20 | +#include "absl/strings/str_cat.h" |
| 21 | +#include "absl/types/span.h" |
| 22 | +#include "common/ast/navigable_ast_kinds.h" |
| 23 | +#include "internal/testing.h" |
| 24 | + |
| 25 | +namespace cel::common_internal { |
| 26 | +namespace { |
| 27 | + |
| 28 | +struct TestRangeTraits { |
| 29 | + using UnderlyingType = int; |
| 30 | + static double Adapt(const UnderlyingType& value) { |
| 31 | + return static_cast<double>(value) + 0.5; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +TEST(NavigableAstRangeTest, BasicIteration) { |
| 36 | + std::vector<int> values{1, 2, 3}; |
| 37 | + NavigableAstRange<TestRangeTraits> range(absl::MakeConstSpan(values)); |
| 38 | + absl::Span<const int> span(values); |
| 39 | + auto it = range.begin(); |
| 40 | + EXPECT_EQ(*it, 1.5); |
| 41 | + EXPECT_EQ(*++it, 2.5); |
| 42 | + EXPECT_EQ(*++it, 3.5); |
| 43 | + EXPECT_EQ(++it, range.end()); |
| 44 | + EXPECT_EQ(*--it, 3.5); |
| 45 | + EXPECT_EQ(*--it, 2.5); |
| 46 | + EXPECT_EQ(*--it, 1.5); |
| 47 | + EXPECT_EQ(it, range.begin()); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(NodeKind, Stringify) { |
| 51 | + // Note: the specific values are not important or guaranteed to be stable, |
| 52 | + // they are only intended to make test outputs clearer. |
| 53 | + EXPECT_EQ(absl::StrCat(NodeKind::kConstant), "Constant"); |
| 54 | + EXPECT_EQ(absl::StrCat(NodeKind::kIdent), "Ident"); |
| 55 | + EXPECT_EQ(absl::StrCat(NodeKind::kSelect), "Select"); |
| 56 | + EXPECT_EQ(absl::StrCat(NodeKind::kCall), "Call"); |
| 57 | + EXPECT_EQ(absl::StrCat(NodeKind::kList), "List"); |
| 58 | + EXPECT_EQ(absl::StrCat(NodeKind::kMap), "Map"); |
| 59 | + EXPECT_EQ(absl::StrCat(NodeKind::kStruct), "Struct"); |
| 60 | + EXPECT_EQ(absl::StrCat(NodeKind::kComprehension), "Comprehension"); |
| 61 | + EXPECT_EQ(absl::StrCat(NodeKind::kUnspecified), "Unspecified"); |
| 62 | + |
| 63 | + EXPECT_EQ(absl::StrCat(absl::bit_cast<NodeKind>(255)), |
| 64 | + "Unknown NodeKind 255"); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(ChildKind, Stringify) { |
| 68 | + // Note: the specific values are not important or guaranteed to be stable, |
| 69 | + // they are only intended to make test outputs clearer. |
| 70 | + EXPECT_EQ(absl::StrCat(ChildKind::kSelectOperand), "SelectOperand"); |
| 71 | + EXPECT_EQ(absl::StrCat(ChildKind::kCallReceiver), "CallReceiver"); |
| 72 | + EXPECT_EQ(absl::StrCat(ChildKind::kCallArg), "CallArg"); |
| 73 | + EXPECT_EQ(absl::StrCat(ChildKind::kListElem), "ListElem"); |
| 74 | + EXPECT_EQ(absl::StrCat(ChildKind::kMapKey), "MapKey"); |
| 75 | + EXPECT_EQ(absl::StrCat(ChildKind::kMapValue), "MapValue"); |
| 76 | + EXPECT_EQ(absl::StrCat(ChildKind::kStructValue), "StructValue"); |
| 77 | + EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionRange), "ComprehensionRange"); |
| 78 | + EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionInit), "ComprehensionInit"); |
| 79 | + EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionCondition), |
| 80 | + "ComprehensionCondition"); |
| 81 | + EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionLoopStep), |
| 82 | + "ComprehensionLoopStep"); |
| 83 | + EXPECT_EQ(absl::StrCat(ChildKind::kComprensionResult), "ComprehensionResult"); |
| 84 | + EXPECT_EQ(absl::StrCat(ChildKind::kUnspecified), "Unspecified"); |
| 85 | + |
| 86 | + EXPECT_EQ(absl::StrCat(absl::bit_cast<ChildKind>(255)), |
| 87 | + "Unknown ChildKind 255"); |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace |
| 91 | +} // namespace cel::common_internal |
0 commit comments