file_test.cpp 3.5 KB

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