Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(&block)
# @return [Object]
def __force__
@thread.join if @thread
@promise
@promise.__force__
end
alias_method :force, :__force__

Expand Down
12 changes: 12 additions & 0 deletions spec/promise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
expect {x = [ 1, @method.call { x / 0 }]}.to_not raise_error
end

describe 'compatibility with Marshal' do
it 'should not respond_to? marshal_dump' do
x = @method.call { 3 + 5 }
expect(x).to_not respond_to(:marshal_dump)
end

it 'should respond_to? _dump' do
x = @method.call { 3 + 5 }
expect(x).to respond_to(:_dump)
end
end

describe 'an object referencing a promise' do
class ClassResulting
attr_reader :value
Expand Down
25 changes: 14 additions & 11 deletions spec/shared_promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
expect(x).to eq 8
end

it "should work in conditions (at least when forced)" do
for value in [true, false, nil, 1, "test", [], {}, Object.new]
result = (value if value)
x = @method.call { value }
expect(x.__force__).to eq value
expect(x).to eq value
expect((value if x.__force__)).to eq result
# Unfortunately this can't be done:
# expect((value if x)).to eq result
# The promise/future itself is always not nil, so we get this instead:
expect((value if x)).to eq value
end
end

it "should respond_to? force" do
x = @method.call { 3 + 5 }
expect(x).to respond_to(:force)
Expand Down Expand Up @@ -129,15 +143,4 @@
expect(changeds.size).to eq 10
end

describe 'compatibility with Marshal' do
it 'should not respond_to? marshal_dump' do
x = @method.call { 3 + 5 }
expect(x).to_not respond_to(:marshal_dump)
end

it 'should respond_to? _dump' do
x = @method.call { 3 + 5 }
expect(x).to respond_to(:_dump)
end
end
end