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

Commit 6ed9d68

Browse files
committed
Add SnippetExtractor spec examples for end-less methods support
Ref: #2892
1 parent d57c371 commit 6ed9d68

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

spec/rspec/core/formatters/snippet_extractor_spec.rb

Lines changed: 60 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,62 @@ 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+
include RSpec::Support::InSubProcess
191+
192+
let(:source) do
193+
in_sub_process do
194+
load(file.path)
195+
end
196+
end
197+
198+
let(:file) do
199+
file = Tempfile.new('source.rb')
200+
201+
file.write(unindent(<<-END))
202+
obj = Object.new
203+
204+
def obj.foo = raise
205+
206+
obj.foo
207+
END
208+
209+
file.close
210+
211+
file
212+
end
213+
214+
after do
215+
file.unlink
216+
end
217+
218+
it 'returns only the line' do
219+
expect(expression_lines).to eq([
220+
'def obj.foo = raise'
221+
])
222+
end
223+
end
224+
225+
context 'when the expression is a setter method definition', :unless => argument_error_points_invoker do
226+
let(:source) do
227+
obj = Object.new
228+
229+
def obj.foo=(arg1, arg2)
230+
@foo = arg1
231+
end
232+
233+
obj.foo = 1
234+
end
235+
236+
it 'returns all the lines without confusing it with "end"-less method' do
237+
expect(expression_lines).to eq([
238+
' def obj.foo=(arg1, arg2)',
239+
' @foo = arg1',
240+
' end'
241+
])
242+
end
243+
end
244+
185245
context "when the expression ends with multiple paren-only lines of same type" do
186246
let(:source) do
187247
do_something_fail(:foo, (:bar

spec/support/aruba_support.rb

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

3032
RSpec.shared_context "aruba support" do
3133
include Aruba::Api
34+
include RSpecHelpers
3235
let(:stderr) { StringIO.new }
3336
let(:stdout) { StringIO.new }
3437

@@ -70,13 +73,6 @@ def write_file_formatted(file_name, contents)
7073
formatted_contents = unindent(contents.sub(/\A\n/, ""))
7174
write_file file_name, formatted_contents
7275
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
8076
end
8177

8278
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)