@@ -197,4 +197,40 @@ defmodule Poison.DecoderTest do
197
197
assert transform ( address , % { as: % Address { } } ) ==
198
198
"1 Main St., Austin, TX 78701"
199
199
end
200
+
201
+ test "decoding a sigle :as function with string keys" do
202
+ person = % { "name" => "Devin Torres" }
203
+ as = fn % { "name" => _name } -> % Person { } end
204
+ expected = % Person { name: "Devin Torres" }
205
+ assert transform ( person , % { as: as } ) == expected
206
+ end
207
+
208
+ test "decoding a single :as function with atom keys" do
209
+ person = % { name: "Devin Torres" }
210
+ as = fn % { name: _name } -> % Person { } end
211
+ expected = % Person { name: "Devin Torres" }
212
+ assert transform ( person , % { as: as , keys: :atoms! } ) == expected
213
+ end
214
+
215
+ test "decoding a :as list function with string keys" do
216
+ person = [ % { "name" => "Devin Torres" } ]
217
+ as = fn _value -> [ % Person { } ] end
218
+ expected = [ % Person { name: "Devin Torres" } ]
219
+ assert transform ( person , % { as: as } ) == expected
220
+ end
221
+
222
+ test "decoding nested :as function with string keys" do
223
+ person = % { "person" => % { "name" => "Devin Torres" } }
224
+ as = fn _value -> % { "person" => % Person { } } end
225
+ actual = transform ( person , % { as: as } )
226
+ expected = % { "person" => % Person { name: "Devin Torres" } }
227
+ assert actual == expected
228
+ end
229
+
230
+ test "decoding nested structs in :as function with string keys" do
231
+ person = % { "name" => "Devin Torres" , "contact" => % { "email" => "[email protected] " } }
232
+ as = fn _value -> % Person { contact: % Contact { } } end
233
+ expected = % Person { name: "Devin Torres" , contact: % Contact { email: "[email protected] " } }
234
+ assert transform ( person , % { as: as } ) == expected
235
+ end
200
236
end
0 commit comments