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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


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

def supported?
  return @supported if instance_variable_defined?(:@supported)
  @supported = TestProf::Utils.verify_gem_version('rubocop', at_least: '0.51.0')

  unless @supported
    warn "RSpec/AggregateFailures cop requires RuboCop >= 0.51.0. Skipping"
  end

  @supported
end

Instance Method Details

#autocorrect(node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 72

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 54

def on_block(node)
  return unless self.class.supported?

  method, _args, body = *node
  return unless body && body.begin_type?

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

  return if check_node(body)

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