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, descendants, #elements, #features, #files, #filled_scenarios, #filter_tag, #initialize, #line, #lint_files, #name, #reference, #render_step, #render_step_argument, #scenarios, #steps, #suppress, #suppress_tags, #tag?

Constructor Details

This class inherits a constructor from GherkinLint::Linter

Instance Method Details

#gather_same_tags(feature) ⇒ Object



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

def gather_same_tags(feature)
  result = nil
  feature[:children].each do |scenario|
    next if scenario[:type] == :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



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

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
12
13
# File 'lib/gherkin_lint/linter/same_tag_for_all_scenarios.rb', line 6

def lint
  features do |file, feature|
    next unless feature.include? :children

    lint_scenarios file, feature
    lint_examples file, feature
  end
end

#lint_examples(file, feature) ⇒ Object



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

def lint_examples(file, feature)
  feature[:children].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



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

def lint_scenarios(file, feature)
  tags = gather_same_tags feature
  return if tags.nil?
  return if tags.empty?
  return unless feature[:children].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