Skip to content

Commit

Permalink
Added EnvData and PactCode with tests
Browse files Browse the repository at this point in the history
Co-authored-by: Santiago Botero <[email protected]>
  • Loading branch information
CristhianRodriguezMolina and boterop committed Sep 29, 2022
1 parent 60ace1b commit d52f61a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/types/env_data.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Kadena.Types.EnvData do
@moduledoc """
`EnvData` struct definition.
"""

@behaviour Kadena.Types.Spec

@type data :: map()

@type t :: %__MODULE__{data: data()}

defstruct [:data]

@impl true
def new(data) when is_map(data), do: %__MODULE__{data: data}
def new(_data), do: {:error, [data: :invalid]}
end
17 changes: 17 additions & 0 deletions lib/types/pact_code.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Kadena.Types.PactCode do
@moduledoc """
`PactCode` struct definition
"""

@behaviour Kadena.Types.Spec

@type code :: String.t()

@type t :: %__MODULE__{code: code()}

defstruct [:code]

@impl true
def new(code) when is_binary(code), do: %__MODULE__{code: code}
def new(_code), do: {:error, [code: :invalid]}
end
27 changes: 27 additions & 0 deletions test/types/env_data_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Kadena.Types.EnvDataTest do
@moduledoc """
`EnvData` struct definition tests.
"""

use ExUnit.Case

alias Kadena.Types.EnvData

describe "new/1" do
test "with valid env data" do
%EnvData{data: %{}} = EnvData.new(%{})
end

test "with nil env data" do
{:error, [data: :invalid]} = EnvData.new(nil)
end

test "with number env data" do
{:error, [data: :invalid]} = EnvData.new(12_345)
end

test "with atom env data" do
{:error, [data: :invalid]} = EnvData.new(:atom)
end
end
end
28 changes: 28 additions & 0 deletions test/types/pact_code_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Kadena.Types.PactCodeTest do
@moduledoc """
`PactCode` struct definition tests.
"""

use ExUnit.Case

alias Kadena.Types.PactCode

describe "new/1" do
test "with a valid pact code" do
%PactCode{code: ~S(format \"hello {}\" [\"world\"])} =
PactCode.new(~S(format \"hello {}\" [\"world\"]))
end

test "with a number pact code" do
{:error, [code: :invalid]} = PactCode.new(12_345)
end

test "with a atom pact code" do
{:error, [code: :invalid]} = PactCode.new(:atom)
end

test "with a nil pact code" do
{:error, [code: :invalid]} = PactCode.new(nil)
end
end
end

0 comments on commit d52f61a

Please sign in to comment.