diff --git a/.credo.exs b/.credo.exs new file mode 100644 index 0000000..d8609cf --- /dev/null +++ b/.credo.exs @@ -0,0 +1,15 @@ +%{ + configs: [ + %{ + name: "default", + strict: true, + files: %{ + included: ["lib/", "test/"] + }, + checks: [ + {Credo.Check.Readability.AliasOrder, false}, + {Credo.Check.Readability.Specs, []} + ] + } + ] +} diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..6778194 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,36 @@ +--- +name: 🐞 Bug report +about: Tell us about a bug you found +title: '' +labels: '🐞 Bug, ⛑ Needs triage' +assignees: '' + +--- + +### Precheck + +- [ ] Do a quick search and make sure the bug has not been reported yet. +- [ ] Please disclose security vulnerabilities privately at [oss@kommit.co](mailto:oss@kommit.co). +- [ ] Finally, be nice and have fun!. + +### Description + +A clear and concise description of what the bug is. + +### Environment + +* Elixir & Erlang/OTP versions (elixir --version): +* Operating system: + +### Current behavior + +Include code samples, errors and stack traces if appropriate. +If reporting a bug, please include the reproducing steps. + +### Expected behavior + +A clear and concise description of what you expected to happen. + +### Additional context + +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..4de8978 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,24 @@ +--- +name: 👋 Feature request +about: Tell us what you'd like to see +title: '' +labels: '👍 Feature, ⛑ Needs triage' +assignees: '' + +--- + +### Is your feature request related to a problem? Please describe. + +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +### Describe the solution you'd like + +A clear and concise description of what you want to happen. + +### Describe alternatives you've considered + +A clear and concise description of any alternative solutions or features you've considered. + +### Additional context + +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index c2fcad3..030f8ad 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -35,6 +35,8 @@ jobs: run: mix format --check-formatted - name: Run Tests run: mix test + - name: Run credo + run: mix credo --strict - name: Run Excoveralls run: mix coveralls.github env: diff --git a/CHANGELOG.md b/CHANGELOG.md index bac1554..fbe30cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.2.0 (27.08.2021) +* Fix array's elements encoding bug. +* Refactor library exceptions. +* Credo integration. +* Code cleanup. + ## 0.1.6 (25.08.2021) * Solve success-typing dialyzer warnings. * Properly define typespecs. diff --git a/README.md b/README.md index 0613514..dcb2299 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/r ```elixir def deps do [ - {:elixir_xdr, "~> 0.1.6"} + {:elixir_xdr, "~> 0.2.0"} ] end ``` diff --git a/lib/error/error.ex b/lib/error/error.ex deleted file mode 100644 index 20d6ceb..0000000 --- a/lib/error/error.ex +++ /dev/null @@ -1,570 +0,0 @@ -defmodule XDR.Error do - @moduledoc """ - This module contains the definitions of the errors resulted from XDR encode or decode operations. - """ - - defmodule Int do - @moduledoc """ - This module contains the definition of `XDR.Error.Int` exception that may be raised by the `XDR.Int` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Int` exception with the message of the `error_type` passed. - """ - def exception(:not_integer) do - new("The value which you try to encode is not an integer") - end - - def exception(:not_binary) do - new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") - end - - def exception(:exceed_upper_limit) do - new( - "The integer which you try to encode exceed the upper limit of an integer, the value must be less than 2_147_483_647" - ) - end - - def exception(:exceed_lower_limit) do - new( - "The integer which you try to encode exceed the lower limit of an integer, the value must be more than -2_147_483_648" - ) - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Int{message: msg} - end - - defmodule UInt do - @moduledoc """ - This module contains the definition of `XDR.Error.UInt` exception that may be raised by the `XDR.UInt` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.UInt` exception with the message of the `error_type` passed. - """ - def exception(:not_integer) do - new("The value which you try to encode is not an integer") - end - - def exception(:not_binary) do - new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") - end - - def exception(:exceed_upper_limit) do - new( - "The integer which you try to encode exceed the upper limit of an unsigned integer, the value must be less than 4_294_967_295" - ) - end - - def exception(:exceed_lower_limit) do - new( - "The integer which you try to encode exceed the lower limit of an unsigned integer, the value must be more than 0" - ) - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.UInt{message: msg} - end - - defmodule Enum do - @moduledoc """ - This module contains the definition of `XDR.Error.Enum` exception that may be raised by the `XDR.Enum` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Enum` exception with the message of the `error_type` passed. - """ - def exception(:not_list) do - new("The declaration inside the Enum structure isn't a list") - end - - def exception(:not_an_atom) do - new("The name of the key which you try to encode isn't an atom") - end - - def exception(:not_binary) do - new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") - end - - def exception(:invalid_key) do - new("The key which you try to encode doesn't belong to the current declarations") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Enum{message: msg} - end - - defmodule Bool do - @moduledoc """ - This module contains the definition of `XDR.Error.Bool` exception that may be raised by the `XDR.Bool` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Bool` exception with the message of the `error_type` passed. - """ - def exception(:not_boolean) do - new("The value which you try to encode is not a boolean") - end - - def exception(:invalid_value) do - new("The value which you try to decode must be <<0, 0, 0, 0>> or <<0, 0, 0, 1>>") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Bool{message: msg} - end - - defmodule HyperInt do - @moduledoc """ - This module contains the definition of `XDR.Error.HyperInt` exception that may be raised by the `XDR.HyperInt` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.HyperInt` exception with the message of the `error_type` passed. - """ - def exception(:not_integer) do - new("The value which you try to encode is not an integer") - end - - def exception(:not_binary) do - new( - "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>" - ) - end - - def exception(:exceed_upper_limit) do - new( - "The integer which you try to encode exceed the upper limit of an Hyper Integer, the value must be less than 9_223_372_036_854_775_807" - ) - end - - def exception(:exceed_lower_limit) do - new( - "The integer which you try to encode exceed the lower limit of an Hyper Integer, the value must be more than -9_223_372_036_854_775_808" - ) - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.HyperInt{message: msg} - end - - defmodule HyperUInt do - @moduledoc """ - This module contains the definition of `XDR.Error.HyperUInt` exception that may be raised by the `XDR.HyperUInt` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.HyperUInt` exception with the message of the `error_type` passed. - """ - def exception(:not_integer) do - new("The value which you try to encode is not an integer") - end - - def exception(:not_binary) do - new( - "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>" - ) - end - - def exception(:exceed_upper_limit) do - new( - "The integer which you try to encode exceed the upper limit of an Hyper Unsigned Integer, the value must be less than 18_446_744_073_709_551_615" - ) - end - - def exception(:exceed_lower_limit) do - new( - "The integer which you try to encode exceed the lower limit of an Hyper Unsigned Integer, the value must be more than 0" - ) - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.HyperUInt{message: msg} - end - - defmodule Float do - @moduledoc """ - This module contains the definition of `XDR.Error.Float` exception that may be raised by the `XDR.Float` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Float` exception with the message of the `error_type` passed. - """ - def exception(:not_number) do - new("The value which you try to encode is not an integer or float value") - end - - def exception(:not_binary) do - new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Float{message: msg} - end - - defmodule DoubleFloat do - @moduledoc """ - This module contains the definition of `XDR.Error.DoubleFloat` exception that may be raised by the `XDR.DoubleFloat` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.DoubleFloat` exception with the message of the `error_type` passed. - """ - def exception(:not_number) do - new("The value which you try to encode is not an integer or float value") - end - - def exception(:not_binary) do - new( - "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>" - ) - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.DoubleFloat{message: msg} - end - - defmodule FixedOpaque do - @moduledoc """ - This module contains the definition of `XDR.Error.FixedOpaque` exception that may be raised by the `XDR.FixedOpaque` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.FixedOpaque` exception with the message of the `error_type` passed. - """ - def exception(:not_number) do - new("The value which you pass through parameters is not an integer") - end - - def exception(:not_binary) do - new( - "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>" - ) - end - - def exception(:invalid_length) do - new( - "The length that is passed through parameters must be equal or less to the byte size of the XDR to complete" - ) - end - - def exception(:exceed_length) do - new("The length is bigger than the byte size of the XDR") - end - - def exception(:not_valid_binary) do - new("The binary size of the binary which you try to decode must be a multiple of 4") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.FixedOpaque{message: msg} - end - - defmodule VariableOpaque do - @moduledoc """ - This module contains the definition of `XDR.Error.VariableOpaque` exception that may be raised by the `XDR.VariableOpaque` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.VariableOpaque` exception with the message of the `error_type` passed. - """ - def exception(:not_number) do - new("The value which you pass through parameters is not an integer") - end - - def exception(:not_binary) do - new( - "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>" - ) - end - - def exception(:invalid_length) do - new( - "The max length that is passed through parameters must be biger to the byte size of the XDR" - ) - end - - def exception(:exceed_lower_bound) do - new("The minimum value of the length of the variable is 0") - end - - def exception(:exceed_upper_bound) do - new("The maximum value of the length of the variable is 4_294_967_295") - end - - def exception(:length_over_max) do - new( - "The number which represents the length from decode the opaque as UInt is bigger than the defined max (max by default is 4_294_967_295)" - ) - end - - def exception(:length_over_rest) do - new("The XDR has an invalid length, it must be less than byte-size of the rest") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.VariableOpaque{message: msg} - end - - defmodule FixedArray do - @moduledoc """ - This module contains the definition of `XDR.Error.FixedArray` exception that may be raised by the `XDR.FixedArray` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.FixedArray` exception with the message of the `error_type` passed. - """ - def exception(:invalid_length) do - new("the length of the array and the length must be the same") - end - - def exception(:not_list) do - new("the value which you try to encode must be a list") - end - - def exception(:not_number) do - new("the length received by parameter must be an integer") - end - - def exception(:not_binary) do - new("the value which you try to decode must be a binary value") - end - - def exception(:not_valid_binary) do - new("the value which you try to decode must have a multiple of 4 byte-size") - end - - def exception(:invalid_type) do - new("the type must be a module") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.FixedArray{message: msg} - end - - defmodule VariableArray do - @moduledoc """ - This module contains the definition of `XDR.Error.VariableArray` exception that may be raised by the `XDR.VariableArray` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.VariableArray` exception with the message of the `error_type` passed. - """ - def exception(:not_list) do - new("the value which you try to encode must be a list") - end - - def exception(:not_number) do - new("the max length must be an integer value") - end - - def exception(:not_binary) do - new( - "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>" - ) - end - - def exception(:exceed_lower_bound) do - new("The minimum value of the length of the variable is 1") - end - - def exception(:exceed_upper_bound) do - new("The maximum value of the length of the variable is 4_294_967_295") - end - - def exception(:length_over_max) do - new( - "The number which represents the length from decode the opaque as UInt is bigger than the defined max" - ) - end - - def exception(:invalid_length) do - new("The length of the binary exceeds the max_length of the type") - end - - def exception(:invalid_binary) do - new( - "The data which you try to decode has an invalid number of bytes, it must be equal to or greater than the size of the array multiplied by 4" - ) - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.VariableArray{message: msg} - end - - defmodule Struct do - @moduledoc """ - This module contains the definition of `XDR.Error.Struct` exception that may be raised by the `XDR.Struct` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Struct` exception with the message of the `error_type` passed. - """ - def exception(:not_list) do - new("The :components received by parameter must be a keyword list") - end - - def exception(:empty_list) do - new("The :components must not be empty, it must be a keyword list") - end - - def exception(:not_binary) do - new("The :struct received by parameter must be a binary value, for example: <<0, 0, 0, 5>>") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Struct{message: msg} - end - - defmodule Union do - @moduledoc """ - This module contains the definition of `XDR.Error.Union` exception that may be raised by the `XDR.Union` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Union` exception with the message of the `error_type` passed. - """ - def exception(:not_list) do - new( - "The :declarations received by parameter must be a keyword list which belongs to an XDR.Enum" - ) - end - - def exception(:not_binary) do - new( - "The :identifier received by parameter must be a binary value, for example: <<0, 0, 0, 5>>" - ) - end - - def exception(:not_number) do - new("The value which you try to decode is not an integer value") - end - - def exception(:not_atom) do - new("The :identifier which you try to decode from the Enum Union is not an atom") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Union{message: msg} - end - - defmodule Void do - @moduledoc """ - This module contains the definition of `XDR.Error.Void` exception that may be raised by the `XDR.Void` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Void` exception with the message of the `error_type` passed. - """ - def exception(:not_binary) do - new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 5>>") - end - - def exception(:not_void) do - new("The value which you try to encode is not void") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Void{message: msg} - end - - defmodule Optional do - @moduledoc """ - This module contains the definition of `XDR.Error.Optional` exception that may be raised by the `XDR.Optional` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.Optional` exception with the message of the `error_type` passed. - """ - def exception(:not_valid) do - new("The value which you try to encode must be Int, UInt or Enum") - end - - def exception(:not_binary) do - new("The value which you try to decode must be a binary value") - end - - def exception(:not_module) do - new("The type of the optional value must be the module which it belongs") - end - - @spec new(msg :: String.t()) :: struct() - defp new(msg), do: %XDR.Error.Optional{message: msg} - end - - defmodule String do - @moduledoc """ - This module contains the definition of `XDR.Error.String` exception that may be raised by the `XDR.String` module. - """ - - defexception [:message] - - @impl true - @doc """ - Create a `XDR.Error.String` exception with the message of the `error_type` passed. - """ - def exception(:not_bitstring) do - new("The value you are trying to encode must be a bitstring value") - end - - def exception(:invalid_length) do - new("The length of the string exceeds the max length allowed") - end - - def exception(:not_binary) do - new("The value you are trying to decode must be a binary value") - end - - @spec new(msg :: binary()) :: struct() - defp new(msg), do: %XDR.Error.String{message: msg} - end -end diff --git a/lib/xdr/bool.ex b/lib/xdr/bool.ex index ed664a1..271d1b8 100644 --- a/lib/xdr/bool.ex +++ b/lib/xdr/bool.ex @@ -5,8 +5,7 @@ defmodule XDR.Bool do @behaviour XDR.Declaration - alias XDR.Enum - alias XDR.Error.Bool, as: BoolError + alias XDR.{Enum, BoolError} @boolean [false: 0, true: 1] diff --git a/lib/xdr/double_float.ex b/lib/xdr/double_float.ex index 9f77698..87739da 100644 --- a/lib/xdr/double_float.ex +++ b/lib/xdr/double_float.ex @@ -5,7 +5,7 @@ defmodule XDR.DoubleFloat do @behaviour XDR.Declaration - alias XDR.Error.DoubleFloat, as: DoubleFloatError + alias XDR.DoubleFloatError defstruct [:float] diff --git a/lib/xdr/enum.ex b/lib/xdr/enum.ex index f6ddc3f..05d11d6 100644 --- a/lib/xdr/enum.ex +++ b/lib/xdr/enum.ex @@ -5,7 +5,7 @@ defmodule XDR.Enum do @behaviour XDR.Declaration - alias XDR.Error.Enum, as: EnumError + alias XDR.EnumError defstruct [:declarations, :identifier] diff --git a/lib/xdr/error.ex b/lib/xdr/error.ex new file mode 100644 index 0000000..4d2ecbd --- /dev/null +++ b/lib/xdr/error.ex @@ -0,0 +1,564 @@ +defmodule XDR.IntError do + @moduledoc """ + This module contains the definition of `XDR.IntError` exception that may be raised by the `XDR.Int` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.IntError` exception with the message of the `error_type` passed. + """ + def exception(:not_integer) do + new("The value which you try to encode is not an integer") + end + + def exception(:not_binary) do + new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") + end + + def exception(:exceed_upper_limit) do + new( + "The integer which you try to encode exceed the upper limit of an integer, the value must be less than 2_147_483_647" + ) + end + + def exception(:exceed_lower_limit) do + new( + "The integer which you try to encode exceed the lower limit of an integer, the value must be more than -2_147_483_648" + ) + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.IntError{message: msg} +end + +defmodule XDR.UIntError do + @moduledoc """ + This module contains the definition of `XDR.UIntError` exception that may be raised by the `XDR.UInt` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.UIntError` exception with the message of the `error_type` passed. + """ + def exception(:not_integer) do + new("The value which you try to encode is not an integer") + end + + def exception(:not_binary) do + new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") + end + + def exception(:exceed_upper_limit) do + new( + "The integer which you try to encode exceed the upper limit of an unsigned integer, the value must be less than 4_294_967_295" + ) + end + + def exception(:exceed_lower_limit) do + new( + "The integer which you try to encode exceed the lower limit of an unsigned integer, the value must be more than 0" + ) + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.UIntError{message: msg} +end + +defmodule XDR.EnumError do + @moduledoc """ + This module contains the definition of `XDR.EnumError` exception that may be raised by the `XDR.Enum` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.EnumError` exception with the message of the `error_type` passed. + """ + def exception(:not_list) do + new("The declaration inside the Enum structure isn't a list") + end + + def exception(:not_an_atom) do + new("The name of the key which you try to encode isn't an atom") + end + + def exception(:not_binary) do + new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") + end + + def exception(:invalid_key) do + new("The key which you try to encode doesn't belong to the current declarations") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.EnumError{message: msg} +end + +defmodule XDR.BoolError do + @moduledoc """ + This module contains the definition of `XDR.BoolError` exception that may be raised by the `XDR.Bool` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.BoolError` exception with the message of the `error_type` passed. + """ + def exception(:not_boolean) do + new("The value which you try to encode is not a boolean") + end + + def exception(:invalid_value) do + new("The value which you try to decode must be <<0, 0, 0, 0>> or <<0, 0, 0, 1>>") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.BoolError{message: msg} +end + +defmodule XDR.HyperIntError do + @moduledoc """ + This module contains the definition of `XDR.HyperIntError` exception that may be raised by the `XDR.HyperInt` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.HyperIntError` exception with the message of the `error_type` passed. + """ + def exception(:not_integer) do + new("The value which you try to encode is not an integer") + end + + def exception(:not_binary) do + new( + "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>" + ) + end + + def exception(:exceed_upper_limit) do + new( + "The integer which you try to encode exceed the upper limit of an Hyper Integer, the value must be less than 9_223_372_036_854_775_807" + ) + end + + def exception(:exceed_lower_limit) do + new( + "The integer which you try to encode exceed the lower limit of an Hyper Integer, the value must be more than -9_223_372_036_854_775_808" + ) + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.HyperIntError{message: msg} +end + +defmodule XDR.HyperUIntError do + @moduledoc """ + This module contains the definition of `XDR.HyperUIntError` exception that may be raised by the `XDR.HyperUInt` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.HyperUIntError` exception with the message of the `error_type` passed. + """ + def exception(:not_integer) do + new("The value which you try to encode is not an integer") + end + + def exception(:not_binary) do + new( + "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>" + ) + end + + def exception(:exceed_upper_limit) do + new( + "The integer which you try to encode exceed the upper limit of an Hyper Unsigned Integer, the value must be less than 18_446_744_073_709_551_615" + ) + end + + def exception(:exceed_lower_limit) do + new( + "The integer which you try to encode exceed the lower limit of an Hyper Unsigned Integer, the value must be more than 0" + ) + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.HyperUIntError{message: msg} +end + +defmodule XDR.FloatError do + @moduledoc """ + This module contains the definition of `XDR.FloatError` exception that may be raised by the `XDR.Float` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.FloatError` exception with the message of the `error_type` passed. + """ + def exception(:not_number) do + new("The value which you try to encode is not an integer or float value") + end + + def exception(:not_binary) do + new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.FloatError{message: msg} +end + +defmodule XDR.DoubleFloatError do + @moduledoc """ + This module contains the definition of `XDR.DoubleFloatError` exception that may be raised by the `XDR.DoubleFloat` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.DoubleFloatError` exception with the message of the `error_type` passed. + """ + def exception(:not_number) do + new("The value which you try to encode is not an integer or float value") + end + + def exception(:not_binary) do + new( + "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>" + ) + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.DoubleFloatError{message: msg} +end + +defmodule XDR.FixedOpaqueError do + @moduledoc """ + This module contains the definition of `XDR.FixedOpaqueError` exception that may be raised by the `XDR.FixedOpaque` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.FixedOpaqueError` exception with the message of the `error_type` passed. + """ + def exception(:not_number) do + new("The value which you pass through parameters is not an integer") + end + + def exception(:not_binary) do + new( + "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>" + ) + end + + def exception(:invalid_length) do + new( + "The length that is passed through parameters must be equal or less to the byte size of the XDR to complete" + ) + end + + def exception(:exceed_length) do + new("The length is bigger than the byte size of the XDR") + end + + def exception(:not_valid_binary) do + new("The binary size of the binary which you try to decode must be a multiple of 4") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.FixedOpaqueError{message: msg} +end + +defmodule XDR.VariableOpaqueError do + @moduledoc """ + This module contains the definition of `XDR.VariableOpaqueError` exception that may be raised by the `XDR.VariableOpaque` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.VariableOpaqueError` exception with the message of the `error_type` passed. + """ + def exception(:not_number) do + new("The value which you pass through parameters is not an integer") + end + + def exception(:not_binary) do + new( + "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>" + ) + end + + def exception(:invalid_length) do + new( + "The max length that is passed through parameters must be biger to the byte size of the XDR" + ) + end + + def exception(:exceed_lower_bound) do + new("The minimum value of the length of the variable is 0") + end + + def exception(:exceed_upper_bound) do + new("The maximum value of the length of the variable is 4_294_967_295") + end + + def exception(:length_over_max) do + new( + "The number which represents the length from decode the opaque as UInt is bigger than the defined max (max by default is 4_294_967_295)" + ) + end + + def exception(:length_over_rest) do + new("The XDR has an invalid length, it must be less than byte-size of the rest") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.VariableOpaqueError{message: msg} +end + +defmodule XDR.FixedArrayError do + @moduledoc """ + This module contains the definition of `XDR.FixedArrayError` exception that may be raised by the `XDR.FixedArray` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.FixedArrayError` exception with the message of the `error_type` passed. + """ + def exception(:invalid_length) do + new("the length of the array and the length must be the same") + end + + def exception(:not_list) do + new("the value which you try to encode must be a list") + end + + def exception(:not_number) do + new("the length received by parameter must be an integer") + end + + def exception(:not_binary) do + new("the value which you try to decode must be a binary value") + end + + def exception(:not_valid_binary) do + new("the value which you try to decode must have a multiple of 4 byte-size") + end + + def exception(:invalid_type) do + new("the type must be a module") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.FixedArrayError{message: msg} +end + +defmodule XDR.VariableArrayError do + @moduledoc """ + This module contains the definition of `XDR.VariableArrayError` exception that may be raised by the `XDR.VariableArray` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.VariableArrayError` exception with the message of the `error_type` passed. + """ + def exception(:not_list) do + new("the value which you try to encode must be a list") + end + + def exception(:not_number) do + new("the max length must be an integer value") + end + + def exception(:not_binary) do + new( + "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>" + ) + end + + def exception(:exceed_lower_bound) do + new("The minimum value of the length of the variable is 1") + end + + def exception(:exceed_upper_bound) do + new("The maximum value of the length of the variable is 4_294_967_295") + end + + def exception(:length_over_max) do + new( + "The number which represents the length from decode the opaque as UInt is bigger than the defined max" + ) + end + + def exception(:invalid_length) do + new("The length of the binary exceeds the max_length of the type") + end + + def exception(:invalid_binary) do + new( + "The data which you try to decode has an invalid number of bytes, it must be equal to or greater than the size of the array multiplied by 4" + ) + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.VariableArrayError{message: msg} +end + +defmodule XDR.StructError do + @moduledoc """ + This module contains the definition of `XDR.StructError` exception that may be raised by the `XDR.Struct` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.StructError` exception with the message of the `error_type` passed. + """ + def exception(:not_list) do + new("The :components received by parameter must be a keyword list") + end + + def exception(:empty_list) do + new("The :components must not be empty, it must be a keyword list") + end + + def exception(:not_binary) do + new("The :struct received by parameter must be a binary value, for example: <<0, 0, 0, 5>>") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.StructError{message: msg} +end + +defmodule XDR.UnionError do + @moduledoc """ + This module contains the definition of `XDR.UnionError` exception that may be raised by the `XDR.Union` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.UnionError` exception with the message of the `error_type` passed. + """ + def exception(:not_list) do + new( + "The :declarations received by parameter must be a keyword list which belongs to an XDR.Enum" + ) + end + + def exception(:not_binary) do + new( + "The :identifier received by parameter must be a binary value, for example: <<0, 0, 0, 5>>" + ) + end + + def exception(:not_number) do + new("The value which you try to decode is not an integer value") + end + + def exception(:not_atom) do + new("The :identifier which you try to decode from the Enum Union is not an atom") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.UnionError{message: msg} +end + +defmodule XDR.VoidError do + @moduledoc """ + This module contains the definition of `XDR.VoidError` exception that may be raised by the `XDR.Void` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.VoidError` exception with the message of the `error_type` passed. + """ + def exception(:not_binary) do + new("The value which you try to decode must be a binary value, for example: <<0, 0, 0, 5>>") + end + + def exception(:not_void) do + new("The value which you try to encode is not void") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.VoidError{message: msg} +end + +defmodule XDR.OptionalError do + @moduledoc """ + This module contains the definition of `XDR.OptionalError` exception that may be raised by the `XDR.Optional` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.OptionalError` exception with the message of the `error_type` passed. + """ + def exception(:not_valid) do + new("The value which you try to encode must be Int, UInt or Enum") + end + + def exception(:not_binary) do + new("The value which you try to decode must be a binary value") + end + + def exception(:not_module) do + new("The type of the optional value must be the module which it belongs") + end + + @spec new(msg :: String.t()) :: struct() + defp new(msg), do: %XDR.OptionalError{message: msg} +end + +defmodule XDR.StringError do + @moduledoc """ + This module contains the definition of `XDR.StringError` exception that may be raised by the `XDR.String` module. + """ + + defexception [:message] + + @impl true + @doc """ + Create a `XDR.StringError` exception with the message of the `error_type` passed. + """ + def exception(:not_bitstring) do + new("The value you are trying to encode must be a bitstring value") + end + + def exception(:invalid_length) do + new("The length of the string exceeds the max length allowed") + end + + def exception(:not_binary) do + new("The value you are trying to decode must be a binary value") + end + + @spec new(msg :: binary()) :: struct() + defp new(msg), do: %XDR.StringError{message: msg} +end diff --git a/lib/xdr/fixed_array.ex b/lib/xdr/fixed_array.ex index b2b2154..221bb47 100644 --- a/lib/xdr/fixed_array.ex +++ b/lib/xdr/fixed_array.ex @@ -5,11 +5,11 @@ defmodule XDR.FixedArray do @behaviour XDR.Declaration - alias XDR.Error.FixedArray, as: FixedArrayError + alias XDR.FixedArrayError defstruct [:elements, :type, :length] - @type elements :: list() | binary() | nil + @type elements :: list(integer() | String.t() | struct()) | nil @typedoc """ `XDR.FixedArray` structure type specification. @@ -83,6 +83,9 @@ defmodule XDR.FixedArray do end @spec encode_element(element :: elements(), type :: module()) :: binary() + defp encode_element(%{__struct__: xdr_type} = element, xdr_type), + do: xdr_type.encode_xdr!(element) + defp encode_element(element, type), do: element |> type.new() |> type.encode_xdr!() @spec decode_elements_from_fixed_array( diff --git a/lib/xdr/fixed_opaque.ex b/lib/xdr/fixed_opaque.ex index 2ac9902..1b68eb1 100644 --- a/lib/xdr/fixed_opaque.ex +++ b/lib/xdr/fixed_opaque.ex @@ -7,7 +7,7 @@ defmodule XDR.FixedOpaque do defstruct [:opaque, :length] - alias XDR.Error.FixedOpaque, as: FixedOpaqueError + alias XDR.FixedOpaqueError @type opaque :: binary() | nil diff --git a/lib/xdr/float.ex b/lib/xdr/float.ex index 115a8f9..d5dfa9f 100644 --- a/lib/xdr/float.ex +++ b/lib/xdr/float.ex @@ -5,7 +5,7 @@ defmodule XDR.Float do @behaviour XDR.Declaration - alias XDR.Error.Float, as: FloatError + alias XDR.FloatError defstruct [:float] diff --git a/lib/xdr/hyper_int.ex b/lib/xdr/hyper_int.ex index 7e673a5..184ea98 100644 --- a/lib/xdr/hyper_int.ex +++ b/lib/xdr/hyper_int.ex @@ -5,7 +5,7 @@ defmodule XDR.HyperInt do @behaviour XDR.Declaration - alias XDR.Error.HyperInt, as: HyperIntError + alias XDR.HyperIntError defstruct [:datum] diff --git a/lib/xdr/hyper_uint.ex b/lib/xdr/hyper_uint.ex index 817e229..71bf402 100644 --- a/lib/xdr/hyper_uint.ex +++ b/lib/xdr/hyper_uint.ex @@ -5,7 +5,7 @@ defmodule XDR.HyperUInt do @behaviour XDR.Declaration - alias XDR.Error.HyperUInt, as: HyperUIntError + alias XDR.HyperUIntError defstruct [:datum] diff --git a/lib/xdr/int.ex b/lib/xdr/int.ex index ccd0eaa..5ee5257 100644 --- a/lib/xdr/int.ex +++ b/lib/xdr/int.ex @@ -5,7 +5,7 @@ defmodule XDR.Int do @behaviour XDR.Declaration - alias XDR.Error.Int, as: IntError + alias XDR.IntError defstruct [:datum] diff --git a/lib/xdr/optional.ex b/lib/xdr/optional.ex index ddb8cee..06faf8f 100644 --- a/lib/xdr/optional.ex +++ b/lib/xdr/optional.ex @@ -5,8 +5,7 @@ defmodule XDR.Optional do @behaviour XDR.Declaration - alias XDR.Bool - alias XDR.Error.Optional, as: OptionalError + alias XDR.{Bool, OptionalError} defstruct [:type] diff --git a/lib/xdr/string.ex b/lib/xdr/string.ex index cb360cf..985aa0c 100644 --- a/lib/xdr/string.ex +++ b/lib/xdr/string.ex @@ -5,8 +5,7 @@ defmodule XDR.String do @behaviour XDR.Declaration - alias XDR.VariableOpaque - alias XDR.Error.String, as: StringError + alias XDR.{VariableOpaque, StringError} defstruct [:string, :max_length] diff --git a/lib/xdr/struct.ex b/lib/xdr/struct.ex index 86e6191..56d5e11 100644 --- a/lib/xdr/struct.ex +++ b/lib/xdr/struct.ex @@ -5,7 +5,7 @@ defmodule XDR.Struct do @behaviour XDR.Declaration - alias XDR.Error.Struct, as: StructError + alias XDR.StructError defstruct [:components] diff --git a/lib/xdr/uint.ex b/lib/xdr/uint.ex index 7709000..62039f4 100644 --- a/lib/xdr/uint.ex +++ b/lib/xdr/uint.ex @@ -5,7 +5,7 @@ defmodule XDR.UInt do @behaviour XDR.Declaration - alias XDR.Error.UInt, as: UIntError + alias XDR.UIntError defstruct [:datum] diff --git a/lib/xdr/union.ex b/lib/xdr/union.ex index 40c9234..5d90ba3 100644 --- a/lib/xdr/union.ex +++ b/lib/xdr/union.ex @@ -5,7 +5,7 @@ defmodule XDR.Union do @behaviour XDR.Declaration - alias XDR.Error.Union, as: UnionError + alias XDR.UnionError defstruct [:discriminant, :arms, :value] @@ -155,10 +155,8 @@ defmodule XDR.Union do defp decode_arm(nil, _discriminant, _rest), do: {:error, :invalid_arm} defp decode_arm(xdr_type, discriminant, rest) do - case xdr_type.decode_xdr(rest) do - {:ok, {decoded_arm, rest}} -> {:ok, {{discriminant, decoded_arm}, rest}} - error -> error - end + {decoded_arm, rest} = xdr_type.decode_xdr!(rest) + {:ok, {{discriminant, decoded_arm}, rest}} end @spec get_arm_module(arm :: struct() | module()) :: module() diff --git a/lib/xdr/variable_array.ex b/lib/xdr/variable_array.ex index ed0de68..5818c5a 100644 --- a/lib/xdr/variable_array.ex +++ b/lib/xdr/variable_array.ex @@ -5,8 +5,7 @@ defmodule XDR.VariableArray do @behaviour XDR.Declaration - alias XDR.Error.VariableArray, as: VariableArrayError - alias XDR.{UInt, FixedArray} + alias XDR.{UInt, FixedArray, VariableArrayError} defstruct [:elements, :type, :max_length] diff --git a/lib/xdr/variable_opaque.ex b/lib/xdr/variable_opaque.ex index cbef770..1922bf0 100644 --- a/lib/xdr/variable_opaque.ex +++ b/lib/xdr/variable_opaque.ex @@ -5,8 +5,7 @@ defmodule XDR.VariableOpaque do @behaviour XDR.Declaration - alias XDR.{FixedOpaque, UInt} - alias XDR.Error.VariableOpaque, as: VariableOpaqueError + alias XDR.{FixedOpaque, UInt, VariableOpaqueError} defstruct [:opaque, :max_size] diff --git a/lib/xdr/void.ex b/lib/xdr/void.ex index d086285..03afb22 100644 --- a/lib/xdr/void.ex +++ b/lib/xdr/void.ex @@ -5,7 +5,7 @@ defmodule XDR.Void do @behaviour XDR.Declaration - alias XDR.Error.Void, as: VoidError + alias XDR.VoidError defstruct [:void] diff --git a/mix.exs b/mix.exs index c46b422..49ae280 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule XDR.MixProject do use Mix.Project @github_url "https://github.com/kommitters/elixir_xdr" - @version "0.1.6" + @version "0.2.0" def project do [ diff --git a/test/errors/error_bool_test.exs b/test/errors/bool_error_test.exs similarity index 50% rename from test/errors/error_bool_test.exs rename to test/errors/bool_error_test.exs index 0ea0486..70304f5 100644 --- a/test/errors/error_bool_test.exs +++ b/test/errors/bool_error_test.exs @@ -1,19 +1,19 @@ -defmodule Error.BoolTest do +defmodule XDR.BoolErrorTest do use ExUnit.Case - alias XDR.Error.Bool + alias XDR.BoolError test "When receives :not_boolean" do - assert_raise Bool, "The value which you try to encode is not a boolean", fn -> - raise Bool, :not_boolean + assert_raise BoolError, "The value which you try to encode is not a boolean", fn -> + raise BoolError, :not_boolean end end test "When receives :invalid_value" do - assert_raise Bool, + assert_raise BoolError, "The value which you try to decode must be <<0, 0, 0, 0>> or <<0, 0, 0, 1>>", fn -> - raise Bool, :invalid_value + raise BoolError, :invalid_value end end end diff --git a/test/errors/error_double_float_test.exs b/test/errors/double_float_error_test.exs similarity index 62% rename from test/errors/error_double_float_test.exs rename to test/errors/double_float_error_test.exs index c011e8a..5169813 100644 --- a/test/errors/error_double_float_test.exs +++ b/test/errors/double_float_error_test.exs @@ -1,21 +1,21 @@ -defmodule Error.DoubleFloatTest do +defmodule XDR.DoubleFloatErrorTest do use ExUnit.Case - alias XDR.Error.DoubleFloat + alias XDR.DoubleFloatError test "When receives :not_number" do - assert_raise DoubleFloat, + assert_raise DoubleFloatError, "The value which you try to encode is not an integer or float value", fn -> - raise DoubleFloat, :not_number + raise DoubleFloatError, :not_number end end test "When receives :not_binary" do - assert_raise DoubleFloat, + assert_raise DoubleFloatError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>", fn -> - raise DoubleFloat, :not_binary + raise DoubleFloatError, :not_binary end end end diff --git a/test/errors/error_enum_test.exs b/test/errors/enum_error_test.exs similarity index 60% rename from test/errors/error_enum_test.exs rename to test/errors/enum_error_test.exs index a4182fd..4bf2af9 100644 --- a/test/errors/error_enum_test.exs +++ b/test/errors/enum_error_test.exs @@ -1,35 +1,35 @@ -defmodule Error.EnumTest do +defmodule XDR.EnumErrorTest do use ExUnit.Case - alias XDR.Error.Enum + alias XDR.EnumError test "When receives :not_list" do - assert_raise Enum, "The declaration inside the Enum structure isn't a list", fn -> - raise Enum, :not_list + assert_raise EnumError, "The declaration inside the Enum structure isn't a list", fn -> + raise EnumError, :not_list end end test "When receives :not_an_atom" do - assert_raise Enum, + assert_raise EnumError, "The name of the key which you try to encode isn't an atom", fn -> - raise Enum, :not_an_atom + raise EnumError, :not_an_atom end end test "When receives :not_binary" do - assert_raise Enum, + assert_raise EnumError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>", fn -> - raise Enum, :not_binary + raise EnumError, :not_binary end end test "When receives :invalid_key" do - assert_raise Enum, + assert_raise EnumError, "The key which you try to encode doesn't belong to the current declarations", fn -> - raise Enum, :invalid_key + raise EnumError, :invalid_key end end end diff --git a/test/errors/error_optional_test.exs b/test/errors/error_optional_test.exs deleted file mode 100644 index cfb5751..0000000 --- a/test/errors/error_optional_test.exs +++ /dev/null @@ -1,27 +0,0 @@ -defmodule Error.OptionalTest do - use ExUnit.Case - - alias XDR.Error.Optional - - test "When receives :not_valid" do - assert_raise Optional, "The value which you try to encode must be Int, UInt or Enum", fn -> - raise Optional, :not_valid - end - end - - test "When receives :not_binary" do - assert_raise Optional, - "The value which you try to decode must be a binary value", - fn -> - raise Optional, :not_binary - end - end - - test "When receives :not_module" do - assert_raise Optional, - "The type of the optional value must be the module which it belongs", - fn -> - raise Optional, :not_module - end - end -end diff --git a/test/errors/error_string_test.exs b/test/errors/error_string_test.exs deleted file mode 100644 index 99ffb76..0000000 --- a/test/errors/error_string_test.exs +++ /dev/null @@ -1,23 +0,0 @@ -defmodule Error.StringTest do - use ExUnit.Case - - alias XDR.Error.String - - test "When receives :not_bitstring" do - assert_raise String, "The value you are trying to encode must be a bitstring value", fn -> - raise String, :not_bitstring - end - end - - test "When receives :invalid_length" do - assert_raise String, "The length of the string exceeds the max length allowed", fn -> - raise String, :invalid_length - end - end - - test "When receives :not_binary" do - assert_raise String, "The value you are trying to decode must be a binary value", fn -> - raise String, :not_binary - end - end -end diff --git a/test/errors/error_struct_test.exs b/test/errors/error_struct_test.exs deleted file mode 100644 index 00ef7bc..0000000 --- a/test/errors/error_struct_test.exs +++ /dev/null @@ -1,25 +0,0 @@ -defmodule Error.StructTest do - use ExUnit.Case - - alias XDR.Error.Struct - - test "When receives :not_list" do - assert_raise Struct, "The :components received by parameter must be a keyword list", fn -> - raise Struct, :not_list - end - end - - test "When receives :empty_list" do - assert_raise Struct, "The :components must not be empty, it must be a keyword list", fn -> - raise Struct, :empty_list - end - end - - test "When receives :not_binary" do - assert_raise Struct, - "The :struct received by parameter must be a binary value, for example: <<0, 0, 0, 5>>", - fn -> - raise Struct, :not_binary - end - end -end diff --git a/test/errors/error_fixed_array_test.exs b/test/errors/fixed_array_error_test.exs similarity index 57% rename from test/errors/error_fixed_array_test.exs rename to test/errors/fixed_array_error_test.exs index 6c59ae2..d76814f 100644 --- a/test/errors/error_fixed_array_test.exs +++ b/test/errors/fixed_array_error_test.exs @@ -1,51 +1,51 @@ -defmodule Error.FixedArrayTest do +defmodule XDR.FixedArrayErrorTest do use ExUnit.Case - alias XDR.Error.FixedArray + alias XDR.FixedArrayError test "When receives :invalid_length" do - assert_raise FixedArray, "the length of the array and the length must be the same", fn -> - raise FixedArray, :invalid_length + assert_raise FixedArrayError, "the length of the array and the length must be the same", fn -> + raise FixedArrayError, :invalid_length end end test "When receives :not_list" do - assert_raise FixedArray, + assert_raise FixedArrayError, "the value which you try to encode must be a list", fn -> - raise FixedArray, :not_list + raise FixedArrayError, :not_list end end test "When receives :not_number" do - assert_raise FixedArray, + assert_raise FixedArrayError, "the length received by parameter must be an integer", fn -> - raise FixedArray, :not_number + raise FixedArrayError, :not_number end end test "When receives :not_binary" do - assert_raise FixedArray, + assert_raise FixedArrayError, "the value which you try to decode must be a binary value", fn -> - raise FixedArray, :not_binary + raise FixedArrayError, :not_binary end end test "When receives :not_valid_binary" do - assert_raise FixedArray, + assert_raise FixedArrayError, "the value which you try to decode must have a multiple of 4 byte-size", fn -> - raise FixedArray, :not_valid_binary + raise FixedArrayError, :not_valid_binary end end test "When receives :invalid_type" do - assert_raise FixedArray, + assert_raise FixedArrayError, "the type must be a module", fn -> - raise FixedArray, :invalid_type + raise FixedArrayError, :invalid_type end end end diff --git a/test/errors/error_fixed_opaque_test.exs b/test/errors/fixed_opaque_error_test.exs similarity index 64% rename from test/errors/error_fixed_opaque_test.exs rename to test/errors/fixed_opaque_error_test.exs index ee2d66a..fca33f6 100644 --- a/test/errors/error_fixed_opaque_test.exs +++ b/test/errors/fixed_opaque_error_test.exs @@ -1,45 +1,45 @@ -defmodule Error.FixedOpaqueTest do +defmodule XDR.FixedOpaqueErrorTest do use ExUnit.Case - alias XDR.Error.FixedOpaque + alias XDR.FixedOpaqueError test "When receives :not_number" do - assert_raise FixedOpaque, + assert_raise FixedOpaqueError, "The value which you pass through parameters is not an integer", fn -> - raise FixedOpaque, :not_number + raise FixedOpaqueError, :not_number end end test "When receives :not_binary" do - assert_raise FixedOpaque, + assert_raise FixedOpaqueError, "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>", fn -> - raise FixedOpaque, :not_binary + raise FixedOpaqueError, :not_binary end end test "When receives :invalid_length" do - assert_raise FixedOpaque, + assert_raise FixedOpaqueError, "The length that is passed through parameters must be equal or less to the byte size of the XDR to complete", fn -> - raise FixedOpaque, :invalid_length + raise FixedOpaqueError, :invalid_length end end test "When receives :exceed_length" do - assert_raise FixedOpaque, + assert_raise FixedOpaqueError, "The length is bigger than the byte size of the XDR", fn -> - raise FixedOpaque, :exceed_length + raise FixedOpaqueError, :exceed_length end end test "When receives :not_valid_binary" do - assert_raise FixedOpaque, + assert_raise FixedOpaqueError, "The binary size of the binary which you try to decode must be a multiple of 4", fn -> - raise FixedOpaque, :not_valid_binary + raise FixedOpaqueError, :not_valid_binary end end end diff --git a/test/errors/error_float_test.exs b/test/errors/float_error_test.exs similarity index 65% rename from test/errors/error_float_test.exs rename to test/errors/float_error_test.exs index 22006c1..0749b95 100644 --- a/test/errors/error_float_test.exs +++ b/test/errors/float_error_test.exs @@ -1,21 +1,21 @@ -defmodule Error.FloatTest do +defmodule XDR.FloatErrorTest do use ExUnit.Case - alias XDR.Error.Float + alias XDR.FloatError test "When receives :not_number" do - assert_raise Float, + assert_raise FloatError, "The value which you try to encode is not an integer or float value", fn -> - raise Float, :not_number + raise FloatError, :not_number end end test "When receives :not_binary" do - assert_raise Float, + assert_raise FloatError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>", fn -> - raise Float, :not_binary + raise FloatError, :not_binary end end end diff --git a/test/errors/error_hyper_int_test.exs b/test/errors/hyper_int_error_test.exs similarity index 63% rename from test/errors/error_hyper_int_test.exs rename to test/errors/hyper_int_error_test.exs index eb50cdf..aa351c6 100644 --- a/test/errors/error_hyper_int_test.exs +++ b/test/errors/hyper_int_error_test.exs @@ -1,35 +1,35 @@ -defmodule Error.HyperIntTest do +defmodule XDR.HyperIntErrorTest do use ExUnit.Case - alias XDR.Error.HyperInt + alias XDR.HyperIntError test "When receives :not_integer" do - assert_raise HyperInt, "The value which you try to encode is not an integer", fn -> - raise HyperInt, :not_integer + assert_raise HyperIntError, "The value which you try to encode is not an integer", fn -> + raise HyperIntError, :not_integer end end test "When receives :not_binary" do - assert_raise HyperInt, + assert_raise HyperIntError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>", fn -> - raise HyperInt, :not_binary + raise HyperIntError, :not_binary end end test "When receives :exceed_upper_limit" do - assert_raise HyperInt, + assert_raise HyperIntError, "The integer which you try to encode exceed the upper limit of an Hyper Integer, the value must be less than 9_223_372_036_854_775_807", fn -> - raise HyperInt, :exceed_upper_limit + raise HyperIntError, :exceed_upper_limit end end test "When receives :exceed_lower_limit" do - assert_raise HyperInt, + assert_raise HyperIntError, "The integer which you try to encode exceed the lower limit of an Hyper Integer, the value must be more than -9_223_372_036_854_775_808", fn -> - raise HyperInt, :exceed_lower_limit + raise HyperIntError, :exceed_lower_limit end end end diff --git a/test/errors/error_hyper_uint_test.exs b/test/errors/hyper_uint_error_test.exs similarity index 62% rename from test/errors/error_hyper_uint_test.exs rename to test/errors/hyper_uint_error_test.exs index ce8bdfb..5595423 100644 --- a/test/errors/error_hyper_uint_test.exs +++ b/test/errors/hyper_uint_error_test.exs @@ -1,35 +1,35 @@ -defmodule Error.HyperUIntTest do +defmodule XDR.HyperUIntErrorTest do use ExUnit.Case - alias XDR.Error.HyperUInt + alias XDR.HyperUIntError test "When receives :not_integer" do - assert_raise HyperUInt, "The value which you try to encode is not an integer", fn -> - raise HyperUInt, :not_integer + assert_raise HyperUIntError, "The value which you try to encode is not an integer", fn -> + raise HyperUIntError, :not_integer end end test "When receives :not_binary" do - assert_raise HyperUInt, + assert_raise HyperUIntError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 0, 0, 0, 0, 5>>", fn -> - raise HyperUInt, :not_binary + raise HyperUIntError, :not_binary end end test "When receives :exceed_upper_limit" do - assert_raise HyperUInt, + assert_raise HyperUIntError, "The integer which you try to encode exceed the upper limit of an Hyper Unsigned Integer, the value must be less than 18_446_744_073_709_551_615", fn -> - raise HyperUInt, :exceed_upper_limit + raise HyperUIntError, :exceed_upper_limit end end test "When receives :exceed_lower_limit" do - assert_raise HyperUInt, + assert_raise HyperUIntError, "The integer which you try to encode exceed the lower limit of an Hyper Unsigned Integer, the value must be more than 0", fn -> - raise HyperUInt, :exceed_lower_limit + raise HyperUIntError, :exceed_lower_limit end end end diff --git a/test/errors/error_int_test.exs b/test/errors/int_error_test.exs similarity index 64% rename from test/errors/error_int_test.exs rename to test/errors/int_error_test.exs index 932024d..3e110ff 100644 --- a/test/errors/error_int_test.exs +++ b/test/errors/int_error_test.exs @@ -1,35 +1,35 @@ -defmodule Error.IntTest do +defmodule XDR.IntErrorTest do use ExUnit.Case - alias XDR.Error.Int + alias XDR.IntError test "When receives :not_integer" do - assert_raise Int, "The value which you try to encode is not an integer", fn -> - raise Int, :not_integer + assert_raise IntError, "The value which you try to encode is not an integer", fn -> + raise IntError, :not_integer end end test "When receives :not_binary" do - assert_raise Int, + assert_raise IntError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>", fn -> - raise Int, :not_binary + raise IntError, :not_binary end end test "When receives :exceed_upper_limit" do - assert_raise Int, + assert_raise IntError, "The integer which you try to encode exceed the upper limit of an integer, the value must be less than 2_147_483_647", fn -> - raise Int, :exceed_upper_limit + raise IntError, :exceed_upper_limit end end test "When receives :exceed_lower_limit" do - assert_raise Int, + assert_raise IntError, "The integer which you try to encode exceed the lower limit of an integer, the value must be more than -2_147_483_648", fn -> - raise Int, :exceed_lower_limit + raise IntError, :exceed_lower_limit end end end diff --git a/test/errors/optional_error_test.exs b/test/errors/optional_error_test.exs new file mode 100644 index 0000000..da07875 --- /dev/null +++ b/test/errors/optional_error_test.exs @@ -0,0 +1,29 @@ +defmodule XDR.OptionalErrorTest do + use ExUnit.Case + + alias XDR.OptionalError + + test "When receives :not_valid" do + assert_raise OptionalError, + "The value which you try to encode must be Int, UInt or Enum", + fn -> + raise OptionalError, :not_valid + end + end + + test "When receives :not_binary" do + assert_raise OptionalError, + "The value which you try to decode must be a binary value", + fn -> + raise OptionalError, :not_binary + end + end + + test "When receives :not_module" do + assert_raise OptionalError, + "The type of the optional value must be the module which it belongs", + fn -> + raise OptionalError, :not_module + end + end +end diff --git a/test/errors/string_error_test.exs b/test/errors/string_error_test.exs new file mode 100644 index 0000000..e7af666 --- /dev/null +++ b/test/errors/string_error_test.exs @@ -0,0 +1,25 @@ +defmodule XDR.StringErrorTest do + use ExUnit.Case + + alias XDR.StringError + + test "When receives :not_bitstring" do + assert_raise StringError, + "The value you are trying to encode must be a bitstring value", + fn -> + raise StringError, :not_bitstring + end + end + + test "When receives :invalid_length" do + assert_raise StringError, "The length of the string exceeds the max length allowed", fn -> + raise StringError, :invalid_length + end + end + + test "When receives :not_binary" do + assert_raise StringError, "The value you are trying to decode must be a binary value", fn -> + raise StringError, :not_binary + end + end +end diff --git a/test/errors/struct_error_test.exs b/test/errors/struct_error_test.exs new file mode 100644 index 0000000..e3f5509 --- /dev/null +++ b/test/errors/struct_error_test.exs @@ -0,0 +1,29 @@ +defmodule XDR.StructErrorTest do + use ExUnit.Case + + alias XDR.StructError + + test "When receives :not_list" do + assert_raise StructError, + "The :components received by parameter must be a keyword list", + fn -> + raise StructError, :not_list + end + end + + test "When receives :empty_list" do + assert_raise StructError, + "The :components must not be empty, it must be a keyword list", + fn -> + raise StructError, :empty_list + end + end + + test "When receives :not_binary" do + assert_raise StructError, + "The :struct received by parameter must be a binary value, for example: <<0, 0, 0, 5>>", + fn -> + raise StructError, :not_binary + end + end +end diff --git a/test/errors/error_uint_test.exs b/test/errors/uint_error_test.exs similarity index 63% rename from test/errors/error_uint_test.exs rename to test/errors/uint_error_test.exs index 0623ce3..c0464bf 100644 --- a/test/errors/error_uint_test.exs +++ b/test/errors/uint_error_test.exs @@ -1,35 +1,35 @@ -defmodule Error.UIntTest do +defmodule XDR.UIntErrorTest do use ExUnit.Case - alias XDR.Error.UInt + alias XDR.UIntError test "When receives :not_integer" do - assert_raise UInt, "The value which you try to encode is not an integer", fn -> - raise UInt, :not_integer + assert_raise UIntError, "The value which you try to encode is not an integer", fn -> + raise UIntError, :not_integer end end test "When receives :not_binary" do - assert_raise UInt, + assert_raise UIntError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 2>>", fn -> - raise UInt, :not_binary + raise UIntError, :not_binary end end test "When receives :exceed_upper_limit" do - assert_raise UInt, + assert_raise UIntError, "The integer which you try to encode exceed the upper limit of an unsigned integer, the value must be less than 4_294_967_295", fn -> - raise UInt, :exceed_upper_limit + raise UIntError, :exceed_upper_limit end end test "When receives :exceed_lower_limit" do - assert_raise UInt, + assert_raise UIntError, "The integer which you try to encode exceed the lower limit of an unsigned integer, the value must be more than 0", fn -> - raise UInt, :exceed_lower_limit + raise UIntError, :exceed_lower_limit end end end diff --git a/test/errors/error_union_test.exs b/test/errors/union_error_test.exs similarity index 67% rename from test/errors/error_union_test.exs rename to test/errors/union_error_test.exs index e49c149..1991516 100644 --- a/test/errors/error_union_test.exs +++ b/test/errors/union_error_test.exs @@ -1,37 +1,37 @@ -defmodule Error.UnionTest do +defmodule XDR.UnionErrorTest do use ExUnit.Case - alias XDR.Error.Union + alias XDR.UnionError test "When receives :not_list" do - assert_raise Union, + assert_raise UnionError, "The :declarations received by parameter must be a keyword list which belongs to an XDR.Enum", fn -> - raise Union, :not_list + raise UnionError, :not_list end end test "When receives :not_binary" do - assert_raise Union, + assert_raise UnionError, "The :identifier received by parameter must be a binary value, for example: <<0, 0, 0, 5>>", fn -> - raise Union, :not_binary + raise UnionError, :not_binary end end test "When receives :not_number" do - assert_raise Union, + assert_raise UnionError, "The value which you try to decode is not an integer value", fn -> - raise Union, :not_number + raise UnionError, :not_number end end test "When receives :not_atom" do - assert_raise Union, + assert_raise UnionError, "The :identifier which you try to decode from the Enum Union is not an atom", fn -> - raise Union, :not_atom + raise UnionError, :not_atom end end end diff --git a/test/errors/error_variable_array_test.exs b/test/errors/variable_array_error_test.exs similarity index 61% rename from test/errors/error_variable_array_test.exs rename to test/errors/variable_array_error_test.exs index fecab1e..a6119ec 100644 --- a/test/errors/error_variable_array_test.exs +++ b/test/errors/variable_array_error_test.exs @@ -1,67 +1,67 @@ -defmodule Error.VariableArrayTest do +defmodule XDR.VariableArrayErrorTest do use ExUnit.Case - alias XDR.Error.VariableArray + alias XDR.VariableArrayError test "When receives :not_list" do - assert_raise VariableArray, "the value which you try to encode must be a list", fn -> - raise VariableArray, :not_list + assert_raise VariableArrayError, "the value which you try to encode must be a list", fn -> + raise VariableArrayError, :not_list end end test "When receives :not_number" do - assert_raise VariableArray, + assert_raise VariableArrayError, "the max length must be an integer value", fn -> - raise VariableArray, :not_number + raise VariableArrayError, :not_number end end test "When receives :not_binary" do - assert_raise VariableArray, + assert_raise VariableArrayError, "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>", fn -> - raise VariableArray, :not_binary + raise VariableArrayError, :not_binary end end test "When receives :exceed_lower_bound" do - assert_raise VariableArray, + assert_raise VariableArrayError, "The minimum value of the length of the variable is 1", fn -> - raise VariableArray, :exceed_lower_bound + raise VariableArrayError, :exceed_lower_bound end end test "When receives :exceed_upper_bound" do - assert_raise VariableArray, + assert_raise VariableArrayError, "The maximum value of the length of the variable is 4_294_967_295", fn -> - raise VariableArray, :exceed_upper_bound + raise VariableArrayError, :exceed_upper_bound end end test "When receives :length_over_max" do - assert_raise VariableArray, + assert_raise VariableArrayError, "The number which represents the length from decode the opaque as UInt is bigger than the defined max", fn -> - raise VariableArray, :length_over_max + raise VariableArrayError, :length_over_max end end test "When receives :invalid_length" do - assert_raise VariableArray, + assert_raise VariableArrayError, "The length of the binary exceeds the max_length of the type", fn -> - raise VariableArray, :invalid_length + raise VariableArrayError, :invalid_length end end test "When receives :invalid_binary" do - assert_raise VariableArray, + assert_raise VariableArrayError, "The data which you try to decode has an invalid number of bytes, it must be equal to or greater than the size of the array multiplied by 4", fn -> - raise VariableArray, :invalid_binary + raise VariableArrayError, :invalid_binary end end end diff --git a/test/errors/error_variable_opaque_test.exs b/test/errors/variable_opaque_error_test.exs similarity index 64% rename from test/errors/error_variable_opaque_test.exs rename to test/errors/variable_opaque_error_test.exs index 89ccf5d..6f016ba 100644 --- a/test/errors/error_variable_opaque_test.exs +++ b/test/errors/variable_opaque_error_test.exs @@ -1,61 +1,61 @@ -defmodule Error.VariableOpaqueTest do +defmodule XDR.VariableOpaqueErrorTest do use ExUnit.Case - alias XDR.Error.VariableOpaque + alias XDR.VariableOpaqueError test "When receives :not_number" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The value which you pass through parameters is not an integer", fn -> - raise VariableOpaque, :not_number + raise VariableOpaqueError, :not_number end end test "When receives :not_binary" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The value which you pass through parameters must be a binary value, for example: <<0, 0, 0, 5>>", fn -> - raise VariableOpaque, :not_binary + raise VariableOpaqueError, :not_binary end end test "When receives :exceed_lower_bound" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The minimum value of the length of the variable is 0", fn -> - raise VariableOpaque, :exceed_lower_bound + raise VariableOpaqueError, :exceed_lower_bound end end test "When receives :exceed_upper_bound" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The maximum value of the length of the variable is 4_294_967_295", fn -> - raise VariableOpaque, :exceed_upper_bound + raise VariableOpaqueError, :exceed_upper_bound end end test "When receives :length_over_max" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The number which represents the length from decode the opaque as UInt is bigger than the defined max (max by default is 4_294_967_295)", fn -> - raise VariableOpaque, :length_over_max + raise VariableOpaqueError, :length_over_max end end test "When receives :length_over_rest" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The XDR has an invalid length, it must be less than byte-size of the rest", fn -> - raise VariableOpaque, :length_over_rest + raise VariableOpaqueError, :length_over_rest end end test "when receives :invalid_length" do - assert_raise VariableOpaque, + assert_raise VariableOpaqueError, "The max length that is passed through parameters must be biger to the byte size of the XDR", fn -> - raise VariableOpaque, :invalid_length + raise VariableOpaqueError, :invalid_length end end end diff --git a/test/errors/error_void_test.exs b/test/errors/void_error_test.exs similarity index 64% rename from test/errors/error_void_test.exs rename to test/errors/void_error_test.exs index 89ccfe9..60ce254 100644 --- a/test/errors/error_void_test.exs +++ b/test/errors/void_error_test.exs @@ -1,21 +1,21 @@ -defmodule Error.VoidTest do +defmodule XDR.VoidErrorTest do use ExUnit.Case - alias XDR.Error.Void + alias XDR.VoidError test "When receives :not_binary" do - assert_raise Void, + assert_raise VoidError, "The value which you try to decode must be a binary value, for example: <<0, 0, 0, 5>>", fn -> - raise Void, :not_binary + raise VoidError, :not_binary end end test "When receives :not_void" do - assert_raise Void, + assert_raise VoidError, "The value which you try to encode is not void", fn -> - raise Void, :not_void + raise VoidError, :not_void end end end diff --git a/test/xdr/bool_test.exs b/test/xdr/bool_test.exs index 0996958..38a22cf 100644 --- a/test/xdr/bool_test.exs +++ b/test/xdr/bool_test.exs @@ -5,8 +5,7 @@ defmodule XDR.BoolTest do use ExUnit.Case - alias XDR.Bool - alias XDR.Error.Bool, as: BoolError + alias XDR.{Bool, BoolError} describe "Encoding Boolean structures" do test "when the value is not boolean" do diff --git a/test/xdr/double_float_test.exs b/test/xdr/double_float_test.exs index be44a9f..366d696 100644 --- a/test/xdr/double_float_test.exs +++ b/test/xdr/double_float_test.exs @@ -5,8 +5,7 @@ defmodule XDR.DoubleFloatTest do use ExUnit.Case - alias XDR.DoubleFloat - alias XDR.Error.DoubleFloat, as: DoubleFloatError + alias XDR.{DoubleFloat, DoubleFloatError} describe "defguard tests" do test "valid_float? guard" do diff --git a/test/xdr/enum_test.exs b/test/xdr/enum_test.exs index 072acce..1ff8c2d 100644 --- a/test/xdr/enum_test.exs +++ b/test/xdr/enum_test.exs @@ -5,8 +5,7 @@ defmodule XDR.EnumTest do use ExUnit.Case - alias XDR.Enum - alias XDR.Error.Enum, as: EnumError + alias XDR.{Enum, EnumError} setup do keyword = [red: 0, blue: 1, yellow: 2] diff --git a/test/xdr/fixed_array_test.exs b/test/xdr/fixed_array_test.exs index 6aa8a92..c6bb0c7 100644 --- a/test/xdr/fixed_array_test.exs +++ b/test/xdr/fixed_array_test.exs @@ -5,8 +5,7 @@ defmodule XDR.FixedArrayTest do use ExUnit.Case - alias XDR.FixedArray - alias XDR.Error.FixedArray, as: FixedArrayError + alias XDR.{FixedArray, IntError, FixedArrayError} describe "Encoding Fixed Array" do test "with invalid type" do @@ -45,6 +44,17 @@ defmodule XDR.FixedArrayTest do assert reason == :not_number end + test "with other XDR types as elements but different type" do + elements = [ + XDR.Enum.new([foo: 0, bar: 1], :foo), + XDR.Enum.new([foo: 0, bar: 2], :bar) + ] + + fixed_array = FixedArray.new(elements, XDR.Int, 2) + + assert_raise IntError, fn -> FixedArray.encode_xdr(fixed_array) end + end + test "with valid data" do {status, result} = FixedArray.new([0, 0, 1], XDR.Int, 3) @@ -54,6 +64,21 @@ defmodule XDR.FixedArrayTest do assert result == <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>> end + test "with other XDR types as elements" do + elements = [ + XDR.Enum.new([foo: 0, bar: 1], :foo), + XDR.Enum.new([foo: 0, bar: 2], :bar) + ] + + {status, result} = + elements + |> FixedArray.new(XDR.Enum, 2) + |> FixedArray.encode_xdr() + + assert status == :ok + assert result == <<0, 0, 0, 0, 0, 0, 0, 2>> + end + test "encode_xdr! with valid data" do result = FixedArray.new(["kommit.co", "kommitter", "kommit"], XDR.String, 3) diff --git a/test/xdr/fixed_opaque_test.exs b/test/xdr/fixed_opaque_test.exs index 53b1217..e0e9cee 100644 --- a/test/xdr/fixed_opaque_test.exs +++ b/test/xdr/fixed_opaque_test.exs @@ -5,8 +5,7 @@ defmodule XDR.FixedOpaqueTest do use ExUnit.Case - alias XDR.FixedOpaque - alias XDR.Error.FixedOpaque, as: FixedOpaqueError + alias XDR.{FixedOpaque, FixedOpaqueError} describe "Encoding Fixed Opaque" do test "when xdr is not binary" do diff --git a/test/xdr/float_test.exs b/test/xdr/float_test.exs index ab0f356..ce80139 100644 --- a/test/xdr/float_test.exs +++ b/test/xdr/float_test.exs @@ -5,8 +5,7 @@ defmodule XDR.FloatTest do use ExUnit.Case - alias XDR.Float - alias XDR.Error.Float, as: FloatError + alias XDR.{Float, FloatError} describe "defguard tests" do test "valid_float? guard" do diff --git a/test/xdr/hyper_int_test.exs b/test/xdr/hyper_int_test.exs index 5a0a3a6..7ac4474 100644 --- a/test/xdr/hyper_int_test.exs +++ b/test/xdr/hyper_int_test.exs @@ -5,8 +5,7 @@ defmodule XDR.HyperIntTest do use ExUnit.Case - alias XDR.HyperInt - alias XDR.Error.HyperInt, as: HyperIntError + alias XDR.{HyperInt, HyperIntError} describe "Encoding Hyper Integer to binary" do test "when is not an integer value" do diff --git a/test/xdr/hyper_uint_test.exs b/test/xdr/hyper_uint_test.exs index 31d47df..24100e7 100644 --- a/test/xdr/hyper_uint_test.exs +++ b/test/xdr/hyper_uint_test.exs @@ -5,8 +5,7 @@ defmodule XDR.HyperUIntTest do use ExUnit.Case - alias XDR.HyperUInt - alias XDR.Error.HyperUInt, as: HyperUIntError + alias XDR.{HyperUInt, HyperUIntError} describe "Encoding Hyper Unsigned Integer to binary" do test "when is not an integer value" do diff --git a/test/xdr/int_test.exs b/test/xdr/int_test.exs index a30b1d0..a0f5fbd 100644 --- a/test/xdr/int_test.exs +++ b/test/xdr/int_test.exs @@ -5,8 +5,7 @@ defmodule XDR.IntTest do use ExUnit.Case - alias XDR.Int - alias XDR.Error.Int, as: IntError + alias XDR.{Int, IntError} describe "Encoding integer to binary" do test "when is not an integer value" do diff --git a/test/xdr/optional_test.exs b/test/xdr/optional_test.exs index 5c12c07..4f0d487 100644 --- a/test/xdr/optional_test.exs +++ b/test/xdr/optional_test.exs @@ -5,8 +5,7 @@ defmodule XDR.OptionalTest do use ExUnit.Case - alias XDR.Optional - alias XDR.Error.Optional, as: OptionalError + alias XDR.{Optional, OptionalError} describe "Encoding Optional type to binary" do test "when receives a string" do diff --git a/test/xdr/string_test.exs b/test/xdr/string_test.exs index d0723ca..6c27eb6 100644 --- a/test/xdr/string_test.exs +++ b/test/xdr/string_test.exs @@ -5,7 +5,7 @@ defmodule XDR.StringTest do use ExUnit.Case - alias XDR.String + alias XDR.{String, StringError} describe "Encoding string to binary" do test "when is not a bitstring value" do @@ -45,7 +45,7 @@ defmodule XDR.StringTest do end test "encode_xdr! when is not a bitstring value" do - assert_raise XDR.Error.String, + assert_raise StringError, "The value you are trying to encode must be a bitstring value", fn -> String.new(2) @@ -54,7 +54,7 @@ defmodule XDR.StringTest do end test "encode_xdr! raise an error when the string exceeds the max length set" do - assert_raise XDR.Error.String, + assert_raise StringError, "The length of the string exceeds the max length allowed", fn -> "kommit.co" @@ -89,7 +89,7 @@ defmodule XDR.StringTest do end test "decode_xdr! when value is not a binary" do - assert_raise XDR.Error.String, + assert_raise StringError, "The value you are trying to decode must be a binary value", fn -> list = [0, 0, 0, 9, 107, 111, 109, 109, 105, 116, 46, 99, 111, 0, 0, 0] diff --git a/test/xdr/struct_test.exs b/test/xdr/struct_test.exs index d8ffbec..88fae75 100644 --- a/test/xdr/struct_test.exs +++ b/test/xdr/struct_test.exs @@ -6,8 +6,7 @@ defmodule XDR.StructTest do use ExUnit.Case alias TestFile - alias XDR.Struct - alias XDR.Error.Struct, as: StructError + alias XDR.{Struct, StructError} describe "Encoding Struct to binary" do test "when is not a list" do @@ -214,6 +213,7 @@ defmodule TestFile do @type t :: %TestFile{file_name: XDR.String.t(), file_size: XDR.Int.t()} + @spec new(file_name :: String.t(), file_size :: integer()) :: TestFile.t() def new(file_name, file_size) do %TestFile{file_name: file_name, file_size: file_size} end diff --git a/test/xdr/uint_test.exs b/test/xdr/uint_test.exs index 9d13511..6ba6c45 100644 --- a/test/xdr/uint_test.exs +++ b/test/xdr/uint_test.exs @@ -5,8 +5,7 @@ defmodule XDR.UIntTest do use ExUnit.Case - alias XDR.UInt - alias XDR.Error.UInt, as: UIntError + alias XDR.{UInt, UIntError} describe "Encoding unsigned integer to binary" do test "when is not an unsigned integer value" do diff --git a/test/xdr/union_test.exs b/test/xdr/union_test.exs index a54c29a..a75dc25 100644 --- a/test/xdr/union_test.exs +++ b/test/xdr/union_test.exs @@ -5,8 +5,7 @@ defmodule XDR.UnionTest do use ExUnit.Case - alias XDR.Union - alias XDR.Error.Union, as: UnionError + alias XDR.{Union, UnionError} describe "new" do test "with Enum" do @@ -413,6 +412,16 @@ defmodule XDR.UnionTest do assert reason == :not_binary end + test "decode_xdr/1 with an invalid arm", %{decoded_enum: decoded_enum} do + arms = [case_1: %XDR.Int{datum: 1}, case_2: %XDR.Int{datum: 2}] + + {status, reason} = + XDR.Union.decode_xdr(<<0, 0, 0, 2, 0, 93, 112, 164>>, XDR.Union.new(decoded_enum, arms)) + + assert status == :error + assert reason == :invalid_arm + end + test "Enum example", %{decoded_enum: decoded_enum} do {status, result} = UnionSCPStatementWithTypes.decode_xdr(<<0, 0, 0, 0, 0, 0, 0, 60>>) @@ -518,6 +527,7 @@ defmodule SCPStatementType do SCP_ST_NOMINATE: 3 ] + @spec new(identifier :: atom()) :: %__MODULE__{} def new(identifier), do: %SCPStatementType{declarations: @scp_statement_type, identifier: identifier} @@ -568,6 +578,7 @@ defmodule UnionSCPStatementWithTypes do SCP_ST_NOMINATE: XDR.Float ] + @spec new(identifier :: atom(), value :: any()) :: XDR.Union.t() def new(identifier, value \\ nil) do identifier |> SCPStatementType.new() |> XDR.Union.new(@arms, value) end diff --git a/test/xdr/variable_array_test.exs b/test/xdr/variable_array_test.exs index adfe95e..4a7e913 100644 --- a/test/xdr/variable_array_test.exs +++ b/test/xdr/variable_array_test.exs @@ -5,8 +5,7 @@ defmodule XDR.VariableArrayTest do use ExUnit.Case - alias XDR.VariableArray - alias XDR.Error.VariableArray, as: VariableArrayError + alias XDR.{VariableArray, VariableArrayError} describe "Encoding Fixed Array" do test "when xdr is not list" do @@ -72,6 +71,21 @@ defmodule XDR.VariableArrayTest do assert result == <<0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>> end + test "with other XDR types as elements" do + elements = [ + XDR.Enum.new([foo: 0, bar: 1], :foo), + XDR.Enum.new([foo: 0, bar: 2], :bar) + ] + + {status, result} = + elements + |> VariableArray.new(XDR.Enum, 5) + |> VariableArray.encode_xdr() + + assert status == :ok + assert result == <<0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2>> + end + test "encode_xdr! with valid data" do result = VariableArray.new(["kommit.co", "kommitter", "kommit"], XDR.String, 3) diff --git a/test/xdr/variable_opaque_test.exs b/test/xdr/variable_opaque_test.exs index fd433ec..b4a210c 100644 --- a/test/xdr/variable_opaque_test.exs +++ b/test/xdr/variable_opaque_test.exs @@ -5,8 +5,7 @@ defmodule XDR.VariableOpaqueTest do use ExUnit.Case - alias XDR.VariableOpaque - alias XDR.Error.VariableOpaque, as: VariableOpaqueError + alias XDR.{VariableOpaque, VariableOpaqueError} describe "Encoding Variable Opaque" do test "when xdr is not binary" do diff --git a/test/xdr/void_test.exs b/test/xdr/void_test.exs index d752d6a..d2d856c 100644 --- a/test/xdr/void_test.exs +++ b/test/xdr/void_test.exs @@ -5,8 +5,7 @@ defmodule XDR.VoidTest do use ExUnit.Case - alias XDR.Void - alias XDR.Error.Void, as: VoidError + alias XDR.{Void, VoidError} describe "Encoding void to binary" do test "when not receive a void struct" do