Skip to content

Commit

Permalink
Modularize forge-std (#126)
Browse files Browse the repository at this point in the history
* refactor: unbundle cheats from assertions

refactor: new category StdUtils
refactor: unbundle Test from Script

* Rename "vm" to "vm_cheats" in "Cheats.sol"

Mark "vm_cheats" as "private"
Instantiate a "vm" in "Test.sol"

* refactor: remove deprecated "lowLevelError"

refactor: rename "vm_cheats" to just "vm"
refactor: rename "vm_std_store" to just "vm"
refactor: delete "INT256_MAX" and "UINT256_MAX"
revert: redeclare "stdstore" in "Test"

* refactor: move "stdErrors" to "Errors.sol"

refactor: move "stdMath" to "Math.sol"

* Add note about versions in "Errors.sol|

Co-authored-by: Zero Ekkusu <94782988+ZeroEkkusu@users.noreply.github.com>

* chore: delete stale delineators in Errors and Math

chore: delete stale "using stdStorage for StdStorage"

* refactor: modularize assertions and utils

docs: add NatSpec tag @dev in "console2"
refactor: delete log from "bound" function
refactor: move "addressFromLast20Bytes" to "Utils.sol"
refactor: move "bound" to "Utils.sol"
refactor: move "computeCreateAddress" to "Utils.sol"
style: move brackets on same line with "if" and "else" in "bound"

* Log bound result with static call to `console.log`

Co-authored-by: Zero Ekkusu <94782988+ZeroEkkusu@users.noreply.github.com>

* fix: reintroduce "vm" in "Script.sol"

chore: silence compiler warning in "bound"
refactor: define console2.log address as constant in "Utils.sol"

* test: move "testGenerateCorrectAddress" to "StdUtils.t.sol"

* Nit: remove unnecessary "bytes20" casts

* style: add white-spaces in "deal"

* fix: readd "deployCode" functions with "val"

* Add "computeCreate2Address" utility

Rename "testGenerateCorrectAddress" to "testGenerateCreateAddress"

* refactor: use "console2" in "Utils.sol"

* style: end lines and white spaces

* test: drop pragma to ">=0.8.0" in "StdError.t.sol"

chore: remove comment about "v0.8.10" in "Errors.sol"

* refactor: define "vm" and "stdStorage" in "TestBase"

feat: add "Components.sol" file which re-exports everything

* fix: inherit from DSTest in Test

* feat: ScriptBase

refactor: delete "TestBase.sol"
refactor: move TestBase in "Test.sol"

* ♻️ Make assertions virtual

* ♻️ Make deployCode virtual

* ✨ (Components) Export consoles

* ♻️ (Script) Import Vm

* ♻️ Import from Components

* ♻️ Make bound view

Co-authored-by: Zero Ekkusu <94782988+ZeroEkkusu@users.noreply.github.com>
  • Loading branch information
PaulRBerg and ZeroEkkusu authored Jul 26, 2022
1 parent 27e14b7 commit 9d323e3
Show file tree
Hide file tree
Showing 17 changed files with 883 additions and 868 deletions.
232 changes: 232 additions & 0 deletions src/Assertions.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;

import "ds-test/test.sol";
import "./Math.sol";

contract Assertions is DSTest {
event log_array(uint256[] val);
event log_array(int256[] val);
event log_array(address[] val);
event log_named_array(string key, uint256[] val);
event log_named_array(string key, int256[] val);
event log_named_array(string key, address[] val);

function fail(string memory err) internal virtual {
emit log_named_string("Error", err);
fail();
}

function assertFalse(bool data) internal virtual {
assertTrue(!data);
}

function assertFalse(bool data, string memory err) internal virtual {
assertTrue(!data, err);
}

function assertEq(bool a, bool b) internal virtual {
if (a != b) {
emit log ("Error: a == b not satisfied [bool]");
emit log_named_string (" Expected", b ? "true" : "false");
emit log_named_string (" Actual", a ? "true" : "false");
fail();
}
}

function assertEq(bool a, bool b, string memory err) internal virtual {
if (a != b) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}

function assertEq(bytes memory a, bytes memory b) internal virtual {
assertEq0(a, b);
}

function assertEq(bytes memory a, bytes memory b, string memory err) internal virtual {
assertEq0(a, b, err);
}

function assertEq(uint256[] memory a, uint256[] memory b) internal virtual {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log("Error: a == b not satisfied [uint[]]");
emit log_named_array(" Expected", b);
emit log_named_array(" Actual", a);
fail();
}
}

function assertEq(int256[] memory a, int256[] memory b) internal virtual {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log("Error: a == b not satisfied [int[]]");
emit log_named_array(" Expected", b);
emit log_named_array(" Actual", a);
fail();
}
}

function assertEq(address[] memory a, address[] memory b) internal virtual {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log("Error: a == b not satisfied [address[]]");
emit log_named_array(" Expected", b);
emit log_named_array(" Actual", a);
fail();
}
}

function assertEq(uint256[] memory a, uint256[] memory b, string memory err) internal virtual {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}

function assertEq(int256[] memory a, int256[] memory b, string memory err) internal virtual {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}


function assertEq(address[] memory a, address[] memory b, string memory err) internal virtual {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}

function assertApproxEqAbs(
uint256 a,
uint256 b,
uint256 maxDelta
) internal virtual {
uint256 delta = stdMath.delta(a, b);

if (delta > maxDelta) {
emit log ("Error: a ~= b not satisfied [uint]");
emit log_named_uint (" Expected", b);
emit log_named_uint (" Actual", a);
emit log_named_uint (" Max Delta", maxDelta);
emit log_named_uint (" Delta", delta);
fail();
}
}

function assertApproxEqAbs(
uint256 a,
uint256 b,
uint256 maxDelta,
string memory err
) internal virtual {
uint256 delta = stdMath.delta(a, b);

if (delta > maxDelta) {
emit log_named_string ("Error", err);
assertApproxEqAbs(a, b, maxDelta);
}
}

function assertApproxEqAbs(
int256 a,
int256 b,
uint256 maxDelta
) internal virtual {
uint256 delta = stdMath.delta(a, b);

if (delta > maxDelta) {
emit log ("Error: a ~= b not satisfied [int]");
emit log_named_int (" Expected", b);
emit log_named_int (" Actual", a);
emit log_named_uint (" Max Delta", maxDelta);
emit log_named_uint (" Delta", delta);
fail();
}
}

function assertApproxEqAbs(
int256 a,
int256 b,
uint256 maxDelta,
string memory err
) internal virtual {
uint256 delta = stdMath.delta(a, b);

if (delta > maxDelta) {
emit log_named_string ("Error", err);
assertApproxEqAbs(a, b, maxDelta);
}
}

function assertApproxEqRel(
uint256 a,
uint256 b,
uint256 maxPercentDelta // An 18 decimal fixed point number, where 1e18 == 100%
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.

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

if (percentDelta > maxPercentDelta) {
emit log ("Error: a ~= b not satisfied [uint]");
emit log_named_uint (" Expected", b);
emit log_named_uint (" Actual", a);
emit log_named_decimal_uint (" Max % Delta", maxPercentDelta, 18);
emit log_named_decimal_uint (" % Delta", percentDelta, 18);
fail();
}
}

function assertApproxEqRel(
uint256 a,
uint256 b,
uint256 maxPercentDelta, // An 18 decimal fixed point number, where 1e18 == 100%
string memory err
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.

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

if (percentDelta > maxPercentDelta) {
emit log_named_string ("Error", err);
assertApproxEqRel(a, b, maxPercentDelta);
}
}

function assertApproxEqRel(
int256 a,
int256 b,
uint256 maxPercentDelta
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.

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

if (percentDelta > maxPercentDelta) {
emit log ("Error: a ~= b not satisfied [int]");
emit log_named_int (" Expected", b);
emit log_named_int (" Actual", a);
emit log_named_decimal_uint(" Max % Delta", maxPercentDelta, 18);
emit log_named_decimal_uint(" % Delta", percentDelta, 18);
fail();
}
}

function assertApproxEqRel(
int256 a,
int256 b,
uint256 maxPercentDelta,
string memory err
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.

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

if (percentDelta > maxPercentDelta) {
emit log_named_string ("Error", err);
assertApproxEqRel(a, b, maxPercentDelta);
}
}
}
Loading

0 comments on commit 9d323e3

Please sign in to comment.