Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tags shouldn't be used mutliple times for a scenario #4

Merged
merged 5 commits into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
51 changes: 51 additions & 0 deletions features/tag_used_multiple_times.feature
Original file line number Diff line number Diff line change
@@ -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 tag>
Feature: Test
@tag <scenario 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:
"""

"""
4 changes: 2 additions & 2 deletions gherkin_lint.gemspec
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
2 changes: 2 additions & 0 deletions lib/gherkin_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -52,6 +53,7 @@ class GherkinLint
InvalidFileName,
InvalidStepFlow,
SameTagForAllScenarios,
TagUsedMultipleTimes,
TooClumsy,
TooManyDifferentTags,
TooManySteps,
Expand Down
21 changes: 21 additions & 0 deletions lib/gherkin_lint/linter/tag_used_multiple_times.rb
Original file line number Diff line number Diff line change
@@ -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