xref: /llvm-project/clang-tools-extra/clangd/unittests/tweaks/ScopifyEnumTests.cpp (revision f2daa32fcaa061daa04546c343198d876e635def)
1 //===-- DefineOutline.cpp ---------------------------------------*- C++ -*-===//
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 #include "TweakTesting.h"
10 #include "gtest/gtest.h"
11 
12 namespace clang::clangd {
13 namespace {
14 
15 TWEAK_TEST(ScopifyEnum);
16 
TEST_F(ScopifyEnumTest,TriggersOnUnscopedEnumDecl)17 TEST_F(ScopifyEnumTest, TriggersOnUnscopedEnumDecl) {
18   FileName = "Test.hpp";
19   // Not available for scoped enum.
20   EXPECT_UNAVAILABLE(R"cpp(enum class ^E { V };)cpp");
21 
22   // Not available for non-definition.
23   EXPECT_UNAVAILABLE(R"cpp(
24 enum E { V };
25 enum ^E;
26 )cpp");
27 }
28 
TEST_F(ScopifyEnumTest,ApplyTestWithPrefix)29 TEST_F(ScopifyEnumTest, ApplyTestWithPrefix) {
30   std::string Original = R"cpp(
31 enum ^E { EV1, EV2, EV3 };
32 enum E;
33 E func(E in)
34 {
35   E out = EV1;
36   if (in == EV2)
37     out = E::EV3;
38   return out;
39 }
40 )cpp";
41   std::string Expected = R"cpp(
42 enum class E { V1, V2, V3 };
43 enum class E;
44 E func(E in)
45 {
46   E out = E::V1;
47   if (in == E::V2)
48     out = E::V3;
49   return out;
50 }
51 )cpp";
52   FileName = "Test.cpp";
53   SCOPED_TRACE(Original);
54   EXPECT_EQ(apply(Original), Expected);
55 }
56 
TEST_F(ScopifyEnumTest,ApplyTestWithPrefixAndUnderscore)57 TEST_F(ScopifyEnumTest, ApplyTestWithPrefixAndUnderscore) {
58   std::string Original = R"cpp(
59 enum ^E { E_V1, E_V2, E_V3 };
60 enum E;
61 E func(E in)
62 {
63   E out = E_V1;
64   if (in == E_V2)
65     out = E::E_V3;
66   return out;
67 }
68 )cpp";
69   std::string Expected = R"cpp(
70 enum class E { V1, V2, V3 };
71 enum class E;
72 E func(E in)
73 {
74   E out = E::V1;
75   if (in == E::V2)
76     out = E::V3;
77   return out;
78 }
79 )cpp";
80   FileName = "Test.cpp";
81   SCOPED_TRACE(Original);
82   EXPECT_EQ(apply(Original), Expected);
83 }
84 
TEST_F(ScopifyEnumTest,ApplyTestWithoutPrefix)85 TEST_F(ScopifyEnumTest, ApplyTestWithoutPrefix) {
86   std::string Original = R"cpp(
87 enum ^E { V1, V2, V3 };
88 enum E;
89 E func(E in)
90 {
91   E out = V1;
92   if (in == V2)
93     out = E::V3;
94   return out;
95 }
96 )cpp";
97   std::string Expected = R"cpp(
98 enum class E { V1, V2, V3 };
99 enum class E;
100 E func(E in)
101 {
102   E out = E::V1;
103   if (in == E::V2)
104     out = E::V3;
105   return out;
106 }
107 )cpp";
108   FileName = "Test.cpp";
109   SCOPED_TRACE(Original);
110   EXPECT_EQ(apply(Original), Expected);
111 }
112 
113 } // namespace
114 } // namespace clang::clangd
115