coverage_helper.h 2.7 KB

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