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#include <unistd.h>
13
14constexpr StringView TESTS_ROOT_DIR = "/home/anon/Tests/cpp-tests/parser"sv;
15
16static DeprecatedString read_all(DeprecatedString const& path)
17{
18 auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));
19 auto file_size = MUST(file->size());
20 auto content = MUST(ByteBuffer::create_uninitialized(file_size));
21 MUST(file->read_until_filled(content.bytes()));
22 return DeprecatedString { content.bytes() };
23}
24
25TEST_CASE(test_regression)
26{
27 MUST(Core::Directory::for_each_entry(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
28 auto path = LexicalPath::join(directory.path().string(), entry.name);
29 if (!path.has_extension(".cpp"sv))
30 return IterationDecision::Continue;
31
32 outln("Checking {}...", path.basename());
33 auto file_path = path.string();
34
35 auto ast_file_path = DeprecatedString::formatted("{}.ast", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
36
37 auto source = read_all(file_path);
38 auto target_ast = read_all(ast_file_path);
39
40 StringView source_view(source);
41 Cpp::Preprocessor preprocessor(file_path, source_view);
42 Cpp::Parser parser(preprocessor.process_and_lex(), file_path);
43 auto root = parser.parse();
44
45 EXPECT(parser.errors().is_empty());
46
47 int pipefd[2] = {};
48 if (pipe(pipefd) < 0) {
49 perror("pipe");
50 exit(1);
51 }
52
53 FILE* input_stream = fdopen(pipefd[0], "r");
54 FILE* output_stream = fdopen(pipefd[1], "w");
55
56 root->dump(output_stream);
57
58 fclose(output_stream);
59
60 ByteBuffer buffer;
61 while (!feof(input_stream)) {
62 char chunk[4096];
63 size_t size = fread(chunk, sizeof(char), sizeof(chunk), input_stream);
64 if (size == 0)
65 break;
66 buffer.append(chunk, size);
67 }
68
69 fclose(input_stream);
70
71 DeprecatedString content { reinterpret_cast<char const*>(buffer.data()), buffer.size() };
72
73 auto equal = content == target_ast;
74 EXPECT(equal);
75 return IterationDecision::Continue;
76 }));
77}