Module: RuboCop::RSpec::ExpectOffense
- Defined in:
- lib/rubocop/rspec/expect_offense.rb
Overview
Mixin for ‘expect_offense` and `expect_no_offenses`
This mixin makes it easier to specify strict offense expectations in a declarative and visual fashion. Just type out the code that should generate a offense, annotate code by writing ‘^’s underneath each character that should be highlighted, and follow the carets with a string (separated by a space) that is the message of the offense. You can include multiple offenses in one code snippet.
Auto-correction can be tested using ‘expect_correction` after `expect_offense`.
If you do not want to specify an offense then use the companion method ‘expect_no_offenses`. This method is a much simpler assertion since it just inspects the source and checks that there were no offenses. The `expect_offense` method has to do more work by parsing out lines that contain carets.
If the code produces an offense that could not be auto-corrected, you can use ‘expect_no_corrections` after `expect_offense`.
If your code has variables of different lengths, you can use ‘%foo`, `^foo`, and `_foo` to format your template; you can also abbreviate offense messages with `[…]`:
%w[raise fail].each do |keyword|
expect_offense(<<~RUBY, keyword: keyword)
%{keyword}(RuntimeError, msg)
^{keyword}^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError` argument [...]
RUBY
%w[has_one has_many].each do |type|
expect_offense(<<~RUBY, type: type)
class Book
%{type} :chapter, foreign_key: 'book_id'
_{type} ^^^^^^^^^^^^^^^^^^^^^^ Specifying the default [...]
end
RUBY
end
If you need to specify an offense on a blank line, use the empty ‘^{}` marker:
Defined Under Namespace
Classes: AnnotatedSource
Instance Method Summary collapse
-
#expect_correction(correction, loop: true, source: nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#expect_no_corrections ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #expect_no_offenses(source, file = nil) ⇒ Object
- #expect_offense(source, file = nil, severity: nil, chomp: false, **replacements) ⇒ Object
- #format_offense(source, **replacements) ⇒ Object
- #parse_annotations(source, raise_error: true, **replacements) ⇒ Object
- #parse_processed_source(source, file = nil) ⇒ Object
- #set_formatter_options ⇒ Object
Instance Method Details
#expect_correction(correction, loop: true, source: nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 131 def expect_correction(correction, loop: true, source: nil) if source expected_annotations = parse_annotations(source, raise_error: false) @processed_source = parse_processed_source(expected_annotations.plain_source) _investigate(cop, @processed_source) end raise '`expect_correction` must follow `expect_offense`' unless @processed_source iteration = 0 new_source = loop do iteration += 1 corrected_source = @last_corrector.rewrite break corrected_source unless loop break corrected_source if @last_corrector.empty? break corrected_source if corrected_source == @processed_source.buffer.source if iteration > RuboCop::Runner::MAX_ITERATIONS raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [@offenses]) end # Prepare for next loop @processed_source = parse_source(corrected_source, @processed_source.path) _investigate(cop, @processed_source) end expect(new_source).to eq(correction) end |
#expect_no_corrections ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 164 def expect_no_corrections raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source return if @last_corrector.empty? # In order to print a nice diff, e.g. what source got corrected to, # we need to run the actual corrections new_source = @last_corrector.rewrite expect(new_source).to eq(@processed_source.buffer.source) end |
#expect_no_offenses(source, file = nil) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 177 def expect_no_offenses(source, file = nil) offenses = inspect_source(source, file) expected_annotations = AnnotatedSource.parse(source) actual_annotations = expected_annotations.with_offense_annotations(offenses) expect(actual_annotations.to_s).to eq(source) end |
#expect_offense(source, file = nil, severity: nil, chomp: false, **replacements) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 114 def expect_offense(source, file = nil, severity: nil, chomp: false, **replacements) expected_annotations = parse_annotations(source, **replacements) source = expected_annotations.plain_source source = source.chomp if chomp @processed_source = parse_processed_source(source, file) @offenses = _investigate(cop, @processed_source) actual_annotations = expected_annotations.with_offense_annotations(@offenses) expect(actual_annotations).to eq(expected_annotations), '' expect(@offenses.map(&:severity).uniq).to eq([severity]) if severity @offenses end |
#format_offense(source, **replacements) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 104 def format_offense(source, **replacements) replacements.each do |keyword, value| value = value.to_s source = source.gsub("%{#{keyword}}", value) .gsub("^{#{keyword}}", '^' * value.size) .gsub("_{#{keyword}}", ' ' * value.size) end source end |
#parse_annotations(source, raise_error: true, **replacements) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 186 def parse_annotations(source, raise_error: true, **replacements) source = format_offense(source, **replacements) annotations = AnnotatedSource.parse(source) return annotations unless raise_error && annotations.plain_source == source raise 'Use `expect_no_offenses` to assert that no offenses are found' end |
#parse_processed_source(source, file = nil) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 196 def parse_processed_source(source, file = nil) processed_source = parse_source(source, file) return processed_source if processed_source.valid_syntax? raise 'Error parsing example code: ' \ "#{processed_source.diagnostics.map(&:render).join("\n")}" end |
#set_formatter_options ⇒ Object
204 205 206 207 208 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 204 def RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} cop.instance_variable_get(:@options)[:auto_correct] = true end |