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

Commit e36aa2a

Browse files
authored
Merge pull request #2894 from rspec/support-end-less-methods-in-snippet-extractor
Add SnippetExtractor spec examples for end-less methods support
2 parents 42a9fe3 + 50f7fb2 commit e36aa2a

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

spec/rspec/core/formatters/snippet_extractor_spec.rb

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

34
module RSpec::Core::Formatters
45
RSpec.describe SnippetExtractor do
6+
include RSpecHelpers
7+
58
subject(:expression_lines) do
69
SnippetExtractor.extract_expression_lines_at(file_path, line_number, max_line_count)
710
end
@@ -182,6 +185,67 @@ def obj.foo(arg)
182185
end
183186
end
184187

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