Serenity Operating System
1/*
2 * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/LexicalPath.h>
8#include <LibCore/Directory.h>
9#include <LibCore/File.h>
10#include <LibCpp/Parser.h>
11#include <LibTest/TestCase.h>
12
13constexpr StringView TESTS_ROOT_DIR = "/home/anon/Tests/cpp-tests/preprocessor"sv;
14
15static DeprecatedString read_all(DeprecatedString const& path)
16{
17 auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));
18 auto file_size = MUST(file->size());
19 auto content = MUST(ByteBuffer::create_uninitialized(file_size));
20 MUST(file->read_until_filled(content.bytes()));
21 return DeprecatedString { content.bytes() };
22}
23
24TEST_CASE(test_regression)
25{
26 MUST(Core::Directory::for_each_entry(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
27 auto path = LexicalPath::join(directory.path().string(), entry.name);
28 if (!path.has_extension(".cpp"sv))
29 return IterationDecision::Continue;
30
31 outln("Checking {}...", path.basename());
32 auto file_path = path.string();
33
34 auto ast_file_path = DeprecatedString::formatted("{}.txt", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
35
36 auto source = read_all(file_path);
37 auto target = read_all(ast_file_path);
38
39 StringView source_view(source);
40 Cpp::Preprocessor preprocessor(file_path, source_view);
41
42 auto target_lines = target.split_view('\n');
43 auto tokens = preprocessor.process_and_lex();
44
45 EXPECT_EQ(tokens.size(), target_lines.size());
46 for (size_t i = 0; i < tokens.size(); ++i) {
47 EXPECT_EQ(tokens[i].to_deprecated_string(), target_lines[i]);
48 }
49 return IterationDecision::Continue;
50 }));
51}