quality: Add test cases for instructions

This commit is contained in:
Eishun Kondoh 2019-04-28 23:57:22 +09:00
parent b4c6f0822c
commit a0ee397921
9 changed files with 120 additions and 43 deletions

View file

@ -1,18 +1,31 @@
defmodule Openflow.Instruction do
def read(instruction_bin) do
do_read([], instruction_bin)
end
@moduledoc """
Openflow instruction codec handler
"""
def to_binary(instructions) when is_list(instructions) do
to_binary(<<>>, instructions)
end
@type instruction ::
Openflow.Instruction.ApplyActions.t()
| Openflow.Instruction.WriteActions.t()
| Openflow.Instruction.ClearActions.t()
| Openflow.Instruction.GotoTable.t()
| Openflow.Instruction.WriteMetadata.t()
| Openflow.Instruction.Meter.t()
def to_binary(instruction) do
to_binary([instruction])
end
@spec read(<<_::32, _::_*8>>) :: [instruction()]
def read(instruction_bin),
do: do_read([], instruction_bin)
@spec to_binary(instruction()) :: <<_::32, _::_*8>>
def to_binary(instructions) when is_list(instructions),
do: to_binary(<<>>, instructions)
@spec to_binary([instruction()]) :: <<_::32, _::_*8>>
def to_binary(instruction),
do: to_binary([instruction])
# private functions
@spec do_read([instruction()], <<_::_*8>>) :: [instruction()]
defp do_read(acc, <<>>), do: Enum.reverse(acc)
defp do_read(acc, <<type::16, length::16, _::bytes>> = binary) do
@ -21,6 +34,7 @@ defmodule Openflow.Instruction do
do_read([codec.read(instruction_bin) | acc], rest)
end
@spec to_binary(<<_::_*8>>) :: [instruction()]
defp to_binary(acc, []), do: acc
defp to_binary(acc, [instruction | rest]) do