Created
November 21, 2013 21:43
-
-
Save kitofr/7590236 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
1) test convert (Hashish.Test) | |
** (ExUnit.ExpectationError) | |
expected: [project: [[[:id, "_Root"], [[:name, "<Root project>"], [[:href, "/guestAuth/app/rest/projects/id:_Root"]]]], [[[:id, "AmazonApiClient"], [[:name, "Amazon API client"], [[:href, "/guestAuth/app/rest/projects/id:AmazonApiClient"]]]]]]] | |
to be equal to (===): [project: [[id: "_Root", name: "<Root project>", href: "/guestAuth/app/rest/projects/id:_Root"], [id: "AmazonApiClient", name: "Amazon API client", href: "/guestAuth/app/rest/projects/id:AmazonApiClient"]]] | |
at test/tcbot_test.exs:15 | |
This file contains hidden or 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
defmodule Hashish | |
def to_atom(v) when is_binary(v) do | |
binary_to_atom(v) | |
end | |
def convert_to_hashdicts({k,v}) when !is_list v do | |
[ to_atom(k), v ] | |
end | |
def convert_to_hashdicts({k,v}) when is_list v do | |
{ to_atom(k), convert_to_hashdicts(v) } | |
end | |
def convert_to_hashdicts([head]) do | |
[ convert_to_hashdicts(head) ] | |
end | |
def convert_to_hashdicts([head|tail]) do | |
[ convert_to_hashdicts(head), convert_to_hashdicts(tail) ] | |
end | |
end |
This file contains hidden or 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
defmodule Hashish.Tests do | |
use ExUnit.Case | |
import Hashish, only: [ convert_to_hashdicts: 1 ] | |
test "convert" do | |
json = [{"project", | |
[[{"id", "_Root"}, {"name", "<Root project>"}, {"href", "/guestAuth/app/rest/projects/id:_Root"}], | |
[{"id", "AmazonApiClient"}, {"name", "Amazon API client"}, {"href", "/guestAuth/app/rest/projects/id:AmazonApiClient"}] | |
] | |
}] | |
exct = [{:project, | |
[[{:id, "_Root"}, {:name, "<Root project>"}, {:href, "/guestAuth/app/rest/projects/id:_Root"}], | |
[{:id, "AmazonApiClient"}, {:name, "Amazon API client"}, {:href, "/guestAuth/app/rest/projects/id:AmazonApiClient"}]]}] | |
assert convert_to_hashdicts(json) === exct | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment