test-cpp-preprocessor.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/File.h>
  9. #include <LibCpp/Parser.h>
  10. #include <LibTest/TestCase.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. constexpr char TESTS_ROOT_DIR[] = "/home/anon/cpp-tests/preprocessor";
  16. static String read_all(const String& path)
  17. {
  18. auto result = Core::File::open(path, Core::OpenMode::ReadOnly);
  19. VERIFY(!result.is_error());
  20. auto content = result.value()->read_all();
  21. return { reinterpret_cast<const char*>(content.data()), content.size() };
  22. }
  23. TEST_CASE(test_regression)
  24. {
  25. Core::DirIterator directory_iterator(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots);
  26. while (directory_iterator.has_next()) {
  27. auto file_path = directory_iterator.next_full_path();
  28. auto path = LexicalPath { file_path };
  29. if (!path.has_extension(".cpp"))
  30. continue;
  31. outln("Checking {}...", path.basename());
  32. auto ast_file_path = String::formatted("{}.txt", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
  33. auto source = read_all(file_path);
  34. auto target = read_all(ast_file_path);
  35. StringView source_view(source);
  36. Cpp::Preprocessor preprocessor(file_path, source_view);
  37. auto target_lines = target.split_view('\n');
  38. auto tokens = preprocessor.process_and_lex();
  39. EXPECT_EQ(tokens.size(), target_lines.size());
  40. for (size_t i = 0; i < tokens.size(); ++i) {
  41. EXPECT_EQ(tokens[i].to_string(), target_lines[i]);
  42. }
  43. }
  44. }