Skip to content

Commit

Permalink
added version and as_server
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreat committed May 14, 2017
1 parent e75c850 commit 591554e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions zmq_ex/lib/zmq_ex.ex
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,29 @@
defmodule ZmqEx do defmodule ZmqEx do


def version(%{major: major, minor: minor}) do
{:ok, <<major, minor>>}
end

def version(<<major, minor>>) do
{:ok, %{major: major, minor: minor}}
end

def as_server(<<as_server>>) do
as_server_convert(as_server)
end

defp as_server_convert(1) do
{:ok, true}
end

defp as_server_convert(0) do
{:ok, false}
end

defp as_server_convert(_) do
{:error, :wrong_as_server}
end

def encode(%{flags: flags = %{type: _, long: :short, more: _}, size: size, body: <<body>>}) do def encode(%{flags: flags = %{type: _, long: :short, more: _}, size: size, body: <<body>>}) do
<<encode_flags(flags), size :: size(8), body>> <<encode_flags(flags), size :: size(8), body>>
end end
Expand Down
26 changes: 26 additions & 0 deletions zmq_ex/test/zmq_ex_test.exs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,4 +61,30 @@ defmodule ZmqExTest do
result = ZmqEx.decode_command(%{flags: %{type: :command, long: :long, more: false}, size: 64, body: "command2A command body"}) result = ZmqEx.decode_command(%{flags: %{type: :command, long: :long, more: false}, size: 64, body: "command2A command body"})
assert result == %{name: "command2", size: 56, data: "A command body"} assert result == %{name: "command2", size: 56, data: "A command body"}
end end

test "test acting as a server" do
result = ZmqEx.as_server(<<1>>)
assert result == {:ok, true}
end

test "test acting not as a server" do
result = ZmqEx.as_server(<<0>>)
assert result == {:ok, false}
end

test "test wrong as a server value" do
result = ZmqEx.as_server(<<2>>)
assert result == {:error, :wrong_as_server}
end

test "decode version" do
result = ZmqEx.version(<<3,0>>)
assert result == {:ok, %{major: 3, minor: 0}}
end

test "encode version" do
result = ZmqEx.version(%{major: 3, minor: 1})
assert result == {:ok, <<3, 1>>}
end

end end

0 comments on commit 591554e

Please sign in to comment.