test-cpp-parser.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/parser";
  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("{}.ast", file_path.substring(0, file_path.length() - sizeof(".cpp") + 1));
  33. auto source = read_all(file_path);
  34. auto target_ast = read_all(ast_file_path);
  35. StringView source_view(source);
  36. ::Cpp::Parser parser(source_view, file_path);
  37. auto root = parser.parse();
  38. EXPECT(parser.errors().is_empty());
  39. int pipefd[2] = {};
  40. if (pipe(pipefd) < 0) {
  41. perror("pipe");
  42. exit(1);
  43. }
  44. FILE* input_stream = fdopen(pipefd[0], "r");
  45. FILE* output_stream = fdopen(pipefd[1], "w");
  46. root->dump(output_stream);
  47. fclose(output_stream);
  48. ByteBuffer buffer;
  49. while (!feof(input_stream)) {
  50. char chunk[4096];
  51. size_t size = fread(chunk, sizeof(char), sizeof(chunk), input_stream);
  52. if (size == 0)
  53. break;
  54. buffer.append(chunk, size);
  55. }
  56. fclose(input_stream);
  57. String content { reinterpret_cast<const char*>(buffer.data()), buffer.size() };
  58. auto equal = content == target_ast;
  59. EXPECT(equal);
  60. }
  61. }