Skip to content

Instantly share code, notes, and snippets.

@mujuni88
Created February 26, 2022 22:39
Show Gist options
  • Select an option

  • Save mujuni88/ec1c4464ee7196ab0f8ef2d29269cdb1 to your computer and use it in GitHub Desktop.

Select an option

Save mujuni88/ec1c4464ee7196ab0f8ef2d29269cdb1 to your computer and use it in GitHub Desktop.

Revisions

  1. mujuni88 created this gist Feb 26, 2022.
    83 changes: 83 additions & 0 deletions tests...EscrowAgent2_test.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    // SPDX-License-Identifier: GPL-3.0

    pragma solidity >=0.7.0 <0.9.0;

    import "hardhat/console.sol";
    import "remix_tests.sol";
    import "remix_accounts.sol";
    import "../contracts/EscrowAgent.sol";

    contract EscrowAgentTest is EscrowAgent {
    address owner;
    address receiver;
    address acc2;

    function beforeEach() public {
    owner = TestsAccounts.getAccount(0);
    receiver = TestsAccounts.getAccount(1);
    acc2 = TestsAccounts.getAccount(2);
    }

    /// #sender: account-0
    function testIfAgentsAccountsIsTheSameAsAssignedAccount() public {
    Assert.equal(agent, owner, "Agent is not the same as owner");
    }

    /// #sender: account-1
    /// #value: 100000000000000000
    function testShouldFailIfDepositAsNotOwner() public payable {
    deposit(receiver);
    console.log("Balance %d", address(this).balance);
    Assert.ok(address(this).balance == msg.value, "Balance should be greater than 0");
    }

    /// #sender: account-0
    /// #value: 100000000000000000
    function testShouldPassIfDepositAsOwner() public payable {
    deposit(receiver);
    console.log("Balance %d", address(this).balance);
    Assert.ok(address(this).balance == msg.value, "Balance should be greater than 0");
    }

    /// #sender: account-0
    /// #value: 100000000000000000
    function testShouldPassIfDepositToAnotherReceiverAsOwner() public payable {
    deposit(acc2);
    Assert.ok(address(this).balance == msg.value * 2, "Balance should be greater than 0");
    }

    /// #sender: account-0
    /// #value: 100000000000000000
    function testShouldFailWhenWithdrawIsCalledBeforeServiceIsCompleted() public {
    uint balance = 0.2 ether;
    Assert.ok(address(this).balance == balance, "Account balance should be 0.2 ETH");

    withdraw(payable(receiver));
    Assert.ok(address(this).balance == balance / 2, "Account balance should be 0.1 ETH");
    }

    /// #sender: account-0
    /// #value: 100000000000000000
    function testShouldPassWhenWithdrawAfterServiceIsCompleted() public {
    completed(receiver);

    uint balance = 0.2 ether;
    Assert.ok(address(this).balance == balance, "Account balance should be 0.2 ETH");

    withdraw(payable(receiver));
    Assert.ok(address(this).balance == balance / 2, "Account balance should be 0.1 ETH");
    }

    /// #sender: account-0
    /// #value: 100000000000000000
    function testShouldPassIfAcc2WithdrawsFunds() public {
    completed(acc2);

    uint balance = 0.1 ether;
    Assert.ok(address(this).balance == balance, "Account balance should be 0.2 ETH");

    withdraw(payable(acc2));
    Assert.ok(address(this).balance == 0, "Account balance should be 0.1 ETH");
    }

    }