Skip to content

Commit beb836e

Browse files
author
Miloš Đurica
authored
test: renaming tests to follow best practices from Foundry Book (#600)
* 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
1 parent e04104a commit beb836e

File tree

10 files changed

+59
-69
lines changed

10 files changed

+59
-69
lines changed

test/StdAssertions.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/StdAssertions.sol";
4+
import {StdAssertions} from "../src/StdAssertions.sol";
55
import {Vm} from "../src/Vm.sol";
66

77
interface VmInternal is Vm {

test/StdChains.t.sol

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/Test.sol";
4+
import {Test} from "../src/Test.sol";
55

66
contract StdChainsMock is Test {
77
function exposed_getChain(string memory chainAlias) public returns (Chain memory) {
@@ -84,23 +84,23 @@ contract StdChainsTest is Test {
8484
// _testRpc("flare_coston2");
8585
// }
8686

87-
function test_ChainNoDefault() public {
87+
function test_RevertIf_ChainNotFound() public {
8888
// We deploy a mock to properly test the revert.
8989
StdChainsMock stdChainsMock = new StdChainsMock();
9090

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

95-
function test_SetChainFirstFails() public {
95+
function test_RevertIf_SetChain_ChainIdExist_FirstTest() public {
9696
// We deploy a mock to properly test the revert.
9797
StdChainsMock stdChainsMock = new StdChainsMock();
9898

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

103-
function test_ChainBubbleUp() public {
103+
function test_RevertIf_ChainBubbleUp() public {
104104
// We deploy a mock to properly test the revert.
105105
StdChainsMock stdChainsMock = new StdChainsMock();
106106

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

114-
function test_CannotSetChain_ChainIdExists() public {
114+
function test_RevertIf_SetChain_ChainIdExists_SecondTest() public {
115115
// We deploy a mock to properly test the revert.
116116
StdChainsMock stdChainsMock = new StdChainsMock();
117117

@@ -148,47 +148,47 @@ contract StdChainsTest is Test {
148148
assertEq(chainById.chainId, 123456789);
149149
}
150150

151-
function test_SetNoEmptyAlias() public {
151+
function test_RevertIf_SetEmptyAlias() public {
152152
// We deploy a mock to properly test the revert.
153153
StdChainsMock stdChainsMock = new StdChainsMock();
154154

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

159-
function test_SetNoChainId0() public {
159+
function test_RevertIf_SetNoChainId0() public {
160160
// We deploy a mock to properly test the revert.
161161
StdChainsMock stdChainsMock = new StdChainsMock();
162162

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

167-
function test_GetNoChainId0() public {
167+
function test_RevertIf_GetNoChainId0() public {
168168
// We deploy a mock to properly test the revert.
169169
StdChainsMock stdChainsMock = new StdChainsMock();
170170

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

175-
function test_GetNoEmptyAlias() public {
175+
function test_RevertIf_GetNoEmptyAlias() public {
176176
// We deploy a mock to properly test the revert.
177177
StdChainsMock stdChainsMock = new StdChainsMock();
178178

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

183-
function test_ChainIdNotFound() public {
183+
function test_RevertIf_ChainIdNotFound() public {
184184
// We deploy a mock to properly test the revert.
185185
StdChainsMock stdChainsMock = new StdChainsMock();
186186

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

191-
function test_ChainAliasNotFound() public {
191+
function test_RevertIf_ChainAliasNotFound() public {
192192
// We deploy a mock to properly test the revert.
193193
StdChainsMock stdChainsMock = new StdChainsMock();
194194

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

217-
function test_DontUseDefaultRpcUrl() public {
217+
function test_RevertIf_DontUseDefaultRpcUrl() public {
218218
// We deploy a mock to properly test the revert.
219219
StdChainsMock stdChainsMock = new StdChainsMock();
220220

test/StdCheats.t.sol

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/StdCheats.sol";
5-
import "../src/Test.sol";
6-
import "../src/StdJson.sol";
7-
import "../src/StdToml.sol";
8-
import "../src/interfaces/IERC20.sol";
4+
import {StdCheats} from "../src/StdCheats.sol";
5+
import {Test} from "../src/Test.sol";
6+
import {stdJson} from "../src/StdJson.sol";
7+
import {stdToml} from "../src/StdToml.sol";
8+
import {IERC20} from "../src/interfaces/IERC20.sol";
99

1010
contract StdCheatsTest is Test {
1111
Bar test;
@@ -205,7 +205,7 @@ contract StdCheatsTest is Test {
205205
deployCode(what);
206206
}
207207

208-
function test_DeployCodeFail() public {
208+
function test_RevertIf_DeployCodeFail() public {
209209
vm.expectRevert(bytes("StdCheats deployCode(string): Deployment failed."));
210210
this.deployCodeHelper("StdCheats.t.sol:RevertingContract");
211211
}
@@ -415,7 +415,7 @@ contract StdCheatsTest is Test {
415415
);
416416
}
417417

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

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

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

498-
function test_AssumeNoBlacklisted_USDT() external {
498+
function test_RevertIf_AssumeNoBlacklisted_USDT() external {
499499
// We deploy a mock version so we can properly test the revert.
500500
StdCheatsMock stdCheatsMock = new StdCheatsMock();
501501
vm.expectRevert();

test/StdError.t.sol

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.8.0 <0.9.0;
33

4-
import "../src/StdError.sol";
5-
import "../src/Test.sol";
4+
import {stdError} from "../src/StdError.sol";
5+
import {Test} from "../src/Test.sol";
66

77
contract StdErrorsTest is Test {
88
ErrorsTest test;
@@ -11,52 +11,52 @@ contract StdErrorsTest is Test {
1111
test = new ErrorsTest();
1212
}
1313

14-
function test_ExpectAssertion() public {
14+
function test_RevertIf_AssertionError() public {
1515
vm.expectRevert(stdError.assertionError);
1616
test.assertionError();
1717
}
1818

19-
function test_ExpectArithmetic() public {
19+
function test_RevertIf_ArithmeticError() public {
2020
vm.expectRevert(stdError.arithmeticError);
2121
test.arithmeticError(10);
2222
}
2323

24-
function test_ExpectDiv() public {
24+
function test_RevertIf_DivisionError() public {
2525
vm.expectRevert(stdError.divisionError);
2626
test.divError(0);
2727
}
2828

29-
function test_ExpectMod() public {
29+
function test_RevertIf_ModError() public {
3030
vm.expectRevert(stdError.divisionError);
3131
test.modError(0);
3232
}
3333

34-
function test_ExpectEnum() public {
34+
function test_RevertIf_EnumConversionError() public {
3535
vm.expectRevert(stdError.enumConversionError);
3636
test.enumConversion(1);
3737
}
3838

39-
function test_ExpectEncodeStg() public {
39+
function test_RevertIf_EncodeStgError() public {
4040
vm.expectRevert(stdError.encodeStorageError);
4141
test.encodeStgError();
4242
}
4343

44-
function test_ExpectPop() public {
44+
function test_RevertIf_PopError() public {
4545
vm.expectRevert(stdError.popError);
4646
test.pop();
4747
}
4848

49-
function test_ExpectOOB() public {
49+
function test_RevertIf_IndexOOBError() public {
5050
vm.expectRevert(stdError.indexOOBError);
5151
test.indexOOBError(1);
5252
}
5353

54-
function test_ExpectMem() public {
54+
function test_RevertIf_MemOverflowError() public {
5555
vm.expectRevert(stdError.memOverflowError);
5656
test.mem();
5757
}
5858

59-
function test_ExpectIntern() public {
59+
function test_RevertIf_InternError() public {
6060
vm.expectRevert(stdError.zeroVarError);
6161
test.intern();
6262
}

test/StdJson.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/Test.sol";
4+
import {Test, stdJson} from "../src/Test.sol";
55

66
contract StdJsonTest is Test {
77
using stdJson for string;

test/StdMath.t.sol

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.8.0 <0.9.0;
33

4-
import "../src/StdMath.sol";
5-
import "../src/Test.sol";
4+
import {stdMath} from "../src/StdMath.sol";
5+
import {Test, stdError} from "../src/Test.sol";
66

77
contract StdMathMock is Test {
88
function exposed_percentDelta(uint256 a, uint256 b) public pure returns (uint256) {
@@ -52,12 +52,7 @@ contract StdMathTest is Test {
5252
}
5353

5454
function testFuzz_GetDelta_Uint(uint256 a, uint256 b) external pure {
55-
uint256 manualDelta;
56-
if (a > b) {
57-
manualDelta = a - b;
58-
} else {
59-
manualDelta = b - a;
60-
}
55+
uint256 manualDelta = a > b ? a - b : b - a;
6156

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

@@ -136,12 +131,7 @@ contract StdMathTest is Test {
136131

137132
function testFuzz_GetPercentDelta_Uint(uint192 a, uint192 b) external pure {
138133
vm.assume(b != 0);
139-
uint256 manualDelta;
140-
if (a > b) {
141-
manualDelta = a - b;
142-
} else {
143-
manualDelta = b - a;
144-
}
134+
uint256 manualDelta = a > b ? a - b : b - a;
145135

146136
uint256 manualPercentDelta = manualDelta * 1e18 / b;
147137
uint256 percentDelta = stdMath.percentDelta(a, b);

test/StdStorage.t.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/StdStorage.sol";
5-
import "../src/Test.sol";
4+
import {stdStorage, StdStorage} from "../src/StdStorage.sol";
5+
import {Test} from "../src/Test.sol";
66

77
contract StdStorageTest is Test {
88
using stdStorage for StdStorage;
@@ -230,7 +230,7 @@ contract StdStorageTest is Test {
230230
assertEq(val, true);
231231
}
232232

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

257-
function testFuzzPacked(uint256 val, uint8 elemToGet) public {
257+
function testFuzz_Packed(uint256 val, uint8 elemToGet) public {
258258
// This function tries an assortment of packed slots, shifts meaning number of elements
259259
// 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.
260260
// Combined, these determine how a slot is packed. Making it random is too hard to avoid global rejection limit
@@ -296,7 +296,7 @@ contract StdStorageTest is Test {
296296
}
297297
}
298298

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

test/StdStyle.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/Test.sol";
4+
import {Test, console2, StdStyle} from "../src/Test.sol";
55

66
contract StdStyleTest is Test {
77
function test_StyleColor() public pure {

test/StdToml.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.0 <0.9.0;
33

4-
import "../src/Test.sol";
4+
import {Test, stdToml} from "../src/Test.sol";
55

66
contract StdTomlTest is Test {
77
using stdToml for string;

0 commit comments

Comments
 (0)