Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 33ea8e9

Browse files
committed
Add SnippetExtractor spec examples for end-less methods support
Ref: #2892
1 parent 247d0a7 commit 33ea8e9

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

spec/rspec/core/formatters/snippet_extractor_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require 'rspec/core/formatters/snippet_extractor'
2+
require 'support/helper_methods'
3+
require 'tempfile'
24

35
module RSpec::Core::Formatters
46
RSpec.describe SnippetExtractor do
7+
include RSpecHelpers
8+
59
subject(:expression_lines) do
610
SnippetExtractor.extract_expression_lines_at(file_path, line_number, max_line_count)
711
end
@@ -182,6 +186,56 @@ def obj.foo(arg)
182186
end
183187
end
184188

189+
context 'when the expression line includes an "end"-less method definition', :if => RUBY_VERSION.to_f >= 3.0 do
190+
let(:source) do
191+
load(file.path)
192+
end
193+
194+
let(:file) do
195+
file = Tempfile.new('source.rb')
196+
197+
file.write(unindent(<<-END))
198+
def foo = raise
199+
200+
foo
201+
END
202+
203+
file.close
204+
205+
file
206+
end
207+
208+
after do
209+
file.unlink
210+
end
211+
212+
it 'returns only the line' do
213+
expect(expression_lines).to eq([
214+
'def foo = raise'
215+
])
216+
end
217+
end
218+
219+
context 'when the expression is a setter method definition', :unless => argument_error_points_invoker do
220+
let(:source) do
221+
obj = Object.new
222+
223+
def obj.foo=(arg1, arg2)
224+
@foo = arg1
225+
end
226+
227+
obj.foo = 1
228+
end
229+
230+
it 'returns all the lines without confusing it with "end"-less method' do
231+
expect(expression_lines).to eq([
232+
' def obj.foo=(arg1, arg2)',
233+
' @foo = arg1',
234+
' end'
235+
])
236+
end
237+
end
238+
185239
context "when the expression ends with multiple paren-only lines of same type" do
186240
let(:source) do
187241
do_something_fail(:foo, (:bar

spec/support/aruba_support.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
require 'support/helper_methods'
3+
14
if RSpec::Support::Ruby.jruby? && RSpec::Support::Ruby.jruby_version == "9.1.17.0"
25
# A regression appeared in require_relative in JRuby 9.1.17.0 where require some
36
# how ends up private, this monkey patch uses `send`
@@ -29,6 +32,7 @@ module ArubaLoader
2932

3033
RSpec.shared_context "aruba support" do
3134
include Aruba::Api
35+
include RSpecHelpers
3236
let(:stderr) { StringIO.new }
3337
let(:stdout) { StringIO.new }
3438

@@ -70,13 +74,6 @@ def write_file_formatted(file_name, contents)
7074
formatted_contents = unindent(contents.sub(/\A\n/, ""))
7175
write_file file_name, formatted_contents
7276
end
73-
74-
# Intended for use with indented heredocs.
75-
# taken from Ruby Tapas:
76-
# https://rubytapas.dpdcart.com/subscriber/post?id=616#files
77-
def unindent(s)
78-
s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "")
79-
end
8077
end
8178

8279
RSpec.configure do |c|

spec/support/helper_methods.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ def ignoring_warnings
1414
result
1515
end
1616

17+
# Intended for use with indented heredocs.
18+
# taken from Ruby Tapas:
19+
# https://rubytapas.dpdcart.com/subscriber/post?id=616#files
20+
def unindent(s)
21+
s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "")
22+
end
23+
1724
# In Ruby 2.7 taint was removed and has no effect, whilst SAFE warns that it
1825
# has no effect and will become a normal varible in 3.0. Other engines do not
1926
# implement SAFE.

0 commit comments

Comments
 (0)