Class: RuboCop::Cop::RSpec::AggregateFailures
- Inherits:
-
Cop
- Object
- Cop
- RuboCop::Cop::RSpec::AggregateFailures
- 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) }
its(:status) { is_expected.to eq(200) }
# 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)
expect(subject.status).to eq(200)
end
Constant Summary collapse
- GROUP_BLOCKS =
%i[ describe context feature example_group ].freeze
- EXAMPLE_BLOCKS =
%i[ it its specify example scenario ].freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.supported? ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 39 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
69 70 71 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 |
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 69 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/test_prof/cops/rspec/aggregate_failures.rb', line 51 def on_block(node) return unless self.class.supported? 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, location: :expression, message: 'Use :aggregate_failures instead of several one-liners.' ) end |