coverage_helper.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef CARBON_TOOLCHAIN_TESTING_COVERAGE_HELPER_H_
  5. #define CARBON_TOOLCHAIN_TESTING_COVERAGE_HELPER_H_
  6. #include <gtest/gtest.h>
  7. #include <fstream>
  8. #include <string>
  9. #include "common/set.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "re2/re2.h"
  12. namespace Carbon::Testing {
  13. // Looks for kinds that aren't covered by a file_test in the manifest path.
  14. // Kinds are identified by the provided regular expression kind_pattern.
  15. //
  16. // should_be_covered should return false when a kind is either untestable or not
  17. // yet tested.
  18. template <typename KindT>
  19. auto TestKindCoverage(const std::string& manifest_path,
  20. llvm::StringLiteral kind_pattern,
  21. llvm::ArrayRef<KindT> kinds,
  22. llvm::ArrayRef<KindT> untested_kinds) {
  23. std::ifstream manifest_in(manifest_path.c_str());
  24. ASSERT_TRUE(manifest_in.good());
  25. RE2 kind_re(kind_pattern.data());
  26. ASSERT_TRUE(kind_re.ok()) << kind_re.error();
  27. Set<std::string> covered_kinds;
  28. std::string test_filename;
  29. while (std::getline(manifest_in, test_filename)) {
  30. std::ifstream test_in(test_filename);
  31. ASSERT_TRUE(test_in.good());
  32. std::string line;
  33. while (std::getline(test_in, line)) {
  34. std::string kind;
  35. if (RE2::PartialMatch(line, kind_re, &kind)) {
  36. covered_kinds.Insert(kind);
  37. }
  38. }
  39. }
  40. llvm::SmallVector<llvm::StringRef> missing_kinds;
  41. for (auto kind : kinds) {
  42. if (llvm::find(untested_kinds, kind) != untested_kinds.end()) {
  43. EXPECT_FALSE(covered_kinds.Erase(kind.name()))
  44. << "Kind " << kind
  45. << " has coverage even though none was expected. If this has "
  46. "changed, update the coverage test.";
  47. continue;
  48. }
  49. if (!covered_kinds.Erase(kind.name())) {
  50. missing_kinds.push_back(kind.name());
  51. }
  52. }
  53. constexpr llvm::StringLiteral Bullet = "\n - ";
  54. std::sort(missing_kinds.begin(), missing_kinds.end());
  55. EXPECT_TRUE(missing_kinds.empty()) << "Some kinds have no tests:" << Bullet
  56. << llvm::join(missing_kinds, Bullet);
  57. llvm::SmallVector<std::string> unexpected_matches;
  58. covered_kinds.ForEach(
  59. [&](const std::string& match) { unexpected_matches.push_back(match); });
  60. std::sort(unexpected_matches.begin(), unexpected_matches.end());
  61. EXPECT_TRUE(unexpected_matches.empty())
  62. << "Matched things that aren't in the kind list:" << Bullet
  63. << llvm::join(unexpected_matches, Bullet);
  64. }
  65. } // namespace Carbon::Testing
  66. #endif // CARBON_TOOLCHAIN_TESTING_COVERAGE_HELPER_H_