file_test.cpp 3.0 KB

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