test_runner.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <cstdlib>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include "testing/base/file_helpers.h"
  10. #include "utils/tree_sitter/src/tree_sitter/parser.h"
  11. extern "C" {
  12. auto tree_sitter_carbon() -> TSLanguage*;
  13. }
  14. // TODO: use file_test.cpp
  15. auto main(int argc, char** argv) -> int {
  16. if (argc < 2) {
  17. std::cerr << "Usage: test_runner <file>...\n";
  18. return 2;
  19. }
  20. auto* parser = ts_parser_new();
  21. ts_parser_set_language(parser, tree_sitter_carbon());
  22. bool fail_tests = std::getenv("FAIL_TESTS") != nullptr;
  23. std::vector<std::string> incorrect;
  24. for (int i = 1; i < argc; i++) {
  25. std::string file_path = argv[i];
  26. std::string source = std::move(*Carbon::Testing::ReadFile(file_path));
  27. auto* tree =
  28. ts_parser_parse_string(parser, nullptr, source.data(), source.size());
  29. auto root = ts_tree_root_node(tree);
  30. auto has_error = ts_node_has_error(root);
  31. char* node_debug = ts_node_string(root);
  32. std::cout << file_path << ":\n" << node_debug << "\n";
  33. if (has_error ^ fail_tests) {
  34. incorrect.push_back(file_path);
  35. }
  36. free(node_debug);
  37. ts_tree_delete(tree);
  38. }
  39. ts_parser_delete(parser);
  40. for (const auto& file : incorrect) {
  41. if (fail_tests) {
  42. std::cout << "INCORRECTLY PASSING " << file << "\n";
  43. } else {
  44. std::cout << "FAILED " << file << "\n";
  45. }
  46. }
  47. if (!incorrect.empty()) {
  48. if (fail_tests) {
  49. std::cout << incorrect.size() << " tests incorrectly passing.\n";
  50. } else {
  51. std::cout << incorrect.size() << " tests failing.\n";
  52. }
  53. return 1;
  54. }
  55. }