Skip to content

Commit

Permalink
Add tests for rule aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
wsipak committed Oct 4, 2021
1 parent a11a0f3 commit cd0fa0d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
33 changes: 33 additions & 0 deletions verilog/analysis/lint_rule_registry_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ class TreeRule1 : public TreeRuleBase {
};
return d;
}
static const std::vector<LintRuleAliasDescriptor>& GetAliasDescriptors() {
static const std::vector<LintRuleAliasDescriptor> d{
{
.name = "rule1-alias1",
.param_defaults = {{"test_param1", "false"}},
},
{
.name = "rule1-alias2",
.param_defaults = {{"test_param1", "false"}},
},
};
return d;
}
};

class TreeRule2 : public TreeRuleBase {
Expand Down Expand Up @@ -240,6 +253,26 @@ TEST(LintRuleRegistryTest, ContainsTextRuleTrue) {
EXPECT_TRUE(IsRegisteredLintRule("text-rule-1"));
}

// Verifies that the alias can be translated to a registered rule name.
TEST(LintRuleRegistryTest, CanBeAliasedTrue) {
EXPECT_TRUE(IsRegisteredLintRule(TranslateAliasIfExists("rule1-alias1")));
EXPECT_TRUE(IsRegisteredLintRule(TranslateAliasIfExists("rule1-alias2")));
}

// Verifies that an invalid alias doesn't translate to a registered rule name
TEST(LintRuleRegistryTest, TranslateInvalidAlias) {
EXPECT_FALSE(IsRegisteredLintRule(TranslateAliasIfExists("invalid-alias")));
}

// Verifies that aliases match the right rule
TEST(LintRuleRegistryTest, AliasedCorrectly) {
std::set<LintRuleId> aliases = GetLintRuleAliases("test-rule-1");
EXPECT_NE(aliases.find("rule1-alias1"), aliases.end());
EXPECT_NE(aliases.find("rule1-alias2"), aliases.end());
EXPECT_EQ(TranslateAliasIfExists("rule1-alias1"), "test-rule-1");
EXPECT_EQ(TranslateAliasIfExists("rule1-alias2"), "test-rule-1");
}

// Verifies that a nonexistent text-structure-based rule yields a nullptr.
TEST(LintRuleRegistryTest, CreateTextLintRuleInvalid) {
EXPECT_EQ(CreateTextStructureLintRule("invalid-id"), nullptr);
Expand Down
40 changes: 40 additions & 0 deletions verilog/analysis/verilog_linter_configuration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
namespace verilog {
namespace {

using analysis::LintRuleAliasDescriptor;
using analysis::LintRuleDescriptor;
using verible::LineLintRule;
using verible::SyntaxTreeLintRule;
Expand Down Expand Up @@ -73,6 +74,20 @@ class TestRule1 : public TestRuleBase {
static const LintRuleDescriptor d{
.name = "test-rule-1",
.desc = "TestRule1",
.param = {{"test_param1", "true", "test rule parameter"}},
};
return d;
}
static const std::vector<LintRuleAliasDescriptor>& GetAliasDescriptors() {
static const std::vector<LintRuleAliasDescriptor> d{
{
.name = "test-rule-1-alias-1",
.param_defaults = {{"test_param1", "false"}},
},
{
.name = "test-rule-1-alias-2",
.param_defaults = {{"test_param1", "true"}},
},
};
return d;
}
Expand Down Expand Up @@ -654,6 +669,31 @@ TEST(RuleBundleTest, ParseRuleBundleEmpty) {
EXPECT_TRUE(bundle.rules.empty());
}

TEST(RuleBundleTest, ParseRuleWithAlias) {
auto text = "+test-rule-1-alias-1";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(1));
EXPECT_TRUE(error.empty());

EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
}

TEST(RuleBundleTest, ParseRuleWithAliases) {
auto text = "+test-rule-1,-test-rule-1-alias-1,+test-rule-1-alias-2";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(1));
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
// aliases have default configuration for this rule
EXPECT_FALSE(bundle.rules["test-rule-1"].configuration.empty());
}

TEST(RuleBundleTest, ParseRuleBundleAcceptSeveral) {
// Allow for an optional '+' to enable a rule for symmetry with '-' disable
constexpr absl::string_view text = "test-rule-1,test-rule-2,+test-rule-3";
Expand Down

0 comments on commit cd0fa0d

Please sign in to comment.