Module: CopHelper
- Extended by:
- RSpec::SharedContext
- Defined in:
- lib/rubocop/rspec/cop_helper.rb
Overview
This module provides methods that make it easier to test Cops.
Instance Method Summary collapse
- #_investigate(cop, processed_source) ⇒ Object
- #autocorrect_source(cop, source, file = nil) ⇒ Object
- #autocorrect_source_file(cop, source) ⇒ Object
- #inspect_source(cop, source, file = nil) ⇒ Object
- #inspect_source_file(cop, source) ⇒ Object
- #parse_source(source, file = nil) ⇒ Object
Instance Method Details
#_investigate(cop, processed_source) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 53 def _investigate(cop, processed_source) forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances| next unless cop.join_force?(klass) instances << klass.new([cop]) end commissioner = RuboCop::Cop::Commissioner.new([cop], forces, raise_error: true) commissioner.investigate(processed_source) commissioner end |
#autocorrect_source(cop, source, file = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 43 def autocorrect_source(cop, source, file = nil) cop.instance_variable_get(:@options)[:auto_correct] = true processed_source = parse_source(source, file) _investigate(cop, processed_source) corrector = RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections) corrector.rewrite end |
#autocorrect_source_file(cop, source) ⇒ Object
39 40 41 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 39 def autocorrect_source_file(cop, source) Tempfile.open('tmp') { |f| autocorrect_source(cop, source, f) } end |
#inspect_source(cop, source, file = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 16 def inspect_source(cop, source, file = nil) if source.is_a?(Array) && source.size == 1 raise "Don't use an array for a single line of code: #{source}" end RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} processed_source = parse_source(source, file) raise 'Error parsing example code' unless processed_source.valid_syntax? _investigate(cop, processed_source) end |
#inspect_source_file(cop, source) ⇒ Object
12 13 14 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 12 def inspect_source_file(cop, source) Tempfile.open('tmp') { |f| inspect_source(cop, source, f) } end |
#parse_source(source, file = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 27 def parse_source(source, file = nil) source = source.join($RS) if source.is_a?(Array) if file && file.respond_to?(:write) file.write(source) file.rewind file = file.path end RuboCop::ProcessedSource.new(source, ruby_version, file) end |