Skip to content

Commit a871470

Browse files
[clang-repl] Introduce common fixture class in unittests (NFC) (#93816)
Reduce code bloat by checking test requirements in a common test fixture
1 parent 1f46729 commit a871470

File tree

5 files changed

+186
-295
lines changed

5 files changed

+186
-295
lines changed

clang/unittests/Interpreter/CodeCompletionTest.cpp

+77-157
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
//===- unittests/Interpreter/CodeCompletionTest.cpp -----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Unit tests for Clang's Interpreter library.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "InterpreterTestFixture.h"
14+
115
#include "clang/Interpreter/CodeCompletion.h"
216
#include "clang/Frontend/CompilerInstance.h"
317
#include "clang/Interpreter/Interpreter.h"
@@ -12,132 +26,87 @@
1226
#include "gmock/gmock.h"
1327
#include "gtest/gtest.h"
1428

15-
#if defined(_AIX) || defined(__MVS__)
16-
#define CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
17-
#endif
18-
1929
using namespace clang;
2030
namespace {
2131
auto CB = clang::IncrementalCompilerBuilder();
2232

23-
static std::unique_ptr<Interpreter> createInterpreter() {
24-
auto CI = cantFail(CB.CreateCpp());
25-
return cantFail(clang::Interpreter::create(std::move(CI)));
26-
}
27-
28-
static std::vector<std::string> runComp(clang::Interpreter &MainInterp,
29-
llvm::StringRef Input,
30-
llvm::Error &ErrR) {
31-
auto CI = CB.CreateCpp();
32-
if (auto Err = CI.takeError()) {
33-
ErrR = std::move(Err);
34-
return {};
35-
}
36-
37-
auto Interp = clang::Interpreter::create(std::move(*CI));
38-
if (auto Err = Interp.takeError()) {
39-
// log the error and returns an empty vector;
40-
ErrR = std::move(Err);
41-
42-
return {};
33+
class CodeCompletionTest : public InterpreterTestBase {
34+
public:
35+
std::unique_ptr<CompilerInstance> CI;
36+
std::unique_ptr<Interpreter> Interp;
37+
38+
CodeCompletionTest()
39+
: CI(cantFail(CB.CreateCpp())),
40+
Interp(cantFail(clang::Interpreter::create(std::move(CI)))) {}
41+
42+
std::vector<std::string> runComp(llvm::StringRef Input, llvm::Error &ErrR) {
43+
auto ComplCI = CB.CreateCpp();
44+
if (auto Err = ComplCI.takeError()) {
45+
ErrR = std::move(Err);
46+
return {};
47+
}
48+
49+
auto ComplInterp = clang::Interpreter::create(std::move(*ComplCI));
50+
if (auto Err = ComplInterp.takeError()) {
51+
ErrR = std::move(Err);
52+
return {};
53+
}
54+
55+
std::vector<std::string> Results;
56+
std::vector<std::string> Comps;
57+
auto *ParentCI = this->Interp->getCompilerInstance();
58+
auto *MainCI = (*ComplInterp)->getCompilerInstance();
59+
auto CC = ReplCodeCompleter();
60+
CC.codeComplete(MainCI, Input, /* Lines */ 1, Input.size() + 1, ParentCI,
61+
Results);
62+
63+
for (auto Res : Results)
64+
if (Res.find(CC.Prefix) == 0)
65+
Comps.push_back(Res);
66+
return Comps;
4367
}
68+
};
4469

45-
std::vector<std::string> Results;
46-
std::vector<std::string> Comps;
47-
auto *MainCI = (*Interp)->getCompilerInstance();
48-
auto CC = ReplCodeCompleter();
49-
CC.codeComplete(MainCI, Input, /* Lines */ 1, Input.size() + 1,
50-
MainInterp.getCompilerInstance(), Results);
51-
52-
for (auto Res : Results)
53-
if (Res.find(CC.Prefix) == 0)
54-
Comps.push_back(Res);
55-
return Comps;
56-
}
57-
58-
static bool HostSupportsJit() {
59-
auto J = llvm::orc::LLJITBuilder().create();
60-
if (J)
61-
return true;
62-
LLVMConsumeError(llvm::wrap(J.takeError()));
63-
return false;
64-
}
65-
66-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
67-
TEST(CodeCompletionTest, DISABLED_Sanity) {
68-
#else
69-
TEST(CodeCompletionTest, Sanity) {
70-
#endif
71-
if (!HostSupportsJit())
72-
GTEST_SKIP();
73-
auto Interp = createInterpreter();
70+
TEST_F(CodeCompletionTest, Sanity) {
7471
cantFail(Interp->Parse("int foo = 12;"));
7572
auto Err = llvm::Error::success();
76-
auto comps = runComp(*Interp, "f", Err);
73+
auto comps = runComp("f", Err);
7774
EXPECT_EQ((size_t)2, comps.size()); // float and foo
7875
EXPECT_EQ(comps[0], std::string("float"));
7976
EXPECT_EQ(comps[1], std::string("foo"));
8077
EXPECT_EQ((bool)Err, false);
8178
}
8279

83-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
84-
TEST(CodeCompletionTest, DISABLED_SanityNoneValid) {
85-
#else
86-
TEST(CodeCompletionTest, SanityNoneValid) {
87-
#endif
88-
if (!HostSupportsJit())
89-
GTEST_SKIP();
90-
auto Interp = createInterpreter();
80+
TEST_F(CodeCompletionTest, SanityNoneValid) {
9181
cantFail(Interp->Parse("int foo = 12;"));
9282
auto Err = llvm::Error::success();
93-
auto comps = runComp(*Interp, "babanana", Err);
83+
auto comps = runComp("babanana", Err);
9484
EXPECT_EQ((size_t)0, comps.size()); // foo and float
9585
EXPECT_EQ((bool)Err, false);
9686
}
9787

98-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
99-
TEST(CodeCompletionTest, DISABLED_TwoDecls) {
100-
#else
101-
TEST(CodeCompletionTest, TwoDecls) {
102-
#endif
103-
if (!HostSupportsJit())
104-
GTEST_SKIP();
105-
auto Interp = createInterpreter();
88+
TEST_F(CodeCompletionTest, TwoDecls) {
10689
cantFail(Interp->Parse("int application = 12;"));
10790
cantFail(Interp->Parse("int apple = 12;"));
10891
auto Err = llvm::Error::success();
109-
auto comps = runComp(*Interp, "app", Err);
92+
auto comps = runComp("app", Err);
11093
EXPECT_EQ((size_t)2, comps.size());
11194
EXPECT_EQ((bool)Err, false);
11295
}
11396

114-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
115-
TEST(CodeCompletionTest, DISABLED_CompFunDeclsNoError) {
116-
#else
117-
TEST(CodeCompletionTest, CompFunDeclsNoError) {
118-
#endif
119-
if (!HostSupportsJit())
120-
GTEST_SKIP();
121-
auto Interp = createInterpreter();
97+
TEST_F(CodeCompletionTest, CompFunDeclsNoError) {
12298
auto Err = llvm::Error::success();
123-
auto comps = runComp(*Interp, "void app(", Err);
99+
auto comps = runComp("void app(", Err);
124100
EXPECT_EQ((bool)Err, false);
125101
}
126102

127-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
128-
TEST(CodeCompletionTest, DISABLED_TypedDirected) {
129-
#else
130-
TEST(CodeCompletionTest, TypedDirected) {
131-
#endif
132-
if (!HostSupportsJit())
133-
GTEST_SKIP();
134-
auto Interp = createInterpreter();
103+
TEST_F(CodeCompletionTest, TypedDirected) {
135104
cantFail(Interp->Parse("int application = 12;"));
136105
cantFail(Interp->Parse("char apple = '2';"));
137106
cantFail(Interp->Parse("void add(int &SomeInt){}"));
138107
{
139108
auto Err = llvm::Error::success();
140-
auto comps = runComp(*Interp, std::string("add("), Err);
109+
auto comps = runComp(std::string("add("), Err);
141110
EXPECT_EQ((size_t)1, comps.size());
142111
EXPECT_EQ((bool)Err, false);
143112
}
@@ -146,7 +115,7 @@ TEST(CodeCompletionTest, TypedDirected) {
146115

147116
{
148117
auto Err = llvm::Error::success();
149-
auto comps = runComp(*Interp, std::string("add("), Err);
118+
auto comps = runComp(std::string("add("), Err);
150119
EXPECT_EQ((size_t)2, comps.size());
151120
EXPECT_EQ(comps[0], "application");
152121
EXPECT_EQ(comps[1], "banana");
@@ -155,156 +124,107 @@ TEST(CodeCompletionTest, TypedDirected) {
155124

156125
{
157126
auto Err = llvm::Error::success();
158-
auto comps = runComp(*Interp, std::string("add(b"), Err);
127+
auto comps = runComp(std::string("add(b"), Err);
159128
EXPECT_EQ((size_t)1, comps.size());
160129
EXPECT_EQ(comps[0], "banana");
161130
EXPECT_EQ((bool)Err, false);
162131
}
163132
}
164133

165-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
166-
TEST(CodeCompletionTest, DISABLED_SanityClasses) {
167-
#else
168-
TEST(CodeCompletionTest, SanityClasses) {
169-
#endif
170-
if (!HostSupportsJit())
171-
GTEST_SKIP();
172-
auto Interp = createInterpreter();
134+
TEST_F(CodeCompletionTest, SanityClasses) {
173135
cantFail(Interp->Parse("struct Apple{};"));
174136
cantFail(Interp->Parse("void takeApple(Apple &a1){}"));
175137
cantFail(Interp->Parse("Apple a1;"));
176138
cantFail(Interp->Parse("void takeAppleCopy(Apple a1){}"));
177139

178140
{
179141
auto Err = llvm::Error::success();
180-
auto comps = runComp(*Interp, "takeApple(", Err);
142+
auto comps = runComp("takeApple(", Err);
181143
EXPECT_EQ((size_t)1, comps.size());
182144
EXPECT_EQ(comps[0], std::string("a1"));
183145
EXPECT_EQ((bool)Err, false);
184146
}
185147
{
186148
auto Err = llvm::Error::success();
187-
auto comps = runComp(*Interp, std::string("takeAppleCopy("), Err);
149+
auto comps = runComp(std::string("takeAppleCopy("), Err);
188150
EXPECT_EQ((size_t)1, comps.size());
189151
EXPECT_EQ(comps[0], std::string("a1"));
190152
EXPECT_EQ((bool)Err, false);
191153
}
192154
}
193155

194-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
195-
TEST(CodeCompletionTest, DISABLED_SubClassing) {
196-
#else
197-
TEST(CodeCompletionTest, SubClassing) {
198-
#endif
199-
if (!HostSupportsJit())
200-
GTEST_SKIP();
201-
auto Interp = createInterpreter();
156+
TEST_F(CodeCompletionTest, SubClassing) {
202157
cantFail(Interp->Parse("struct Fruit {};"));
203158
cantFail(Interp->Parse("struct Apple : Fruit{};"));
204159
cantFail(Interp->Parse("void takeFruit(Fruit &f){}"));
205160
cantFail(Interp->Parse("Apple a1;"));
206161
cantFail(Interp->Parse("Fruit f1;"));
207162
auto Err = llvm::Error::success();
208-
auto comps = runComp(*Interp, std::string("takeFruit("), Err);
163+
auto comps = runComp(std::string("takeFruit("), Err);
209164
EXPECT_EQ((size_t)2, comps.size());
210165
EXPECT_EQ(comps[0], std::string("a1"));
211166
EXPECT_EQ(comps[1], std::string("f1"));
212167
EXPECT_EQ((bool)Err, false);
213168
}
214169

215-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
216-
TEST(CodeCompletionTest, DISABLED_MultipleArguments) {
217-
#else
218-
TEST(CodeCompletionTest, MultipleArguments) {
219-
#endif
220-
if (!HostSupportsJit())
221-
GTEST_SKIP();
222-
auto Interp = createInterpreter();
170+
TEST_F(CodeCompletionTest, MultipleArguments) {
223171
cantFail(Interp->Parse("int foo = 42;"));
224172
cantFail(Interp->Parse("char fowl = 'A';"));
225173
cantFail(Interp->Parse("void takeTwo(int &a, char b){}"));
226174
auto Err = llvm::Error::success();
227-
auto comps = runComp(*Interp, std::string("takeTwo(foo, "), Err);
175+
auto comps = runComp(std::string("takeTwo(foo, "), Err);
228176
EXPECT_EQ((size_t)1, comps.size());
229177
EXPECT_EQ(comps[0], std::string("fowl"));
230178
EXPECT_EQ((bool)Err, false);
231179
}
232180

233-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
234-
TEST(CodeCompletionTest, DISABLED_Methods) {
235-
#else
236-
TEST(CodeCompletionTest, Methods) {
237-
#endif
238-
if (!HostSupportsJit())
239-
GTEST_SKIP();
240-
auto Interp = createInterpreter();
181+
TEST_F(CodeCompletionTest, Methods) {
241182
cantFail(Interp->Parse(
242183
"struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
243184
cantFail(Interp->Parse("Foo f1;"));
244185

245186
auto Err = llvm::Error::success();
246-
auto comps = runComp(*Interp, std::string("f1."), Err);
187+
auto comps = runComp(std::string("f1."), Err);
247188
EXPECT_EQ((size_t)2, comps.size());
248189
EXPECT_EQ(comps[0], std::string("add"));
249190
EXPECT_EQ(comps[1], std::string("par"));
250191
EXPECT_EQ((bool)Err, false);
251192
}
252193

253-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
254-
TEST(CodeCompletionTest, DISABLED_MethodsInvocations) {
255-
#else
256-
TEST(CodeCompletionTest, MethodsInvocations) {
257-
#endif
258-
if (!HostSupportsJit())
259-
GTEST_SKIP();
260-
auto Interp = createInterpreter();
194+
TEST_F(CodeCompletionTest, MethodsInvocations) {
261195
cantFail(Interp->Parse(
262196
"struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
263197
cantFail(Interp->Parse("Foo f1;"));
264198
cantFail(Interp->Parse("int a = 84;"));
265199

266200
auto Err = llvm::Error::success();
267-
auto comps = runComp(*Interp, std::string("f1.add("), Err);
201+
auto comps = runComp(std::string("f1.add("), Err);
268202
EXPECT_EQ((size_t)1, comps.size());
269203
EXPECT_EQ(comps[0], std::string("a"));
270204
EXPECT_EQ((bool)Err, false);
271205
}
272206

273-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
274-
TEST(CodeCompletionTest, DISABLED_NestedInvocations) {
275-
#else
276-
TEST(CodeCompletionTest, NestedInvocations) {
277-
#endif
278-
if (!HostSupportsJit())
279-
GTEST_SKIP();
280-
auto Interp = createInterpreter();
207+
TEST_F(CodeCompletionTest, NestedInvocations) {
281208
cantFail(Interp->Parse(
282209
"struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
283210
cantFail(Interp->Parse("Foo f1;"));
284211
cantFail(Interp->Parse("int a = 84;"));
285212
cantFail(Interp->Parse("int plus(int a, int b) { return a + b; }"));
286213

287214
auto Err = llvm::Error::success();
288-
auto comps = runComp(*Interp, std::string("plus(42, f1.add("), Err);
215+
auto comps = runComp(std::string("plus(42, f1.add("), Err);
289216
EXPECT_EQ((size_t)1, comps.size());
290217
EXPECT_EQ(comps[0], std::string("a"));
291218
EXPECT_EQ((bool)Err, false);
292219
}
293220

294-
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
295-
TEST(CodeCompletionTest, DISABLED_TemplateFunctions) {
296-
#else
297-
TEST(CodeCompletionTest, TemplateFunctions) {
298-
#endif
299-
if (!HostSupportsJit())
300-
GTEST_SKIP();
301-
auto Interp = createInterpreter();
221+
TEST_F(CodeCompletionTest, TemplateFunctions) {
302222
cantFail(
303223
Interp->Parse("template <typename T> T id(T a) { return a;} "));
304224
cantFail(Interp->Parse("int apple = 84;"));
305225
{
306226
auto Err = llvm::Error::success();
307-
auto comps = runComp(*Interp, std::string("id<int>("), Err);
227+
auto comps = runComp(std::string("id<int>("), Err);
308228
EXPECT_EQ((size_t)1, comps.size());
309229
EXPECT_EQ(comps[0], std::string("apple"));
310230
EXPECT_EQ((bool)Err, false);
@@ -315,7 +235,7 @@ TEST(CodeCompletionTest, TemplateFunctions) {
315235
cantFail(Interp->Parse("char pear = '4';"));
316236
{
317237
auto Err = llvm::Error::success();
318-
auto comps = runComp(*Interp, std::string("pickFirst(apple, "), Err);
238+
auto comps = runComp(std::string("pickFirst(apple, "), Err);
319239
EXPECT_EQ((size_t)1, comps.size());
320240
EXPECT_EQ(comps[0], std::string("apple"));
321241
EXPECT_EQ((bool)Err, false);

0 commit comments

Comments
 (0)