2021-08-13 19:24:16 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/LexicalPath.h>
|
2023-03-02 20:03:53 +03:00
|
|
|
#include <LibCore/Directory.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-08-13 19:24:16 +03:00
|
|
|
#include <LibCpp/Parser.h>
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
|
2023-03-02 20:03:53 +03:00
|
|
|
constexpr StringView TESTS_ROOT_DIR = "/home/anon/Tests/cpp-tests/preprocessor"sv;
|
2021-08-13 19:24:16 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
static ByteString read_all(ByteString const& path)
|
2021-08-13 19:24:16 +03:00
|
|
|
{
|
2023-02-09 05:02:46 +03:00
|
|
|
auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));
|
2022-03-10 15:27:32 +03:00
|
|
|
auto file_size = MUST(file->size());
|
|
|
|
auto content = MUST(ByteBuffer::create_uninitialized(file_size));
|
2023-03-01 17:27:35 +03:00
|
|
|
MUST(file->read_until_filled(content.bytes()));
|
2023-12-16 17:19:34 +03:00
|
|
|
return ByteString { content.bytes() };
|
2021-08-13 19:24:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(test_regression)
|
|
|
|
{
|
2023-03-02 20:03:53 +03:00
|
|
|
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);
|
2022-07-11 20:32:29 +03:00
|
|
|
if (!path.has_extension(".cpp"sv))
|
2023-03-02 20:03:53 +03:00
|
|
|
return IterationDecision::Continue;
|
2021-08-13 19:24:16 +03:00
|
|
|
|
|
|
|
outln("Checking {}...", path.basename());
|
2023-03-02 20:03:53 +03:00
|
|
|
auto file_path = path.string();
|
2021-08-13 19:24:16 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
auto ast_file_path = ByteString::formatted("{}.txt", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
|
2021-08-13 19:24:16 +03:00
|
|
|
|
|
|
|
auto source = read_all(file_path);
|
|
|
|
auto target = read_all(ast_file_path);
|
|
|
|
|
|
|
|
StringView source_view(source);
|
|
|
|
Cpp::Preprocessor preprocessor(file_path, source_view);
|
|
|
|
|
|
|
|
auto target_lines = target.split_view('\n');
|
|
|
|
auto tokens = preprocessor.process_and_lex();
|
|
|
|
|
|
|
|
EXPECT_EQ(tokens.size(), target_lines.size());
|
|
|
|
for (size_t i = 0; i < tokens.size(); ++i) {
|
2023-12-16 17:19:34 +03:00
|
|
|
EXPECT_EQ(tokens[i].to_byte_string(), target_lines[i]);
|
2021-08-13 19:24:16 +03:00
|
|
|
}
|
2023-03-02 20:03:53 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
}));
|
2021-08-13 19:24:16 +03:00
|
|
|
}
|