Skip to content

Commit

Permalink
Split encoding/decoding tests and minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Torres committed Nov 16, 2012
1 parent ec66c5f commit 5d13b5b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lib/exn/decoder.ex
Expand Up @@ -17,14 +17,11 @@ defmodule Exn.Decoder do
defp ast_to_value({:__r__, _, [val, opts]}) do
Regex.compile!(ast_to_value(val), ast_to_value(opts))
end
defp ast_to_value({:__p__, _, [pid, _opts]}) do
list_to_pid(to_char_list("<#{ast_to_value(pid)}>"))
end
defp ast_to_value({:__aliases__, _, aliases}) do
aliases = ast_to_value(aliases)
Module.concat aliases
end
defp ast_to_value({:"..", _, values}) do
defp ast_to_value({:.., _, values}) do
values = ast_to_value(values)
[first, last] = values
first..last
Expand All @@ -39,7 +36,7 @@ defmodule Exn.Decoder do
{b, _} = Code.eval_quoted({:<<>>, line,
lc item inlist items do
case item do
{:"::", line, [v|t]} -> {:"::", line, [ast_to_value(v)|t]}
{:::, line, [v|t]} -> {:::, line, [ast_to_value(v)|t]}
_ -> ast_to_value(item)
end
end})
Expand Down
25 changes: 22 additions & 3 deletions test/exn_test.exs
Expand Up @@ -23,18 +23,37 @@ defmodule ExnTest do
assert Exn.encode([{:b, 1}, {:a, 2}]) == "[b: 1, a: 2]"
end

test "keyword decoding" do
assert Exn.decode("[a: 1, b: 2]") == [a: 1, b: 2]
assert Exn.decode("[{:b,1},{:a,2}]") == [b: 1, a: 2]
end

test "range encoding" do
assert Exn.encode(1..2) == "1..2"
assert Exn.decode("1..2") == 1..2
assert Exn.encode(-9..-1) == "-9..-1"
end

test "range decoding" do
assert Exn.decode("1..2") == 1..2
assert Exn.decode("-9..-1") == -9..-1
end

test "regexp encoding" do
assert Exn.encode(%r(.*)) == "%r\".*\""
end

test "regexp decoding" do
assert Exn.decode("%r\".*\"") == %r(.*)
end

test "record encoding" do
assert Exn.encode(TestRecord.new(a: 1)) == "{TestRecord,[a: 1]}"
end

test "record decoding" do
assert Exn.decode("{TestRecord,[a: 1]}") == {TestRecord,[a: 1]}
end

test "pid encoding" do
me = self
assert Exn.EncodeError[value: ^me] = catch_error(Exn.encode(me))
Expand All @@ -57,12 +76,12 @@ defmodule ExnTest do

test "default record encoding" do
record = File.Stat.new
assert Exn.decode(Exn.encode(record)) == record
assert Exn.decode(Exn.encode(record)) == record
end

test "overridden record encoding" do
record = File.Stat.new
{:module, module, _, _} =
{:module, module, _, _} =
defimpl Exn.Encoder, for: File.Stat do
def encode(_), do: "custom"
end
Expand Down

0 comments on commit 5d13b5b

Please sign in to comment.