From 0b9191484b3e6ef69a26aa609bed9469ba0ea6b3 Mon Sep 17 00:00:00 2001 From: osmannyildiz Date: Sun, 19 May 2024 15:13:29 +0300 Subject: [PATCH] add test file --- MyToken.t.sol | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 MyToken.t.sol diff --git a/MyToken.t.sol b/MyToken.t.sol new file mode 100644 index 0000000..a557eb3 --- /dev/null +++ b/MyToken.t.sol @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import {Test, console} from "forge-std/Test.sol"; +import {MyToken} from "../src/MyToken.sol"; + +contract MyTokenTest is Test { + MyToken public myToken; + address public owner; + address public user; + + function setUp() public { + owner = address(0x100); + user = address(0x101); + + vm.deal(owner, 100 ether); + vm.deal(user, 100 ether); + + vm.startPrank(owner); + myToken = new MyToken(1000); + vm.stopPrank(); + } + + function test_buyAndTransferTokens() public { + vm.startPrank(user); // User operations under here + uint ethAmount = 0.01 ether; + // uint ethAmount = 0.001 ether; // This will cause myToken.buyTokens() to revert + + uint userBalanceBeforeBuy = myToken.balanceOf(user); + uint totalSupplyBeforeBuy = myToken.totalSupply(); + console.log("User balance before buy:", userBalanceBeforeBuy); + console.log("Total supply before buy:", totalSupplyBeforeBuy); + + uint expectedBalance = userBalanceBeforeBuy + ethAmount / myToken.tokenPrice(); + + myToken.buyTokens{value: ethAmount}(); + + uint userBalanceAfterBuy = myToken.balanceOf(user); + uint totalSupplyAfterBuy = myToken.totalSupply(); + console.log("User balance after buy:", userBalanceAfterBuy); + console.log("Total supply after buy:", totalSupplyAfterBuy); + + assert(userBalanceAfterBuy > userBalanceBeforeBuy); + assertEq(userBalanceAfterBuy, expectedBalance); + assertEq(totalSupplyAfterBuy, totalSupplyBeforeBuy + ethAmount / myToken.tokenPrice()); + + myToken.transfer(owner, userBalanceAfterBuy); + vm.stopPrank(); + } +}