From fc7f35072ae92199e05e4e5b375a0e02bfb7ce7f Mon Sep 17 00:00:00 2001 From: Ignacio Encinas Date: Tue, 19 Nov 2024 19:43:19 +0100 Subject: [PATCH] linter_test_utils: allow testing that a LintViolation has no autofixes Autofixes might be undesirable in some scenarios, for example: they change the behavior of the original code. --- common/analysis/linter_test_utils.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common/analysis/linter_test_utils.h b/common/analysis/linter_test_utils.h index f0b3431b3..76031c97e 100644 --- a/common/analysis/linter_test_utils.h +++ b/common/analysis/linter_test_utils.h @@ -115,6 +115,8 @@ void RunLintTestCases(std::initializer_list 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 @@ -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());