test-cpp-preprocessor.cpp 1.7 KB

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