/
expect_offense.rb
319 lines (278 loc) · 10.4 KB
/
expect_offense.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# frozen_string_literal: true
module RuboCop
module RSpec
# 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.
#
# @example Usage
#
# expect_offense(<<~RUBY)
# a do
# b
# end.c
# ^^^^^ Avoid chaining a method call on a do...end block.
# RUBY
#
# @example Equivalent assertion without `expect_offense`
#
# inspect_source(<<~RUBY)
# a do
# b
# end.c
# RUBY
#
# expect(cop.offenses.size).to be(1)
#
# offense = cop.offenses.first
# expect(offense.line).to be(3)
# expect(offense.column_range).to be(0...5)
# expect(offense.message).to eql(
# 'Avoid chaining a method call on a do...end block.'
# )
#
# Auto-correction can be tested using `expect_correction` after
# `expect_offense`.
#
# @example `expect_offense` and `expect_correction`
#
# expect_offense(<<~RUBY)
# x % 2 == 0
# ^^^^^^^^^^ Replace with `Integer#even?`.
# RUBY
#
# expect_correction(<<~RUBY)
# x.even?
# RUBY
#
# 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`.
#
# @example `expect_offense` and `expect_no_corrections`
#
# expect_offense(<<~RUBY)
# a do
# b
# end.c
# ^^^^^ Avoid chaining a method call on a do...end block.
# RUBY
#
# expect_no_corrections
#
# 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:
#
# @example `^{}` empty line offense
#
# expect_offense(<<~RUBY)
#
# ^{} Missing frozen string literal comment.
# puts 1
# RUBY
module ExpectOffense
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
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def expect_offense(source, file = nil, severity: nil, **replacements)
source = format_offense(source, **replacements)
RuboCop::Formatter::DisabledConfigFormatter
.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
cop.instance_variable_get(:@options)[:auto_correct] = true
expected_annotations = AnnotatedSource.parse(source)
if expected_annotations.plain_source == source
raise 'Use `expect_no_offenses` to assert that no offenses are found'
end
@processed_source = parse_source(expected_annotations.plain_source,
file)
unless @processed_source.valid_syntax?
raise 'Error parsing example code: ' \
"#{@processed_source.diagnostics.map(&:render).join("\n")}"
end
@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
def expect_correction(correction, loop: true)
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
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
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
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
# Parsed representation of code annotated with the `^^^ Message` style
class AnnotatedSource
ANNOTATION_PATTERN = /\A\s*(\^+|\^{}) /.freeze
ABBREV = "[...]\n"
# @param annotated_source [String] string passed to the matchers
#
# Separates annotation lines from source lines. Tracks the real
# source line number that each annotation corresponds to.
#
# @return [AnnotatedSource]
def self.parse(annotated_source)
source = []
annotations = []
annotated_source.each_line do |source_line|
if ANNOTATION_PATTERN.match?(source_line)
annotations << [source.size, source_line]
else
source << source_line
end
end
annotations.each { |a| a[0] = 1 } if source.empty?
new(source, annotations)
end
# @param lines [Array<String>]
# @param annotations [Array<(Integer, String)>]
# each entry is the annotated line number and the annotation text
#
# @note annotations are sorted so that reconstructing the annotation
# text via {#to_s} is deterministic
def initialize(lines, annotations)
@lines = lines.freeze
@annotations = annotations.sort.freeze
end
def ==(other)
other.is_a?(self.class) &&
other.lines == lines &&
match_annotations?(other)
end
# Dirty hack: expectations with [...] are rewritten when they match
# This way the diff is clean.
def match_annotations?(other)
annotations.zip(other.annotations) do |(_actual_line, actual_annotation),
(_expected_line, expected_annotation)|
if expected_annotation&.end_with?(ABBREV) &&
actual_annotation.start_with?(expected_annotation[0...-ABBREV.length])
expected_annotation.replace(actual_annotation)
end
end
annotations == other.annotations
end
# Construct annotated source string (like what we parse)
#
# Reconstruct a deterministic annotated source string. This is
# useful for eliminating semantically irrelevant annotation
# ordering differences.
#
# @example standardization
#
# source1 = AnnotatedSource.parse(<<-RUBY)
# line1
# ^ Annotation 1
# ^^ Annotation 2
# RUBY
#
# source2 = AnnotatedSource.parse(<<-RUBY)
# line1
# ^^ Annotation 2
# ^ Annotation 1
# RUBY
#
# source1.to_s == source2.to_s # => true
#
# @return [String]
def to_s
reconstructed = lines.dup
annotations.reverse_each do |line_number, annotation|
reconstructed.insert(line_number, annotation)
end
reconstructed.join
end
alias inspect to_s
# Return the plain source code without annotations
#
# @return [String]
def plain_source
lines.join
end
# Annotate the source code with the RuboCop offenses provided
#
# @param offenses [Array<RuboCop::Cop::Offense>]
#
# @return [self]
def with_offense_annotations(offenses)
offense_annotations =
offenses.map do |offense|
indent = ' ' * offense.column
carets = '^' * offense.column_length
carets = '^{}' if offense.column_length.zero?
[offense.line, "#{indent}#{carets} #{offense.message}\n"]
end
self.class.new(lines, offense_annotations)
end
protected
attr_reader :lines, :annotations
end
end
end
end