fn_inserter_test.cpp 2.2 KB

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