test_file_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "testing/file_test/test_file.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/error_test_helpers.h"
  8. #include "testing/base/file_helpers.h"
  9. #include "testing/file_test/file_test_base.h"
  10. // The current BUILD structure of this library means that in order to unit-test
  11. // test_file.h, we have to pretend to be a file_test. That means there has to
  12. // be a definition of CarbonFileTestManifest (normally generated by a file_test
  13. // Bazel rule), and there has to be a registered FileTestFactory, so we define
  14. // them both as dummies.
  15. // TODO: restructure BUILD rules to avoid the need for this hack.
  16. // Dummy empty manifest.
  17. // NOLINTNEXTLINE(readability-identifier-naming): manifest.cpp dictates spelling
  18. const char* CarbonFileTestManifest[] = {nullptr};
  19. namespace Carbon::Testing {
  20. // Dummy unusable FileTestBase.
  21. class DummyFileTest : public FileTestBase {
  22. public:
  23. DummyFileTest(llvm::StringRef /*exe_path*/, llvm::StringRef test_name)
  24. : FileTestBase(test_name) {}
  25. auto Run(const llvm::SmallVector<llvm::StringRef>& /*test_args*/,
  26. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>& /*fs*/,
  27. FILE* /*input_stream*/, llvm::raw_pwrite_stream& /*output_stream*/,
  28. llvm::raw_pwrite_stream& /*error_stream*/) const
  29. -> ErrorOr<RunResult> override {
  30. CARBON_FATAL("Called method of dummy object");
  31. }
  32. auto GetDefaultArgs() const -> llvm::SmallVector<std::string> override {
  33. CARBON_FATAL("Called method of dummy object");
  34. }
  35. };
  36. CARBON_FILE_TEST_FACTORY(DummyFileTest)
  37. namespace {
  38. using ::testing::_;
  39. using ::testing::StrEq;
  40. // Returns the non-check lines of test_file as a string.
  41. auto NonCheckFileContents(const TestFile& test_file) -> std::string {
  42. std::string buffer;
  43. llvm::raw_string_ostream out(buffer);
  44. for (const auto& line : test_file.non_check_lines) {
  45. line.Print(out);
  46. out << "\n";
  47. }
  48. return buffer;
  49. }
  50. // Returns a string consisting of 7 repetitions of `c`, for use as a simulated
  51. // merge conflict marker. We use this in test inputs instead of writing the
  52. // conflict markers directly in the string literals so that tools don't treat
  53. // them as genuine merge conflicts in this file.
  54. auto Marker(char c) -> std::string { return std::string(7, c); }
  55. TEST(AutoupdateTest, SnapshotMergeConflict) {
  56. std::string initial_contents = R"(
  57. // AUTOUPDATE
  58. Some text
  59. )" + Marker('<') + R"(
  60. // CHECK:STDOUT: side 1
  61. )" + Marker('=') + R"(
  62. // CHECK:STDOUT: side 2
  63. )" + Marker('>') + R"(
  64. More text
  65. )";
  66. constexpr std::string_view ExpectedContents = R"(
  67. // AUTOUPDATE
  68. Some text
  69. More text
  70. )";
  71. auto file_path = *WriteTestFile("tmp", initial_contents);
  72. auto test_file =
  73. ProcessTestFile(file_path.c_str(), /*running_autoupdate=*/true);
  74. ASSERT_THAT(test_file, IsSuccess(_)) << test_file.error();
  75. EXPECT_THAT(NonCheckFileContents(*test_file), StrEq(ExpectedContents));
  76. }
  77. TEST(AutoupdateTest, SnapshotTheeWayMergeConflict) {
  78. std::string initial_contents = R"(
  79. // AUTOUPDATE
  80. Some text
  81. )" + Marker('<') + R"(
  82. // CHECK:STDOUT: side 1
  83. )" + Marker('|') + R"(
  84. // CHECK:STDOUT: base
  85. )" + Marker('=') + R"(
  86. // CHECK:STDOUT: side 2
  87. )" + Marker('>') + R"(
  88. More text
  89. )";
  90. constexpr std::string_view ExpectedContents = R"(
  91. // AUTOUPDATE
  92. Some text
  93. More text
  94. )";
  95. auto file_path = *WriteTestFile("tmp", initial_contents);
  96. auto test_file =
  97. ProcessTestFile(file_path.c_str(), /*running_autoupdate=*/true);
  98. ASSERT_THAT(test_file, IsSuccess(_)) << test_file.error();
  99. EXPECT_THAT(NonCheckFileContents(*test_file), StrEq(ExpectedContents));
  100. }
  101. TEST(AutoupdateTest, DiffMergeConflict) {
  102. std::string initial_contents = R"(
  103. // AUTOUPDATE
  104. Some text
  105. )" + Marker('<') + R"(
  106. )" + Marker('%') + R"(
  107. )" + Marker('\\') + R"(
  108. // CHECK:STDOUT: unchanged
  109. -// CHECK:STDOUT: base
  110. +// CHECK:STDOUT: side 1
  111. )" + Marker('+') + R"(
  112. // CHECK:STDOUT: side 2
  113. )" + Marker('>') + R"(
  114. More text
  115. )";
  116. constexpr std::string_view ExpectedContents = R"(
  117. // AUTOUPDATE
  118. Some text
  119. More text
  120. )";
  121. auto file_path = *WriteTestFile("tmp", initial_contents);
  122. auto test_file =
  123. ProcessTestFile(file_path.c_str(), /*running_autoupdate=*/true);
  124. ASSERT_THAT(test_file, IsSuccess(_)) << test_file.error();
  125. EXPECT_THAT(NonCheckFileContents(*test_file), StrEq(ExpectedContents));
  126. }
  127. TEST(AutoupdateTest, NonCheckInDiffRegion) {
  128. std::string initial_contents = R"(
  129. // AUTOUPDATE
  130. Some text
  131. )" + Marker('<') + R"(
  132. )" + Marker('%') + R"(
  133. )" + Marker('\\') + R"(
  134. // unchanged
  135. -// CHECK:STDOUT: base
  136. +// CHECK:STDOUT: side 1
  137. )" + Marker('+') + R"(
  138. // CHECK:STDOUT: side 2
  139. )" + Marker('>') + R"(
  140. More text
  141. )";
  142. auto file_path = *WriteTestFile("tmp", initial_contents);
  143. auto test_file =
  144. ProcessTestFile(file_path.c_str(), /*running_autoupdate=*/true);
  145. ASSERT_THAT(test_file, IsError(_));
  146. }
  147. } // namespace
  148. } // namespace Carbon::Testing