Class: GherkinLint::UseBackground

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

Overview

service class to lint for using background

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

#expand_examples(examples, prototypes) ⇒ Object



55
56
57
58
59
60
# File 'lib/gherkin_lint/linter/use_background.rb', line 55

def expand_examples(examples, prototypes)
  examples.each do |example|
    prototypes = prototypes.map { |prototype| expand_outlines(prototype, example) }.flatten
  end
  prototypes
end

#expand_outlines(sentence, example) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gherkin_lint/linter/use_background.rb', line 62

def expand_outlines(sentence, example)
  result = []
  headers = example[:tableHeader][:cells].map { |cell| cell[:value] }
  example[:tableBody].each do |row| # .slice(1, example[:tableBody].length).each do |row|
    modified_sentence = sentence.dup
    headers.zip(row[:cells].map { |cell| cell[:value] }).map do |key, value|
      modified_sentence.gsub!("<#{key}>", value)
    end
    result.push modified_sentence
  end
  result
end

#expanded_steps(feature) ⇒ Object



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

def expanded_steps(feature)
  feature[:children].each do |scenario|
    next unless scenario[:type] != :Background
    next unless scenario.include? :steps
    next if scenario[:steps].empty?
    prototypes = [render_step(scenario[:steps].first)]
    prototypes = expand_examples(scenario[:examples], prototypes) if scenario.key? :examples
    prototypes.each { |prototype| yield prototype }
  end
end

#gather_givens(feature) ⇒ Object



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

def gather_givens(feature)
  return unless feature.include? :children
  has_non_given_step = false
  feature[:children].each do |scenario|
    next unless scenario.include? :steps
    next if scenario[:steps].empty?
    has_non_given_step = true unless scenario[:steps].first[:keyword] == 'Given '
  end
  return if has_non_given_step

  result = []
  expanded_steps(feature) { |given| result.push given }
  result
end

#lintObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/gherkin_lint/linter/use_background.rb', line 6

def lint
  features do |file, feature|
    next if scenarios_with_steps(feature) <= 1
    givens = gather_givens feature
    next if givens.nil?
    next if givens.length <= 1
    next if givens.uniq.length > 1
    references = [reference(file, feature)]
    add_error(references, "Step '#{givens.uniq.first}' should be part of background")
  end
end

#scenarios_with_steps(feature) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/gherkin_lint/linter/use_background.rb', line 18

def scenarios_with_steps(feature)
  scenarios = 0
  return 0 unless feature.key? :children
  feature[:children].each do |scenario|
    next unless scenario.include? :steps
    next if scenario[:steps].empty?
    scenarios += 1
  end
  scenarios
end