Use correct LLVM gtest/gmock for inference tests Define a EqualsProto matcher that's missing from public gmock PiperOrigin-RevId: 543694206
diff --git a/nullability/BUILD b/nullability/BUILD index 8fc6c50..55e37e0 100644 --- a/nullability/BUILD +++ b/nullability/BUILD
@@ -147,3 +147,16 @@ "@llvm-project//third-party/unittest:gtest_main", ], ) + +cc_library( + name = "proto_matchers", + testonly = 1, + srcs = ["proto_matchers.cc"], + hdrs = ["proto_matchers.h"], + visibility = [":__subpackages__"], + deps = [ + "//third_party/protobuf", + "@llvm-project//llvm:Support", + "@llvm-project//third-party/unittest:gmock", + ], +)
diff --git a/nullability/inference/BUILD b/nullability/inference/BUILD index 67af447..5071479 100644 --- a/nullability/inference/BUILD +++ b/nullability/inference/BUILD
@@ -31,12 +31,15 @@ ":analyze_target_for_test", ":infer_nullability_constraints", ":inference_cc_proto", + "//nullability:proto_matchers", "@absl//absl/log:check", - "@com_google_googletest//:gtest_main", "@llvm-project//clang:analysis", "@llvm-project//clang:ast", "@llvm-project//clang:ast_matchers", "@llvm-project//llvm:Support", + "@llvm-project//third-party/unittest:gmock", + "@llvm-project//third-party/unittest:gtest", + "@llvm-project//third-party/unittest:gtest_main", ], ) @@ -88,10 +91,13 @@ ":inference_cc_proto", ":resolve_constraints", "//nullability:pointer_nullability", - "@com_google_googletest//:gtest_main", + "//nullability:proto_matchers", "@llvm-project//clang:analysis", "@llvm-project//clang:ast", "@llvm-project//llvm:Support", + "@llvm-project//third-party/unittest:gmock", + "@llvm-project//third-party/unittest:gtest", + "@llvm-project//third-party/unittest:gtest_main", ], ) @@ -105,12 +111,14 @@ "//nullability:pointer_nullability_analysis", "//nullability:pointer_nullability_lattice", "@absl//absl/log:check", - "@com_google_googletest//:gtest_main", "@llvm-project//clang:analysis", "@llvm-project//clang:ast", "@llvm-project//clang:ast_matchers", "@llvm-project//clang:basic", "@llvm-project//llvm:Support", + "@llvm-project//third-party/unittest:gmock", + "@llvm-project//third-party/unittest:gtest", + "@llvm-project//third-party/unittest:gtest_main", ], )
diff --git a/nullability/inference/infer_nullability_constraints_test.cc b/nullability/inference/infer_nullability_constraints_test.cc index 3433760..ce0543b 100644 --- a/nullability/inference/infer_nullability_constraints_test.cc +++ b/nullability/inference/infer_nullability_constraints_test.cc
@@ -7,11 +7,10 @@ #include <string> #include <utility> -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "absl/log/check.h" #include "nullability/inference/analyze_target_for_test.h" #include "nullability/inference/inference.proto.h" +#include "nullability/proto_matchers.h" #include "clang/AST/Decl.h" #include "clang/AST/Expr.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -19,12 +18,13 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/STLFunctionalExtras.h" #include "llvm/ADT/StringRef.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h" namespace clang::tidy::nullability { namespace { using ::clang::ast_matchers::MatchFinder; -using ::testing::EqualsProto; using ::testing::IsEmpty; using ::testing::Pair; using ::testing::UnorderedElementsAre; @@ -56,8 +56,8 @@ return false; } - return testing::ExplainMatchResult(testing::StrEq(Name), - Identifier->getName(), result_listener); + return testing::ExplainMatchResult(Name, Identifier->getName(), + result_listener); } TEST(InferAnnotationsTest, NoParams) {
diff --git a/nullability/inference/resolve_constraints_test.cc b/nullability/inference/resolve_constraints_test.cc index 0e3d98f..9e7cce1 100644 --- a/nullability/inference/resolve_constraints_test.cc +++ b/nullability/inference/resolve_constraints_test.cc
@@ -6,10 +6,9 @@ #include <memory> -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "nullability/inference/inference.proto.h" #include "nullability/pointer_nullability.h" +#include "nullability/proto_matchers.h" #include "clang/AST/Type.h" #include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h" #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" @@ -17,10 +16,11 @@ #include "clang/Analysis/FlowSensitive/Value.h" #include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h" #include "llvm/ADT/DenseSet.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h" namespace clang::tidy::nullability { namespace { -using ::testing::EqualsProto; class ResolveConstraintsTest : public testing::Test { public: @@ -58,8 +58,7 @@ clang::dataflow::BoolValue &NotAtom1 = Environment.makeNot(Atom1); const llvm::DenseSet<clang::dataflow::BoolValue *> Constraints = {&Atom1, &NotAtom1}; - EXPECT_THAT(resolveConstraints(Constraints, Pointer), - EqualsProto(NullabilityConstraint::default_instance())); + EXPECT_THAT(resolveConstraints(Constraints, Pointer), EqualsProto("")); } TEST_F(ResolveConstraintsTest, NotIsNullConstraintImpliesNonNull) {
diff --git a/nullability/inference/safety_constraint_generator_test.cc b/nullability/inference/safety_constraint_generator_test.cc index 7412f85..4011296 100644 --- a/nullability/inference/safety_constraint_generator_test.cc +++ b/nullability/inference/safety_constraint_generator_test.cc
@@ -8,8 +8,6 @@ #include <optional> #include <vector> -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "absl/log/check.h" #include "nullability/inference/analyze_target_for_test.h" #include "nullability/pointer_nullability.h" @@ -27,6 +25,8 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h" namespace clang::tidy::nullability { namespace {
diff --git a/nullability/proto_matchers.cc b/nullability/proto_matchers.cc new file mode 100644 index 0000000..4a94944 --- /dev/null +++ b/nullability/proto_matchers.cc
@@ -0,0 +1,53 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "nullability/proto_matchers.h" + +#include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" +#include "third_party/protobuf/text_format.h" + +namespace clang::tidy::nullability { +namespace { + +class EqualsProtoMatcher + : public testing::MatcherInterface<const proto2::Message &> { + std::string Expected; + + public: + EqualsProtoMatcher(llvm::StringRef Expected) : Expected(Expected) {} + + bool MatchAndExplain(const proto2::Message &M, + testing::MatchResultListener *Listener) const override { + std::unique_ptr<proto2::Message> Parsed(M.New()); + if (!proto2::TextFormat::ParseFromString(Expected, Parsed.get())) { + *Listener << "where <<<\n" + << Expected << "\n>>> doesn't parse as " << M.GetTypeName(); + return false; + } + // Compare textual representations. + std::string PrintedExpected, PrintedActual; + if (!proto2::TextFormat::PrintToString(*Parsed, &PrintedExpected)) { + *Listener << "where expected message failed to print!"; + return false; + } + if (!proto2::TextFormat::PrintToString(M, &PrintedActual)) { + *Listener << "where actual message failed to print!"; + return false; + } + return testing::ExplainMatchResult(PrintedExpected, PrintedActual, + Listener); + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "equals proto <<<\n" << Expected << "\n>>>"; + } +}; + +} // namespace + +testing::Matcher<const proto2::Message &> EqualsProto(llvm::StringRef Textual) { + return testing::MakeMatcher(new EqualsProtoMatcher(Textual)); +} + +} // namespace clang::tidy::nullability
diff --git a/nullability/proto_matchers.h b/nullability/proto_matchers.h new file mode 100644 index 0000000..0e63a5f --- /dev/null +++ b/nullability/proto_matchers.h
@@ -0,0 +1,18 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef CRUBIT_NULLABILITY_PROTO_MATCHERS_H_ +#define CRUBIT_NULLABILITY_PROTO_MATCHERS_H_ + +#include "llvm/ADT/StringRef.h" +#include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" +#include "third_party/protobuf/message.h" + +namespace clang::tidy::nullability { + +testing::Matcher<const proto2::Message&> EqualsProto(llvm::StringRef Textual); + +} // namespace clang::tidy::nullability + +#endif