1 #include "failure_list_trie_node.h"
2
3 #include <memory>
4
5 #include <gmock/gmock.h>
6 #include <gtest/gtest.h>
7 #include "absl/status/status.h"
8 #include "absl/status/statusor.h"
9 #include "absl/strings/str_cat.h"
10 #include "absl/types/optional.h"
11
12 using ::testing::Eq;
13 using ::testing::HasSubstr;
14 using ::testing::Optional;
15
GetStatus(const absl::Status & s)16 absl::Status GetStatus(const absl::Status& s) { return s; }
17 template <typename T>
GetStatus(const absl::StatusOr<T> & s)18 absl::Status GetStatus(const absl::StatusOr<T>& s) {
19 return s.status();
20 }
21 MATCHER_P2(StatusIs, status, message,
22 absl::StrCat(".status() is ", testing::PrintToString(status))) {
23 return GetStatus(arg).code() == status &&
24 testing::ExplainMatchResult(message, GetStatus(arg).message(),
25 result_listener);
26 }
27 #define EXPECT_OK(x) EXPECT_THAT(x, StatusIs(absl::StatusCode::kOk, testing::_))
28 #define ASSERT_OK(x) ASSERT_THAT(x, StatusIs(absl::StatusCode::kOk, testing::_))
29
30 namespace google {
31 namespace protobuf {
32
TEST(FailureListTrieTest,WalkDownMatchWithoutWildcard)33 TEST(FailureListTrieTest, WalkDownMatchWithoutWildcard) {
34 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
35 ASSERT_OK(root_->Insert("Recommended.Proto2.ProtobufInput.World"));
36
37 EXPECT_THAT(root_->WalkDownMatch("Recommended.Proto2.ProtobufInput.World"),
38 Optional(Eq("Recommended.Proto2.ProtobufInput.World")));
39 }
40
TEST(FailureListTrieTest,WalkDownMatchWithoutWildcardNoMatch)41 TEST(FailureListTrieTest, WalkDownMatchWithoutWildcardNoMatch) {
42 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
43
44 ASSERT_OK(root_->Insert("Recommended.Proto2.JsonInput.World"));
45
46 EXPECT_EQ(root_->WalkDownMatch("Recommended.Proto2.TextFormatInput"),
47 absl::nullopt);
48 }
49
TEST(FailureListTrieTest,WalkDownMatchWithWildcard)50 TEST(FailureListTrieTest, WalkDownMatchWithWildcard) {
51 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
52 ASSERT_OK(root_->Insert("Recommended.*.ProtobufInput.World"));
53
54 EXPECT_THAT(root_->WalkDownMatch("Recommended.Proto2.ProtobufInput.World"),
55 Optional(Eq("Recommended.*.ProtobufInput.World")));
56 }
57
TEST(FailureListTrieTest,WalkDownMatchWithWildcardNoMatch)58 TEST(FailureListTrieTest, WalkDownMatchWithWildcardNoMatch) {
59 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
60 ASSERT_OK(root_->Insert("Recommended.*.ProtobufInput.World"));
61
62 EXPECT_EQ(root_->WalkDownMatch("Recommended.Proto2.JsonInput.World"),
63 absl::nullopt);
64 }
65
TEST(FailureListTrieTest,WalkDownMatchTestLessNumberofSectionsNoMatch)66 TEST(FailureListTrieTest, WalkDownMatchTestLessNumberofSectionsNoMatch) {
67 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
68 ASSERT_OK(root_->Insert("Recommended.*.*.*"));
69
70 EXPECT_EQ(root_->WalkDownMatch("Recommended.Proto2.JsonInput"),
71 absl::nullopt);
72 }
73
TEST(FailureListTrieTest,WalkDownMatchTestMoreNumberOfSectionsNoMatch)74 TEST(FailureListTrieTest, WalkDownMatchTestMoreNumberOfSectionsNoMatch) {
75 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
76 ASSERT_OK(root_->Insert("*"));
77
78 EXPECT_EQ(root_->WalkDownMatch("Recommended.Proto2.JsonInput.World"),
79 absl::nullopt);
80 }
81
TEST(FailureListTrieTest,WalkDownMatchTakeMoreThanOneBranch)82 TEST(FailureListTrieTest, WalkDownMatchTakeMoreThanOneBranch) {
83 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
84 ASSERT_OK(root_->Insert(
85 "Recommended.*.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace"));
86 ASSERT_OK(root_->Insert(
87 "Recommended.Proto3.*.RepeatedFieldTrailingCommaWithSpaceCommaSpace"));
88
89 EXPECT_THAT(
90 root_->WalkDownMatch("Recommended.Proto3.JsonInput."
91 "RepeatedFieldTrailingCommaWithSpaceCommaSpace"),
92 Optional(Eq("Recommended.Proto3.*."
93 "RepeatedFieldTrailingCommaWithSpaceCommaSpace")));
94 }
95
TEST(FailureListTrieTest,InsertWilcardedAmbiguousMatchFails)96 TEST(FailureListTrieTest, InsertWilcardedAmbiguousMatchFails) {
97 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
98 ASSERT_OK(root_->Insert(
99 "Recommended.*.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace"));
100
101 // Essentially a duplicated test name if inserted.
102 EXPECT_THAT(
103 root_->Insert(
104 "Recommended.Proto3.*.TrailingCommaInAnObjectWithSpaceCommaSpace"),
105 StatusIs(absl::StatusCode::kAlreadyExists, HasSubstr("already exists")));
106 }
107
TEST(FailureListTrieTest,InsertWilcardedAmbiguousMatchMutlipleWildcardsFails)108 TEST(FailureListTrieTest, InsertWilcardedAmbiguousMatchMutlipleWildcardsFails) {
109 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
110 ASSERT_OK(root_->Insert("Recommended.*.JsonInput.FieldMaskInvalidCharacter"));
111
112 // Essentially a duplicated test name if inserted.
113 EXPECT_THAT(
114 root_->Insert("Recommended.*.*.*"),
115 StatusIs(absl::StatusCode::kAlreadyExists, HasSubstr("already exists")));
116 }
117
TEST(FailureListTrieTest,InsertInvalidWildcardFails)118 TEST(FailureListTrieTest, InsertInvalidWildcardFails) {
119 auto root_ = std::make_unique<google::protobuf::FailureListTrieNode>("dummy");
120 EXPECT_THAT(root_->Insert("This*Is.Not.A.Valid.Wildcard"),
121 StatusIs(absl::StatusCode::kInvalidArgument,
122 HasSubstr("invalid wildcard")));
123 }
124 } // namespace protobuf
125 } // namespace google
126