Skip to content

Instantly share code, notes, and snippets.

@kitofr
Created November 21, 2013 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitofr/7590236 to your computer and use it in GitHub Desktop.
Save kitofr/7590236 to your computer and use it in GitHub Desktop.
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
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
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