fn_inserter_test.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/fn_inserter.h"
  5. #include "migrate_cpp/cpp_refactoring/matcher_test_base.h"
  6. namespace Carbon {
  7. namespace {
  8. class FnInserterTest : public MatcherTestBase {
  9. protected:
  10. FnInserterTest() : fn_inserter(replacements, &finder) {}
  11. Carbon::FnInserter fn_inserter;
  12. };
  13. TEST_F(FnInserterTest, TrailingReturn) {
  14. constexpr char Before[] = "auto A() -> int;";
  15. constexpr char After[] = "fn A() -> int;";
  16. ExpectReplacement(Before, After);
  17. }
  18. TEST_F(FnInserterTest, Inline) {
  19. constexpr char Before[] = "inline auto A() -> int;";
  20. constexpr char After[] = "fn inline A() -> int;";
  21. ExpectReplacement(Before, After);
  22. }
  23. TEST_F(FnInserterTest, Void) {
  24. constexpr char Before[] = "void A();";
  25. constexpr char After[] = "fn A();";
  26. ExpectReplacement(Before, After);
  27. }
  28. TEST_F(FnInserterTest, Methods) {
  29. constexpr char Before[] = R"cpp(
  30. class Shape {
  31. public:
  32. virtual void Draw() = 0;
  33. virtual auto NumSides() -> int = 0;
  34. };
  35. class Circle : public Shape {
  36. public:
  37. void Draw() override;
  38. auto NumSides() -> int override;
  39. auto Radius() -> double { return radius_; }
  40. private:
  41. double radius_;
  42. };
  43. void Shape::Draw() {}
  44. )cpp";
  45. constexpr char After[] = R"(
  46. class Shape {
  47. public:
  48. fn virtual Draw() = 0;
  49. fn virtual NumSides() -> int = 0;
  50. };
  51. class Circle : public Shape {
  52. public:
  53. fn Draw() override;
  54. fn NumSides() -> int override;
  55. fn Radius() -> double { return radius_; }
  56. private:
  57. double radius_;
  58. };
  59. fn Shape::Draw() {}
  60. )";
  61. ExpectReplacement(Before, After);
  62. }
  63. TEST_F(FnInserterTest, ConstructorDestructor) {
  64. constexpr char Before[] = R"cpp(
  65. class Shape {
  66. public:
  67. Shape() {}
  68. ~Shape() {}
  69. };
  70. )cpp";
  71. ExpectReplacement(Before, Before);
  72. }
  73. TEST_F(FnInserterTest, LegacyReturn) {
  74. // Code should be migrated to trailing returns by clang-tidy, so this is okay
  75. // to miss.
  76. constexpr char Before[] = "int A();";
  77. ExpectReplacement(Before, Before);
  78. }
  79. } // namespace
  80. } // namespace Carbon