/ lib / truffle / src / AssertBool.sol
AssertBool.sol
  1  //SPDX-License-Identifier: MIT
  2  pragma solidity >= 0.4.15 < 0.9.0;
  3  
  4  library AssertBool {
  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      // ************************************** bool **************************************
 18  
 19      /*
 20          Function: isTrue
 21  
 22          Assert that a boolean is 'true'.
 23  
 24          : b == true
 25  
 26          Params:
 27              b (bool) - The boolean.
 28              message (string) - A message that is sent if the assertion fails.
 29  
 30          Returns:
 31              result (bool) - The result.
 32      */
 33      function isTrue(bool b, string memory message) public returns (bool result) {
 34          result = b;
 35          _report(result, message);
 36      }
 37  
 38      /*
 39          Function: isFalse
 40  
 41          Assert that a boolean is 'false'.
 42  
 43          : b == false
 44  
 45          Params:
 46              b (bool) - The boolean.
 47              message (string) - A message that is sent if the assertion fails.
 48  
 49          Returns:
 50              result (bool) - The result.
 51      */
 52      function isFalse(bool b, string memory message) public returns (bool result) {
 53          result = !b;
 54          _report(result, message);
 55      }
 56  
 57      /*
 58          Function: equal(bool)
 59  
 60          Assert that two booleans are equal.
 61  
 62          : A == B
 63  
 64          Params:
 65              A (bool) - The first boolean.
 66              B (bool) - The second boolean.
 67              message (string) - A message that is sent if the assertion fails.
 68  
 69          Returns:
 70              result (bool) - The result.
 71      */
 72      function equal(bool a, bool b, string memory message) public returns (bool result) {
 73          result = (a == b);
 74          if (result)
 75              _report(result, message);
 76          else
 77              _report(result, _appendTagged(_tag(a, "Tested"), _tag(b, "Against"), message));
 78      }
 79  
 80      /*
 81          Function: notEqual(bool)
 82  
 83          Assert that two booleans are not equal.
 84  
 85          : A != B
 86  
 87          Params:
 88              A (bool) - The first boolean.
 89              B (bool) - The second boolean.
 90              message (string) - A message that is sent if the assertion fails.
 91  
 92          Returns:
 93              result (bool) - The result.
 94      */
 95      function notEqual(bool a, bool b, string memory message) public returns (bool result) {
 96          result = (a != b);
 97          if (result)
 98              _report(result, message);
 99          else
100              _report(result, _appendTagged(_tag(a, "Tested"), _tag(b, "Against"), message));
101      }
102  
103      /******************************** internal ********************************/
104  
105          /*
106              Function: _report
107  
108              Internal function for triggering <TestEvent>.
109  
110              Params:
111                  result (bool) - The test result (true or false).
112                  message (string) - The message that is sent if the assertion fails.
113          */
114      function _report(bool result, string memory message) internal {
115          if(result)
116              emit TestEvent(true, "");
117          else
118              emit TestEvent(false, message);
119      }
120  
121      /*
122          Function: _ltoa
123  
124          Convert an boolean to a string.
125  
126          Params:
127              val (bool) - The boolean.
128  
129          Returns:
130              result (string) - "true" if true, "false" if false.
131      */
132      function _ltoa(bool val) internal pure returns (string memory) {
133          bytes memory b;
134          if (val) {
135              b = new bytes(4);
136              b[0] = 't';
137              b[1] = 'r';
138              b[2] = 'u';
139              b[3] = 'e';
140              return string(b);
141          }
142          else {
143              b = new bytes(5);
144              b[0] = 'f';
145              b[1] = 'a';
146              b[2] = 'l';
147              b[3] = 's';
148              b[4] = 'e';
149              return string(b);
150          }
151      }
152  
153      /*
154      function htoa(address addr) constant returns (string) {
155          bytes memory bts = new bytes(40);
156          bytes20 addrBts = bytes20(addr);
157          for (uint i = 0; i < 20; i++) {
158              bts[2*i] = addrBts[i] % 16;
159              bts[2*i + 1] = (addrBts[i] / 16) % 16;
160          }
161          return string(bts);
162      }
163      */
164  
165      /*
166          Function: _tag(string)
167  
168          Add a tag to a string. The 'value' and 'tag' strings are returned on the form "tag: value".
169  
170          Params:
171              value (string) - The value.
172              tag (string) - The tag.
173  
174          Returns:
175              result (string) - "tag: value"
176      */
177      function _tag(string memory value, string memory tag) internal pure returns (string memory) {
178  
179          bytes memory valueB = bytes(value);
180          bytes memory tagB = bytes(tag);
181  
182          uint vl = valueB.length;
183          uint tl = tagB.length;
184  
185          bytes memory newB = new bytes(vl + tl + 2);
186  
187          uint i;
188          uint j;
189  
190          for (i = 0; i < tl; i++)
191              newB[j++] = tagB[i];
192          newB[j++] = ':';
193          newB[j++] = ' ';
194          for (i = 0; i < vl; i++)
195              newB[j++] = valueB[i];
196  
197          return string(newB);
198      }
199  
200      /*
201          Function: _tag(bool)
202  
203          Add a tag to a boolean.
204  
205          Params:
206              value (bool) - The value.
207              tag (string) - The tag.
208  
209          Returns:
210              result (string) - "tag: _ltoa(value)"
211      */
212      function _tag(bool value, string memory tag) internal pure returns (string memory) {
213          string memory nstr = _ltoa(value);
214          return _tag(nstr, tag);
215      }
216  
217      /*
218          Function: _appendTagged(string, string)
219  
220          Append two tagged values to a string.
221  
222          Params:
223              tagged0 (string) - The first tagged value.
224              tagged1 (string) - The second tagged value.
225              str (string) - The string.
226  
227          Returns:
228              result (string) - "str (tagged0, tagged1)"
229      */
230      function _appendTagged(string memory tagged0, string memory tagged1, string memory str) internal pure returns (string memory) {
231  
232          bytes memory tagged0B = bytes(tagged0);
233          bytes memory tagged1B = bytes(tagged1);
234          bytes memory strB = bytes(str);
235  
236          uint sl = strB.length;
237          uint t0l = tagged0B.length;
238          uint t1l = tagged1B.length;
239  
240          bytes memory newB = new bytes(sl + t0l + t1l + 5);
241  
242          uint i;
243          uint j;
244  
245          for (i = 0; i < sl; i++)
246              newB[j++] = strB[i];
247          newB[j++] = ' ';
248          newB[j++] = '(';
249          for (i = 0; i < t0l; i++)
250              newB[j++] = tagged0B[i];
251          newB[j++] = ',';
252          newB[j++] = ' ';
253          for (i = 0; i < t1l; i++)
254              newB[j++] = tagged1B[i];
255          newB[j++] = ')';
256  
257          return string(newB);
258      }
259  
260  }