Skip to content

Commit

Permalink
✅ Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ImNotAVirus committed Jul 14, 2019
1 parent a56a7e1 commit 1bf9dc2
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions lib/elven_gard/protocol/binary/integer_type.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/elven_gard/protocol/binary/long_type.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/elven_gard/protocol/binary/short_type.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/heavens_strike_test.exs

This file was deleted.

37 changes: 37 additions & 0 deletions test/lib/elven_gard/protocol/binary/byte_type_test.exs
Original file line number Diff line number Diff line change
@@ -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
133 changes: 133 additions & 0 deletions test/lib/elven_gard/protocol/binary/integer_type_test.exs
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/lib/elven_gard/protocol/textual/float_type_test.exs
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/lib/elven_gard/protocol/textual/integer_type_test.exs
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/lib/elven_gard/protocol/textual/string_type_test.exs
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions test/lib/elven_gard/type_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ExUnit.configure(exclude: [:pending])
ExUnit.start()

0 comments on commit 1bf9dc2

Please sign in to comment.