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, #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

#expand_examples(examples, prototypes) ⇒ Object



52
53
54
55
56
57
# File 'lib/gherkin_lint/linter/use_background.rb', line 52

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/gherkin_lint/linter/use_background.rb', line 59

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

#expanded_steps(feature) ⇒ Object



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

def expanded_steps(feature)
  feature['elements'].each do |scenario|
    next unless scenario['keyword'] != 'Background'
    next unless scenario.include? 'steps'
    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



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

def gather_givens(feature)
  return unless feature.include? 'elements'
  has_non_given_step = false
  feature['elements'].each do |scenario|
    next unless scenario.include? 'steps'
    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
# File 'lib/gherkin_lint/linter/use_background.rb', line 18

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