-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAssertString.sol
271 lines (205 loc) · 7.32 KB
/
AssertString.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//SPDX-License-Identifier: MIT
pragma solidity >= 0.4.15 < 0.8.0;
library AssertString {
// Constant: STRING_NULL
// The null string: ""
string constant STRING_NULL = "";
/*
Event: TestEvent
Fired when an assertion is made.
Params:
result (bool) - Whether or not the assertion holds.
message (string) - A message to display if the assertion does not hold.
*/
event TestEvent(bool indexed result, string message);
// ************************************** strings **************************************
/*
Function: equal(string)
Assert that two strings are equal.
: _stringsEqual(A, B) == true
Params:
A (string) - The first string.
B (string) - The second string.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function equal(string memory a, string memory b, string memory message) public returns (bool result) {
result = _stringsEqual(a, b);
if (result)
_report(result, message);
else
_report(result, _appendTagged(_tag(a, "Tested"), _tag(b, "Against"), message));
}
/*
Function: notEqual(string)
Assert that two strings are not equal.
: _stringsEqual(A, B) == false
Params:
A (string) - The first string.
B (string) - The second string.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function notEqual(string memory a, string memory b, string memory message) public returns (bool result) {
result = !_stringsEqual(a, b);
if (result)
_report(result, message);
else
_report(result, _appendTagged(_tag(a, "Tested"), _tag(b, "Against"), message));
}
/*
Function: isEmpty(string)
Assert that a string is empty.
: _stringsEqual(str, STRING_NULL) == true
Params:
str (string) - The string.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function isEmpty(string memory str, string memory message) public returns (bool result) {
result = _stringsEqual(str, STRING_NULL);
if (result)
_report(result, message);
else
_report(result, _appendTagged(_tag(str, "Tested"), message));
}
/*
Function: isNotEmpty(string)
Assert that a string is not empty.
: _stringsEqual(str, STRING_NULL) == false
Params:
str (string) - The string.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function isNotEmpty(string memory str, string memory message) public returns (bool result) {
result = !_stringsEqual(str, STRING_NULL);
if (result)
_report(result, message);
else
_report(result, _appendTagged(_tag(str, "Tested"), message));
}
/******************************** internal ********************************/
/*
Function: _report
Internal function for triggering <TestEvent>.
Params:
result (bool) - The test result (true or false).
message (string) - The message that is sent if the assertion fails.
*/
function _report(bool result, string memory message) internal {
if(result)
emit TestEvent(true, "");
else
emit TestEvent(false, message);
}
/*
Function: _stringsEqual
Compares two strings. Taken from the StringUtils contract in the Ethereum Dapp-bin
(https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol).
Params:
a (string) - The first string.
b (string) - The second string.
Returns:
result (bool) - 'true' if the strings are equal, otherwise 'false'.
*/
function _stringsEqual(string memory a, string memory b) internal pure returns (bool result) {
bytes memory ba = bytes(a);
bytes memory bb = bytes(b);
if (ba.length != bb.length)
return false;
for (uint i = 0; i < ba.length; i ++) {
if (ba[i] != bb[i])
return false;
}
return true;
}
/*
Function: _tag(string)
Add a tag to a string. The 'value' and 'tag' strings are returned on the form "tag: value".
Params:
value (string) - The value.
tag (string) - The tag.
Returns:
result (string) - "tag: value"
*/
function _tag(string memory value, string memory tag) internal pure returns (string memory) {
bytes memory valueB = bytes(value);
bytes memory tagB = bytes(tag);
uint vl = valueB.length;
uint tl = tagB.length;
bytes memory newB = new bytes(vl + tl + 2);
uint i;
uint j;
for (i = 0; i < tl; i++)
newB[j++] = tagB[i];
newB[j++] = ':';
newB[j++] = ' ';
for (i = 0; i < vl; i++)
newB[j++] = valueB[i];
return string(newB);
}
/*
Function: _appendTagged(string)
Append a tagged value to a string.
Params:
tagged (string) - The tagged value.
str (string) - The string.
Returns:
result (string) - "str (tagged)"
*/
function _appendTagged(string memory tagged, string memory str) internal pure returns (string memory) {
bytes memory taggedB = bytes(tagged);
bytes memory strB = bytes(str);
uint sl = strB.length;
uint tl = taggedB.length;
bytes memory newB = new bytes(sl + tl + 3);
uint i;
uint j;
for (i = 0; i < sl; i++)
newB[j++] = strB[i];
newB[j++] = ' ';
newB[j++] = '(';
for (i = 0; i < tl; i++)
newB[j++] = taggedB[i];
newB[j++] = ')';
return string(newB);
}
/*
Function: _appendTagged(string, string)
Append two tagged values to a string.
Params:
tagged0 (string) - The first tagged value.
tagged1 (string) - The second tagged value.
str (string) - The string.
Returns:
result (string) - "str (tagged0, tagged1)"
*/
function _appendTagged(string memory tagged0, string memory tagged1, string memory str) internal pure returns (string memory) {
bytes memory tagged0B = bytes(tagged0);
bytes memory tagged1B = bytes(tagged1);
bytes memory strB = bytes(str);
uint sl = strB.length;
uint t0l = tagged0B.length;
uint t1l = tagged1B.length;
bytes memory newB = new bytes(sl + t0l + t1l + 5);
uint i;
uint j;
for (i = 0; i < sl; i++)
newB[j++] = strB[i];
newB[j++] = ' ';
newB[j++] = '(';
for (i = 0; i < t0l; i++)
newB[j++] = tagged0B[i];
newB[j++] = ',';
newB[j++] = ' ';
for (i = 0; i < t1l; i++)
newB[j++] = tagged1B[i];
newB[j++] = ')';
return string(newB);
}
}