var_decl_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, Assignment) {
  30. constexpr char Before[] = "int i = 0;";
  31. // TODO: Include init.
  32. constexpr char After[] = "var i: int;";
  33. ExpectReplacement(Before, After);
  34. }
  35. TEST_F(VarDeclTest, Auto) {
  36. constexpr char Before[] = "auto i = 0;";
  37. // TODO: Keep auto.
  38. constexpr char After[] = "var i: int;";
  39. ExpectReplacement(Before, After);
  40. }
  41. TEST_F(VarDeclTest, Const) {
  42. // TODO: Include init, have `const` indicate `let`.
  43. constexpr char Before[] = "const int i = 0;";
  44. constexpr char After[] = "var i: const int;";
  45. ExpectReplacement(Before, After);
  46. }
  47. TEST_F(VarDeclTest, Params) {
  48. constexpr char Before[] = "auto Foo(int i) -> int;";
  49. constexpr char After[] = "auto Foo(i: int) -> int;";
  50. ExpectReplacement(Before, After);
  51. }
  52. TEST_F(VarDeclTest, ParamsDefault) {
  53. // TODO: Include init.
  54. constexpr char Before[] = "auto Foo(int i = 0) -> int;";
  55. constexpr char After[] = "auto Foo(i: int) -> int;";
  56. ExpectReplacement(Before, After);
  57. }
  58. TEST_F(VarDeclTest, ParamsConst) {
  59. constexpr char Before[] = "auto Foo(const int i) -> int;";
  60. constexpr char After[] = "auto Foo(i: const int) -> int;";
  61. ExpectReplacement(Before, After);
  62. }
  63. TEST_F(VarDeclTest, ParamStruct) {
  64. // This is to ensure the 'struct' keyword doesn't get added to the call type.
  65. constexpr char Before[] = R"cpp(
  66. struct Circle {};
  67. auto Draw(int times, const Circle& circle) -> bool;
  68. )cpp";
  69. constexpr char After[] = R"(
  70. struct Circle {};
  71. auto Draw(times: int, circle: const Circle &) -> bool;
  72. )";
  73. ExpectReplacement(Before, After);
  74. }
  75. TEST_F(VarDeclTest, Member) {
  76. // TODO: Handle member variables.
  77. constexpr char Before[] = R"cpp(
  78. struct Circle {
  79. Circle() : x(0), y(0), radius(1) {}
  80. int x;
  81. int y;
  82. int radius;
  83. };
  84. )cpp";
  85. ExpectReplacement(Before, Before);
  86. }
  87. TEST_F(VarDeclTest, RangeFor) {
  88. // TODO: Handle range based for loops.
  89. constexpr char Before[] = R"cpp(
  90. void Foo() {
  91. int items[] = {1};
  92. for (int i : items) {
  93. }
  94. }
  95. )cpp";
  96. constexpr char After[] = R"(
  97. void Foo() {
  98. var items: int [1];
  99. for (int i var __begin1: int * var __range1: int (&)[1]) {
  100. }
  101. }
  102. )";
  103. ExpectReplacement(Before, After);
  104. }
  105. } // namespace
  106. } // namespace Carbon