Tests: Migrate to Directory::for_each_entry()

This commit is contained in:
Sam Atkins 2023-03-02 17:03:53 +00:00 committed by Andreas Kling
parent 23aec16e8b
commit 728b07fbf6
3 changed files with 23 additions and 30 deletions

View file

@ -5,9 +5,8 @@
*/
#include <AK/LexicalPath.h>
#include <AK/Types.h>
#include <LibAudio/FlacLoader.h>
#include <LibCore/DirIterator.h>
#include <LibCore/Directory.h>
#include <LibTest/TestCase.h>
struct FlacTest : Test::TestCase {
@ -46,14 +45,12 @@ struct DiscoverFLACTestsHack {
DiscoverFLACTestsHack()
{
// FIXME: Also run (our own) tests in this directory.
auto test_iterator = Core::DirIterator { "./SpecTests", Core::DirIterator::Flags::SkipParentAndBaseDir };
while (test_iterator.has_next()) {
auto file = LexicalPath { test_iterator.next_full_path() };
if (file.extension() == "flac"sv) {
Test::add_test_case_to_suite(make_ref_counted<FlacTest>(move(file)));
}
}
(void)Core::Directory::for_each_entry("./SpecTests"sv, Core::DirIterator::Flags::SkipParentAndBaseDir, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
auto path = LexicalPath::join(directory.path().string(), entry.name);
if (path.extension() == "flac"sv)
Test::add_test_case_to_suite(make_ref_counted<FlacTest>(path));
return IterationDecision::Continue;
});
}
};
// Hack taken from TEST_CASE; the above constructor will run as part of global initialization before the tests are actually executed

View file

@ -5,13 +5,13 @@
*/
#include <AK/LexicalPath.h>
#include <LibCore/DirIterator.h>
#include <LibCore/Directory.h>
#include <LibCore/File.h>
#include <LibCpp/Parser.h>
#include <LibTest/TestCase.h>
#include <unistd.h>
constexpr char TESTS_ROOT_DIR[] = "/home/anon/Tests/cpp-tests/parser";
constexpr StringView TESTS_ROOT_DIR = "/home/anon/Tests/cpp-tests/parser"sv;
static DeprecatedString read_all(DeprecatedString const& path)
{
@ -24,16 +24,13 @@ static DeprecatedString read_all(DeprecatedString const& path)
TEST_CASE(test_regression)
{
Core::DirIterator directory_iterator(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots);
while (directory_iterator.has_next()) {
auto file_path = directory_iterator.next_full_path();
auto path = LexicalPath { file_path };
MUST(Core::Directory::for_each_entry(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
auto path = LexicalPath::join(directory.path().string(), entry.name);
if (!path.has_extension(".cpp"sv))
continue;
return IterationDecision::Continue;
outln("Checking {}...", path.basename());
auto file_path = path.string();
auto ast_file_path = DeprecatedString::formatted("{}.ast", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
@ -75,5 +72,6 @@ TEST_CASE(test_regression)
auto equal = content == target_ast;
EXPECT(equal);
}
return IterationDecision::Continue;
}));
}

View file

@ -5,12 +5,12 @@
*/
#include <AK/LexicalPath.h>
#include <LibCore/DirIterator.h>
#include <LibCore/Directory.h>
#include <LibCore/File.h>
#include <LibCpp/Parser.h>
#include <LibTest/TestCase.h>
constexpr char TESTS_ROOT_DIR[] = "/home/anon/Tests/cpp-tests/preprocessor";
constexpr StringView TESTS_ROOT_DIR = "/home/anon/Tests/cpp-tests/preprocessor"sv;
static DeprecatedString read_all(DeprecatedString const& path)
{
@ -23,16 +23,13 @@ static DeprecatedString read_all(DeprecatedString const& path)
TEST_CASE(test_regression)
{
Core::DirIterator directory_iterator(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots);
while (directory_iterator.has_next()) {
auto file_path = directory_iterator.next_full_path();
auto path = LexicalPath { file_path };
MUST(Core::Directory::for_each_entry(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
auto path = LexicalPath::join(directory.path().string(), entry.name);
if (!path.has_extension(".cpp"sv))
continue;
return IterationDecision::Continue;
outln("Checking {}...", path.basename());
auto file_path = path.string();
auto ast_file_path = DeprecatedString::formatted("{}.txt", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
@ -49,5 +46,6 @@ TEST_CASE(test_regression)
for (size_t i = 0; i < tokens.size(); ++i) {
EXPECT_EQ(tokens[i].to_deprecated_string(), target_lines[i]);
}
}
return IterationDecision::Continue;
}));
}