| 55 | # function for the unit tests. We also verify each category we see |
| 56 | # is in cpplint._ERROR_CATEGORIES, to help keep that list up to date. |
| 57 | class ErrorCollector: |
| 58 | # These are a global list, covering all categories seen ever. |
| 59 | _ERROR_CATEGORIES = cpplint._ERROR_CATEGORIES |
| 60 | _SEEN_ERROR_CATEGORIES: set[str] = set() |
| 61 | |
| 62 | def __init__(self, assert_fn): |
| 63 | """assert_fn: a function to call when we notice a problem.""" |
| 64 | self._assert_fn = assert_fn |
| 65 | self._errors = [] |
| 66 | cpplint.ResetNolintSuppressions() |
| 67 | |
| 68 | def __call__(self, filename, linenum, category, confidence, message): |
| 69 | self._assert_fn( |
| 70 | category in self._ERROR_CATEGORIES, |
| 71 | 'Message "%s" has category "%s",' |
| 72 | " which is not in _ERROR_CATEGORIES" % (message, category), |
| 73 | ) |
| 74 | self._SEEN_ERROR_CATEGORIES.add(category) |
| 75 | if cpplint._ShouldPrintError(category, confidence, filename, linenum): |
| 76 | self._errors.append("%s [%s] [%d]" % (message, category, confidence)) |
| 77 | |
| 78 | def Results(self): |
| 79 | if len(self._errors) < 2: |
| 80 | return "".join(self._errors) # Most tests expect to have a string. |
| 81 | return self._errors # Let's give a list if there is more than one. |
| 82 | |
| 83 | def ResultList(self): |
| 84 | return self._errors |
| 85 | |
| 86 | def VerifyAllCategoriesAreSeen(self): |
| 87 | """Fails if there's a category in _ERROR_CATEGORIES~_SEEN_ERROR_CATEGORIES. |
| 88 | |
| 89 | This should only be called after all tests are run, so |
| 90 | _SEEN_ERROR_CATEGORIES has had a chance to fully populate. Since |
| 91 | this isn't called from within the normal unittest framework, we |
| 92 | can't use the normal unittest assert macros. Instead we just exit |
| 93 | when we see an error. Good thing this test is always run last! |
| 94 | """ |
| 95 | for category in self._ERROR_CATEGORIES: |
| 96 | if category not in self._SEEN_ERROR_CATEGORIES: |
| 97 | sys.exit('FATAL ERROR: There are no tests for category "%s"' % category) |
| 98 | |
| 99 | def RemoveIfPresent(self, substr): |
| 100 | for index, error in enumerate(self._errors): |
| 101 | if error.find(substr) != -1: |
| 102 | self._errors = self._errors[0:index] + self._errors[(index + 1) :] |
| 103 | break |
| 104 | |
| 105 | |
| 106 | # This class is a lame mock of codecs. We do not verify filename, mode, or |
no outgoing calls