generated from kommitters/.template
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Santiago Botero
committed
Sep 19, 2022
1 parent
e73eb8e
commit 6a2fa60
Showing
11 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,6 @@ kadena-*.tar | |
|
||
# Temporary files, for example, from tests. | ||
/tmp/ | ||
|
||
# Dialyzer plt files | ||
/priv/plts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
elixir 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Kadena.Types.Base16String do | ||
@moduledoc """ | ||
`Base16String` struct definition. | ||
""" | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type t :: %__MODULE__{value: String.t()} | ||
|
||
defstruct [:value] | ||
|
||
@impl true | ||
def new(str) when is_binary(str), do: %__MODULE__{value: str} | ||
def new(_str), do: {:error, :invalid_string} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Kadena.Types.Base64Url do | ||
@moduledoc """ | ||
`Base64Url` struct definition. | ||
""" | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type t :: %__MODULE__{url: String.t()} | ||
|
||
defstruct [:url] | ||
|
||
@impl true | ||
def new(str) when is_binary(str), do: %__MODULE__{url: str} | ||
def new(_str), do: {:error, :invalid_url} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Kadena.Types.Base64UrlsList do | ||
@moduledoc """ | ||
`Base64UrlsList` struct definition. | ||
""" | ||
alias Kadena.Types.Base64Url | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type urls :: list(Base64Url.t()) | ||
|
||
@type t :: %__MODULE__{list: urls()} | ||
|
||
defstruct list: [] | ||
|
||
@impl true | ||
def new(urls), do: build_list(%__MODULE__{}, urls) | ||
|
||
@spec build_list(list :: t(), urls :: urls()) :: t() | ||
defp build_list(list, []), do: list | ||
|
||
defp build_list(%__MODULE__{list: list}, [%Base64Url{} = url | rest]), | ||
do: build_list(%__MODULE__{list: [url | list]}, rest) | ||
|
||
defp build_list(_list, _urls), do: {:error, :invalid_urls} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Kadena.Types.Spec do | ||
@moduledoc """ | ||
Defines base types constructions. | ||
""" | ||
|
||
@type error :: {:error, atom()} | ||
|
||
@callback new(any()) :: struct() | error() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Kadena.Types.Uint8Array do | ||
@moduledoc """ | ||
`Uint8Array` struct definition. | ||
""" | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type values :: String.t() | integer() | list(integer()) | %__MODULE__{} | ||
@type t :: %__MODULE__{value: values} | ||
|
||
defstruct [:value] | ||
|
||
@impl true | ||
def new(str) when is_binary(str), do: %__MODULE__{value: str} | ||
def new(size) when is_integer(size) and size >= 0, do: %__MODULE__{value: size} | ||
def new(list) when is_list(list), do: check_list(%__MODULE__{value: list}, list) | ||
def new(%__MODULE__{value: value}), do: new(value) | ||
def new(_str), do: {:error, :invalid_uint8_array} | ||
|
||
@spec check_list(module_list :: t(), values :: list(integer())) :: t() | ||
defp check_list(module_list, []), do: module_list | ||
|
||
defp check_list(module_list, [value | rest]) when is_integer(value), | ||
do: check_list(module_list, rest) | ||
|
||
defp check_list(_module_list, _values), do: {:error, :invalid_uint8_array} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Kadena.Types.Base16StringTest do | ||
@moduledoc """ | ||
`Base16String` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.Base16String | ||
|
||
describe "new/1" do | ||
test "with a valid string value" do | ||
%Base16String{value: "string"} = Base16String.new("string") | ||
end | ||
|
||
test "with an invalid atom value" do | ||
{:error, :invalid_string} = Base16String.new(:atom) | ||
end | ||
|
||
test "with an invalid nil value" do | ||
{:error, :invalid_string} = Base16String.new(nil) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Kadena.Types.Base64UrlTest do | ||
@moduledoc """ | ||
`Base64Url` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.Base64Url | ||
|
||
describe "new/1" do | ||
test "with a valid url" do | ||
%Base64Url{url: "valid_url"} = Base64Url.new("valid_url") | ||
end | ||
|
||
test "with an invalid url" do | ||
{:error, :invalid_url} = Base64Url.new(:atom) | ||
end | ||
|
||
test "with a nil url" do | ||
{:error, :invalid_url} = Base64Url.new(nil) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Kadena.Types.Base64UrlsListTest do | ||
@moduledoc """ | ||
`Base64UrlsList` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.{Base64Url, Base64UrlsList} | ||
|
||
describe "new/1" do | ||
test "with a valid urls list" do | ||
base_url = Base64Url.new("valid_url") | ||
%Base64UrlsList{list: [base_url, base_url]} = Base64UrlsList.new([base_url, base_url]) | ||
end | ||
|
||
test "with an invalid urls list" do | ||
base_url = Base64Url.new("valid_url") | ||
{:error, :invalid_urls} = Base64UrlsList.new([base_url, :atom, base_url]) | ||
end | ||
|
||
test "with a list of nil" do | ||
{:error, :invalid_urls} = Base64UrlsList.new([nil]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Kadena.Types.Uint8ArrayTest do | ||
@moduledoc """ | ||
`Uint8Array` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.Uint8Array | ||
|
||
describe "new/1" do | ||
test "with a valid size" do | ||
%Uint8Array{value: 32} = Uint8Array.new(32) | ||
end | ||
|
||
test "with a valid value" do | ||
%Uint8Array{value: "valid_string_value"} = Uint8Array.new("valid_string_value") | ||
end | ||
|
||
test "with a valid list" do | ||
values_list = [134, 147, 230, 65, 174, 43, 190, 158] | ||
%Uint8Array{value: ^values_list} = Uint8Array.new(values_list) | ||
end | ||
|
||
test "with an invalid size" do | ||
{:error, :invalid_uint8_array} = Uint8Array.new(-5) | ||
end | ||
|
||
test "with a nil value" do | ||
{:error, :invalid_uint8_array} = Uint8Array.new(nil) | ||
end | ||
|
||
test "with an invalid list" do | ||
values_list = [134, 147, 230, 65, 174, 43, 190, :atom, 158] | ||
{:error, :invalid_uint8_array} = Uint8Array.new(values_list) | ||
end | ||
end | ||
|
||
describe "new/1 from another uint8_array" do | ||
test "with a valid size" do | ||
uint8_array = Uint8Array.new(32) | ||
%Uint8Array{value: 32} = Uint8Array.new(uint8_array) | ||
end | ||
|
||
test "with a valid value" do | ||
uint8_array = Uint8Array.new("valid_string_value") | ||
%Uint8Array{value: "valid_string_value"} = Uint8Array.new(uint8_array) | ||
end | ||
|
||
test "with a valid list" do | ||
values_list = [134, 147, 230, 65, 174, 43, 190, 158] | ||
%Uint8Array{value: ^values_list} = values_list |> Uint8Array.new() |> Uint8Array.new() | ||
end | ||
|
||
test "with an invalid size" do | ||
uint8_array = Uint8Array.new(-5) | ||
{:error, :invalid_uint8_array} = Uint8Array.new(uint8_array) | ||
end | ||
|
||
test "with a nil value" do | ||
uint8_array = Uint8Array.new(nil) | ||
{:error, :invalid_uint8_array} = Uint8Array.new(uint8_array) | ||
end | ||
end | ||
end |