Skip to content

Commit

Permalink
Merge pull request dapphub#46 from tommyrharper/add-assert-not-equal
Browse files Browse the repository at this point in the history
feat: add assert not equal functions
  • Loading branch information
d-xo authored Mar 3, 2023
2 parents a69ff6c + 429e8a0 commit e282159
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
123 changes: 123 additions & 0 deletions src/test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,99 @@ contract DSTest {
}
}

function assertNotEq(address a, address b) internal {
if (a == b) {
emit log("Error: a != b not satisfied [address]");
emit log_named_address(" Left", a);
emit log_named_address(" Right", b);
fail();
}
}
function assertNotEq(address a, address b, string memory err) internal {
if (a == b) {
emit log_named_string ("Error", err);
assertNotEq(a, b);
}
}

function assertNotEq(bytes32 a, bytes32 b) internal {
if (a == b) {
emit log("Error: a != b not satisfied [bytes32]");
emit log_named_bytes32(" Left", a);
emit log_named_bytes32(" Right", b);
fail();
}
}
function assertNotEq(bytes32 a, bytes32 b, string memory err) internal {
if (a == b) {
emit log_named_string ("Error", err);
assertNotEq(a, b);
}
}
function assertNotEq32(bytes32 a, bytes32 b) internal {
assertNotEq(a, b);
}
function assertNotEq32(bytes32 a, bytes32 b, string memory err) internal {
assertNotEq(a, b, err);
}

function assertNotEq(int a, int b) internal {
if (a == b) {
emit log("Error: a != b not satisfied [int]");
emit log_named_int(" Left", a);
emit log_named_int(" Right", b);
fail();
}
}
function assertNotEq(int a, int b, string memory err) internal {
if (a == b) {
emit log_named_string("Error", err);
assertNotEq(a, b);
}
}
function assertNotEq(uint a, uint b) internal {
if (a == b) {
emit log("Error: a != b not satisfied [uint]");
emit log_named_uint(" Left", a);
emit log_named_uint(" Right", b);
fail();
}
}
function assertNotEq(uint a, uint b, string memory err) internal {
if (a == b) {
emit log_named_string("Error", err);
assertNotEq(a, b);
}
}
function assertNotEqDecimal(int a, int b, uint decimals) internal {
if (a == b) {
emit log("Error: a != b not satisfied [decimal int]");
emit log_named_decimal_int(" Left", a, decimals);
emit log_named_decimal_int(" Right", b, decimals);
fail();
}
}
function assertNotEqDecimal(int a, int b, uint decimals, string memory err) internal {
if (a == b) {
emit log_named_string("Error", err);
assertNotEqDecimal(a, b, decimals);
}
}
function assertNotEqDecimal(uint a, uint b, uint decimals) internal {
if (a == b) {
emit log("Error: a != b not satisfied [decimal uint]");
emit log_named_decimal_uint(" Left", a, decimals);
emit log_named_decimal_uint(" Right", b, decimals);
fail();
}
}
function assertNotEqDecimal(uint a, uint b, uint decimals, string memory err) internal {
if (a == b) {
emit log_named_string("Error", err);
assertNotEqDecimal(a, b, decimals);
}
}

function assertGt(uint a, uint b) internal {
if (a <= b) {
emit log("Error: a > b not satisfied [uint]");
Expand Down Expand Up @@ -440,6 +533,21 @@ contract DSTest {
}
}

function assertNotEq(string memory a, string memory b) internal {
if (keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b))) {
emit log("Error: a != b not satisfied [string]");
emit log_named_string(" Left", a);
emit log_named_string(" Right", b);
fail();
}
}
function assertNotEq(string memory a, string memory b, string memory err) internal {
if (keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b))) {
emit log_named_string("Error", err);
assertNotEq(a, b);
}
}

function checkEq0(bytes memory a, bytes memory b) internal pure returns (bool ok) {
ok = true;
if (a.length == b.length) {
Expand All @@ -466,4 +574,19 @@ contract DSTest {
assertEq0(a, b);
}
}

function assertNotEq0(bytes memory a, bytes memory b) internal {
if (checkEq0(a, b)) {
emit log("Error: a != b not satisfied [bytes]");
emit log_named_bytes(" Left", a);
emit log_named_bytes(" Right", b);
fail();
}
}
function assertNotEq0(bytes memory a, bytes memory b, string memory err) internal {
if (checkEq0(a, b)) {
emit log_named_string("Error", err);
assertNotEq0(a, b);
}
}
}
104 changes: 104 additions & 0 deletions src/test.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,84 @@ contract DemoTest is DSTest {
assertEqDecimal(uint(1), uint(2), 18, "msg");
}

// --- assertNotEq (Addr) ---

function testAssertNotEqAddr() public {
assertNotEq(address(0x0), address(0x1), "msg");
assertNotEq(address(0x0), address(0x1));
}
function testFailAssertNotEqAddr() public {
assertNotEq(address(0x0), address(0x0));
}
function testFailAssertNotEqAddrWithMsg() public {
assertNotEq(address(0x0), address(0x0), "msg");
}

// --- assertNotEq (Bytes32) ---

function testAssertNotEqBytes32() public {
assertNotEq(bytes32("hi"), bytes32("ho"), "msg");
assertNotEq(bytes32("hi"), bytes32("ho"));
}
function testFailAssertNotEqBytes32() public {
assertNotEq(bytes32("hi"), bytes32("hi"));
}
function testFailAssertNotEqBytes32WithMsg() public {
assertNotEq(bytes32("hi"), bytes32("hi"), "msg");
}

// --- assertNotEq (Int) ---

function testAssertNotEqInt() public {
assertNotEq(-1, -2, "msg");
assertNotEq(-1, -2);
}
function testFailAssertNotEqInt() public {
assertNotEq(-1, -1);
}
function testFailAssertNotEqIntWithMsg() public {
assertNotEq(-1, -1, "msg");
}

// --- assertNotEq (UInt) ---

function testAssertNotEqUInt() public {
assertNotEq(uint(1), uint(2), "msg");
assertNotEq(uint(1), uint(2));
}
function testFailAssertNotEqUInt() public {
assertNotEq(uint(1), uint(1));
}
function testFailAssertNotEqUIntWithMsg() public {
assertNotEq(uint(1), uint(1), "msg");
}

// --- assertNotEqDecimal (Int) ---

function testAssertNotEqDecimalInt() public {
assertNotEqDecimal(-1, -2, 18, "msg");
assertNotEqDecimal(-1, -2, 18);
}
function testFailAssertNotEqDecimalInt() public {
assertNotEqDecimal(-1, -1, 18);
}
function testFailAssertNotEqDecimalIntWithMsg() public {
assertNotEqDecimal(-1, -1, 18, "msg");
}

// --- assertNotEqDecimal (UInt) ---

function testAssertNotEqDecimalUInt() public {
assertNotEqDecimal(uint(1), uint(2), 18, "msg");
assertNotEqDecimal(uint(1), uint(2), 18);
}
function testFailAssertNotEqDecimalUInt() public {
assertNotEqDecimal(uint(1), uint(1), 18);
}
function testFailAssertNotEqDecimalUIntWithMsg() public {
assertNotEqDecimal(uint(1), uint(1), 18, "msg");
}

// --- assertGt (UInt) ---

function testAssertGtUInt() public {
Expand Down Expand Up @@ -304,6 +382,32 @@ contract DemoTest is DSTest {
assertLeDecimal(-1, -2, 18, "msg");
}

// --- assertNotEq (String) ---

function testAssertNotEqString() public {
assertNotEq(new string(1), new string(2), "msg");
assertNotEq(new string(1), new string(2));
}
function testFailAssertNotEqString() public {
assertNotEq(new string(1), new string(1));
}
function testFailAssertNotEqStringWithMsg() public {
assertNotEq(new string(1), new string(1), "msg");
}

// --- assertNotEq0 (Bytes) ---

function testAssertNotEq0Bytes() public {
assertNotEq0(bytes("hi"), bytes("ho"), "msg");
assertNotEq0(bytes("hi"), bytes("ho"));
}
function testFailAssertNotEq0Bytes() public {
assertNotEq0(bytes("hi"), bytes("hi"));
}
function testFailAssertNotEq0BytesWithMsg() public {
assertNotEq0(bytes("hi"), bytes("hi"), "msg");
}

// --- fail override ---

// ensure that fail can be overridden
Expand Down

0 comments on commit e282159

Please sign in to comment.