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.
Base16String and Base64Url structs definitions
- Loading branch information
Santiago Botero
committed
Sep 15, 2022
1 parent
469d945
commit 94a130e
Showing
6 changed files
with
92 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,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__{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,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,25 @@ | ||
defmodule Kadena.Types.Base16StringTest do | ||
@moduledoc """ | ||
`Base16String` struct definition tests | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.Base16String | ||
|
||
setup do | ||
%{valid_param: "valid_param", invalid_param: :atom} | ||
end | ||
|
||
test "new/1 with valid params", %{valid_param: valid_param} do | ||
%Base16String{value: ^valid_param} = Base16String.new(valid_param) | ||
end | ||
|
||
test "new/1 with invalid params", %{invalid_param: invalid_param} do | ||
{:error, :invalid_string} = Base16String.new(invalid_param) | ||
end | ||
|
||
test "new/1 with nil params" do | ||
{:error, :invalid_string} = Base16String.new(nil) | ||
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.Base64UrlTest do | ||
@moduledoc """ | ||
`Base64Url` struct definition tests | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.Base64Url | ||
|
||
setup do | ||
%{valid_param: "valid_param", invalid_param: :atom} | ||
end | ||
|
||
test "new/1 with valid params", %{valid_param: valid_param} do | ||
%Base64Url{value: ^valid_param} = Base64Url.new(valid_param) | ||
end | ||
|
||
test "new/1 with invalid params", %{invalid_param: invalid_param} do | ||
{:error, :invalid_string} = Base64Url.new(invalid_param) | ||
end | ||
|
||
test "new/1 with nil params" do | ||
{:error, :invalid_string} = Base64Url.new(nil) | ||
end | ||
end |