Skip to content

Commit

Permalink
test: renaming tests to follow best practices from Foundry Book (#600)
Browse files Browse the repository at this point in the history
* Added _RevertIf_ in test/StdChains.t.sol

* changed tests names in test/StdChains.t.sol

* changed tests names in test/StdError.t.sol

* named imports and ternary operators instead of if-else for cleaner code

* named imports in test/StdStyle.t.sol

* named imports in test/StdToml.t.sol

* named imports and changed tests names in test/StdUtils.t.sol

* named imports in test/StdAssertions.t.sol

* named imports and changed tests names in test/StdCheats.t.sol

* named imports in test/StdJson.t.sol

* named imports and changed tests names in test/StdStorage.t.sol
  • Loading branch information
Miloš Đurica authored Sep 17, 2024
1 parent e04104a commit beb836e
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 69 deletions.
2 changes: 1 addition & 1 deletion test/StdAssertions.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/StdAssertions.sol";
import {StdAssertions} from "../src/StdAssertions.sol";
import {Vm} from "../src/Vm.sol";

interface VmInternal is Vm {
Expand Down
24 changes: 12 additions & 12 deletions test/StdChains.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/Test.sol";
import {Test} from "../src/Test.sol";

contract StdChainsMock is Test {
function exposed_getChain(string memory chainAlias) public returns (Chain memory) {
Expand Down Expand Up @@ -84,23 +84,23 @@ contract StdChainsTest is Test {
// _testRpc("flare_coston2");
// }

function test_ChainNoDefault() public {
function test_RevertIf_ChainNotFound() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains getChain(string): Chain with alias \"does_not_exist\" not found.");
stdChainsMock.exposed_getChain("does_not_exist");
}

function test_SetChainFirstFails() public {
function test_RevertIf_SetChain_ChainIdExist_FirstTest() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains setChain(string,ChainData): Chain ID 31337 already used by \"anvil\".");
stdChainsMock.exposed_setChain("anvil2", ChainData("Anvil", 31337, "URL"));
}

function test_ChainBubbleUp() public {
function test_RevertIf_ChainBubbleUp() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

Expand All @@ -111,7 +111,7 @@ contract StdChainsTest is Test {
stdChainsMock.exposed_getChain("needs_undefined_env_var");
}

function test_CannotSetChain_ChainIdExists() public {
function test_RevertIf_SetChain_ChainIdExists_SecondTest() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

Expand Down Expand Up @@ -148,47 +148,47 @@ contract StdChainsTest is Test {
assertEq(chainById.chainId, 123456789);
}

function test_SetNoEmptyAlias() public {
function test_RevertIf_SetEmptyAlias() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains setChain(string,ChainData): Chain alias cannot be the empty string.");
stdChainsMock.exposed_setChain("", ChainData("", 123456789, ""));
}

function test_SetNoChainId0() public {
function test_RevertIf_SetNoChainId0() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains setChain(string,ChainData): Chain ID cannot be 0.");
stdChainsMock.exposed_setChain("alias", ChainData("", 0, ""));
}

function test_GetNoChainId0() public {
function test_RevertIf_GetNoChainId0() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains getChain(uint256): Chain ID cannot be 0.");
stdChainsMock.exposed_getChain(0);
}

function test_GetNoEmptyAlias() public {
function test_RevertIf_GetNoEmptyAlias() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains getChain(string): Chain alias cannot be the empty string.");
stdChainsMock.exposed_getChain("");
}

function test_ChainIdNotFound() public {
function test_RevertIf_ChainIdNotFound() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

vm.expectRevert("StdChains getChain(string): Chain with alias \"no_such_alias\" not found.");
stdChainsMock.exposed_getChain("no_such_alias");
}

function test_ChainAliasNotFound() public {
function test_RevertIf_ChainAliasNotFound() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

Expand All @@ -214,7 +214,7 @@ contract StdChainsTest is Test {
assertEq(modifiedChain.rpcUrl, "https://modified.chain/");
}

function test_DontUseDefaultRpcUrl() public {
function test_RevertIf_DontUseDefaultRpcUrl() public {
// We deploy a mock to properly test the revert.
StdChainsMock stdChainsMock = new StdChainsMock();

Expand Down
20 changes: 10 additions & 10 deletions test/StdCheats.t.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/StdCheats.sol";
import "../src/Test.sol";
import "../src/StdJson.sol";
import "../src/StdToml.sol";
import "../src/interfaces/IERC20.sol";
import {StdCheats} from "../src/StdCheats.sol";
import {Test} from "../src/Test.sol";
import {stdJson} from "../src/StdJson.sol";
import {stdToml} from "../src/StdToml.sol";
import {IERC20} from "../src/interfaces/IERC20.sol";

contract StdCheatsTest is Test {
Bar test;
Expand Down Expand Up @@ -205,7 +205,7 @@ contract StdCheatsTest is Test {
deployCode(what);
}

function test_DeployCodeFail() public {
function test_RevertIf_DeployCodeFail() public {
vm.expectRevert(bytes("StdCheats deployCode(string): Deployment failed."));
this.deployCodeHelper("StdCheats.t.sol:RevertingContract");
}
Expand Down Expand Up @@ -415,7 +415,7 @@ contract StdCheatsTest is Test {
);
}

function test_CannotDeployCodeTo() external {
function test_RevertIf_CannotDeployCodeTo() external {
vm.expectRevert("StdCheats deployCodeTo(string,bytes,uint256,address): Failed to create runtime bytecode.");
this._revertDeployCodeTo();
}
Expand Down Expand Up @@ -470,7 +470,7 @@ contract StdCheatsForkTest is Test {
vm.createSelectFork({urlOrAlias: "mainnet", blockNumber: 16_428_900});
}

function test_CannotAssumeNoBlacklisted_EOA() external {
function test_RevertIf_CannotAssumeNoBlacklisted_EOA() external {
// We deploy a mock version so we can properly test the revert.
StdCheatsMock stdCheatsMock = new StdCheatsMock();
address eoa = vm.addr({privateKey: 1});
Expand All @@ -483,7 +483,7 @@ contract StdCheatsForkTest is Test {
assertTrue(true);
}

function test_AssumeNoBlacklisted_USDC() external {
function test_RevertIf_AssumeNoBlacklisted_USDC() external {
// We deploy a mock version so we can properly test the revert.
StdCheatsMock stdCheatsMock = new StdCheatsMock();
vm.expectRevert();
Expand All @@ -495,7 +495,7 @@ contract StdCheatsForkTest is Test {
assertFalse(USDCLike(USDC).isBlacklisted(addr));
}

function test_AssumeNoBlacklisted_USDT() external {
function test_RevertIf_AssumeNoBlacklisted_USDT() external {
// We deploy a mock version so we can properly test the revert.
StdCheatsMock stdCheatsMock = new StdCheatsMock();
vm.expectRevert();
Expand Down
24 changes: 12 additions & 12 deletions test/StdError.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "../src/StdError.sol";
import "../src/Test.sol";
import {stdError} from "../src/StdError.sol";
import {Test} from "../src/Test.sol";

contract StdErrorsTest is Test {
ErrorsTest test;
Expand All @@ -11,52 +11,52 @@ contract StdErrorsTest is Test {
test = new ErrorsTest();
}

function test_ExpectAssertion() public {
function test_RevertIf_AssertionError() public {
vm.expectRevert(stdError.assertionError);
test.assertionError();
}

function test_ExpectArithmetic() public {
function test_RevertIf_ArithmeticError() public {
vm.expectRevert(stdError.arithmeticError);
test.arithmeticError(10);
}

function test_ExpectDiv() public {
function test_RevertIf_DivisionError() public {
vm.expectRevert(stdError.divisionError);
test.divError(0);
}

function test_ExpectMod() public {
function test_RevertIf_ModError() public {
vm.expectRevert(stdError.divisionError);
test.modError(0);
}

function test_ExpectEnum() public {
function test_RevertIf_EnumConversionError() public {
vm.expectRevert(stdError.enumConversionError);
test.enumConversion(1);
}

function test_ExpectEncodeStg() public {
function test_RevertIf_EncodeStgError() public {
vm.expectRevert(stdError.encodeStorageError);
test.encodeStgError();
}

function test_ExpectPop() public {
function test_RevertIf_PopError() public {
vm.expectRevert(stdError.popError);
test.pop();
}

function test_ExpectOOB() public {
function test_RevertIf_IndexOOBError() public {
vm.expectRevert(stdError.indexOOBError);
test.indexOOBError(1);
}

function test_ExpectMem() public {
function test_RevertIf_MemOverflowError() public {
vm.expectRevert(stdError.memOverflowError);
test.mem();
}

function test_ExpectIntern() public {
function test_RevertIf_InternError() public {
vm.expectRevert(stdError.zeroVarError);
test.intern();
}
Expand Down
2 changes: 1 addition & 1 deletion test/StdJson.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/Test.sol";
import {Test, stdJson} from "../src/Test.sol";

contract StdJsonTest is Test {
using stdJson for string;
Expand Down
18 changes: 4 additions & 14 deletions test/StdMath.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "../src/StdMath.sol";
import "../src/Test.sol";
import {stdMath} from "../src/StdMath.sol";
import {Test, stdError} from "../src/Test.sol";

contract StdMathMock is Test {
function exposed_percentDelta(uint256 a, uint256 b) public pure returns (uint256) {
Expand Down Expand Up @@ -52,12 +52,7 @@ contract StdMathTest is Test {
}

function testFuzz_GetDelta_Uint(uint256 a, uint256 b) external pure {
uint256 manualDelta;
if (a > b) {
manualDelta = a - b;
} else {
manualDelta = b - a;
}
uint256 manualDelta = a > b ? a - b : b - a;

uint256 delta = stdMath.delta(a, b);

Expand Down Expand Up @@ -136,12 +131,7 @@ contract StdMathTest is Test {

function testFuzz_GetPercentDelta_Uint(uint192 a, uint192 b) external pure {
vm.assume(b != 0);
uint256 manualDelta;
if (a > b) {
manualDelta = a - b;
} else {
manualDelta = b - a;
}
uint256 manualDelta = a > b ? a - b : b - a;

uint256 manualPercentDelta = manualDelta * 1e18 / b;
uint256 percentDelta = stdMath.percentDelta(a, b);
Expand Down
10 changes: 5 additions & 5 deletions test/StdStorage.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/StdStorage.sol";
import "../src/Test.sol";
import {stdStorage, StdStorage} from "../src/StdStorage.sol";
import {Test} from "../src/Test.sol";

contract StdStorageTest is Test {
using stdStorage for StdStorage;
Expand Down Expand Up @@ -230,7 +230,7 @@ contract StdStorageTest is Test {
assertEq(val, true);
}

function test_StorageReadBool_Revert() public {
function test_RevertIf_ReadingNonBoolValue() public {
vm.expectRevert("stdStorage read_bool(StdStorage): Cannot decode. Make sure you are reading a bool.");
this.readNonBoolValue();
}
Expand All @@ -254,7 +254,7 @@ contract StdStorageTest is Test {
assertEq(val, type(int256).min);
}

function testFuzzPacked(uint256 val, uint8 elemToGet) public {
function testFuzz_Packed(uint256 val, uint8 elemToGet) public {
// This function tries an assortment of packed slots, shifts meaning number of elements
// that are packed. Shiftsizes are the size of each element, i.e. 8 means a data type that is 8 bits, 16 == 16 bits, etc.
// Combined, these determine how a slot is packed. Making it random is too hard to avoid global rejection limit
Expand Down Expand Up @@ -296,7 +296,7 @@ contract StdStorageTest is Test {
}
}

function testFuzzPacked2(uint256 nvars, uint256 seed) public {
function testFuzz_Packed2(uint256 nvars, uint256 seed) public {
// Number of random variables to generate.
nvars = bound(nvars, 1, 20);

Expand Down
2 changes: 1 addition & 1 deletion test/StdStyle.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/Test.sol";
import {Test, console2, StdStyle} from "../src/Test.sol";

contract StdStyleTest is Test {
function test_StyleColor() public pure {
Expand Down
2 changes: 1 addition & 1 deletion test/StdToml.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "../src/Test.sol";
import {Test, stdToml} from "../src/Test.sol";

contract StdTomlTest is Test {
using stdToml for string;
Expand Down
Loading

0 comments on commit beb836e

Please sign in to comment.