Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions spec/rspec/core/formatters/snippet_extractor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'rspec/core/formatters/snippet_extractor'
require 'support/helper_methods'

module RSpec::Core::Formatters
RSpec.describe SnippetExtractor do
include RSpecHelpers

subject(:expression_lines) do
SnippetExtractor.extract_expression_lines_at(file_path, line_number, max_line_count)
end
Expand Down Expand Up @@ -182,6 +185,67 @@ def obj.foo(arg)
end
end

context 'when the expression line includes an "end"-less method definition', :if => RUBY_VERSION.to_f >= 3.0 do
include RSpec::Support::InSubProcess

around(:example) do |example|
require 'tempfile'
example.call
end

let(:source) do
in_sub_process do
load(file.path)
end
end

let(:file) do
file = Tempfile.new('source.rb')

file.write(unindent(<<-END))
obj = Object.new

def obj.foo = raise

obj.foo
END

file.close

file
end

after do
file.unlink
end

it 'returns only the line' do
expect(expression_lines).to eq([
'def obj.foo = raise'
])
end
end

context 'when the expression is a setter method definition', :unless => argument_error_points_invoker do
let(:source) do
obj = Object.new

def obj.foo=(arg1, arg2)
@foo = arg1
end

obj.foo = 1
end

it 'returns all the lines without confusing it with "end"-less method' do
expect(expression_lines).to eq([
' def obj.foo=(arg1, arg2)',
' @foo = arg1',
' end'
])
end
end

context "when the expression ends with multiple paren-only lines of same type" do
let(:source) do
do_something_fail(:foo, (:bar
Expand Down
10 changes: 3 additions & 7 deletions spec/support/aruba_support.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'support/helper_methods'

if RSpec::Support::Ruby.jruby? && RSpec::Support::Ruby.jruby_version == "9.1.17.0"
# A regression appeared in require_relative in JRuby 9.1.17.0 where require some
# how ends up private, this monkey patch uses `send`
Expand Down Expand Up @@ -29,6 +31,7 @@ module ArubaLoader

RSpec.shared_context "aruba support" do
include Aruba::Api
include RSpecHelpers
let(:stderr) { StringIO.new }
let(:stdout) { StringIO.new }

Expand Down Expand Up @@ -70,13 +73,6 @@ def write_file_formatted(file_name, contents)
formatted_contents = unindent(contents.sub(/\A\n/, ""))
write_file file_name, formatted_contents
end

# Intended for use with indented heredocs.
# taken from Ruby Tapas:
# https://rubytapas.dpdcart.com/subscriber/post?id=616#files
def unindent(s)
s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "")
end
end

RSpec.configure do |c|
Expand Down
7 changes: 7 additions & 0 deletions spec/support/helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def ignoring_warnings
result
end

# Intended for use with indented heredocs.
# taken from Ruby Tapas:
# https://rubytapas.dpdcart.com/subscriber/post?id=616#files
def unindent(s)
s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "")
end

# In Ruby 2.7 taint was removed and has no effect, whilst SAFE warns that it
# has no effect and will become a normal varible in 3.0. Other engines do not
# implement SAFE.
Expand Down