-
Notifications
You must be signed in to change notification settings - Fork 219
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 #207 from rauann/rauann/as-function
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,4 +197,40 @@ defmodule Poison.DecoderTest do | |
assert transform(address, %{as: %Address{}}) == | ||
"1 Main St., Austin, TX 78701" | ||
end | ||
|
||
test "decoding a sigle :as function with string keys" do | ||
person = %{"name" => "Devin Torres"} | ||
as = fn %{"name" => _name} -> %Person{} end | ||
expected = %Person{name: "Devin Torres"} | ||
assert transform(person, %{as: as}) == expected | ||
end | ||
|
||
test "decoding a single :as function with atom keys" do | ||
person = %{name: "Devin Torres"} | ||
as = fn %{name: _name} -> %Person{} end | ||
expected = %Person{name: "Devin Torres"} | ||
assert transform(person, %{as: as, keys: :atoms!}) == expected | ||
end | ||
|
||
test "decoding a :as list function with string keys" do | ||
person = [%{"name" => "Devin Torres"}] | ||
as = fn _value -> [%Person{}] end | ||
expected = [%Person{name: "Devin Torres"}] | ||
assert transform(person, %{as: as}) == expected | ||
end | ||
|
||
test "decoding nested :as function with string keys" do | ||
person = %{"person" => %{"name" => "Devin Torres"}} | ||
as = fn _value -> %{"person" => %Person{}} end | ||
actual = transform(person, %{as: as}) | ||
expected = %{"person" => %Person{name: "Devin Torres"}} | ||
assert actual == expected | ||
end | ||
|
||
test "decoding nested structs in :as function with string keys" do | ||
person = %{"name" => "Devin Torres", "contact" => %{"email" => "[email protected]"}} | ||
as = fn _value -> %Person{contact: %Contact{}} end | ||
expected = %Person{name: "Devin Torres", contact: %Contact{email: "[email protected]"}} | ||
assert transform(person, %{as: as}) == expected | ||
end | ||
end |