Class: GherkinLint::SameTagForAllScenarios

Inherits:
Linter
  • Object
show all
Defined in:
lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb

Overview

service class to lint for using same tag on all scenarios

Instance Attribute Summary

Attributes inherited from Linter

#issues

Instance Method Summary collapse

Methods inherited from Linter

#add_error, #add_warning, #backgrounds, #elements, #features, #files, #filled_scenarios, #filter_tag, #gather_tags, #initialize, #line, #lint_files, #name, #reference, #render_step, #scenarios, #steps, #suppress, #suppress_tags, #tag?

Constructor Details

This class inherits a constructor from GherkinLint::Linter

Instance Method Details

#gather_same_tags(feature) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb', line 40

def gather_same_tags(feature)
  result = nil
  return result unless feature.include? 'elements'
  feature['elements'].each do |scenario|
    next if scenario['keyword'] == 'Background'
    return nil unless scenario.include? 'tags'
    tags = scenario['tags'].map { |tag| tag['name'] }
    result = tags if result.nil?
    result &= tags
  end
  result
end

#gather_same_tags_for_outline(scenario) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb', line 53

def gather_same_tags_for_outline(scenario)
  result = nil
  return result unless scenario.include? 'examples'
  scenario['examples'].each do |example|
    return nil unless example.include? 'tags'
    tags = example['tags'].map { |tag| tag['name'] }
    result = tags if result.nil?
    result &= tags
  end
  result
end

#lintObject



6
7
8
9
10
11
# File 'lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb', line 6

def lint
  features do |file, feature|
    lint_scenarios file, feature
    lint_examples file, feature
  end
end

#lint_examples(file, feature) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb', line 26

def lint_examples(file, feature)
  feature['elements'].each do |scenario|
    tags = gather_same_tags_for_outline scenario
    next if tags.nil? || tags.empty?
    next unless scenario['examples'].length > 1
    references = [reference(file, feature, scenario)]
    tags.each do |tag|
      next if tag == '@skip'

      add_error(references, "Tag '#{tag}' should be used at Scenario Outline level")
    end
  end
end

#lint_scenarios(file, feature) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb', line 13

def lint_scenarios(file, feature)
  tags = gather_same_tags feature
  return if tags.nil?
  return if tags.length < 1
  return unless feature['elements'].length > 1
  references = [reference(file, feature)]
  tags.each do |tag|
    next if tag == '@skip'

    add_error(references, "Tag '#{tag}' should be used at Feature level")
  end
end