test-cpp-preprocessor.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/cpp-tests/preprocessor";
  12. static String read_all(String 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. if (!file->read_or_error(content.bytes()))
  18. VERIFY_NOT_REACHED();
  19. return String { content.bytes() };
  20. }
  21. TEST_CASE(test_regression)
  22. {
  23. Core::DirIterator directory_iterator(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots);
  24. while (directory_iterator.has_next()) {
  25. auto file_path = directory_iterator.next_full_path();
  26. auto path = LexicalPath { file_path };
  27. if (!path.has_extension(".cpp"))
  28. continue;
  29. outln("Checking {}...", path.basename());
  30. auto ast_file_path = String::formatted("{}.txt", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
  31. auto source = read_all(file_path);
  32. auto target = read_all(ast_file_path);
  33. StringView source_view(source);
  34. Cpp::Preprocessor preprocessor(file_path, source_view);
  35. auto target_lines = target.split_view('\n');
  36. auto tokens = preprocessor.process_and_lex();
  37. EXPECT_EQ(tokens.size(), target_lines.size());
  38. for (size_t i = 0; i < tokens.size(); ++i) {
  39. EXPECT_EQ(tokens[i].to_string(), target_lines[i]);
  40. }
  41. }
  42. }