Skip to content

Commit

Permalink
linter_test_utils: allow testing that a LintViolation has no autofixes
Browse files Browse the repository at this point in the history
Autofixes might be undesirable in some scenarios, for example: they
change the behavior of the original code.
  • Loading branch information
IEncinas10 committed Nov 19, 2024
1 parent ca79bbf commit fc7f350
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/analysis/linter_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ void RunLintTestCases(std::initializer_list<LintTestCase> tests,
}

struct AutoFixInOut {
static constexpr int NO_FIX_AVAILABLE = -1;

absl::string_view code;
absl::string_view expected_output;
int fix_alternative = 0; // Some rules provide alternative fixes
Expand All @@ -139,6 +141,12 @@ void RunLintAutoFixCase(const AutoFixInOut &test,
CHECK_GT(violations.size(), test.violation_number);
const LintViolation &violation =
*std::next(violations.begin(), test.violation_number);

if (test.fix_alternative == AutoFixInOut::NO_FIX_AVAILABLE) {
CHECK_EQ(violation.autofixes.size(), 0);
return;
}

CHECK_GT(violation.autofixes.size(), test.fix_alternative);
const verible::AutoFix &fix = violation.autofixes[test.fix_alternative];
std::string fix_out = fix.Apply(analyzer.Data().Contents());
Expand Down

0 comments on commit fc7f350

Please sign in to comment.