test_runner.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <tree_sitter/api.h>
  5. #include <tree_sitter/parser.h>
  6. #include <filesystem>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <sstream>
  10. #include <string>
  11. #include <vector>
  12. extern "C" {
  13. TSLanguage* tree_sitter_carbon();
  14. }
  15. // Reads a file to string.
  16. static auto ReadFile(std::filesystem::path path) -> std::string {
  17. std::ifstream file(path);
  18. std::stringstream buffer;
  19. buffer << file.rdbuf();
  20. file.close();
  21. return buffer.str();
  22. }
  23. // TODO: use file_test.cpp
  24. auto main(int argc, char** argv) -> int {
  25. if (argc < 2) {
  26. std::cerr << "Usage: treesitter_carbon_tester <file>...\n";
  27. return 2;
  28. }
  29. auto* parser = ts_parser_new();
  30. ts_parser_set_language(parser, tree_sitter_carbon());
  31. std::vector<std::string> failed;
  32. std::vector<std::string> skipped;
  33. for (int i = 1; i < argc; i++) {
  34. std::string file_path = argv[i];
  35. std::string source = ReadFile(file_path);
  36. // `and` in where clauses is not parsed correctly.
  37. // TODO: remove once where clause is implemented correctly.
  38. if (source.find("where") != std::string::npos &&
  39. source.find("and") != std::string::npos) {
  40. skipped.push_back(file_path);
  41. continue;
  42. }
  43. auto* tree =
  44. ts_parser_parse_string(parser, nullptr, source.data(), source.size());
  45. auto root = ts_tree_root_node(tree);
  46. auto has_error = ts_node_has_error(root);
  47. char* node_debug = ts_node_string(root);
  48. std::cout << file_path << ":\n" << node_debug << "\n";
  49. if (has_error) {
  50. failed.push_back(file_path);
  51. }
  52. free(node_debug);
  53. ts_tree_delete(tree);
  54. }
  55. ts_parser_delete(parser);
  56. for (const auto& file : skipped) {
  57. std::cout << "SKIPPED " << file << "\n";
  58. }
  59. for (const auto& file : failed) {
  60. std::cout << "FAILED " << file << "\n";
  61. }
  62. if (!skipped.empty()) {
  63. std::cout << skipped.size() << " tests skipped.\n";
  64. }
  65. if (!failed.empty()) {
  66. std::cout << failed.size() << " tests failing.\n";
  67. return 1;
  68. }
  69. }