Skip to content

Instantly share code, notes, and snippets.

@conr2d
Last active May 7, 2021 02:34
Show Gist options
  • Save conr2d/8cc137119e2831517208a6f7dffd4151 to your computer and use it in GitHub Desktop.
Save conr2d/8cc137119e2831517208a6f7dffd4151 to your computer and use it in GitHub Desktop.
pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol";
import "remix_accounts.sol";
import "../ERC20.sol";
contract ERC20Test is ERC20 {
string testName = "MyToken";
string testSymbol = "MTN";
uint8 testDecimals = 2;
uint testTotalSupply = 1000000;
address acc0;
address acc1;
address acc2;
bool account1Approved;
bool account2Approved;
constructor() public ERC20(testName, testSymbol, testDecimals, testTotalSupply) {
}
function beforeAll() public {
acc0 = TestsAccounts.getAccount(0);
acc1 = TestsAccounts.getAccount(1);
acc2 = TestsAccounts.getAccount(2);
}
function beforeEach() public {
transfer(acc1, 10000);
transfer(acc2, 10000);
}
function afterEach() public {
if (account1Approved) {
transferFrom(acc1, acc0, balanceOf(acc1));
}
if (account2Approved) {
transferFrom(acc2, acc0, balanceOf(acc2));
}
}
/// #sender: account-1
function approveAccount1() public {
approve(acc0, uint(-1));
account1Approved = true;
}
/// #sender: account-1
function approveAccount1_2() public {
approve(acc2, uint(-1));
}
/// #sender: account-2
function approveAccount2() public {
approve(acc0, uint(-1));
account2Approved = true;
}
function checkName() public {
Assert.equal(name, testName, "invalid value");
}
function checkSymbol() public {
Assert.equal(symbol, testSymbol, "invalid value");
}
function checkDecimals() public {
Assert.equal(uint(decimals), uint(testDecimals), "invalid value");
}
function checkBalanceOf() public {
Assert.equal(balanceOf(acc1), 10000, "invalid balance of account-1");
}
function checkTotalSupply() public {
Assert.equal(totalSupply, testTotalSupply, "invalid amount of totalSupply");
}
/// #sender: account-1
function checkTransfer() public {
Assert.ok(transfer(acc2, 5000), "transfer to acc1 has failed");
Assert.equal(balanceOf(acc1), 5000, "invalid balance of account-1");
Assert.equal(balanceOf(acc2), 15000, "invalid balance of account-2");
}
function checkTransferThrowsOverdrawnBalance() public {
ERC20 erc20 = new ERC20(testName, testSymbol, testDecimals, 10000);
try erc20.transfer(acc0, 15000) returns (bool r) {
Assert.ok(false, "transfer should fail");
} catch Error(string memory reason) {
Assert.equal(reason, "overdrawn balance", reason);
}
}
/// #sender: account-2
function checkTransferFrom() public {
Assert.ok(transferFrom(acc1, acc2, 5000), "transfer to acc1 has failed");
Assert.equal(balanceOf(acc1), 5000, "invalid balance of account-1");
Assert.equal(balanceOf(acc2), 15000, "invalid balance of account-2");
}
function checkApprove() public {
Assert.ok(approve(acc1, 10000), "approve to acc1 has failed");
Assert.equal(allowance(acc0, acc1), 10000, "invalid amount of allowance");
}
}
@baizetianxia
Copy link

baizetianxia commented May 6, 2021

Hello,thanks for your shared ERC20_test.sol file in github. I have a question about changing sender in xx_test.sol. Just like line 96 "/// #sender: account-2". how to do it?

@conr2d
Copy link
Author

conr2d commented May 6, 2021

@baizetianxia

Refer to "Unit Testing Plugin - Customization - 2. Custom Transaction Context" from the next link:
https://remix-ide.readthedocs.io/en/latest/unittesting.html#customization

import "remix_accounts.sol"; provides 10 test accounts, and you can get the address of those accounts by TestsAccounts.getAccount(index);. It is allowed to set index from 0 to 9. If you mean setting the sender to arbitrary address, I don't think it's possible.

@baizetianxia
Copy link

I read docs and test my contract successfully. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment