4
0
Fork 0
Fork of the exw3 library. With our own additions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exw3/test/exw3_test.exs

159 lines
4.0 KiB

7 years ago
defmodule EXW3Test do
use ExUnit.Case
doctest ExW3
7 years ago
setup_all do
%{
simple_storage_abi: ExW3.load_abi("test/examples/build/SimpleStorage.abi"),
array_tester_abi: ExW3.load_abi("test/examples/build/ArrayTester.abi"),
event_tester_abi: ExW3.load_abi("test/examples/build/EventTester.abi"),
complex_abi: ExW3.load_abi("test/examples/build/Complex.abi"),
7 years ago
accounts: ExW3.accounts()
}
7 years ago
end
test "gets accounts" do
7 years ago
assert ExW3.accounts() |> is_list
end
test "gets balance", context do
7 years ago
assert ExW3.balance(Enum.at(context[:accounts], 0)) |> is_integer
end
test "gets block number" do
7 years ago
assert ExW3.block_number() |> is_integer
end
7 years ago
test "loads abi", context do
assert context[:simple_storage_abi] |> is_map
7 years ago
end
test "mines a block" do
7 years ago
block_number = ExW3.block_number()
ExW3.mine()
assert ExW3.block_number() == block_number + 1
end
test "mines multiple blocks" do
7 years ago
block_number = ExW3.block_number()
ExW3.mine(5)
assert ExW3.block_number() == block_number + 5
end
test "starts a Contract GenServer for simple storage contract", context do
ExW3.Contract.start_link(SimpleStorage, abi: context[:simple_storage_abi])
7 years ago
{:ok, address} =
ExW3.Contract.deploy(
SimpleStorage,
bin: ExW3.load_bin("test/examples/build/SimpleStorage.bin"),
options: %{
gas: 300_000,
from: Enum.at(context[:accounts], 0)
}
)
7 years ago
ExW3.Contract.at(SimpleStorage, address)
assert address == ExW3.Contract.address(SimpleStorage)
7 years ago
{:ok, data} = ExW3.Contract.call(SimpleStorage, :get)
assert data == 0
ExW3.Contract.send(SimpleStorage, :set, [1], %{from: Enum.at(context[:accounts], 0)})
{:ok, data} = ExW3.Contract.call(SimpleStorage, :get)
assert data == 1
7 years ago
end
test "starts a Contract GenServer for array tester contract", context do
ExW3.Contract.start_link(ArrayTester, abi: context[:array_tester_abi])
7 years ago
{:ok, address} =
ExW3.Contract.deploy(
ArrayTester,
bin: ExW3.load_bin("test/examples/build/ArrayTester.bin"),
options: %{
gas: 300_000,
from: Enum.at(context[:accounts], 0)
}
)
ExW3.Contract.at(ArrayTester, address)
assert address == ExW3.Contract.address(ArrayTester)
arr = [1, 2, 3, 4, 5]
{:ok, result} = ExW3.Contract.call(ArrayTester, :staticUint, [arr])
assert result == arr
{:ok, result} = ExW3.Contract.call(ArrayTester, :dynamicUint, [arr])
assert result == arr
end
test "starts a Contract GenServer for event tester contract", context do
ExW3.Contract.start_link(EventTester, abi: context[:event_tester_abi])
7 years ago
{:ok, address} =
ExW3.Contract.deploy(
EventTester,
bin: ExW3.load_bin("test/examples/build/EventTester.bin"),
options: %{
gas: 300_000,
from: Enum.at(context[:accounts], 0)
}
)
ExW3.Contract.at(EventTester, address)
assert address == ExW3.Contract.address(EventTester)
7 years ago
{:ok, tx_hash} =
ExW3.Contract.send(EventTester, :simple, ["Hello, World!"], %{
from: Enum.at(context[:accounts], 0)
})
{:ok, {receipt, logs}} = ExW3.Contract.tx_receipt(EventTester, tx_hash)
assert receipt |> is_map
data =
logs
|> Enum.at(0)
|> Map.get("data")
|> ExW3.bytes_to_string()
7 years ago
assert data == "Hello, World!"
end
test "starts a Contract GenServer for Complex contract", context do
ExW3.Contract.start_link(Complex, abi: context[:complex_abi])
7 years ago
{:ok, address} =
ExW3.Contract.deploy(
Complex,
bin: ExW3.load_bin("test/examples/build/Complex.bin"),
args: [42, "Hello, world!"],
options: %{
from: Enum.at(context[:accounts], 0),
gas: 300_000
}
)
ExW3.Contract.at(Complex, address)
assert address == ExW3.Contract.address(Complex)
{:ok, foo, foobar} = ExW3.Contract.call(Complex, :getBoth)
assert foo == 42
assert ExW3.bytes_to_string(foobar) == "Hello, world!"
7 years ago
end
7 years ago
end