class Chutney::UseBackground

service class to lint for using background

Public Instance Methods

expand_examples(examples, prototypes) click to toggle source
# File lib/chutney/linter/use_background.rb, line 43
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) click to toggle source
# File lib/chutney/linter/use_background.rb, line 50
def expand_outlines(sentence, example)
  result = []
  headers = example.rows.first.cells.map(&:value)
  example.rows.each_with_index do |row, idx|
    next if idx.zero? # skip the header

    modified_sentence = sentence.dup
    headers.zip(row.cells.map(&:value)).map do |key, value|
      modified_sentence.gsub!("<#{key}>", value)
    end
    result.push modified_sentence
  end
  result
end
expanded_steps(&block) click to toggle source
# File lib/chutney/linter/use_background.rb, line 33
def expanded_steps(&block)
  scenarios do |_feature, scenario|
    next unless scenario.steps

    prototypes = [render_step(scenario.steps.first)]
    prototypes = expand_examples(scenario.examples, prototypes) if scenario.is_a? CukeModeler::Outline
    prototypes.each(&block)
  end
end
gather_givens() click to toggle source
# File lib/chutney/linter/use_background.rb, line 17
def gather_givens
  return unless feature.children

  has_non_given_step = false
  scenarios do |_feature, scenario|
    next unless scenario.steps

    has_non_given_step = true unless given_word?(scenario.steps.first.keyword)
  end
  return if has_non_given_step

  result = []
  expanded_steps { |given| result.push given }
  result
end
lint() click to toggle source
# File lib/chutney/linter/use_background.rb, line 6
def lint
  return unless filled_scenarios.count > 1

  givens = gather_givens
  return if givens.nil?
  return if givens.length <= 1
  return if givens.uniq.length > 1

  add_issue(I18n.t('linters.use_background', step: givens.uniq.first), feature)
end