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.
Add PactValue struct definitions (#19)
- Loading branch information
1 parent
eaaec68
commit 3bd9fcc
Showing
12 changed files
with
315 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Kadena.Types.PactDecimal do | ||
@moduledoc """ | ||
`PactDecimal` struct definition. | ||
""" | ||
|
||
alias Decimal | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type t :: %__MODULE__{value: Decimal.t(), raw_value: String.t()} | ||
|
||
defstruct [:value, :raw_value] | ||
|
||
@impl true | ||
def new(str) when is_binary(str), do: %__MODULE__{value: Decimal.new(str), raw_value: str} | ||
def new(_str), do: {:error, :invalid_decimal} | ||
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,17 @@ | ||
defmodule Kadena.Types.PactInt do | ||
@moduledoc """ | ||
`PactInt` struct definition. | ||
""" | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type t :: %__MODULE__{value: integer(), raw_value: String.t()} | ||
|
||
defstruct [:value, :raw_value] | ||
|
||
@impl true | ||
def new(value) when is_integer(value), | ||
do: %__MODULE__{value: value, raw_value: to_string(value)} | ||
|
||
def new(_value), do: {:error, :invalid_int} | ||
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,22 @@ | ||
defmodule Kadena.Types.PactLiteral do | ||
@moduledoc """ | ||
`PactLiteral` struct definition. | ||
""" | ||
alias Kadena.Types.{PactDecimal, PactInt} | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type literal :: PactInt.t() | PactDecimal.t() | String.t() | number() | boolean() | ||
|
||
@type t :: %__MODULE__{literal: literal()} | ||
|
||
defstruct [:literal] | ||
|
||
@impl true | ||
def new(%PactInt{} = pact_int), do: %__MODULE__{literal: pact_int} | ||
def new(%PactDecimal{} = pact_decimal), do: %__MODULE__{literal: pact_decimal} | ||
def new(str) when is_binary(str), do: %__MODULE__{literal: str} | ||
def new(number) when is_number(number), do: %__MODULE__{literal: number} | ||
def new(bool) when is_boolean(bool), do: %__MODULE__{literal: bool} | ||
def new(_literal), do: {:error, :invalid_literal} | ||
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.PactLiteralsList do | ||
@moduledoc """ | ||
`PactLiteralsList` struct definition. | ||
""" | ||
alias Kadena.Types.PactLiteral | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type literals :: list(PactLiteral.t()) | ||
|
||
@type t :: %__MODULE__{list: literals()} | ||
|
||
defstruct list: [] | ||
|
||
@impl true | ||
def new(literals), do: build_list(%__MODULE__{}, literals) | ||
|
||
@spec build_list(list :: t(), literals :: literals()) :: t() | ||
defp build_list(list, []), do: list | ||
|
||
defp build_list(%__MODULE__{list: list}, [%PactLiteral{} = literal | rest]), | ||
do: build_list(%__MODULE__{list: [literal | list]}, rest) | ||
|
||
defp build_list(_list, _literals), do: {:error, :invalid_literal} | ||
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,19 @@ | ||
defmodule Kadena.Types.PactValue do | ||
@moduledoc """ | ||
`PactValue` structure definition. | ||
""" | ||
alias Kadena.Types.{PactLiteral, PactLiteralsList} | ||
|
||
@behaviour Kadena.Types.Spec | ||
|
||
@type pact_value :: PactLiteral.t() | PactLiteralsList.t() | ||
|
||
@type t :: %__MODULE__{value: pact_value()} | ||
|
||
defstruct [:value] | ||
|
||
@impl true | ||
def new(%PactLiteral{} = literal), do: %__MODULE__{value: literal} | ||
def new(%PactLiteralsList{} = literal_list), do: %__MODULE__{value: literal_list} | ||
def new(_value), do: {:error, :invalid_value} | ||
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
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
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,28 @@ | ||
defmodule Kadena.Types.PactDecimalTest do | ||
@moduledoc """ | ||
`PactDecimal` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.PactDecimal | ||
|
||
describe "new/1" do | ||
test "with a valid value" do | ||
expected_value = Decimal.new("4.3333333") | ||
%PactDecimal{value: ^expected_value, raw_value: "4.3333333"} = PactDecimal.new("4.3333333") | ||
end | ||
|
||
test "with a nil value" do | ||
{:error, :invalid_decimal} = PactDecimal.new(nil) | ||
end | ||
|
||
test "with an atom value" do | ||
{:error, :invalid_decimal} = PactDecimal.new(:atom) | ||
end | ||
|
||
test "with an empty list" do | ||
{:error, :invalid_decimal} = PactDecimal.new([]) | ||
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,27 @@ | ||
defmodule Kadena.Types.PactIntTest do | ||
@moduledoc """ | ||
`PactInt` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.PactInt | ||
|
||
describe "new/1" do | ||
test "with a valid integer" do | ||
%PactInt{value: 500, raw_value: "500"} = PactInt.new(500) | ||
end | ||
|
||
test "with a nil value" do | ||
{:error, :invalid_int} = PactInt.new(nil) | ||
end | ||
|
||
test "with an atom value" do | ||
{:error, :invalid_int} = PactInt.new(:atom) | ||
end | ||
|
||
test "with an empty list" do | ||
{:error, :invalid_int} = PactInt.new([]) | ||
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,45 @@ | ||
defmodule Kadena.Types.PactLiteralTest do | ||
@moduledoc """ | ||
`PactLiteral` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.{PactDecimal, PactInt, PactLiteral} | ||
|
||
describe "new/1" do | ||
test "with a valid string literal" do | ||
%PactLiteral{literal: "string"} = PactLiteral.new("string") | ||
end | ||
|
||
test "with a valid number literal" do | ||
%PactLiteral{literal: 123} = PactLiteral.new(123) | ||
end | ||
|
||
test "with a valid PactInt literal" do | ||
pact_int = PactInt.new(123) | ||
%PactLiteral{literal: ^pact_int} = PactLiteral.new(pact_int) | ||
end | ||
|
||
test "with a valid PactDecimal literal" do | ||
pact_decimal = PactDecimal.new("4.634") | ||
%PactLiteral{literal: ^pact_decimal} = PactLiteral.new(pact_decimal) | ||
end | ||
|
||
test "with a valid boolean literal" do | ||
%PactLiteral{literal: true} = PactLiteral.new(true) | ||
end | ||
|
||
test "with a nil literal" do | ||
{:error, :invalid_literal} = PactLiteral.new(nil) | ||
end | ||
|
||
test "with an atom literal" do | ||
{:error, :invalid_literal} = PactLiteral.new(:atom) | ||
end | ||
|
||
test "with an empty list literal" do | ||
{:error, :invalid_literal} = PactLiteral.new([]) | ||
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,43 @@ | ||
defmodule Kadena.Types.PactLiteralsListTest do | ||
@moduledoc """ | ||
`PactLiteralsList` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.{PactDecimal, PactInt, PactLiteral, PactLiteralsList} | ||
|
||
describe "new/1" do | ||
test "with a valid list" do | ||
literal_string = PactLiteral.new("string") | ||
literal_int = 123 |> PactInt.new() |> PactLiteral.new() | ||
literal_decimal = "2.3333" |> PactDecimal.new() |> PactLiteral.new() | ||
literal_boolean = PactLiteral.new(true) | ||
|
||
literals_list = [literal_string, literal_int, literal_decimal, literal_boolean] | ||
reversed_literals_list = [literal_boolean, literal_decimal, literal_int, literal_string] | ||
%PactLiteralsList{list: ^reversed_literals_list} = PactLiteralsList.new(literals_list) | ||
end | ||
|
||
test "with an empty list value" do | ||
%PactLiteralsList{list: []} = PactLiteralsList.new([]) | ||
end | ||
|
||
test "with a nil value" do | ||
{:error, :invalid_literal} = PactLiteralsList.new(nil) | ||
end | ||
|
||
test "with an atom value" do | ||
{:error, :invalid_literal} = PactLiteralsList.new(:atom) | ||
end | ||
|
||
test "with a list of nil" do | ||
{:error, :invalid_literal} = PactLiteralsList.new([nil]) | ||
end | ||
|
||
test "when the list has invalid values" do | ||
invalid_literals_list = [PactLiteral.new(true), :atom] | ||
{:error, :invalid_literal} = PactLiteralsList.new(invalid_literals_list) | ||
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,70 @@ | ||
defmodule Kadena.Types.PactValueTest do | ||
@moduledoc """ | ||
`PactValue` struct definition tests. | ||
""" | ||
|
||
use ExUnit.Case | ||
|
||
alias Kadena.Types.{PactDecimal, PactInt, PactLiteral, PactLiteralsList, PactValue} | ||
|
||
describe "new/1" do | ||
setup do | ||
literal_string = PactLiteral.new("string") | ||
literal_int = 123 |> PactInt.new() |> PactLiteral.new() | ||
literal_decimal = "2.3333" |> PactDecimal.new() |> PactLiteral.new() | ||
literal_boolean = PactLiteral.new(true) | ||
|
||
literal_list = | ||
PactLiteralsList.new([literal_string, literal_int, literal_decimal, literal_boolean]) | ||
|
||
%{ | ||
literal_string: literal_string, | ||
literal_int: literal_int, | ||
literal_decimal: literal_decimal, | ||
literal_boolean: literal_boolean, | ||
literal_list: literal_list | ||
} | ||
end | ||
|
||
test "with a valid string", %{literal_string: literal_string} do | ||
%PactValue{value: ^literal_string} = PactValue.new(literal_string) | ||
end | ||
|
||
test "with a valid PactInt", %{literal_int: literal_int} do | ||
%PactValue{value: ^literal_int} = PactValue.new(literal_int) | ||
end | ||
|
||
test "with a valid PactDecimal", %{literal_decimal: literal_decimal} do | ||
%PactValue{value: ^literal_decimal} = PactValue.new(literal_decimal) | ||
end | ||
|
||
test "with a valid boolean", %{literal_boolean: literal_boolean} do | ||
%PactValue{value: ^literal_boolean} = PactValue.new(literal_boolean) | ||
end | ||
|
||
test "with a valid PactLiteralList", %{literal_list: literal_list} do | ||
%PactValue{value: ^literal_list} = PactValue.new(literal_list) | ||
end | ||
|
||
test "with an invalid list" do | ||
{:error, :invalid_value} = | ||
["string", :atom, true] |> PactLiteralsList.new() |> PactValue.new() | ||
end | ||
|
||
test "with a nil value" do | ||
{:error, :invalid_value} = PactValue.new(nil) | ||
end | ||
|
||
test "with an atom value" do | ||
{:error, :invalid_value} = PactValue.new(:atom) | ||
end | ||
|
||
test "with a list of nil" do | ||
{:error, :invalid_value} = PactValue.new([nil]) | ||
end | ||
|
||
test "with empty list value" do | ||
{:error, :invalid_value} = PactValue.new([]) | ||
end | ||
end | ||
end |