Skip to content

Commit

Permalink
Support .vars for Fiber only when available on platform
Browse files Browse the repository at this point in the history
  • Loading branch information
mblumtritt committed May 30, 2024
1 parent 2822585 commit b3e299b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
6 changes: 5 additions & 1 deletion lib/im-lost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def untrace_all!
#
# Inspect internal variables of a given object.
#
# @note The dedictaed handling of `Fiber` is platform dependend!
#
# @example Inspect current instance variables
# @a = 22
# b = 20
Expand Down Expand Up @@ -293,7 +295,7 @@ def vars(object)
@output.puts("= #{location.path}:#{location.lineno}")
if Thread === object
_thread_vars(object)
elsif Fiber === object
elsif @fiber_supported && Fiber === object
_fiber_vars(object)
elsif defined?(object.instance_variables)
_instance_vars(object)
Expand Down Expand Up @@ -618,5 +620,7 @@ def initialize(&block)
@output.puts(" #{tp.path}:#{tp.lineno}") if @exception_locations
end

@fiber_supported = !!(defined?(Fiber.current) && defined?(Fiber.storage))

self.trace_calls = true
end
62 changes: 33 additions & 29 deletions spec/lib/im-lost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,44 +423,48 @@ def bar = :bar
end
end

context 'when the current Fiber is given' do
before do
Fiber[:var1] = 22
Fiber[:var2] = 20
Fiber[:var3] = Fiber[:var1] + Fiber[:var2]
end
if defined?(Fiber.current) && defined?(Fiber.storage)
context 'when the current Fiber is given' do
before do
Fiber[:var1] = 22
Fiber[:var2] = 20
Fiber[:var3] = Fiber[:var1] + Fiber[:var2]
end

it 'prints the fiber storage' do
ImLost.vars(Fiber.current)
it 'prints the fiber storage' do
ImLost.vars(Fiber.current)

expect(output).to eq <<~OUTPUT
= #{__FILE__}:#{__LINE__ - 3}
> fiber storage
var1: 22
var2: 20
var3: 42
OUTPUT
end
expect(output).to eq <<~OUTPUT
= #{__FILE__}:#{__LINE__ - 3}
> fiber storage
var1: 22
var2: 20
var3: 42
OUTPUT
end

it 'returns given fiber' do
expect(ImLost.vars(Fiber.current)).to be Fiber.current
it 'returns given fiber' do
expect(ImLost.vars(Fiber.current)).to be Fiber.current
end
end
end

context 'when a different Fiber is given' do
let(:fiber) { Fiber.new { 42 } }
context 'when a different Fiber is given' do
let(:fiber) { Fiber.new { 42 } }

after { fiber.kill }
after { fiber.kill if defined?(fiber.kill) } # Ruby > v3.3.0

it 'it prints an error message' do
ImLost.vars(fiber)
it 'it prints an error message' do
ImLost.vars(fiber)

expect(output).to eq <<~OUTPUT
= #{__FILE__}:#{__LINE__ - 3}
!!! given Fiber is not the current Fiber
#{fiber.inspect}
OUTPUT
expect(output).to eq <<~OUTPUT
= #{__FILE__}:#{__LINE__ - 3}
!!! given Fiber is not the current Fiber
#{fiber.inspect}
OUTPUT
end
end
else
pending 'for Fiber is not supported in this platform'
end
end

Expand Down

0 comments on commit b3e299b

Please sign in to comment.