diff --git a/lib/future.rb b/lib/future.rb index 06c5fd8..581a188 100644 --- a/lib/future.rb +++ b/lib/future.rb @@ -27,7 +27,7 @@ def initialize(&block) # @return [Object] def __force__ @thread.join if @thread - @promise + @promise.__force__ end alias_method :force, :__force__ diff --git a/spec/promise_spec.rb b/spec/promise_spec.rb index 80ad7c1..4d5051a 100644 --- a/spec/promise_spec.rb +++ b/spec/promise_spec.rb @@ -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 diff --git a/spec/shared_promise.rb b/spec/shared_promise.rb index 6cc0b15..e654d5d 100644 --- a/spec/shared_promise.rb +++ b/spec/shared_promise.rb @@ -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) @@ -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