diff --git a/README.md b/README.md index cc5c364..aeb3e35 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Detailed usage within the [disable_tags](https://github.com/funkwerk/gherkin_lin - [missing test action](https://github.com/funkwerk/gherkin_lint/blob/master/features/missing_test_action.feature) - [missing verification](https://github.com/funkwerk/gherkin_lint/blob/master/features/missing_verification.feature) - [same tag for all scenarios](https://github.com/funkwerk/gherkin_lint/blob/master/features/same_tag_for_all_scenarios.feature) + - [tag used multiple times](https://github.com/funkwerk/gherkin_lint/blob/master/features/tag_used_multiple_times.feature) - [too clumsy](https://github.com/funkwerk/gherkin_lint/blob/master/features/too_clumsy.feature) - [too long step](https://github.com/funkwerk/gherkin_lint/blob/master/features/too_long_step.feature) - [too many different tags](https://github.com/funkwerk/gherkin_lint/blob/master/features/too_many_different_tags.feature) diff --git a/Rakefile b/Rakefile index dcccd70..d37ca1f 100644 --- a/Rakefile +++ b/Rakefile @@ -11,7 +11,7 @@ end desc 'Publishes the Gem' task :push do - sh 'gem push gherkin_lint-0.5.0.gem' + sh 'gem push gherkin_lint-0.6.0.gem' end desc 'Checks ruby style' diff --git a/features/tag_used_multiple_times.feature b/features/tag_used_multiple_times.feature new file mode 100644 index 0000000..e760eb1 --- /dev/null +++ b/features/tag_used_multiple_times.feature @@ -0,0 +1,51 @@ +Feature: Tag Used Multiple Times + As a Business Analyst + I want to use tags just once + so that redundancy is minimized. + + Background: Prepare Testee + Given a file named "lint.rb" with: + """ + $LOAD_PATH << '../../lib' + require 'gherkin_lint' + + linter = GherkinLint::GherkinLint.new + linter.enable %w(TagUsedMultipleTimes) + linter.analyze 'lint.feature' + exit linter.report + + """ + + Scenario Outline: Tag used twice + Given a file named "lint.feature" with: + """ + + Feature: Test + @tag + Scenario: A + """ + When I run `ruby lint.rb` + Then it should fail with exactly: + """ + TagUsedMultipleTimes - Tag @tag used multiple times + lint.feature (4): Test.A + + """ + + Examples: Invalid Tag Combinations + | feature tag | scenario tag | + | @tag | | + | | @tag | + + Scenario: Just unique tags + Given a file named "lint.feature" with: + """ + Feature: Test + @tag_a @tag_b + Scenario: A + """ + When I run `ruby lint.rb` + Then it should pass with exactly: + """ + + """ diff --git a/gherkin_lint.gemspec b/gherkin_lint.gemspec index 944eb2d..90d0608 100644 --- a/gherkin_lint.gemspec +++ b/gherkin_lint.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'gherkin_lint' - s.version = '0.5.0' - s.date = '2016-08-31' + s.version = '0.6.0' + s.date = '2016-10-14' s.summary = 'Gherkin Lint' s.description = 'Lint Gherkin Files' s.authors = ['Stefan Rohe'] diff --git a/lib/gherkin_lint.rb b/lib/gherkin_lint.rb index 4d3f1f5..ae564f2 100644 --- a/lib/gherkin_lint.rb +++ b/lib/gherkin_lint.rb @@ -18,6 +18,7 @@ require 'gherkin_lint/linter/missing_test_action' require 'gherkin_lint/linter/missing_verification' require 'gherkin_lint/linter/same_tag_for_all_scenarios' +require 'gherkin_lint/linter/tag_used_multiple_times' require 'gherkin_lint/linter/too_clumsy' require 'gherkin_lint/linter/too_long_step' require 'gherkin_lint/linter/too_many_different_tags' @@ -52,6 +53,7 @@ class GherkinLint InvalidFileName, InvalidStepFlow, SameTagForAllScenarios, + TagUsedMultipleTimes, TooClumsy, TooManyDifferentTags, TooManySteps, diff --git a/lib/gherkin_lint/linter/tag_used_multiple_times.rb b/lib/gherkin_lint/linter/tag_used_multiple_times.rb new file mode 100644 index 0000000..a3609fc --- /dev/null +++ b/lib/gherkin_lint/linter/tag_used_multiple_times.rb @@ -0,0 +1,21 @@ +require 'gherkin_lint/linter' + +module GherkinLint + # service class to lint for tags used multiple times + class TagUsedMultipleTimes < Linter + def lint + scenarios do |file, feature, scenario| + references = [reference(file, feature, scenario)] + total_tags = tags(feature) + tags(scenario) + double_used_tags = total_tags.find_all { |a| total_tags.count(a) > 1 }.uniq! + next if double_used_tags.nil? + add_error(references, "Tag #{double_used_tags.join(' and ')} used multiple times") + end + end + + def tags(element) + return [] unless element.include? :tags + element[:tags].map { |a| a[:name] } + end + end +end