You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
return'Expected a boolean, but received: `'+actual+'`';
96
+
}
97
+
returntrue;
98
+
}
99
+
if(expected==='<string>'||expected==='<String>'){
100
+
if(!isString(actual)){
101
+
return'Expected a string, but received: `'+actual+'`';
102
+
}
103
+
returntrue;
104
+
}
105
+
if(expected==='<number>'||expected==='<Number>'){
106
+
if(!isNumber(actual)){
107
+
return'Expected a number, but received: `'+actual+'`';
108
+
}
109
+
returntrue;
110
+
}
111
+
if(expected==='<Node>'){
112
+
if(!isObject(actual)){
113
+
return'Expected a node object, but received: `'+actual+'`';
114
+
}
115
+
returntrue;
116
+
}
117
+
returnfalse;
118
+
}
119
+
120
+
/**
121
+
* For a typed array, if the return annotation asserts deep instance equality, check whether it corresponds to the actual value; otherwise, check whether the return annotation signals the correct type.
122
+
*
123
+
* @private
124
+
* @param {*} actual - actual return value
125
+
* @param {string} expected - return value annotation
126
+
* @returns {(string|null)} error message in case the annotation and value do not match, `null` otherwise
127
+
*/
128
+
functioncheckTypedArrays(actual,expected){
129
+
varentries;
130
+
varmatch;
131
+
vartype;
132
+
vari;
133
+
134
+
match=RE_INSTANCE_ANNOTATION.exec(expected);
135
+
if(match){
136
+
type=match[1];
137
+
entries=match[2];
138
+
if(actual.constructor.name!==type){
139
+
return'Expected instance type <'+actual.constructor.name+'>, but observed <'+type+'>';
140
+
}
141
+
if(entries){
142
+
entries=JSON.parse(entries);
143
+
for(i=0;i<entries.length;i++){
144
+
if(entries[i]!==actual[i]){
145
+
return'Expected array entries ['+entries+'], but observed ['+actual+']';
146
+
}
147
+
}
148
+
}
149
+
returnnull;
150
+
}
151
+
return'Typed arrays should be documented using instance annotation';
152
+
}
153
+
78
154
79
155
// MAIN //
80
156
@@ -91,14 +167,18 @@ function formatJSON( str ) {
91
167
* // returns null
92
168
*/
93
169
functioncompareValues(actual,expected){
94
-
varparsed;
170
+
varexpectedString;
171
+
varexpectedValue;
172
+
varactualString;
173
+
varactualValue;
95
174
varparts;
96
175
vardgts;
97
176
vartype;
98
177
varmsg1;
99
178
varmsg2;
100
179
vara;
101
180
varb;
181
+
102
182
if(contains(expected,'||')){
103
183
parts=expected.split('||');
104
184
a=trim(parts[0]);
@@ -110,6 +190,14 @@ function compareValues( actual, expected ) {
110
190
}
111
191
returnnull;
112
192
}
193
+
if(contains(expected,'e.g.')){
194
+
// Early return since we cannot compare actual value to return annotation value:
195
+
returnnull;
196
+
}
197
+
msg1=checkForPlaceholders(actual,expected);
198
+
if(msg1){
199
+
return(isString(msg1)) ? msg1 : null;
200
+
}
113
201
if(expected==='NaN'){
114
202
if(!isNaN(actual)){
115
203
return'Displayed return value is `NaN`, but function returns `'+actual+'` instead';
@@ -160,13 +248,21 @@ function compareValues( actual, expected ) {
160
248
return'Displayed return value is `'+expected+'`, but function returns `'+actual+'` instead';
161
249
}
162
250
}
163
-
elseif(isObjectLike(actual)){
251
+
elseif(isTypedArray(actual)){
252
+
returncheckTypedArrays(actual,expected);
253
+
}
254
+
if(isObjectLike(actual)){
164
255
if(!contains(expected,'...')){
165
-
// Check for deep equality via `JSON.stringify`:
166
-
parsed=formatJSON(expected);
167
-
actual=JSON.stringify(actual);
168
-
if(parsed!==actual){
169
-
return'Displayed return value is `'+parsed+'`, but function returns `'+actual+'` instead';
256
+
expectedString=formatJSON(expected);
257
+
try{
258
+
expectedValue=JSON.parse(expectedString);
259
+
}catch(err){
260
+
return'Malformed return annotation. Encountered error while parsing: '+err.message;
261
+
}
262
+
actualString=JSON.stringify(actual);
263
+
actualValue=JSON.parse(actualString);
264
+
if(!deepEqual(expectedValue,actualValue)){
265
+
return'Displayed return value is `'+expectedString+'`, but function returns `'+actualString+'` instead';
0 commit comments