Skip to content

Commit

Permalink
test: import all doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed May 23, 2024
1 parent 8909d92 commit d35be55
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 37 deletions.
18 changes: 5 additions & 13 deletions lib/crypto/base58_check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Tezex.Crypto.Base58Check do
Prepends `prefix` to `payload`, computes its checksum (double sha256) and encodes in Base58.
## Examples
iex> Tezex.Crypto.Base58Check.encode(<<165, 37, 29, 103, 204, 101, 232, 200, 87, 148, 178, 91, 43, 72, 191, 252, 190, 134, 75, 170>>, <<6, 161, 164>>)
iex> encode(<<165, 37, 29, 103, 204, 101, 232, 200, 87, 148, 178, 91, 43, 72, 191, 252, 190, 134, 75, 170>>, <<6, 161, 164>>)
"tz3bPFa6mGv8m4Ppn7w5KSDyAbEPwbJNpC9p"
"""
@spec encode(binary, binary) :: nonempty_binary
Expand Down Expand Up @@ -45,26 +45,18 @@ defmodule Tezex.Crypto.Base58Check do
Decodes the given string.
## Examples
iex> Tezex.Crypto.Base58Check.decode58!("1")
iex> decode58!("1")
<<0>>
iex> Tezex.Crypto.Base58Check.decode58!("z")
iex> decode58!("z")
<<57>>
iex> Tezex.Crypto.Base58Check.decode58!("Jf")
iex> decode58!("Jf")
:binary.encode_unsigned(1024)
iex> Tezex.Crypto.Base58Check.decode58!("BukQL")
iex> decode58!("BukQL")
:binary.encode_unsigned(123_456_789)
"""
@spec decode58!(binary) :: binary
defdelegate decode58!(encoded), to: Base58Check

@spec decode58check!(binary) :: binary
defdelegate decode58check!(encoded), to: Base58Check

def decode58check(encoded) do
try do
{:ok, decode58check!(encoded)}
rescue
_e in FunctionClauseError -> :error
end
end
end
8 changes: 4 additions & 4 deletions lib/micheline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ defmodule Tezex.Micheline do
Serialize a piece of data to its optimized binary representation.
## Examples
iex> Micheline.pack(-6407, :int)
iex> pack(-6407, :int)
"0500c764"
iex> Micheline.pack(%{"prim" => "Pair", "args" => [%{"int" => "-33"}, %{"int" => "71"}]})
iex> pack(%{"prim" => "Pair", "args" => [%{"int" => "-33"}, %{"int" => "71"}]})
"0507070061008701"
"""
@spec pack(binary() | integer() | map(), pack_types()) :: nonempty_binary()
Expand Down Expand Up @@ -51,9 +51,9 @@ defmodule Tezex.Micheline do
Deserialize a piece of data from its optimized binary representation.
## Examples
iex> Micheline.unpack("050a0000001601e67bac124dff100a57644de0cf26d341ebf9492600", :address)
iex> unpack("050a0000001601e67bac124dff100a57644de0cf26d341ebf9492600", :address)
"KT1VbT8n6YbrzPSjdAscKfJGDDNafB5yHn1H"
iex> Micheline.unpack("0507070001000c")
iex> unpack("0507070001000c")
%{"prim" => "Pair", "args" => [%{"int" => "1"}, %{"int" => "12"}]}
"""
@type unpack_result :: binary() | integer() | map() | list(unpack_result())
Expand Down
21 changes: 9 additions & 12 deletions lib/rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ defmodule Tezex.Rpc do
forged_operation <> payload_signature
end

@spec preapply_operation(t(), map(), encoded_private_key(), any()) ::
{:ok, any()} | {:error, Finch.Error.t()} | {:error, Jason.DecodeError.t()}
@doc """
Simulate the application of the operations with the context of the given block and return the result of each operation application.
"""
@spec preapply_operation(t(), map(), encoded_private_key(), any()) ::
{:ok, any()} | {:error, Finch.Error.t()} | {:error, Jason.DecodeError.t()}
def preapply_operation(%Rpc{} = rpc, operation, encoded_private_key, protocol) do
forged_operation = ForgeOperation.operation_group(operation)
signature = Crypto.sign_operation(encoded_private_key, forged_operation)
Expand All @@ -184,23 +184,21 @@ defmodule Tezex.Rpc do
end
end

@spec get_next_counter_for_account(t(), nonempty_binary()) :: integer()
def get_next_counter_for_account(%Rpc{} = rpc, address) do
get_counter_for_account(rpc, address) + 1
end

@spec get_block(t()) ::
{:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
| {:ok, any()}
@spec get_block(t(), any()) ::
{:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
| {:ok, any()}
{:ok, map()} | {:error, Finch.Error.t()} | {:error, Jason.DecodeError.t()}
@spec get_block(t(), nonempty_binary()) ::
{:ok, map()} | {:error, Finch.Error.t()} | {:error, Jason.DecodeError.t()}
def get_block(%Rpc{} = rpc, hash \\ "head") do
get(rpc, "/blocks/#{hash}")
end

@spec get_block_at_offset(t(), number()) ::
{:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
| {:ok, any()}
@spec get_block_at_offset(t(), integer()) ::
{:ok, map()} | {:error, Finch.Error.t()} | {:error, Jason.DecodeError.t()}
def get_block_at_offset(%Rpc{} = rpc, offset) do
if offset <= 0 do
get_block(rpc)
Expand All @@ -211,8 +209,7 @@ defmodule Tezex.Rpc do
end

@spec inject_operation(t(), any()) ::
{:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
| {:ok, any()}
{:ok, any()} | {:error, Finch.Error.t()} | {:error, Jason.DecodeError.t()}
def inject_operation(%Rpc{} = rpc, payload) do
post(rpc, "/injection/operation", payload)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/zarith.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ defmodule Tezex.Zarith do
Takes a binary and returns the integer in base 10.
## Examples
iex> Tezex.Zarith.decode("a1d22c")
iex> decode("a1d22c")
365_729
iex> Tezex.Zarith.decode("e1d22c")
iex> decode("e1d22c")
-365_729
"""
@spec decode(nonempty_binary()) :: integer()
Expand All @@ -33,10 +33,10 @@ defmodule Tezex.Zarith do
Implementation based on [anchorageoss/tezosprotocol (MIT License - Copyright (c) 2019 Anchor Labs, Inc.)](https://github.com/anchorageoss/tezosprotocol/blob/23a051d34fcfda8393940141f8151113a1aca10b/zarith/zarith.go#L153)
## Examples
iex> Tezex.Zarith.encode(365_729)
iex> encode(365_729)
"a1d22c"
iex> Tezex.Zarith.encode(-365_729)
iex> encode(-365_729)
"e1d22c"
"""
@spec encode(integer()) :: nonempty_binary()
Expand Down
2 changes: 1 addition & 1 deletion test/crypto/base58_check_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Tezex.Crypto.Base58CheckTest do
use ExUnit.Case, async: true
doctest Tezex.Crypto.Base58Check
doctest Tezex.Crypto.Base58Check, import: true
end
2 changes: 1 addition & 1 deletion test/micheline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Tezex.MichelineTest do
alias Tezex.Forge
alias Tezex.Micheline

doctest Tezex.Micheline
doctest Tezex.Micheline, import: true

describe "pack" do
result = Micheline.pack(9, :int)
Expand Down
2 changes: 1 addition & 1 deletion test/rpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Tezex.RpcTest do
use ExUnit.Case, async: true

alias Tezex.Rpc
doctest Tezex.Rpc
doctest Tezex.Rpc, import: true

@endpoint "https://ghostnet.tezos.marigold.dev/"
@ghostnet_1_address "tz1ZW1ZSN4ruXYc3nCon8EaTXp1t3tKWb9Ew"
Expand Down
2 changes: 1 addition & 1 deletion test/zarith_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Tezex.ZarithTest do
use ExUnit.Case, async: true

alias Tezex.Zarith
doctest Zarith
doctest Zarith, import: true

test "encode/decode" do
n = 1_000_000_000_000
Expand Down

0 comments on commit d35be55

Please sign in to comment.