file_test.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 RunWithFiles(const llvm::SmallVector<std::string>& test_files,
  31. llvm::raw_ostream& stdout, llvm::raw_ostream& stderr)
  32. -> bool override {
  33. if (test_files.size() != 1) {
  34. ADD_FAILURE() << "Only 1 file is supported: " << test_files.size()
  35. << " provided";
  36. return false;
  37. }
  38. // Capture trace streaming, but only when in debug mode.
  39. TraceStream trace_stream;
  40. std::string trace_stream_str;
  41. llvm::raw_string_ostream trace_stream_ostream(trace_stream_str);
  42. if (trace_) {
  43. trace_stream.set_stream(&trace_stream_ostream);
  44. }
  45. // Set the location of the prelude.
  46. char* test_srcdir = getenv("TEST_SRCDIR");
  47. CARBON_CHECK(test_srcdir != nullptr);
  48. std::string prelude_path(test_srcdir);
  49. prelude_path += "/carbon/explorer/data/prelude.carbon";
  50. // Run the parse. Parser debug output is always off because it's difficult
  51. // to redirect.
  52. auto result =
  53. ParseAndExecuteFile(prelude_path, path().filename().string(),
  54. /*parser_debug=*/false, &trace_stream, &stdout);
  55. // This mirrors printing currently done by main.cpp.
  56. if (result.ok()) {
  57. stdout << "result: " << *result << "\n";
  58. } else {
  59. stderr << result.error() << "\n";
  60. }
  61. if (trace_) {
  62. EXPECT_FALSE(trace_stream_str.empty())
  63. << "Tracing should always do something";
  64. }
  65. return result.ok();
  66. }
  67. private:
  68. bool trace_;
  69. };
  70. } // namespace
  71. extern auto RegisterFileTests(
  72. const llvm::SmallVector<std::filesystem::path>& paths) -> void {
  73. ParseAndExecuteTestFile::RegisterTests(
  74. "ParseAndExecuteTestFile", paths, [=](const std::filesystem::path& path) {
  75. return new ParseAndExecuteTestFile(path, /*trace=*/false);
  76. });
  77. ParseAndExecuteTestFile::RegisterTests(
  78. "ParseAndExecuteTestFile.trace", paths,
  79. [=](const std::filesystem::path& path) {
  80. return new ParseAndExecuteTestFile(path, /*trace=*/true);
  81. });
  82. }
  83. } // namespace Carbon::Testing