file_test.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "explorer/parse_and_execute/parse_and_execute.h"
  5. #include "testing/file_test/file_test_base.h"
  6. #include "testing/util/test_raw_ostream.h"
  7. namespace Carbon::Testing {
  8. namespace {
  9. class ParseAndExecuteTestFile : public FileTestBase {
  10. public:
  11. explicit ParseAndExecuteTestFile(const std::filesystem::path& path,
  12. bool trace)
  13. : FileTestBase(path), trace_(trace) {}
  14. auto SetUp() -> void override {
  15. std::string path_str = path().string();
  16. llvm::StringRef path_ref = path_str;
  17. if (path_ref.find("trace_testdata") != llvm::StringRef::npos) {
  18. is_trace_test = true;
  19. }
  20. if (trace_) {
  21. if (path_ref.find("/limits/") != llvm::StringRef::npos) {
  22. GTEST_SKIP()
  23. << "`limits` tests check for various limit conditions (such as an "
  24. "infinite loop). The tests collectively don't test tracing "
  25. "because it creates substantial additional overhead.";
  26. } else if (path_ref.endswith(
  27. "testdata/assoc_const/rewrite_large_type.carbon") ||
  28. path_ref.endswith(
  29. "testdata/linked_list/typed_linked_list.carbon")) {
  30. GTEST_SKIP() << "Expensive test to trace";
  31. }
  32. } else {
  33. if (is_trace_test) {
  34. GTEST_SKIP() << "`trace` tests only check for trace output.";
  35. }
  36. }
  37. }
  38. auto RunWithFiles(const llvm::SmallVector<TestFile>& test_files,
  39. llvm::raw_pwrite_stream& stdout,
  40. llvm::raw_pwrite_stream& stderr) -> bool override {
  41. if (test_files.size() != 1) {
  42. ADD_FAILURE() << "Only 1 file is supported: " << test_files.size()
  43. << " provided";
  44. return false;
  45. }
  46. // Capture trace streaming, but only when in debug mode.
  47. TraceStream trace_stream;
  48. TestRawOstream trace_stream_ostream;
  49. if (trace_) {
  50. trace_stream.set_stream(is_trace_test ? &stdout : &trace_stream_ostream);
  51. trace_stream.set_allowed_phases({ProgramPhase::All});
  52. trace_stream.set_allowed_file_kinds({FileKind::Main});
  53. }
  54. // Set the location of the prelude.
  55. char* test_srcdir = getenv("TEST_SRCDIR");
  56. CARBON_CHECK(test_srcdir != nullptr);
  57. std::string prelude_path(test_srcdir);
  58. prelude_path += "/carbon/explorer/data/prelude.carbon";
  59. // Run the parse. Parser debug output is always off because it's difficult
  60. // to redirect.
  61. auto result = ParseAndExecute(
  62. prelude_path, test_files[0].filename, test_files[0].content,
  63. /*parser_debug=*/false, &trace_stream, &stdout);
  64. // This mirrors printing currently done by main.cpp.
  65. if (result.ok()) {
  66. stdout << "result: " << *result << "\n";
  67. } else {
  68. stderr << result.error() << "\n";
  69. }
  70. // Skip trace test check as they use stdout stream instead of
  71. // trace_stream_ostream
  72. if (trace_ && !is_trace_test) {
  73. EXPECT_FALSE(trace_stream_ostream.TakeStr().empty())
  74. << "Tracing should always do something";
  75. }
  76. return result.ok();
  77. }
  78. private:
  79. bool trace_;
  80. bool is_trace_test = false;
  81. };
  82. } // namespace
  83. extern auto RegisterFileTests(
  84. const llvm::SmallVector<std::filesystem::path>& paths) -> void {
  85. ParseAndExecuteTestFile::RegisterTests(
  86. "ParseAndExecuteTestFile", paths, [](const std::filesystem::path& path) {
  87. return new ParseAndExecuteTestFile(path, /*trace=*/false);
  88. });
  89. ParseAndExecuteTestFile::RegisterTests("ParseAndExecuteTestFile.trace", paths,
  90. [](const std::filesystem::path& path) {
  91. return new ParseAndExecuteTestFile(
  92. path, /*trace=*/true);
  93. });
  94. }
  95. } // namespace Carbon::Testing