1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
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
17 #include <thrift/compiler/ast/name_index.h>
18
19 #include <memory>
20
21 #include <folly/portability/GMock.h>
22 #include <folly/portability/GTest.h>
23 #include <thrift/compiler/ast/t_program.h>
24 #include <thrift/compiler/ast/t_struct.h>
25
26 namespace apache::thrift::compiler {
27 namespace {
28
29 class NameIndexTest : public ::testing::Test {};
30
TEST_F(NameIndexTest,FindPutReplace)31 TEST_F(NameIndexTest, FindPutReplace) {
32 t_program program("/path/to/file.thrift");
33 name_index<t_struct> index;
34 EXPECT_EQ(index.contains("s"), false);
35 EXPECT_EQ(index.contains("t"), false);
36 EXPECT_EQ(index.find("s"), nullptr);
37
38 // Can't use string temporaries.
39 // Uncomment to see expected compiler error.
40 // index.put("hi", nullptr);
41
42 auto s1 = std::make_unique<t_struct>(&program, "s");
43 EXPECT_EQ(index.put(*s1), nullptr);
44 EXPECT_EQ(index.contains("s"), true);
45 EXPECT_EQ(index.contains("t"), false);
46 EXPECT_EQ(index.find("s"), s1.get());
47
48 auto s2 = std::make_unique<t_struct>(&program, "s");
49 EXPECT_EQ(index.put(*s2), s1.get());
50 s1.reset(); // Safe to delete once replaced in the index.
51 EXPECT_EQ(index.contains("s"), true);
52 EXPECT_EQ(index.contains("t"), false);
53 EXPECT_EQ(index.find("s"), s2.get());
54
55 index.clear();
56 s2.reset(); // Safe to delete once cleared from the index.
57 EXPECT_EQ(index.contains("s"), false);
58 EXPECT_EQ(index.contains("t"), false);
59 EXPECT_EQ(index.find("s"), nullptr);
60 }
61
TEST_F(NameIndexTest,PutAll)62 TEST_F(NameIndexTest, PutAll) {
63 t_program program("/path/to/file.thrift");
64 auto s1 = std::make_unique<t_struct>(&program, "s1");
65 auto s2a = std::make_unique<t_struct>(&program, "s2");
66 auto s2b = std::make_unique<t_struct>(&program, "s2");
67 auto s3 = std::make_unique<t_struct>(&program, "s3");
68
69 name_index<t_struct> index1;
70 name_index<t_struct> index2;
71
72 index1.put(*s1);
73 index1.put(*s2a);
74 index2.put(*s2b);
75 index2.put(*s3);
76 index1.put_all(index2);
77
78 EXPECT_EQ(index1.find("s1"), s1.get());
79 EXPECT_EQ(index1.find("s2"), s2b.get());
80 EXPECT_EQ(index1.find("s3"), s3.get());
81
82 std::vector<const t_struct*> nodes;
83 index1.for_each([&](const std::string& name, const t_struct& node) {
84 EXPECT_EQ(&name, &node.name());
85 nodes.emplace_back(&node);
86 });
87 EXPECT_THAT(
88 nodes, ::testing::UnorderedElementsAre(s1.get(), s2b.get(), s3.get()));
89 }
90
91 } // namespace
92 } // namespace apache::thrift::compiler
93