var_decl_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "migrate_cpp/cpp_refactoring/var_decl.h"
  5. #include "migrate_cpp/cpp_refactoring/matcher_test_base.h"
  6. namespace Carbon {
  7. namespace {
  8. class VarDeclTest : public MatcherTestBase {
  9. protected:
  10. VarDeclTest() : var_decl(replacements, &finder) {}
  11. Carbon::VarDecl var_decl;
  12. };
  13. TEST_F(VarDeclTest, Declaration) {
  14. constexpr char Before[] = "int i;";
  15. constexpr char After[] = "var i: int;";
  16. ExpectReplacement(Before, After);
  17. }
  18. TEST_F(VarDeclTest, DeclarationArray) {
  19. constexpr char Before[] = "int i[4];";
  20. constexpr char After[] = "var i: int[4];";
  21. ExpectReplacement(Before, After);
  22. }
  23. TEST_F(VarDeclTest, DeclarationConstArray) {
  24. constexpr char Before[] = "const int i[] = {0, 1};";
  25. constexpr char After[] = "let i: const int[];";
  26. ExpectReplacement(Before, After);
  27. }
  28. TEST_F(VarDeclTest, DeclarationConstPointer) {
  29. // TODO: Include init.
  30. // TODO: Fix j replacement location.
  31. constexpr char Before[] = R"cpp(
  32. int i = 0;
  33. int* const j = &i;
  34. const int* k = &i;
  35. )cpp";
  36. constexpr char After[] = R"(
  37. var i: int;
  38. int* const let j: int* const;
  39. var k: const int*;
  40. )";
  41. ExpectReplacement(Before, After);
  42. }
  43. TEST_F(VarDeclTest, DeclarationComma) {
  44. // TODO: Maybe replace the comma with a `;`.
  45. constexpr char Before[] = "int i, j;";
  46. constexpr char After[] = "var i: int, var j: int;";
  47. ExpectReplacement(Before, After);
  48. }
  49. TEST_F(VarDeclTest, DeclarationCommaArray) {
  50. // TODO: Maybe replace the comma with a `;`.
  51. // TODO: Need to handle j's array.
  52. constexpr char Before[] = "int i[4], j[4];";
  53. constexpr char After[] = "var i: int[4], j[4];";
  54. ExpectReplacement(Before, After);
  55. }
  56. TEST_F(VarDeclTest, DeclarationCommaPointers) {
  57. // TODO: Maybe replace the comma with a `;`.
  58. // TODO: Need to handle j's pointer.
  59. // constexpr char After[] = "var i: int *, var j: int *;";
  60. constexpr char Before[] = "int *i, *j;";
  61. constexpr char After[] = "var i: int*, *j;";
  62. ExpectReplacement(Before, After);
  63. }
  64. TEST_F(VarDeclTest, Assignment) {
  65. constexpr char Before[] = "int i = 0;";
  66. // TODO: Include init.
  67. constexpr char After[] = "var i: int;";
  68. ExpectReplacement(Before, After);
  69. }
  70. TEST_F(VarDeclTest, Auto) {
  71. constexpr char Before[] = "auto i = 0;";
  72. constexpr char After[] = "var i: auto;";
  73. ExpectReplacement(Before, After);
  74. }
  75. TEST_F(VarDeclTest, AutoRef) {
  76. // TODO: Include init.
  77. constexpr char Before[] = R"cpp(
  78. auto i = 0;
  79. const auto& j = i;
  80. )cpp";
  81. constexpr char After[] = R"(
  82. var i: auto;
  83. var j: const auto&;
  84. )";
  85. ExpectReplacement(Before, After);
  86. }
  87. TEST_F(VarDeclTest, Const) {
  88. // TODO: Include init.
  89. constexpr char Before[] = "const int i = 0;";
  90. constexpr char After[] = "let i: const int;";
  91. ExpectReplacement(Before, After);
  92. }
  93. TEST_F(VarDeclTest, ConstPointer) {
  94. constexpr char Before[] = "const int* i;";
  95. constexpr char After[] = "var i: const int*;";
  96. ExpectReplacement(Before, After);
  97. }
  98. TEST_F(VarDeclTest, Namespace) {
  99. constexpr char Before[] = R"cpp(
  100. namespace Foo {
  101. typedef int Bar;
  102. }
  103. Foo::Bar x;
  104. )cpp";
  105. constexpr char After[] = R"(
  106. namespace Foo {
  107. typedef int Bar;
  108. }
  109. var x: Foo::Bar;
  110. )";
  111. ExpectReplacement(Before, After);
  112. ExpectReplacement(Before, After);
  113. }
  114. TEST_F(VarDeclTest, Params) {
  115. constexpr char Before[] = "auto Foo(int i) -> int;";
  116. constexpr char After[] = "auto Foo(i: int) -> int;";
  117. ExpectReplacement(Before, After);
  118. }
  119. TEST_F(VarDeclTest, ParamsDefault) {
  120. // TODO: Include init.
  121. constexpr char Before[] = "auto Foo(int i = 0) -> int;";
  122. constexpr char After[] = "auto Foo(i: int) -> int;";
  123. ExpectReplacement(Before, After);
  124. }
  125. TEST_F(VarDeclTest, ParamsConst) {
  126. constexpr char Before[] = "auto Foo(const int i) -> int;";
  127. constexpr char After[] = "auto Foo(let i: const int) -> int;";
  128. ExpectReplacement(Before, After);
  129. }
  130. TEST_F(VarDeclTest, ParamStruct) {
  131. // This is to ensure the 'struct' keyword doesn't get added to the qualified
  132. // type.
  133. constexpr char Before[] = R"cpp(
  134. struct Circle {};
  135. auto Draw(int times, const Circle& circle) -> bool;
  136. )cpp";
  137. constexpr char After[] = R"(
  138. struct Circle {};
  139. auto Draw(times: int, circle: const Circle&) -> bool;
  140. )";
  141. ExpectReplacement(Before, After);
  142. }
  143. TEST_F(VarDeclTest, Member) {
  144. // TODO: Handle member variables.
  145. constexpr char Before[] = R"cpp(
  146. struct Circle {
  147. Circle() : x(0), y(0), radius(1) {}
  148. int x;
  149. int y;
  150. int radius;
  151. };
  152. )cpp";
  153. ExpectReplacement(Before, Before);
  154. }
  155. TEST_F(VarDeclTest, RangeFor) {
  156. constexpr char Before[] = R"cpp(
  157. void Foo() {
  158. int items[] = {1};
  159. for (int i : items) {
  160. int j;
  161. }
  162. }
  163. )cpp";
  164. constexpr char After[] = R"(
  165. void Foo() {
  166. var items: int[];
  167. for (int i : items) {
  168. var j: int;
  169. }
  170. }
  171. )";
  172. ExpectReplacement(Before, After);
  173. }
  174. TEST_F(VarDeclTest, Template) {
  175. constexpr char Before[] = R"cpp(
  176. template <typename T>
  177. struct R {};
  178. template <typename T>
  179. struct S {};
  180. R<S<int>> x;
  181. )cpp";
  182. constexpr char After[] = R"(
  183. template <typename T>
  184. struct R {};
  185. template <typename T>
  186. struct S {};
  187. var x: R<S<int>>;
  188. )";
  189. ExpectReplacement(Before, After);
  190. }
  191. } // namespace
  192. } // namespace Carbon