Class: RuboCop::Cop::RSpec::AggregateFailures

Inherits:
Cop
  • Object
show all
Defined in:
lib/test_prof/cops/rspec/aggregate_failures.rb

Overview

Rejects and auto-corrects the usage of one-liners examples in favour of :aggregate_failures feature.

Example:

# bad
it { is_expected.to be_success }
it { is_expected.to have_header('X-TOTAL-PAGES', 10) }
it { is_expected.to have_header('X-NEXT-PAGE', 2) }

# good
it "returns the second page", :aggregate_failures do
  is_expected.to be_success
  is_expected.to have_header('X-TOTAL-PAGES', 10)
  is_expected.to have_header('X-NEXT-PAGE', 2)
end

Constant Summary collapse

GROUP_BLOCKS =
%i[
  describe context feature example_group
  xdescribe xcontext xfeature
  fdescribe fcontext ffeature
].freeze
EXAMPLE_BLOCKS =
%i[
  it specify example scenario its
  fit fspecify fexample fscenario focus
  xit xspecify xexample xscenario ski
  pending
].freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 56

def autocorrect(node)
  _method, _args, body = *node
  iter = body.children.each

  first_example = loop do
    child = iter.next
    break child if oneliner?(child)
  end

  base_indent = " " * first_example.source_range.column

  replacements = [
    header_from(first_example),
    body_from(first_example, base_indent)
  ]

  last_example = nil

  loop do
    child = iter.next
    break unless oneliner?(child)
    last_example = child
    replacements << body_from(child, base_indent)
  end

  replacements << "#{base_indent}end"

  range = first_example.source_range.begin.join(
    last_example.source_range.end
  )

  replacement = replacements.join("\n")

  lambda do |corrector|
    corrector.replace(range, replacement)
  end
end

#on_block(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 40

def on_block(node)
  method, _args, body = *node
  return unless body&.begin_type?

  _receiver, method_name, _object = *method
  return unless GROUP_BLOCKS.include?(method_name)

  return if check_node(body)

  add_offense(
    node,
    :expression,
    'Use :aggregate_failures instead of several one-liners.'
  )
end