module CopHelper
This module provides methods that make it easier to test Cops.
Public Instance Methods
_investigate(cop, processed_source)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 67 def _investigate(cop, processed_source) forces = RubbyCop::Cop::Force.all.each_with_object([]) do |klass, instances| next unless cop.join_force?(klass) instances << klass.new([cop]) end commissioner = RubbyCop::Cop::Commissioner.new([cop], forces, raise_error: true) commissioner.investigate(processed_source) commissioner end
autocorrect_source(cop, source, file = nil)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 48 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 = RubbyCop::Cop::Corrector.new(processed_source.buffer, cop.corrections) corrector.rewrite end
autocorrect_source_file(cop, source)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 44 def autocorrect_source_file(cop, source) Tempfile.open('tmp') { |f| autocorrect_source(cop, source, f) } end
autocorrect_source_with_loop(cop, source, file = nil)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 58 def autocorrect_source_with_loop(cop, source, file = nil) loop do cop.instance_variable_set(:@corrections, []) new_source = autocorrect_source(cop, source, file) return new_source if new_source == source source = new_source end end
inspect_gemfile(cop, source)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 17 def inspect_gemfile(cop, source) inspect_source(cop, source, 'Gemfile') end
inspect_source(cop, source, file = nil)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 21 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 RubbyCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} RubbyCop::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)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 13 def inspect_source_file(cop, source) Tempfile.open('tmp') { |f| inspect_source(cop, source, f) } end
parse_source(source, file = nil)
click to toggle source
# File lib/rubbycop/rspec/cop_helper.rb, line 32 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 RubbyCop::ProcessedSource.new(source, ruby_version, file) end