Skip to content

Commit

Permalink
Merge pull request #366 from esparta/array_try_specs
Browse files Browse the repository at this point in the history
Fix error on Dry::Types::Array#try
  • Loading branch information
flash-gordon committed Oct 22, 2019
2 parents 3d54d08 + a78fbd0 commit c263c7e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dry/types/array/member.rb
Expand Up @@ -89,7 +89,7 @@ def try(input, &block)
block ? yield(failure) : failure
end
else
failure = failure(input, "#{input} is not an array")
failure = failure(input, CoercionError.new("#{input} is not an array"))
block ? yield(failure) : failure
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/dry/types/array_spec.rb
Expand Up @@ -19,6 +19,36 @@
include_context 'array with a member type'
end

context 'try' do
subject(:array) do
Dry::Types['nominal.array'].of(Dry::Types['strict.string'])
end

it 'with a valid array' do
expect(array.try(%w[a b])).to be_success
end

it 'an invalid type should be a failure' do
expect(array.try('some string')).to be_failure
end

it 'a broken constraint should be a failure' do
expect(array.try(['1', 2])).to be_failure
end

it 'a broken constraint with block' do
expect(
array.try(['1', '2', 3]) { |error| "error: #{error}" }
).to match(/error: 3/)
end

it 'an invalid type with a block' do
expect(
array.try('X') { |x| 'error: ' + x.to_s }
).to eql('error: X is not an array')
end
end

context 'using method' do
subject(:array) { Dry::Types['coercible.array'].of(Dry::Types['coercible.string']) }

Expand Down

0 comments on commit c263c7e

Please sign in to comment.