diff --git a/.travis.yml b/.travis.yml index 8d2a6ee..45836f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,6 @@ script: # Check that code is formatted - "mix format --dry-run --check-formatted" # Run all tests except pending ones - - "mix test --exclude pending --trace" + - "mix test --trace" # Push code coverage - "mix coveralls.travis" diff --git a/lib/elven_gard/protocol/binary/integer_type.ex b/lib/elven_gard/protocol/binary/integer_type.ex index 39fff9c..5f9b85c 100644 --- a/lib/elven_gard/protocol/binary/integer_type.ex +++ b/lib/elven_gard/protocol/binary/integer_type.ex @@ -1,6 +1,8 @@ defmodule ElvenGard.Protocol.Binary.IntegerType do @moduledoc """ Define a custom integer type (uint32_t) for game protocols + + TODO: Manage signed/unsigned number & little/big/native endianness """ use ElvenGard.Type diff --git a/lib/elven_gard/protocol/binary/long_type.ex b/lib/elven_gard/protocol/binary/long_type.ex index ab4ada8..0941abd 100644 --- a/lib/elven_gard/protocol/binary/long_type.ex +++ b/lib/elven_gard/protocol/binary/long_type.ex @@ -1,6 +1,8 @@ defmodule ElvenGard.Protocol.Binary.LongType do @moduledoc """ Define a custom long type (uint64_t) for game protocols + + TODO: Manage signed/unsigned number & little/big/native endianness """ use ElvenGard.Type diff --git a/lib/elven_gard/protocol/binary/short_type.ex b/lib/elven_gard/protocol/binary/short_type.ex index 064004e..b97307b 100644 --- a/lib/elven_gard/protocol/binary/short_type.ex +++ b/lib/elven_gard/protocol/binary/short_type.ex @@ -1,6 +1,8 @@ defmodule ElvenGard.Protocol.Binary.ShortType do @moduledoc """ Define a custom short type (uint16_t) for game protocols + + TODO: Manage signed/unsigned number & little/big/native endianness """ use ElvenGard.Type diff --git a/test/heavens_strike_test.exs b/test/heavens_strike_test.exs deleted file mode 100644 index 18dae9b..0000000 --- a/test/heavens_strike_test.exs +++ /dev/null @@ -1,4 +0,0 @@ -defmodule ElvenGardTest do - use ExUnit.Case - doctest ElvenGard -end diff --git a/test/lib/elven_gard/protocol/binary/byte_type_test.exs b/test/lib/elven_gard/protocol/binary/byte_type_test.exs new file mode 100644 index 0000000..ac9519f --- /dev/null +++ b/test/lib/elven_gard/protocol/binary/byte_type_test.exs @@ -0,0 +1,37 @@ +defmodule ElvenGard.Protocol.Binary.ByteTypeTest do + use ExUnit.Case + + alias ElvenGard.Protocol.Binary.ByteType + + describe "Encode binary byte type:" do + test "basic behaviour" do + got = ByteType.encode(0x13) + expected = <<0x13>> + + assert expected == got + end + + test "with number overflow" do + got = ByteType.encode(0x1337, []) + expected = <<0x37>> + + assert expected == got + end + end + + describe "Decode binary byte type:" do + test "without rest" do + got = ByteType.decode(<<0x13>>) + expected = {0x13, <<>>} + + assert expected == got + end + + test "with rest" do + got = ByteType.decode(<<0x13, 0x37, 0x00>>, []) + expected = {0x13, <<0x37, 0x00>>} + + assert expected == got + end + end +end diff --git a/test/lib/elven_gard/protocol/binary/integer_type_test.exs b/test/lib/elven_gard/protocol/binary/integer_type_test.exs new file mode 100644 index 0000000..86dd621 --- /dev/null +++ b/test/lib/elven_gard/protocol/binary/integer_type_test.exs @@ -0,0 +1,133 @@ +defmodule ElvenGard.Protocol.Binary.IntegerTypeTest do + use ExUnit.Case + + alias ElvenGard.Protocol.Binary.IntegerType + + describe "Encode binary integer type:" do + @tag :pending + test "default behaviour (unsigned + big)" do + got = IntegerType.encode(0x1337) + expected = <<0x1337::size(32)>> + + assert expected == got + end + + @tag :pending + test "default behaviour with overflow (unsigned + big)" do + got = IntegerType.encode(0x1337133700) + expected = <<0x37133700::size(32)>> + + assert expected == got + end + + test "signed + little" do + got = IntegerType.encode(0x1337, signed: true, endian: :little) + expected = <<0x1337::signed-little-size(32)>> + + assert expected == got + end + + test "unsigned + little" do + got = IntegerType.encode(0x1337, signed: false, endian: :little) + expected = <<0x1337::unsigned-little-size(32)>> + + assert expected == got + end + + @tag :pending + test "signed + big" do + got = IntegerType.encode(0x1337, signed: true, endian: :big) + expected = <<0x1337::signed-big-size(32)>> + + assert expected == got + end + + @tag :pending + test "unsigned + big" do + got = IntegerType.encode(0x1337, signed: false, endian: :big) + expected = <<0x1337::unsigned-big-size(32)>> + + assert expected == got + end + + test "signed + native" do + got = IntegerType.encode(0x1337, signed: true, endian: :native) + expected = <<0x1337::signed-native-size(32)>> + + assert expected == got + end + + test "unsigned + native" do + got = IntegerType.encode(0x1337, signed: false, endian: :native) + expected = <<0x1337::unsigned-native-size(32)>> + + assert expected == got + end + end + + describe "Decode binary integer type:" do + @tag :pending + test "default behaviour without rest (unsigned + big)" do + got = IntegerType.decode(<<0x1337::size(32)>>) + expected = {0x1337, <<>>} + + assert expected == got + end + + @tag :pending + test "default behaviour with rest (unsigned + big)" do + got = IntegerType.decode(<<0x1337::size(32), 0x42::signed-size(32), 0x01::little-size(32)>>) + expected = {0x1337, <<0x42::signed-size(32), 0x01::little-size(32)>>} + + assert expected == got + end + + test "signed + little" do + got = IntegerType.decode(<<0x1337::signed-little-size(32)>>, signed: true, endian: :little) + expected = {0x1337, <<>>} + + assert expected == got + end + + test "unsigned + little" do + got = + IntegerType.decode(<<0x1337::unsigned-little-size(32)>>, signed: false, endian: :little) + + expected = {0x1337, <<>>} + + assert expected == got + end + + @tag :pending + test "signed + big" do + got = IntegerType.decode(<<0x1337::signed-big-size(32)>>, signed: true, endian: :big) + expected = {0x1337, <<>>} + + assert expected == got + end + + @tag :pending + test "unsigned + big" do + got = IntegerType.decode(<<0x1337::unsigned-big-size(32)>>, signed: false, endian: :big) + expected = {0x1337, <<>>} + + assert expected == got + end + + test "signed + native" do + got = IntegerType.decode(<<0x1337::signed-native-size(32)>>, signed: true, endian: :native) + expected = {0x1337, <<>>} + + assert expected == got + end + + test "unsigned + native" do + got = + IntegerType.decode(<<0x1337::unsigned-native-size(32)>>, signed: false, endian: :native) + + expected = {0x1337, <<>>} + + assert expected == got + end + end +end diff --git a/test/lib/elven_gard/protocol/textual/float_type_test.exs b/test/lib/elven_gard/protocol/textual/float_type_test.exs new file mode 100644 index 0000000..4f2e4e0 --- /dev/null +++ b/test/lib/elven_gard/protocol/textual/float_type_test.exs @@ -0,0 +1,19 @@ +defmodule ElvenGard.Protocol.Textual.FloatTypeTest do + use ExUnit.Case + + alias ElvenGard.Protocol.Textual.FloatType + + test "Encode textual float type" do + got = FloatType.encode(13.37) + expected = "13.37" + + assert expected == got + end + + test "Decode textual float type" do + got = FloatType.decode("13.37") + expected = 13.37 + + assert expected == got + end +end diff --git a/test/lib/elven_gard/protocol/textual/integer_type_test.exs b/test/lib/elven_gard/protocol/textual/integer_type_test.exs new file mode 100644 index 0000000..500e443 --- /dev/null +++ b/test/lib/elven_gard/protocol/textual/integer_type_test.exs @@ -0,0 +1,19 @@ +defmodule ElvenGard.Protocol.Textual.IntegerTypeTest do + use ExUnit.Case + + alias ElvenGard.Protocol.Textual.IntegerType + + test "Encode textual integer type" do + got = IntegerType.encode(1337) + expected = "1337" + + assert expected == got + end + + test "Decode textual integer type" do + got = IntegerType.decode("1337") + expected = 1337 + + assert expected == got + end +end diff --git a/test/lib/elven_gard/protocol/textual/string_type_test.exs b/test/lib/elven_gard/protocol/textual/string_type_test.exs new file mode 100644 index 0000000..b1539d5 --- /dev/null +++ b/test/lib/elven_gard/protocol/textual/string_type_test.exs @@ -0,0 +1,19 @@ +defmodule ElvenGard.Protocol.Textual.StringTypeTest do + use ExUnit.Case + + alias ElvenGard.Protocol.Textual.StringType + + test "Encode textual string type" do + got = StringType.encode("1337 1337 1337") + expected = "1337 1337 1337" + + assert expected == got + end + + test "Decode textual string type" do + got = StringType.decode("1337 1337 1337") + expected = "1337 1337 1337" + + assert expected == got + end +end diff --git a/test/lib/elven_gard/type_test.exs b/test/lib/elven_gard/type_test.exs new file mode 100644 index 0000000..ca30aed --- /dev/null +++ b/test/lib/elven_gard/type_test.exs @@ -0,0 +1,23 @@ +defmodule ElvenGard.TypeTest do + use ExUnit.Case + + defmodule BasicType do + use ElvenGard.Type + + @impl ElvenGard.Type + def encode(val, _opts), do: val + + @impl ElvenGard.Type + def decode(val, _opts), do: val + end + + describe "Type behaviour defines:" do + test "encode/1" do + assert :erlang.function_exported(BasicType, :encode, 1) + end + + test "decode/1" do + assert :erlang.function_exported(BasicType, :decode, 1) + end + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..1f9d0cd 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,2 @@ +ExUnit.configure(exclude: [:pending]) ExUnit.start()