fn_inserter_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // TODO: Need to re-lex tokens, this should probably be "fn inline" for now.
  20. constexpr char Before[] = "inline auto A() -> int;";
  21. constexpr char After[] = "fn auto A() -> int;";
  22. ExpectReplacement(Before, After);
  23. }
  24. TEST_F(FnInserterTest, Void) {
  25. constexpr char Before[] = "void A();";
  26. constexpr char After[] = "fn A();";
  27. ExpectReplacement(Before, After);
  28. }
  29. TEST_F(FnInserterTest, Methods) {
  30. // TODO: Need to re-lex tokens, this should probably be "fn virtual" for now.
  31. constexpr char Before[] = R"cpp(
  32. class Shape {
  33. public:
  34. virtual void Draw() = 0;
  35. virtual auto NumSides() -> int = 0;
  36. };
  37. class Circle : public Shape {
  38. public:
  39. void Draw() override;
  40. auto NumSides() -> int override;
  41. auto Radius() -> double { return radius_; }
  42. private:
  43. double radius_;
  44. };
  45. )cpp";
  46. constexpr char After[] = R"(
  47. class Shape {
  48. public:
  49. fn void Draw() = 0;
  50. fn auto NumSides() -> int = 0;
  51. };
  52. class Circle : public Shape {
  53. public:
  54. fn Draw() override;
  55. fn NumSides() -> int override;
  56. fn Radius() -> double { return radius_; }
  57. private:
  58. double radius_;
  59. };
  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