-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from dry-rb/bugs-in-arrays-of-struct
Fix bugs with coercing input to arrays of struct types
- Loading branch information
Showing
6 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Dry::Types::Array do | ||
before do | ||
module Test | ||
class Street < Dry::Struct | ||
attribute :street_name, "string" | ||
end | ||
|
||
class City < Dry::Struct | ||
attribute :city_name, "string" | ||
end | ||
|
||
CityOrStreet = City | Street | ||
end | ||
end | ||
|
||
describe "#try" do | ||
context "simple struct" do | ||
subject(:array) { Dry::Types["array"].of(Test::Street) } | ||
it "returns success for valid array" do | ||
expect(array.try([{street_name: "Oxford"}, {street_name: "London"}])).to be_success | ||
end | ||
|
||
it "returns failure for invalid array" do | ||
expect(array.try([{name: "Oxford"}, {name: 123}])).to be_failure | ||
expect(array.try([{}])).to be_failure | ||
end | ||
end | ||
|
||
context "sum struct" do | ||
subject(:array) { Dry::Types["array"].of(Test::CityOrStreet) } | ||
|
||
it "returns success for valid array" do | ||
expect(array.try([{city_name: "London"}, {street_name: "Oxford"}])).to be_success | ||
expect(array.try([Test::Street.new(street_name: "Oxford")])).to be_success | ||
end | ||
|
||
it "returns failure for invalid array" do | ||
expect(array.try([{city_name: "London"}, {street_name: 123}])).to be_failure | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters