AssertBalance.sol
1 //SPDX-License-Identifier: MIT 2 pragma solidity >= 0.4.15 < 0.9.0; 3 4 library AssertBalance { 5 6 /* 7 Event: TestEvent 8 9 Fired when an assertion is made. 10 11 Params: 12 result (bool) - Whether or not the assertion holds. 13 message (string) - A message to display if the assertion does not hold. 14 */ 15 event TestEvent(bool indexed result, string message); 16 17 // ************************************** balances ************************************** 18 19 /* 20 Function: balanceEqual 21 22 Assert that the balance of an account 'A' is equal to a given number 'b'. 23 24 : A.balance = b 25 26 Params: 27 A (address) - The first address. 28 b (uint) - The balance. 29 message (string) - A message that is sent if the assertion fails. 30 31 Returns: 32 result (bool) - The result. 33 */ 34 function balanceEqual(address a, uint b, string memory message) public returns (bool result) { 35 result = (a.balance == b); 36 _report(result, message); 37 } 38 39 /* 40 Function: balanceNotEqual 41 42 Assert that the balance of an account 'A' is not equal to a given number 'b'. 43 44 : A.balance != b 45 46 Params: 47 A (address) - The first address. 48 b (uint) - The balance. 49 message (string) - A message that is sent if the assertion fails. 50 51 Returns: 52 result (bool) - The result. 53 */ 54 function balanceNotEqual(address a, uint b, string memory message) public returns (bool result) { 55 result = (a.balance != b); 56 _report(result, message); 57 } 58 59 /* 60 Function: balanceIsZero 61 62 Assert that the balance of an account 'A' is zero. 63 64 : A.balance == 0 65 66 Params: 67 A (address) - The first address. 68 message (string) - A message that is sent if the assertion fails. 69 70 Returns: 71 result (bool) - The result. 72 */ 73 function balanceIsZero(address a, string memory message) public returns (bool result) { 74 result = (a.balance == 0); 75 _report(result, message); 76 } 77 78 /* 79 Function: balanceIsNotZero 80 81 Assert that the balance of an account 'A' is not zero. 82 83 : A.balance != 0 84 85 Params: 86 A (address) - The first address. 87 message (string) - A message that is sent if the assertion fails. 88 89 Returns: 90 result (bool) - The result. 91 */ 92 function balanceIsNotZero(address a, string memory message) public returns (bool result) { 93 result = (a.balance != 0); 94 _report(result, message); 95 } 96 97 /******************************** internal ********************************/ 98 99 /* 100 Function: _report 101 102 Internal function for triggering <TestEvent>. 103 104 Params: 105 result (bool) - The test result (true or false). 106 message (string) - The message that is sent if the assertion fails. 107 */ 108 function _report(bool result, string memory message) internal { 109 if(result) 110 emit TestEvent(true, ""); 111 else 112 emit TestEvent(false, message); 113 } 114 115 }