Skip to content

Commit

Permalink
Provide a RunConfiguredLintTestCases() to be able to
Browse files Browse the repository at this point in the history
test lint rules with configuration.

issues #133

PiperOrigin-RevId: 298741802
  • Loading branch information
hzeller committed Mar 4, 2020
1 parent 91e7cc7 commit b920203
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 15 additions & 6 deletions common/analysis/linter_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,29 @@ void RunLintTestCase(const LintTestCase& test,
<< absl::StrCat("code:\n", test.code, "\nDiffs:\n", diffs.str(), "\n");
}

// Accepts an array of LintTestCases and tests them all on a linter
// containing rule generated by make_rule
// Accepts an array of LintTestCases and tests them all on a linter containing
// rule generated by make_rule with a particular configuration.
template <class AnalyzerType, class RuleClass>
void RunLintTestCases(std::initializer_list<LintTestCase> tests,
const std::string& filename = "<<inline-test>>") {
void RunConfiguredLintTestCases(
std::initializer_list<LintTestCase> tests, absl::string_view configuration,
const std::string& filename = "<<inline-test>>") {
typedef typename RuleClass::rule_type rule_type;
auto rule_generator = []() {
return std::unique_ptr<rule_type>(new RuleClass());
auto rule_generator = [&configuration]() -> std::unique_ptr<rule_type> {
std::unique_ptr<rule_type> instance(new RuleClass());
absl::Status config_status = instance->Configure(configuration);
CHECK(config_status.ok()) << config_status.message();
return instance;
};
for (const auto& test : tests) {
RunLintTestCase<AnalyzerType, rule_type>(test, rule_generator, filename);
}
}

template <class AnalyzerType, class RuleClass>
void RunLintTestCases(std::initializer_list<LintTestCase> tests,
const std::string& filename = "<<inline-test>>") {
RunConfiguredLintTestCases<AnalyzerType, RuleClass>(tests, "", filename);
}
} // namespace verible

#endif // VERIBLE_COMMON_ANALYSIS_LINTER_TEST_UTILS_H_
10 changes: 10 additions & 0 deletions verilog/analysis/checkers/line_length_rule_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace analysis {
namespace {

using verible::LintTestCase;
using verible::RunConfiguredLintTestCases;
using verible::RunLintTestCases;

TEST(LineLengthRuleTest, Configuration) {
Expand Down Expand Up @@ -189,6 +190,15 @@ TEST(LineLengthRuleTest, RejectsText) {
RunLintTestCases<VerilogAnalyzer, LineLengthRule>(kTestCases);
}

TEST(LineLengthRuleTest, RejectsTextConfigured) {
const std::initializer_list<LintTestCase> kTestCases = {
{"aaaaaaaaaabbbbbbbbbbcccc cccccdddddddddd", {TK_OTHER, "X"}, "\n"},
{"aaaaaaaaaabbbbbbbbbbcccc cccccdddddddddd\n"}, // shorter line ok.
};
RunConfiguredLintTestCases<VerilogAnalyzer, LineLengthRule>(kTestCases,
"length:40");
}

#if 0
TEST(LineLengthRuleTest, Encrypted) {
const LintTestCase kTestCases[] = {
Expand Down

0 comments on commit b920203

Please sign in to comment.