-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #20 from williamthome/smart-modules
Implement smart modules
- Loading branch information
Showing
17 changed files
with
4,651 additions
and
846 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule EuneusTest.EuneusDecoderJsonSuiteTest do | ||
use ExUnit.Case, async: true | ||
|
||
# Implementation-dependent tests | ||
i_succeeds = [ | ||
"number_double_huge_neg_exp.json", | ||
"number_real_underflow.json", | ||
"number_too_big_neg_int.json", | ||
"number_too_big_pos_int.json", | ||
"number_very_big_negative_int.json", | ||
"structure_500_nested_arrays.json" | ||
] | ||
|
||
i_fails = [ | ||
"number_huge_exp.json", | ||
"number_neg_int_huge_exp.json", | ||
"number_pos_double_huge_exp.json", | ||
"number_real_neg_overflow.json", | ||
"number_real_pos_overflow.json", | ||
"object_key_lone_2nd_surrogate.json", | ||
"string_1st_surrogate_but_2nd_missing.json", | ||
"string_1st_valid_surrogate_2nd_invalid.json", | ||
"string_UTF-16LE_with_BOM.json", | ||
"string_UTF-8_invalid_sequence.json", | ||
"string_UTF8_surrogate_U+D800.json", | ||
"string_incomplete_surrogate_and_escape_valid.json", | ||
"string_incomplete_surrogate_pair.json", | ||
"string_incomplete_surrogates_escape_valid.json", | ||
"string_invalid_lonely_surrogate.json", | ||
"string_invalid_surrogate.json", | ||
"string_invalid_utf-8.json", | ||
"string_inverted_surrogates_U+1D11E.json", | ||
"string_iso_latin_1.json", | ||
"string_lone_second_surrogate.json", | ||
"string_lone_utf8_continuation_byte.json", | ||
"string_not_in_unicode_range.json", | ||
"string_overlong_sequence_2_bytes.json", | ||
"string_overlong_sequence_6_bytes.json", | ||
"string_overlong_sequence_6_bytes_null.json", | ||
"string_truncated-utf-8.json", | ||
"string_utf16BE_no_BOM.json", | ||
"string_utf16LE_no_BOM.json", | ||
"structure_UTF-8_BOM_empty_object.json" | ||
] | ||
|
||
for path <- Path.wildcard("priv/data/json/*") do | ||
case Path.basename(path) do | ||
"y_" <> name -> | ||
test name do | ||
{:ok, _} = :euneus.decode(File.read!(unquote(path)), %{}) | ||
end | ||
|
||
"n_" <> name -> | ||
test name do | ||
{:error, _} = :euneus.decode(File.read!(unquote(path)), %{}) | ||
end | ||
|
||
"i_" <> name -> | ||
cond do | ||
name in i_fails -> | ||
test name do | ||
{:error, _} = :euneus.decode(File.read!(unquote(path)), %{}) | ||
end | ||
|
||
name in i_succeeds -> | ||
test name do | ||
{:ok, _} = :euneus.decode(File.read!(unquote(path)), %{}) | ||
end | ||
end | ||
end | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
euneus_test/test/property_test.exs → euneus_test/test/euneus_property_test.exs
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
2 changes: 1 addition & 1 deletion
2
euneus_test/test/json_suite_test.exs → .../euneus_smart_decoder_json_suite_test.exs
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
defmodule EuneusTest.EuneusSmartDecoderTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "numbers" do | ||
assert_fail_with("-", :unexpected_end_of_input) | ||
assert_fail_with("--1", {:unexpected_byte, "0x2D", 1}) | ||
assert_fail_with("01", {:unexpected_byte, "0x31", 1}) | ||
assert_fail_with(".1", {:unexpected_byte, "0x2E", 0}) | ||
assert_fail_with("1.", :unexpected_end_of_input) | ||
assert_fail_with("1e", :unexpected_end_of_input) | ||
assert_fail_with("1.0e+", :unexpected_end_of_input) | ||
assert_fail_with("1e999", {:unexpected_sequence, "1e999", 0}) | ||
|
||
assert decode!("0") == 0 | ||
assert decode!("1") == 1 | ||
assert decode!("-0") == 0 | ||
assert decode!("-1") == -1 | ||
assert decode!("0.1") == 0.1 | ||
assert decode!("-0.1") == -0.1 | ||
assert decode!("0e0") == 0 | ||
assert decode!("0E0") == 0 | ||
assert decode!("1e0") == 1 | ||
assert decode!("1E0") == 1 | ||
assert decode!("1.0e0") == 1.0 | ||
assert decode!("1e+0") == 1 | ||
assert decode!("1.0e+0") == 1.0 | ||
assert decode!("0.1e1") == 0.1e1 | ||
assert decode!("0.1e-1") == 0.1e-1 | ||
assert decode!("99.99e99") == 99.99e99 | ||
assert decode!("-99.99e-99") == -99.99e-99 | ||
assert decode!("123456789.123456789e123") == 123_456_789.123456789e123 | ||
end | ||
|
||
test "strings" do | ||
assert_fail_with(~s("), :unexpected_end_of_input) | ||
assert_fail_with(~s("\\"), :unexpected_end_of_input) | ||
assert_fail_with(~s("\\k"), {:unexpected_byte, "0x6B", 2}) | ||
assert_fail_with(<<?\", 128, ?\">>, {:unexpected_byte, "0x80", 1}) | ||
assert_fail_with(~s("\\u2603\\"), :unexpected_end_of_input) | ||
|
||
assert_fail_with( | ||
~s("Here's a snowman for you: ☃. Good day!), | ||
:unexpected_end_of_input | ||
) | ||
|
||
assert_fail_with(~s("𝄞), :unexpected_end_of_input) | ||
assert_fail_with(~s(\u001F), {:unexpected_byte, "0x1F", 0}) | ||
assert_fail_with(~s("\\ud8aa\\udcxx"), {:unexpected_sequence, "\\udcxx", 7}) | ||
|
||
assert_fail_with( | ||
~s("\\ud8aa\\uda00"), | ||
{:unexpected_sequence, "\\ud8aa\\uda00", 1} | ||
) | ||
|
||
assert_fail_with(~s("\\uxxxx"), {:unexpected_sequence, "\\uxxxx", 1}) | ||
|
||
assert decode!(~s("\\"\\\\\\/\\b\\f\\n\\r\\t")) == ~s("\\/\b\f\n\r\t) | ||
assert decode!(~s("\\u2603")) == "☃" | ||
assert decode!(~s("\\u2028\\u2029")) == "\u2028\u2029" | ||
assert decode!(~s("\\uD834\\uDD1E")) == "𝄞" | ||
assert decode!(~s("\\uD834\\uDD1E")) == "𝄞" | ||
assert decode!(~s("\\uD799\\uD799")) == "힙힙" | ||
assert decode!(~s("✔︎")) == "✔︎" | ||
end | ||
|
||
test "objects" do | ||
assert_fail_with("{", :unexpected_end_of_input) | ||
assert_fail_with("{,", {:unexpected_byte, "0x2C", 1}) | ||
assert_fail_with(~s({"foo"}), {:unexpected_byte, "0x7D", 6}) | ||
assert_fail_with(~s({"foo": "bar",}), {:unexpected_byte, "0x7D", 14}) | ||
|
||
assert decode!("{}") == %{} | ||
assert decode!(~s({"foo": "bar"})) == %{"foo" => "bar"} | ||
assert decode!(~s({"foo" : "bar"})) == %{"foo" => "bar"} | ||
|
||
expected = %{"foo" => "bar", "baz" => "quux"} | ||
assert decode!(~s({"foo": "bar", "baz": "quux"})) == expected | ||
|
||
expected = %{"foo" => %{"bar" => "baz"}} | ||
assert decode!(~s({"foo": {"bar": "baz"}})) == expected | ||
end | ||
|
||
test "arrays" do | ||
assert_fail_with("[", :unexpected_end_of_input) | ||
assert_fail_with("[,", {:unexpected_byte, "0x2C", 1}) | ||
assert_fail_with("[1,]", {:unexpected_byte, "0x5D", 3}) | ||
|
||
assert decode!("[]") == [] | ||
assert decode!("[1, 2, 3]") == [1, 2, 3] | ||
assert decode!(~s(["foo", "bar", "baz"])) == ["foo", "bar", "baz"] | ||
assert decode!(~s([{"foo": "bar"}])) == [%{"foo" => "bar"}] | ||
end | ||
|
||
test "whitespace" do | ||
assert_fail_with("", :unexpected_end_of_input) | ||
assert_fail_with(" ", :unexpected_end_of_input) | ||
|
||
assert decode!(" [ ] ") == [] | ||
assert decode!(" { } ") == %{} | ||
|
||
assert decode!(" [ 1 , 2 , 3 ] ") == [1, 2, 3] | ||
|
||
expected = %{"foo" => "bar", "baz" => "quux"} | ||
assert decode!(~s( { "foo" : "bar" , "baz" : "quux" } )) == expected | ||
end | ||
|
||
test "iodata" do | ||
body = String.split(~s([1,2,3,4]), "") | ||
expected = [1, 2, 3, 4] | ||
assert decode!(body) == expected | ||
end | ||
|
||
defp assert_fail_with(string, error) do | ||
assert {:error, error} == decode(string) | ||
end | ||
|
||
defp decode!(json) do | ||
{:ok, x} = decode(json) | ||
x | ||
end | ||
|
||
defp decode(json) do | ||
:euneus.decode(json) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule EuneusTest.EuneusSmartEncoderTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "atom" do | ||
assert encode!(:undefined) == "null" | ||
assert encode!(true) == "true" | ||
assert encode!(false) == "false" | ||
assert encode!(:poison) == ~s("poison") | ||
end | ||
|
||
test "integer" do | ||
assert encode!(42) == "42" | ||
end | ||
|
||
test "float" do | ||
assert encode!(99.99) == "99.99" | ||
assert encode!(9.9e100) == "9.9e100" | ||
end | ||
|
||
test "binaries" do | ||
assert encode!("hello world") == ~s("hello world") | ||
assert encode!("hello\nworld") == ~s("hello\\nworld") | ||
assert encode!("\nhello\nworld\n") == ~s("\\nhello\\nworld\\n") | ||
|
||
assert encode!("\"") == ~s("\\"") | ||
assert encode!("\0") == ~s("\\u0000") | ||
assert encode!(<<31>>) == ~s("\\u001F") | ||
|
||
assert encode!("áéíóúàèìòùâêîôûãẽĩõũ") == ~s("áéíóúàèìòùâêîôûãẽĩõũ") | ||
end | ||
|
||
test "Map" do | ||
assert encode!(%{}) == "{}" | ||
assert encode!(%{"foo" => "bar"}) == ~s({"foo":"bar"}) | ||
assert encode!(%{foo: :bar}) == ~s({"foo":"bar"}) | ||
assert encode!(%{~c"foo" => :bar}) == ~s({"foo":"bar"}) | ||
assert encode!(%{0 => 0}) == ~s({"0":0}) | ||
|
||
multi_key_map = %{"foo" => "foo1", :foo => "foo2"} | ||
|
||
assert encode!(multi_key_map) == ~s({"foo":"foo2","foo":"foo1"}) | ||
end | ||
|
||
test "list" do | ||
assert encode!([]) == "[]" | ||
assert encode!([1, 2, 3]) == "[1,2,3]" | ||
end | ||
|
||
test "throws error" do | ||
assert encode(<<0x80>>) == {:error, {:invalid_byte, ~s(0x80), <<128>>}} | ||
|
||
assert encode(<<?a, 208>>) == | ||
{:error, {:invalid_byte, ~s(0xD0), <<97, 208>>}} | ||
end | ||
|
||
defp encode!(x) do | ||
{:ok, json} = encode(x) | ||
json | ||
end | ||
|
||
defp encode(x) do | ||
:euneus.encode_to_binary(x) | ||
end | ||
end |
Oops, something went wrong.