[clangd] Add missing unittests to build graph
authorKadir Cetinkaya <kadircet@google.com>
Mon, 13 Mar 2023 11:04:05 +0000 (12:04 +0100)
committerKadir Cetinkaya <kadircet@google.com>
Mon, 13 Mar 2023 12:06:31 +0000 (13:06 +0100)
Also fix tests

Differential Revision: https://reviews.llvm.org/D145921

clang-tools-extra/clangd/refactor/tweaks/SpecialMembers.cpp
clang-tools-extra/clangd/unittests/CMakeLists.txt
clang-tools-extra/clangd/unittests/support/FileCacheTests.cpp
clang-tools-extra/clangd/unittests/tweaks/SpecialMembersTests.cpp

index e15605191c752ea8a87f2b8030c1e66a298b26eb..0b86b48422707170d718daedab888927f62ef873 100644 (file)
@@ -8,10 +8,7 @@
 #include "ParsedAST.h"
 #include "refactor/InsertionPoint.h"
 #include "refactor/Tweak.h"
-#include "support/Logger.h"
 #include "clang/AST/DeclCXX.h"
-#include "clang/Basic/SourceLocation.h"
-#include "clang/Basic/SourceManager.h"
 #include "clang/Sema/Sema.h"
 #include "clang/Tooling/Core/Replacement.h"
 #include "llvm/ADT/StringRef.h"
@@ -84,7 +81,7 @@ std::string buildSpecialMemberDeclarations(const CXXRecordDecl &Class) {
 //  - to understand the implicit behavior
 //  - to avoid relying on the implicit behavior
 //  - as a baseline for explicit modification
-class DeclareCopyMove : public Tweak {
+class SpecialMembers : public Tweak {
 public:
   const char *id() const final;
   llvm::StringLiteral kind() const override {
@@ -103,7 +100,7 @@ public:
     // Trigger only on class definitions.
     if (auto *N = Inputs.ASTSelection.commonAncestor())
       Class = const_cast<CXXRecordDecl *>(N->ASTNode.get<CXXRecordDecl>());
-    if (!Class || !Class->isThisDeclarationADefinition())
+    if (!Class || !Class->isThisDeclarationADefinition() || Class->isUnion())
       return false;
 
     // Tweak is only available if some members are missing.
@@ -146,7 +143,7 @@ private:
   bool NeedCopy = false, NeedMove = false;
   CXXRecordDecl *Class = nullptr;
 };
-REGISTER_TWEAK(DeclareCopyMove)
+REGISTER_TWEAK(SpecialMembers)
 
 } // namespace
 } // namespace clangd
index a68d8cb0b78ff781013af3c721b0f21eede54bb2..39fd6ee85378d3abba3d0d2fd399c4f195d88aef 100644 (file)
@@ -104,12 +104,13 @@ add_unittest(ClangdUnitTests ClangdTests
 
   support/CancellationTests.cpp
   support/ContextTests.cpp
+  support/FileCacheTests.cpp
   support/FunctionTests.cpp
   support/MarkupTests.cpp
   support/MemoryTreeTests.cpp
   support/PathTests.cpp
-  support/ThreadingTests.cpp
   support/TestTracer.cpp
+  support/ThreadingTests.cpp
   support/TraceTests.cpp
 
   tweaks/AddUsingTests.cpp
@@ -130,6 +131,7 @@ add_unittest(ClangdUnitTests ClangdTests
   tweaks/RawStringLiteralTests.cpp
   tweaks/RemoveUsingNamespaceTests.cpp
   tweaks/ShowSelectionTreeTests.cpp
+  tweaks/SpecialMembersTests.cpp
   tweaks/SwapIfBranchesTests.cpp
   tweaks/TweakTesting.cpp
   tweaks/TweakTests.cpp
index 840cad623d776d31cb2ae3050946cd6023830c3c..3cdaceb5cf23c653b137cef5ea74ff5278d92585 100644 (file)
@@ -11,9 +11,9 @@
 #include "TestFS.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
-#include <atomic>
 #include <chrono>
 #include <optional>
+#include <utility>
 
 namespace clang {
 namespace clangd {
@@ -34,10 +34,10 @@ public:
       FS.Files.erase(testPath("foo.cc"));
   }
 
-  std::string get(std::chrono::steady_clock::time_point FreshTime,
-                  bool ExpectParse) const {
+  std::pair<std::string, /*Parsed=*/bool>
+  get(std::chrono::steady_clock::time_point FreshTime) const {
     bool GotParse = false;
-    bool GotRead;
+    bool GotRead = false;
     std::string Result;
     read(
         FS, FreshTime,
@@ -49,12 +49,14 @@ public:
           GotRead = true;
           Result = Value;
         });
-    EXPECT_EQ(GotParse, ExpectParse);
     EXPECT_TRUE(GotRead);
-    return Result;
+    return {Result, GotParse};
   }
 };
 
+MATCHER_P(Parsed, Value, "") { return arg.second && arg.first == Value; }
+MATCHER_P(Cached, Value, "") { return !arg.second && arg.first == Value; }
+
 TEST(FileCacheTest, Invalidation) {
   TestCache C;
 
@@ -62,20 +64,20 @@ TEST(FileCacheTest, Invalidation) {
   auto MustBeFresh = StaleOK + std::chrono::hours(1);
 
   C.setContents("a");
-  EXPECT_EQ("a", C.get(StaleOK, /*ExpectParse=*/true)) << "Parsed first time";
-  EXPECT_EQ("a", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)";
-  EXPECT_EQ("a", C.get(MustBeFresh, /*ExpectParse=*/false)) << "Cached (stat)";
+  EXPECT_THAT(C.get(StaleOK), Parsed("a")) << "Parsed first time";
+  EXPECT_THAT(C.get(StaleOK), Cached("a")) << "Cached (time)";
+  EXPECT_THAT(C.get(MustBeFresh), Cached("a")) << "Cached (stat)";
   C.setContents("bb");
-  EXPECT_EQ("a", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)";
-  EXPECT_EQ("bb", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Size changed";
-  EXPECT_EQ("bb", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Cached (stat)";
+  EXPECT_THAT(C.get(StaleOK), Cached("a")) << "Cached (time)";
+  EXPECT_THAT(C.get(MustBeFresh), Parsed("bb")) << "Size changed";
+  EXPECT_THAT(C.get(MustBeFresh), Cached("bb")) << "Cached (stat)";
   C.setContents(nullptr);
-  EXPECT_EQ("bb", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)";
-  EXPECT_EQ("", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Stat failed";
-  EXPECT_EQ("", C.get(MustBeFresh, /*ExpectParse=*/false)) << "Cached (404)";
+  EXPECT_THAT(C.get(StaleOK), Cached("bb")) << "Cached (time)";
+  EXPECT_THAT(C.get(MustBeFresh), Parsed("")) << "stat failed";
+  EXPECT_THAT(C.get(MustBeFresh), Cached("")) << "Cached (404)";
   C.setContents("bb"); // Match the previous stat values!
-  EXPECT_EQ("", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)";
-  EXPECT_EQ("bb", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Size changed";
+  EXPECT_THAT(C.get(StaleOK), Cached("")) << "Cached (time)";
+  EXPECT_THAT(C.get(MustBeFresh), Parsed("bb")) << "Size changed";
 }
 
 } // namespace
index 7fcaf41b426b1b4aafdb420634868f3c6de320f6..eeb3f1d0fe4eb08df4bd19ccab56f9fbfe2e42e4 100644 (file)
@@ -7,8 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "TweakTesting.h"
-#include "gmock/gmock-matchers.h"
-#include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
 namespace clang {
@@ -27,17 +25,17 @@ TEST_F(SpecialMembersTest, Test) {
                      "S &operator=(S&&); S &operator=(const S&);"
                      "};");
 
-  const char *Output = R"cpp(struct S{S(const S &) = default;
-  S(S &&) = default;
-  S &operator=(const S &) = default;
-  S &operator=(S &&) = default;
+  const char *Output = R"cpp(struct S{S(const S&) = default;
+S(S&&) = default;
+S &operator=(const S&) = default;
+S &operator=(S&&) = default;
 };)cpp";
   EXPECT_EQ(apply("struct ^S{};"), Output);
 
-  Output = R"cpp(struct S{S(const S &) = default;
-S(S &&) = default;
-S &operator=(const S &) = delete;
-S &operator=(S &&) = delete;
+  Output = R"cpp(struct S{S(const S&) = default;
+S(S&&) = default;
+S &operator=(const S&) = delete;
+S &operator=(S&&) = delete;
 int& ref;};)cpp";
   EXPECT_EQ(apply("struct ^S{int& ref;};"), Output);
 }