var_decl_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, DeclarationComma) {
  24. // TODO: Maybe replace the comma with a `;`.
  25. constexpr char Before[] = "int i, j;";
  26. constexpr char After[] = "var i: int, var j: int;";
  27. ExpectReplacement(Before, After);
  28. }
  29. TEST_F(VarDeclTest, DeclarationCommaArray) {
  30. // TODO: Maybe replace the comma with a `;`.
  31. // TODO: Need to handle j's array.
  32. constexpr char Before[] = "int i[4], j[4];";
  33. constexpr char After[] = "var i: int [4], j[4];";
  34. ExpectReplacement(Before, After);
  35. }
  36. TEST_F(VarDeclTest, DeclarationCommaPointers) {
  37. // TODO: Maybe replace the comma with a `;`.
  38. // TODO: Need to handle j's pointer.
  39. // constexpr char After[] = "var i: int *, var j: int *;";
  40. constexpr char Before[] = "int *i, *j;";
  41. constexpr char After[] = "var i: int *, *j;";
  42. ExpectReplacement(Before, After);
  43. }
  44. TEST_F(VarDeclTest, Assignment) {
  45. constexpr char Before[] = "int i = 0;";
  46. // TODO: Include init.
  47. constexpr char After[] = "var i: int;";
  48. ExpectReplacement(Before, After);
  49. }
  50. TEST_F(VarDeclTest, Auto) {
  51. constexpr char Before[] = "auto i = 0;";
  52. constexpr char After[] = "var i: auto;";
  53. ExpectReplacement(Before, After);
  54. }
  55. TEST_F(VarDeclTest, AutoRef) {
  56. // TODO: Include init.
  57. // TODO: j should have const.
  58. constexpr char Before[] = R"cpp(
  59. auto i = 0;
  60. const auto& j = i;
  61. )cpp";
  62. constexpr char After[] = R"(
  63. var i: auto;
  64. var j: auto&;
  65. )";
  66. ExpectReplacement(Before, After);
  67. }
  68. TEST_F(VarDeclTest, Const) {
  69. // TODO: Include init.
  70. constexpr char Before[] = "const int i = 0;";
  71. constexpr char After[] = "let i: const int;";
  72. ExpectReplacement(Before, After);
  73. }
  74. TEST_F(VarDeclTest, ConstPointer) {
  75. constexpr char Before[] = "const int* i;";
  76. constexpr char After[] = "var i: const int *;";
  77. ExpectReplacement(Before, After);
  78. }
  79. TEST_F(VarDeclTest, Namespace) {
  80. // This is to ensure the 'struct' keyword doesn't get added to the qualified type.
  81. constexpr char Before[] = R"cpp(
  82. namespace Foo {
  83. typedef int Bar;
  84. }
  85. Foo::Bar x;
  86. )cpp";
  87. constexpr char After[] = R"(
  88. namespace Foo {
  89. typedef int Bar;
  90. }
  91. var x: Foo::Bar;
  92. )";
  93. ExpectReplacement(Before, After);
  94. ExpectReplacement(Before, After);
  95. }
  96. TEST_F(VarDeclTest, Params) {
  97. constexpr char Before[] = "auto Foo(int i) -> int;";
  98. constexpr char After[] = "auto Foo(i: int) -> int;";
  99. ExpectReplacement(Before, After);
  100. }
  101. TEST_F(VarDeclTest, ParamsDefault) {
  102. // TODO: Include init.
  103. constexpr char Before[] = "auto Foo(int i = 0) -> int;";
  104. constexpr char After[] = "auto Foo(i: int) -> int;";
  105. ExpectReplacement(Before, After);
  106. }
  107. TEST_F(VarDeclTest, ParamsConst) {
  108. constexpr char Before[] = "auto Foo(const int i) -> int;";
  109. constexpr char After[] = "auto Foo(let i: const int) -> int;";
  110. ExpectReplacement(Before, After);
  111. }
  112. TEST_F(VarDeclTest, ParamStruct) {
  113. // This is to ensure the 'struct' keyword doesn't get added to the call type.
  114. constexpr char Before[] = R"cpp(
  115. struct Circle {};
  116. auto Draw(int times, const Circle& circle) -> bool;
  117. )cpp";
  118. constexpr char After[] = R"(
  119. struct Circle {};
  120. auto Draw(times: int, circle: const Circle &) -> bool;
  121. )";
  122. ExpectReplacement(Before, After);
  123. }
  124. TEST_F(VarDeclTest, Member) {
  125. // TODO: Handle member variables.
  126. constexpr char Before[] = R"cpp(
  127. struct Circle {
  128. Circle() : x(0), y(0), radius(1) {}
  129. int x;
  130. int y;
  131. int radius;
  132. };
  133. )cpp";
  134. ExpectReplacement(Before, Before);
  135. }
  136. TEST_F(VarDeclTest, DISABLED_RangeFor) {
  137. // TODO: Handle range based for loops. Test shouldn't be enabled without
  138. // addressing this because the output is quirky and fragile.
  139. constexpr char Before[] = R"cpp(
  140. void Foo() {
  141. int items[] = {1};
  142. for (int i : items) {
  143. }
  144. }
  145. )cpp";
  146. constexpr char After[] = R"(
  147. void Foo() {
  148. var items: int[] = {1};
  149. for (int i : items) {
  150. }
  151. }
  152. )";
  153. ExpectReplacement(Before, After);
  154. }
  155. } // namespace
  156. } // namespace Carbon