1 //===-- MemberwiseConstructorTests.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "TweakTesting.h"
10 #include "gmock/gmock-matchers.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
19 using testing::HasSubstr
;
22 TWEAK_TEST(MemberwiseConstructor
);
24 TEST_F(MemberwiseConstructorTest
, Availability
) {
25 EXPECT_AVAILABLE("^struct ^S ^{ int x, y; };");
26 // Verify no crashes on incomplete member fields.
27 EXPECT_UNAVAILABLE("/*error-ok*/class Forward; class ^A { Forward f;}");
28 EXPECT_UNAVAILABLE("struct S { ^int ^x, y; }; struct ^S;");
29 EXPECT_UNAVAILABLE("struct ^S {};");
30 EXPECT_UNAVAILABLE("union ^S { int x; };");
31 EXPECT_UNAVAILABLE("struct ^S { int x = 0; };");
32 EXPECT_UNAVAILABLE("struct ^S { struct { int x; }; };");
33 EXPECT_UNAVAILABLE("struct ^{ int x; } e;");
36 TEST_F(MemberwiseConstructorTest
, Edits
) {
39 Move(Move&&) = default;
40 Move(const Move&) = delete;
43 Copy(Copy&&) = delete;
47 EXPECT_EQ(apply("struct ^S{Move M; Copy C; int I; int J=4;};"),
49 "S(Move M, const Copy &C, int I) : M(std::move(M)), C(C), I(I) {}\n"
50 "Move M; Copy C; int I; int J=4;};");
53 TEST_F(MemberwiseConstructorTest
, FieldTreatment
) {
56 MoveOnly(MoveOnly&&) = default;
57 MoveOnly(const MoveOnly&) = delete;
60 CopyOnly(CopyOnly&&) = delete;
61 CopyOnly(const CopyOnly&);
64 CopyTrivial(CopyTrivial&&) = default;
65 CopyTrivial(const CopyTrivial&) = default;
68 Immovable(Immovable&&) = delete;
69 Immovable(const Immovable&) = delete;
72 struct Traits { using Type = typename T::Type; };
76 auto Fail
= Eq("unavailable");
77 auto Move
= HasSubstr(": Member(std::move(Member))");
78 auto CopyRef
= AllOf(HasSubstr("S(const "), HasSubstr(": Member(Member)"));
79 auto Copy
= AllOf(Not(HasSubstr("S(const ")), HasSubstr(": Member(Member)"));
80 auto With
= [](llvm::StringRef Type
) {
81 return ("struct ^S { " + Type
+ " Member; };").str();
84 EXPECT_THAT(apply(With("Immovable")), Fail
);
85 EXPECT_THAT(apply(With("MoveOnly")), Move
);
86 EXPECT_THAT(apply(With("CopyOnly")), CopyRef
);
87 EXPECT_THAT(apply(With("CopyTrivial")), Copy
);
88 EXPECT_THAT(apply(With("int")), Copy
);
89 EXPECT_THAT(apply(With("IntAlias")), Copy
);
90 EXPECT_THAT(apply(With("Immovable*")), Copy
);
91 EXPECT_THAT(apply(With("Immovable&")), Copy
);
93 EXPECT_THAT(apply("template <typename T>" + With("T")), Move
);
94 EXPECT_THAT(apply("template <typename T>" + With("typename Traits<T>::Type")),
96 EXPECT_THAT(apply("template <typename T>" + With("T*")), Copy
);
100 } // namespace clangd