fn_inserter_test.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // TODO: void needs to be handled.
  26. constexpr char Before[] = "void A();";
  27. ExpectReplacement(Before, Before);
  28. }
  29. TEST_F(FnInserterTest, Methods) {
  30. // TODO: void needs to be handled.
  31. // TODO: Need to re-lex tokens, this should probably be "fn virtual" for now.
  32. constexpr char Before[] = R"cpp(
  33. class Shape {
  34. public:
  35. virtual void Draw() = 0;
  36. virtual auto NumSides() -> int = 0;
  37. };
  38. class Circle : public Shape {
  39. public:
  40. void Draw() override;
  41. auto NumSides() -> int override;
  42. auto Radius() -> double { return radius_; }
  43. private:
  44. double radius_;
  45. };
  46. )cpp";
  47. constexpr char After[] = R"(
  48. class Shape {
  49. public:
  50. virtual void Draw() = 0;
  51. fn auto NumSides() -> int = 0;
  52. };
  53. class Circle : public Shape {
  54. public:
  55. void Draw() override;
  56. fn NumSides() -> int override;
  57. fn Radius() -> double { return radius_; }
  58. private:
  59. double radius_;
  60. };
  61. )";
  62. ExpectReplacement(Before, After);
  63. }
  64. TEST_F(FnInserterTest, LegacyReturn) {
  65. // Code should be migrated to trailing returns by clang-tidy, so this is okay
  66. // to miss.
  67. constexpr char Before[] = "int A();";
  68. ExpectReplacement(Before, Before);
  69. }
  70. } // namespace
  71. } // namespace Carbon