file_test.cpp 2.7 KB

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