var_decl_test.cpp 5.2 KB

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