11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
20TEST_F(DefineOutlineTest, TriggersOnFunctionDecl) {
24 [[void [[f^o^o]]() [[{
34 [[vo^id ]]foo[[()]] {[[
41 [[void [[f^o^o]]() [[{
51 [[void [[Bar::[[b^a^z]]]]() [[{
58 [[void [[f^o^o^]]() [[{ return; }]]]]
62 [[void [[f^o^o]]() [[{
70 F^oo(const Foo&) = delete;
76 template <typename> struct Foo { void fo^o(){} };
82 template <typename> void fo^o() {};
83 template <> void fo^o<int>() {};
89 struct { void b^ar() {} } Bar;
98 struct Bar { void b^ar() {} };
113TEST_F(DefineOutlineTest, FailsWithoutSource) {
115 llvm::StringRef Test =
"void fo^o() { return; }";
117 "fail: Couldn't find a suitable implementation file.";
121TEST_F(DefineOutlineTest, ApplyTest) {
122 llvm::StringMap<std::string> EditedFiles;
123 ExtraFiles[
"Test.cpp"] =
"";
127 llvm::StringRef Test;
128 llvm::StringRef ExpectedHeader;
129 llvm::StringRef ExpectedSource;
133 "void fo^o() { return; }",
135 "void foo() { return; }",
139 "inline void fo^o() { return; }",
141 " void foo() { return; }",
145 "void fo^o(int x, int y = 5, int = 2, int (*foo)(int) = nullptr) {}",
146 "void foo(int x, int y = 5, int = 2, int (*foo)(int) = nullptr) ;",
147 "void foo(int x, int y , int , int (*foo)(int) ) {}",
150 "struct Bar{Bar();}; void fo^o(Bar x = {}) {}",
151 "struct Bar{Bar();}; void foo(Bar x = {}) ;",
152 "void foo(Bar x ) {}",
157 class Foo {public: Foo(); Foo(int);};
160 Bar(int x) : f1(x) {}
165 class Foo {public: Foo(); Foo(int);};
168 Bar(int x) : f1(x) {}
177 class Foo {public: Foo(); Foo(int);};
180 B^ar(int x) : f1(x), f2(3) {}
185 class Foo {public: Foo(); Foo(int);};
192 "Bar::Bar(int x) : f1(x), f2(3) {}\n",
198 F^oo(int z) __attribute__((weak)) : bar(2){}
203 Foo(int z) __attribute__((weak)) ;
206 "Foo::Foo(int z) __attribute__((weak)) : bar(2){}\n",
212 virtual void f^oo() {}
218 " void A::foo() {}\n",
223 virtual virtual void virtual f^oo() {}
227 virtual virtual void virtual foo() ;
229 " void A::foo() {}\n",
234 virtual void foo() = 0;
237 void fo^o() override {}
241 virtual void foo() = 0;
244 void foo() override ;
246 "void B::foo() {}\n",
251 virtual void foo() = 0;
258 virtual void foo() = 0;
263 "void B::foo() {}\n",
268 virtual void foo() = 0;
271 void fo^o() final override {}
275 virtual void foo() = 0;
278 void foo() final override ;
280 "void B::foo() {}\n",
285 static void fo^o() {}
291 " void A::foo() {}\n",
296 static static void fo^o() {}
300 static static void foo() ;
302 " void A::foo() {}\n",
307 explicit Fo^o(int) {}
313 " Foo::Foo(int) {}\n",
318 explicit explicit Fo^o(int) {}
322 explicit explicit Foo(int) ;
324 " Foo::Foo(int) {}\n",
329 inline void f^oo(int) {}
335 " void A::foo(int) {}\n",
339 "class A { ~A^(){} };",
340 "class A { ~A(); };",
344 for (
const auto &Case : Cases) {
345 SCOPED_TRACE(Case.Test);
346 EXPECT_EQ(apply(Case.Test, &EditedFiles), Case.ExpectedHeader);
347 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
348 testPath(
"Test.cpp"), Case.ExpectedSource)));
352TEST_F(DefineOutlineTest, HandleMacros) {
353 llvm::StringMap<std::string> EditedFiles;
354 ExtraFiles[
"Test.cpp"] =
"";
356 ExtraArgs.push_back(
"-DVIRTUAL=virtual");
357 ExtraArgs.push_back(
"-DOVER=override");
360 llvm::StringRef Test;
361 llvm::StringRef ExpectedHeader;
362 llvm::StringRef ExpectedSource;
365 #define BODY { return; }
366 void f^oo()BODY)cpp",
368 #define BODY { return; }
374 void f^oo(){BODY})cpp",
381 #define TARGET void foo()
382 [[TARGET]]{ return; })cpp",
384 #define TARGET void foo()
386 "TARGET{ return; }"},
390 void [[TARGET]](){ return; })cpp",
394 "void TARGET(){ return; }"},
395 {R
"cpp(#define VIRT virtual
399 R"cpp(#define VIRT virtual
403 " void A::foo() {}\n"},
406 VIRTUAL void f^oo() {}
412 " void A::foo() {}\n"},
415 virtual void foo() = 0;
422 virtual void foo() = 0;
427 "void B::foo() {}\n"},
428 {R
"cpp(#define STUPID_MACRO(X) virtual
430 STUPID_MACRO(sizeof sizeof int) void f^oo() {}
432 R"cpp(#define STUPID_MACRO(X) virtual
434 STUPID_MACRO(sizeof sizeof int) void foo() ;
436 " void A::foo() {}\n"},
437 {R
"cpp(#define STAT static
441 R"cpp(#define STAT static
445 " void A::foo() {}\n"},
446 {R
"cpp(#define STUPID_MACRO(X) static
448 STUPID_MACRO(sizeof sizeof int) void f^oo() {}
450 R"cpp(#define STUPID_MACRO(X) static
452 STUPID_MACRO(sizeof sizeof int) void foo() ;
454 " void A::foo() {}\n"},
456 for (
const auto &Case : Cases) {
457 SCOPED_TRACE(Case.Test);
458 EXPECT_EQ(apply(Case.Test, &EditedFiles), Case.ExpectedHeader);
459 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
460 testPath(
"Test.cpp"), Case.ExpectedSource)));
464TEST_F(DefineOutlineTest, QualifyReturnValue) {
466 ExtraFiles[
"Test.cpp"] =
"";
469 llvm::StringRef Test;
470 llvm::StringRef ExpectedHeader;
471 llvm::StringRef ExpectedSource;
474 namespace a { class Foo{}; }
476 Foo fo^o() { return {}; })cpp",
478 namespace a { class Foo{}; }
481 "a::Foo foo() { return {}; }"},
486 Bar fo^o() { return {}; }
496 "a::Foo::Bar a::Foo::foo() { return {}; }\n"},
499 Foo fo^o() { return {}; })cpp",
503 "Foo foo() { return {}; }"},
505 llvm::StringMap<std::string> EditedFiles;
506 for (
auto &Case : Cases) {
507 apply(Case.Test, &EditedFiles);
508 EXPECT_EQ(apply(Case.Test, &EditedFiles), Case.ExpectedHeader);
509 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
510 testPath(
"Test.cpp"), Case.ExpectedSource)));
514TEST_F(DefineOutlineTest, QualifyFunctionName) {
517 llvm::StringRef TestHeader;
518 llvm::StringRef TestSource;
519 llvm::StringRef ExpectedHeader;
520 llvm::StringRef ExpectedSource;
540 "void a::b::Foo::foo() {}\n",
543 "namespace a { namespace b { void f^oo() {} } }",
545 "namespace a { namespace b { void foo() ; } }",
546 "namespace a{void b::foo() {} }",
549 "namespace a { namespace b { void f^oo() {} } }",
550 "using namespace a;",
551 "namespace a { namespace b { void foo() ; } }",
554 "using namespace a;void a::b::foo() {} ",
557 "namespace a { class A { ~A^(){} }; }",
559 "namespace a { class A { ~A(); }; }",
563 "namespace a { class A { ~A^(){} }; }",
565 "namespace a { class A { ~A(); }; }",
566 "namespace a{A::~A(){} }",
569 llvm::StringMap<std::string> EditedFiles;
570 for (
auto &Case : Cases) {
571 ExtraFiles[
"Test.cpp"] = std::string(Case.TestSource);
572 EXPECT_EQ(apply(Case.TestHeader, &EditedFiles), Case.ExpectedHeader);
573 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
574 testPath(
"Test.cpp"), Case.ExpectedSource)))
579TEST_F(DefineOutlineTest, FailsMacroSpecifier) {
581 ExtraFiles[
"Test.cpp"] =
"";
582 ExtraArgs.push_back(
"-DFINALOVER=final override");
584 std::pair<StringRef, StringRef> Cases[] = {
587 #define VIRT virtual void
591 "fail: define outline: couldn't remove `virtual` keyword."},
594 #define OVERFINAL final override
596 virtual void foo() {}
599 void fo^o() OVERFINAL {}
601 "fail: define outline: Can't move out of line as function has a "
602 "macro `override` specifier.\ndefine outline: Can't move out of line "
603 "as function has a macro `final` specifier."},
607 virtual void foo() {}
610 void fo^o() FINALOVER {}
612 "fail: define outline: Can't move out of line as function has a "
613 "macro `override` specifier.\ndefine outline: Can't move out of line "
614 "as function has a macro `final` specifier."},
616 for (
const auto &Case : Cases) {
617 EXPECT_EQ(apply(Case.first), Case.second);
std::vector< const char * > Expected
#define TWEAK_TEST(TweakID)
#define EXPECT_AVAILABLE(MarkedCode)
#define EXPECT_UNAVAILABLE(MarkedCode)
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
std::string testPath(PathRef File, llvm::sys::path::Style Style)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//