diff --git a/explorer/file_test.cpp b/explorer/file_test.cpp index 46a1b7f7b318..cf198529cf7d 100644 --- a/explorer/file_test.cpp +++ b/explorer/file_test.cpp @@ -85,14 +85,14 @@ class ParseAndExecuteTestFile : public FileTestBase { extern auto RegisterFileTests( const llvm::SmallVector& paths) -> void { ParseAndExecuteTestFile::RegisterTests( - "ParseAndExecuteTestFile", paths, [=](const std::filesystem::path& path) { + "ParseAndExecuteTestFile", paths, [](const std::filesystem::path& path) { return new ParseAndExecuteTestFile(path, /*trace=*/false); }); - ParseAndExecuteTestFile::RegisterTests( - "ParseAndExecuteTestFile.trace", paths, - [=](const std::filesystem::path& path) { - return new ParseAndExecuteTestFile(path, /*trace=*/true); - }); + ParseAndExecuteTestFile::RegisterTests("ParseAndExecuteTestFile.trace", paths, + [](const std::filesystem::path& path) { + return new ParseAndExecuteTestFile( + path, /*trace=*/true); + }); } } // namespace Carbon::Testing diff --git a/testing/file_test/file_test_base.h b/testing/file_test/file_test_base.h index 6c0f844fba8c..25cb2434c421 100644 --- a/testing/file_test/file_test_base.h +++ b/testing/file_test/file_test_base.h @@ -38,10 +38,20 @@ class FileTestBase : public testing::Test { ~FileTestBase() override; // Used by children to register tests with gtest. - static void RegisterTests( + static auto RegisterTests( const char* fixture_label, const llvm::SmallVector& paths, - std::function factory); + std::function factory) + -> void; + + template + static auto RegisterTests( + const char* fixture_label, + const llvm::SmallVector& paths) -> void { + RegisterTests(fixture_label, paths, [](const std::filesystem::path& path) { + return new FileTestChildT(path); + }); + } // Implemented by children to run the test. Called by the TestBody // implementation, which will validate stdout and stderr. The return value diff --git a/testing/file_test/file_test_base_test.cpp b/testing/file_test/file_test_base_test.cpp index bf5ea98e7eaf..6dab279e01ac 100644 --- a/testing/file_test/file_test_base_test.cpp +++ b/testing/file_test/file_test_base_test.cpp @@ -72,10 +72,7 @@ class FileTestBaseTest : public FileTestBase { auto RegisterFileTests(const llvm::SmallVector& paths) -> void { - FileTestBaseTest::RegisterTests("FileTestBaseTest", paths, - [](const std::filesystem::path& path) { - return new FileTestBaseTest(path); - }); + FileTestBaseTest::RegisterTests("FileTestBaseTest", paths); } } // namespace Carbon::Testing diff --git a/toolchain/driver/BUILD b/toolchain/driver/BUILD index ea36288d3752..f54c84e9ae2d 100644 --- a/toolchain/driver/BUILD +++ b/toolchain/driver/BUILD @@ -53,6 +53,17 @@ cc_fuzz_test( ], ) +cc_library( + name = "driver_file_test_base", + testonly = 1, + hdrs = ["driver_file_test_base.h"], + deps = [ + ":driver", + "//testing/file_test:file_test_base", + "@llvm-project//llvm:Support", + ], +) + cc_binary( name = "carbon", srcs = ["driver_main.cpp"], diff --git a/toolchain/driver/driver_file_test_base.h b/toolchain/driver/driver_file_test_base.h new file mode 100644 index 000000000000..f2f41b074f12 --- /dev/null +++ b/toolchain/driver/driver_file_test_base.h @@ -0,0 +1,34 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_ +#define CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_ + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "testing/file_test/file_test_base.h" +#include "toolchain/driver/driver.h" + +namespace Carbon::Testing { + +// Provides common test support for the driver. This is used by file tests in +// phase subdirectories. +class DriverFileTestBase : public FileTestBase { + public: + using FileTestBase::FileTestBase; + + auto RunWithFiles(const llvm::SmallVector& test_files, + llvm::raw_ostream& stdout, llvm::raw_ostream& stderr) + -> bool override { + Driver driver(stdout, stderr); + return driver.RunFullCommand(MakeArgs(test_files)); + } + + virtual auto MakeArgs(const llvm::SmallVector& test_files) + -> llvm::SmallVector = 0; +}; + +} // namespace Carbon::Testing + +#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_ diff --git a/toolchain/lexer/BUILD b/toolchain/lexer/BUILD index 0c23f7a7a75e..3daa25c4fbb6 100644 --- a/toolchain/lexer/BUILD +++ b/toolchain/lexer/BUILD @@ -236,9 +236,7 @@ file_test( srcs = ["lexer_file_test.cpp"], tests = glob(["testdata/**/*.carbon"]), deps = [ - "//testing/file_test:file_test_base", - "//toolchain/driver", - "@com_google_googletest//:gtest", + "//toolchain/driver:driver_file_test_base", "@llvm-project//llvm:Support", ], ) diff --git a/toolchain/lexer/lexer_file_test.cpp b/toolchain/lexer/lexer_file_test.cpp index dabce801291d..a906e75ab74f 100644 --- a/toolchain/lexer/lexer_file_test.cpp +++ b/toolchain/lexer/lexer_file_test.cpp @@ -2,33 +2,27 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include -#include - -#include +#include +#include +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/raw_ostream.h" -#include "testing/file_test/file_test_base.h" -#include "toolchain/driver/driver.h" +#include "toolchain/driver/driver_file_test_base.h" namespace Carbon::Testing { namespace { -class LexerFileTest : public FileTestBase { +class LexerFileTest : public DriverFileTestBase { public: - explicit LexerFileTest(const std::filesystem::path& path) - : FileTestBase(path) {} + using DriverFileTestBase::DriverFileTestBase; - auto RunWithFiles(const llvm::SmallVector& test_files, - llvm::raw_ostream& stdout, llvm::raw_ostream& stderr) - -> bool override { + auto MakeArgs(const llvm::SmallVector& test_files) + -> llvm::SmallVector override { llvm::SmallVector args({"dump", "tokens"}); for (const auto& file : test_files) { args.push_back(file); } - Driver driver(stdout, stderr); - return driver.RunFullCommand(args); + return args; } }; @@ -36,10 +30,7 @@ class LexerFileTest : public FileTestBase { auto RegisterFileTests(const llvm::SmallVector& paths) -> void { - LexerFileTest::RegisterTests("LexerFileTest", paths, - [](const std::filesystem::path& path) { - return new LexerFileTest(path); - }); + LexerFileTest::RegisterTests("LexerFileTest", paths); } } // namespace Carbon::Testing diff --git a/toolchain/lowering/BUILD b/toolchain/lowering/BUILD index a048527ec222..0566709d29b0 100644 --- a/toolchain/lowering/BUILD +++ b/toolchain/lowering/BUILD @@ -42,9 +42,7 @@ file_test( srcs = ["lowering_file_test.cpp"], tests = glob(["testdata/**/*.carbon"]), deps = [ - "//testing/file_test:file_test_base", - "//toolchain/driver", - "@com_google_googletest//:gtest", + "//toolchain/driver:driver_file_test_base", "@llvm-project//llvm:Support", ], ) diff --git a/toolchain/lowering/lowering_file_test.cpp b/toolchain/lowering/lowering_file_test.cpp index a2716cee37ba..04a8730e2a6a 100644 --- a/toolchain/lowering/lowering_file_test.cpp +++ b/toolchain/lowering/lowering_file_test.cpp @@ -2,33 +2,27 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include -#include - -#include +#include +#include +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/raw_ostream.h" -#include "testing/file_test/file_test_base.h" -#include "toolchain/driver/driver.h" +#include "toolchain/driver/driver_file_test_base.h" namespace Carbon::Testing { namespace { -class LoweringFileTest : public FileTestBase { +class LoweringFileTest : public DriverFileTestBase { public: - explicit LoweringFileTest(const std::filesystem::path& path) - : FileTestBase(path) {} + using DriverFileTestBase::DriverFileTestBase; - auto RunWithFiles(const llvm::SmallVector& test_files, - llvm::raw_ostream& stdout, llvm::raw_ostream& stderr) - -> bool override { + auto MakeArgs(const llvm::SmallVector& test_files) + -> llvm::SmallVector override { llvm::SmallVector args({"dump", "llvm-ir"}); for (const auto& file : test_files) { args.push_back(file); } - Driver driver(stdout, stderr); - return driver.RunFullCommand(args); + return args; } }; @@ -36,10 +30,7 @@ class LoweringFileTest : public FileTestBase { auto RegisterFileTests(const llvm::SmallVector& paths) -> void { - LoweringFileTest::RegisterTests("LoweringFileTest", paths, - [=](const std::filesystem::path& path) { - return new LoweringFileTest(path); - }); + LoweringFileTest::RegisterTests("LoweringFileTest", paths); } } // namespace Carbon::Testing diff --git a/toolchain/parser/BUILD b/toolchain/parser/BUILD index a06d333be86f..4eba4f0e496f 100644 --- a/toolchain/parser/BUILD +++ b/toolchain/parser/BUILD @@ -121,9 +121,7 @@ file_test( srcs = ["parse_tree_file_test.cpp"], tests = glob(["testdata/**/*.carbon"]), deps = [ - "//testing/file_test:file_test_base", - "//toolchain/driver", - "@com_google_googletest//:gtest", + "//toolchain/driver:driver_file_test_base", "@llvm-project//llvm:Support", ], ) diff --git a/toolchain/parser/parse_tree_file_test.cpp b/toolchain/parser/parse_tree_file_test.cpp index 393066ae0a8b..e7d910853bdf 100644 --- a/toolchain/parser/parse_tree_file_test.cpp +++ b/toolchain/parser/parse_tree_file_test.cpp @@ -2,33 +2,27 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include -#include - -#include +#include +#include +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/raw_ostream.h" -#include "testing/file_test/file_test_base.h" -#include "toolchain/driver/driver.h" +#include "toolchain/driver/driver_file_test_base.h" namespace Carbon::Testing { namespace { -class ParserFileTest : public FileTestBase { +class ParseTreeFileTest : public DriverFileTestBase { public: - explicit ParserFileTest(const std::filesystem::path& path) - : FileTestBase(path) {} + using DriverFileTestBase::DriverFileTestBase; - auto RunWithFiles(const llvm::SmallVector& test_files, - llvm::raw_ostream& stdout, llvm::raw_ostream& stderr) - -> bool override { + auto MakeArgs(const llvm::SmallVector& test_files) + -> llvm::SmallVector override { llvm::SmallVector args({"dump", "parse-tree"}); for (const auto& file : test_files) { args.push_back(file); } - Driver driver(stdout, stderr); - return driver.RunFullCommand(args); + return args; } }; @@ -36,10 +30,8 @@ class ParserFileTest : public FileTestBase { auto RegisterFileTests(const llvm::SmallVector& paths) -> void { - ParserFileTest::RegisterTests("ParserFileTest", paths, - [](const std::filesystem::path& path) { - return new ParserFileTest(path); - }); + ParseTreeFileTest::RegisterTests("ParseTreeFileTest", + paths); } } // namespace Carbon::Testing diff --git a/toolchain/semantics/BUILD b/toolchain/semantics/BUILD index a2619b89d8a7..55392afb1484 100644 --- a/toolchain/semantics/BUILD +++ b/toolchain/semantics/BUILD @@ -119,9 +119,7 @@ file_test( srcs = ["semantics_file_test.cpp"], tests = glob(["testdata/**/*.carbon"]), deps = [ - "//testing/file_test:file_test_base", - "//toolchain/driver", - "@com_google_googletest//:gtest", + "//toolchain/driver:driver_file_test_base", "@llvm-project//llvm:Support", ], ) diff --git a/toolchain/semantics/semantics_file_test.cpp b/toolchain/semantics/semantics_file_test.cpp index 74b76f4da974..4edb4f534d64 100644 --- a/toolchain/semantics/semantics_file_test.cpp +++ b/toolchain/semantics/semantics_file_test.cpp @@ -2,33 +2,27 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include -#include - -#include +#include +#include +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/raw_ostream.h" -#include "testing/file_test/file_test_base.h" -#include "toolchain/driver/driver.h" +#include "toolchain/driver/driver_file_test_base.h" namespace Carbon::Testing { namespace { -class SemanticsFileTest : public FileTestBase { +class SemanticsFileTest : public DriverFileTestBase { public: - explicit SemanticsFileTest(const std::filesystem::path& path) - : FileTestBase(path) {} + using DriverFileTestBase::DriverFileTestBase; - auto RunWithFiles(const llvm::SmallVector& test_files, - llvm::raw_ostream& stdout, llvm::raw_ostream& stderr) - -> bool override { + auto MakeArgs(const llvm::SmallVector& test_files) + -> llvm::SmallVector override { llvm::SmallVector args({"dump", "semantics-ir"}); for (const auto& file : test_files) { args.push_back(file); } - Driver driver(stdout, stderr); - return driver.RunFullCommand(args); + return args; } }; @@ -36,10 +30,8 @@ class SemanticsFileTest : public FileTestBase { auto RegisterFileTests(const llvm::SmallVector& paths) -> void { - SemanticsFileTest::RegisterTests("SemanticsFileTest", paths, - [](const std::filesystem::path& path) { - return new SemanticsFileTest(path); - }); + SemanticsFileTest::RegisterTests("SemanticsFileTest", + paths); } } // namespace Carbon::Testing