-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Promise#rescue
can now take a list of error class filters.
#32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8742dda
138bdd8
04229b1
7b2e725
48cf3fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -736,6 +736,85 @@ | |
expect(result).to eq(reason) | ||
expect(subject.reason).to eq(reason) | ||
end | ||
|
||
it 'allows specifying a list of error types' do | ||
result = nil | ||
p = subject.rescue(StandardError) do |reason| | ||
result = reason | ||
end | ||
|
||
subject.reject(reason) | ||
expect(result).to eq(reason) | ||
expect(subject.reason).to eq(reason) | ||
expect(p).to be_fulfilled | ||
|
||
result = nil | ||
p = subject.rescue(RuntimeError) do |reason| | ||
result = reason | ||
end | ||
|
||
subject.reject(reason) | ||
expect(result).to be_nil | ||
expect(subject.reason).to eq(reason) | ||
expect(p).to be_rejected | ||
|
||
result = nil | ||
p = subject.rescue(RuntimeError, StandardError) do |reason| | ||
result = reason | ||
end | ||
|
||
subject.reject(reason) | ||
expect(result).to eq(reason) | ||
expect(subject.reason).to eq(reason) | ||
expect(p).to be_fulfilled | ||
end | ||
|
||
it 'can not be called without a block' do | ||
p1 = Promise.new | ||
|
||
expect { | ||
p1.rescue | ||
}.to raise_error(ArgumentError, 'no block given') | ||
end | ||
|
||
it 'can be called with with a block not taking an error argument' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the ability to specify a block or proc without all the arguments just a property of ruby? E.g. irb> proc { :foo }.call(1)
=> :foo It looks like this is an unnecessary test. |
||
p1 = Promise.new | ||
p2 = p1.rescue { :foo } | ||
|
||
p1.reject(RuntimeError.new('fail')) | ||
|
||
expect(p2).to be_fulfilled | ||
expect(p2.value).to equal(:foo) | ||
end | ||
|
||
it 'correctly handles error type subclasses' do | ||
rescued = [] | ||
|
||
promise = Promise.new | ||
promise.rescue(StandardError) do | ||
rescued << StandardError | ||
end | ||
promise.rescue(RuntimeError) do | ||
rescued << RuntimeError | ||
end | ||
|
||
promise.reject(RuntimeError.new('fail')) | ||
|
||
expect(rescued).to eq([StandardError, RuntimeError]) | ||
end | ||
|
||
it 'does not handle unspecified error types' do | ||
p1 = Promise.new | ||
p2 = p1.rescue(RuntimeError) { raise 'fail' } | ||
p3 = p2.rescue do |error| | ||
expect(error).to be_an_instance_of(StandardError) | ||
expect(error.message).to eq('testing') | ||
end | ||
|
||
p1.reject(StandardError.new('testing')) | ||
expect(p2).to be_rejected | ||
expect(p3).to be_fulfilled | ||
end | ||
end | ||
|
||
describe '#catch' do | ||
|
@@ -747,6 +826,32 @@ | |
expect(result).to eq(reason) | ||
expect(subject.reason).to eq(reason) | ||
end | ||
|
||
it 'allows specifying a list of error types' do | ||
result = nil | ||
p = subject.catch(StandardError) { |reas| result = reas } | ||
|
||
subject.reject(reason) | ||
expect(result).to eq(reason) | ||
expect(subject.reason).to eq(reason) | ||
expect(p).to be_fulfilled | ||
|
||
result = nil | ||
p = subject.catch(RuntimeError) { |reas| result = reas } | ||
|
||
subject.reject(reason) | ||
expect(result).to be_nil | ||
expect(subject.reason).to eq(reason) | ||
expect(p).to be_rejected | ||
|
||
result = nil | ||
p = subject.catch(RuntimeError, StandardError) { |reas| result = reas } | ||
|
||
subject.reject(reason) | ||
expect(result).to eq(reason) | ||
expect(subject.reason).to eq(reason) | ||
expect(p).to be_fulfilled | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these tests for |
||
end | ||
|
||
describe '#progress' do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like second test of three that got grouped together in a single test.