Skip to content

Commit

Permalink
Test the InquiryComplete module
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspofford committed May 27, 2019
1 parent bc7d55a commit 6d05a76
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/harald/error_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule Harald.ErrorCode do
Reference: Version 5.0, Vol 2, Part D, 1
## Non-standard Names
For error codes `0x2B`, `0x31`, and `0x33`, their respective names are suffixed like ` (0x2B)`
as to differentiate between the three when serializing.
"""
Expand Down Expand Up @@ -88,9 +87,13 @@ defmodule Harald.ErrorCode do

Enum.each(@error_codes, fn
{error_code, name} ->
def name(unquote(error_code)), do: unquote(name)
def error_code(unquote(name)), do: unquote(error_code)
def name(unquote(error_code)), do: {:ok, unquote(name)}
def error_code(unquote(name)), do: {:ok, unquote(error_code)}
end)

def name(_), do: :error

def error_code(_), do: :error

def all, do: @error_codes
end
14 changes: 12 additions & 2 deletions lib/harald/hci/event/inquiry_complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ defmodule Harald.HCI.Event.InquiryComplete do
def event_code, do: @event_code

@impl Serializable
def serialize(%__MODULE__{status: status}), do: {:ok, <<ErrorCode.error_code(status)>>}
def serialize(%__MODULE__{status: status} = data) do
case ErrorCode.error_code(status) do
{:ok, error_code} -> {:ok, <<error_code>>}
:error -> {:error, data}
end
end

@impl Serializable
def deserialize(<<status>>), do: {:ok, %__MODULE__{status: ErrorCode.name(status)}}
def deserialize(<<status>>) do
case ErrorCode.name(status) do
{:ok, name} -> {:ok, %__MODULE__{status: name}}
:error -> {:error, %__MODULE__{status: status}}
end
end

def deserialize(bin), do: {:error, bin}
end
28 changes: 28 additions & 0 deletions test/harald/hci/event/inquiry_complete_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Harald.HCI.Event.InquiryCompleteTest do
use ExUnit.Case, async: true
use ExUnitProperties
alias Harald.Generators.HCI.Event.InquiryComplete, as: InquiryCompleteGen
alias Harald.HCI.Event.InquiryComplete

doctest InquiryComplete, import: true

property "symmetric serialization" do
check all parameters <- InquiryCompleteGen.parameters() do
case InquiryComplete.deserialize(parameters) do
{:ok, data} ->
assert {:ok, bin} = InquiryComplete.serialize(data)
assert :binary.bin_to_list(parameters) == :binary.bin_to_list(bin)

{:error, _} ->
:ok
end
end

check all parameters <- StreamData.binary() do
case InquiryComplete.deserialize(parameters) do
{:ok, data} -> assert {:ok, parameters} == InquiryComplete.serialize(data)
{:error, _} -> :ok
end
end
end
end

0 comments on commit 6d05a76

Please sign in to comment.