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(source, file = nil) ⇒ Object
- #autocorrect_source_file(source) ⇒ Object
- #inspect_source(source, file = nil) ⇒ Object
- #parse_source(source, file = nil) ⇒ Object
Instance Method Details
#_investigate(cop, processed_source) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 48 def _investigate(cop, processed_source) team = RuboCop::Cop::Team.new([cop], nil, raise_error: true) report = team.investigate(processed_source) @last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source) report.offenses end |
#autocorrect_source(source, file = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 38 def autocorrect_source(source, file = nil) RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} cop.instance_variable_get(:@options)[:auto_correct] = true processed_source = parse_source(source, file) _investigate(cop, processed_source) @last_corrector.rewrite end |
#autocorrect_source_file(source) ⇒ Object
34 35 36 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 34 def autocorrect_source_file(source) Tempfile.open('tmp') { |f| autocorrect_source(source, f) } end |
#inspect_source(source, file = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 12 def inspect_source(source, file = nil) RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} processed_source = parse_source(source, file) unless processed_source.valid_syntax? raise 'Error parsing example code: ' \ "#{processed_source.diagnostics.map(&:render).join("\n")}" end _investigate(cop, processed_source) end |
#parse_source(source, file = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 24 def parse_source(source, file = nil) if file.respond_to?(:write) file.write(source) file.rewind file = file.path end RuboCop::ProcessedSource.new(source, ruby_version, file) end |