From 1a17752342108ecf9552ceb31e81a80ef77cd159 Mon Sep 17 00:00:00 2001 From: Wojciech Sipak Date: Wed, 15 Sep 2021 17:58:08 +0200 Subject: [PATCH] Add tests for rule aliases --- verilog/analysis/lint_rule_registry_test.cc | 33 +++++++++++++++ .../verilog_linter_configuration_test.cc | 40 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/verilog/analysis/lint_rule_registry_test.cc b/verilog/analysis/lint_rule_registry_test.cc index cdadb76b74..5ddfe2ee23 100644 --- a/verilog/analysis/lint_rule_registry_test.cc +++ b/verilog/analysis/lint_rule_registry_test.cc @@ -68,6 +68,19 @@ class TreeRule1 : public TreeRuleBase { }; return d; } + static const std::vector& GetAliasDescriptors() { + static const std::vector d{ + { + .name = "rule1-alias1", + .param_defaults = {{"test_param1", "false"}}, + }, + { + .name = "rule1-alias2", + .param_defaults = {{"test_param1", "false"}}, + }, + }; + return d; + } }; class TreeRule2 : public TreeRuleBase { @@ -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 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); diff --git a/verilog/analysis/verilog_linter_configuration_test.cc b/verilog/analysis/verilog_linter_configuration_test.cc index 9223a4cdca..0be75c9682 100644 --- a/verilog/analysis/verilog_linter_configuration_test.cc +++ b/verilog/analysis/verilog_linter_configuration_test.cc @@ -45,6 +45,7 @@ namespace verilog { namespace { +using analysis::LintRuleAliasDescriptor; using analysis::LintRuleDescriptor; using verible::LineLintRule; using verible::SyntaxTreeLintRule; @@ -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& GetAliasDescriptors() { + static const std::vector 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; } @@ -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";