From 0e3f3153ff1df1c117370d35e6d8da3326048077 Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Fri, 30 Dec 2022 21:27:22 +0530 Subject: [PATCH] more tests for ERC20 template --- .../src/templates/ozerc20/index.ts | 10 +- .../ozerc20/tests/MyToken_mintable_test.sol | 104 ++++++++++++++++++ .../templates/ozerc20/tests/MyToken_test.sol | 15 +-- 3 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_mintable_test.sol diff --git a/libs/remix-ws-templates/src/templates/ozerc20/index.ts b/libs/remix-ws-templates/src/templates/ozerc20/index.ts index 3df48f3e02..9c087fd36d 100644 --- a/libs/remix-ws-templates/src/templates/ozerc20/index.ts +++ b/libs/remix-ws-templates/src/templates/ozerc20/index.ts @@ -23,7 +23,13 @@ export default async (opts) => { // If no options is selected, opts.upgradeable will be undefined // We do not show test file for upgradeable contract - // @ts-ignore - if (!opts || opts.upgradeable === undefined || !opts.upgradeable) filesObj['tests/MyToken_test.sol'] = (await import('raw-loader!./tests/MyToken_test.sol')).default + + if (!opts || opts.upgradeable === undefined || !opts.upgradeable) { + // @ts-ignore + if(opts.mintable) filesObj['tests/MyToken_test.sol'] = (await import('raw-loader!./tests/MyToken_mintable_test.sol')).default + // @ts-ignore + else filesObj['tests/MyToken_test.sol'] = (await import('raw-loader!./tests/MyToken_test.sol')).default + + } return filesObj } \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_mintable_test.sol b/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_mintable_test.sol new file mode 100644 index 0000000000..b6b3cd2972 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_mintable_test.sol @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity >=0.7.0 <0.9.0; +import "remix_tests.sol"; +import "remix_accounts.sol"; +import "../contracts/MyToken.sol"; + +contract MyTokenTest is MyToken { + + address acc0; + address acc1; + address acc2; + address acc3; + address acc4; + + function beforeAll() public { + acc0 = TestsAccounts.getAccount(0); + acc1 = TestsAccounts.getAccount(1); + acc2 = TestsAccounts.getAccount(2); + acc3 = TestsAccounts.getAccount(3); + acc4 = TestsAccounts.getAccount(4); + } + + function testTokenInitialValues() public { + Assert.equal(name(), "MyToken", "token name did not match"); + Assert.equal(symbol(), "MTK", "token symbol did not match"); + Assert.equal(decimals(), 18, "token decimals did not match"); + Assert.equal(totalSupply(), 0, "token supply should be zero"); + } + + function testTokenMinting() public { + Assert.equal(balanceOf(acc0), 0, "token balance should be zero initially"); + mint(acc0, 10000); + Assert.equal(balanceOf(acc0), 10000, "token balance did not match"); + } + + function testTotalSupply() public { + Assert.equal(totalSupply(), 10000, "total supply did not match"); + } + + /// #sender: account-1 + function failTestTokenMintingWithWrongOwner() public { + Assert.equal(balanceOf(acc0), 0, "token balance should be zero initially"); + mint(acc0, 10000); + Assert.equal(balanceOf(acc0), 10000, "token balance did not match"); + } + + function failTestTokenMintingForZeroAddress() public { + mint(address(0), 10000); + } + + function testTokenTransfer() public { + Assert.equal(balanceOf(acc1), 0, "token balance should be zero initially"); + transfer(acc1, 500); + Assert.equal(balanceOf(acc0), 9500, "token balance did not match"); + Assert.equal(balanceOf(acc1), 500, "token balance did not match"); + } + + /// #sender: account-1 + function testTokenTransferToOtherAddress() public { + Assert.equal(balanceOf(acc1), 500, "acc1 token balance did not match"); + transfer(acc2, 100); + Assert.equal(balanceOf(acc1), 400, "acc1 token balance did not match"); + Assert.equal(balanceOf(acc2), 100, "acc2 token balance did not match"); + } + + function failTestTokenTransferToZeroAddress() public { + transfer(address(0), 100); + } + + /// #sender: account-2 + function failTestTokenTransferMoreThanBalance() public { + transfer(acc3, 110); + } + + function testTokenApprove() public { + Assert.equal(allowance(acc0, acc3), 0, "token allowance should be zero initially"); + approve(acc3, 500); + Assert.equal(allowance(acc0, acc3), 500, "token allowance did not match"); + } + + function failTestTokenApproveForZeroSpenderAddress() public { + approve(address(0), 500); + } + + /// #sender: account-3 + function testTokenTransferfrom() public { + Assert.equal(allowance(acc0, acc3), 500, "token allowance did not match"); + transferFrom(acc0, acc4, 400); + Assert.equal(balanceOf(acc4), 500, "acc4 token balance did not match"); + Assert.equal(allowance(acc0, acc3), 100, "token allowance did not match"); + } + + /// #sender: account-3 + function failTestTokenTransferfromForMoreThanAllowance() public { + transferFrom(acc0, acc4, 110); + } + + /// #sender: account-3 + function failTestTokenTransferfromForZeroToAddress() public { + transferFrom(acc0, address(0), 100); + } +} + diff --git a/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_test.sol b/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_test.sol index 0fb1d12117..bdf8f6e69a 100644 --- a/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_test.sol +++ b/libs/remix-ws-templates/src/templates/ozerc20/tests/MyToken_test.sol @@ -4,15 +4,12 @@ pragma solidity >=0.7.0 <0.9.0; import "remix_tests.sol"; import "../contracts/MyToken.sol"; -contract MyTokenTest { +contract MyTokenTest is MyToken { - MyToken s; - function beforeAll () public { - s = new MyToken(); - } - - function testTokenNameAndSymbol () public { - Assert.equal(s.name(), "MyToken", "token name did not match"); - Assert.equal(s.symbol(), "MTK", "token symbol did not match"); + function testTokenInitialValues() public { + Assert.equal(name(), "MyToken", "token name did not match"); + Assert.equal(symbol(), "MTK", "token symbol did not match"); + Assert.equal(decimals(), 18, "token decimals did not match"); + Assert.equal(totalSupply(), 0, "token supply should be zero"); } } \ No newline at end of file