@@ -22,6 +22,10 @@ function contentToDiffLine(key, value) {
22
22
return `"${ key } ": "${ value } ",` ;
23
23
}
24
24
25
+ function shouldIgnoreField ( fieldName ) {
26
+ return fieldName === "query" || fieldName === "correction" ;
27
+ }
28
+
25
29
// This function is only called when no matching result was found and therefore will only display
26
30
// the diff between the two items.
27
31
function betterLookingDiff ( entry , data ) {
@@ -135,6 +139,9 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
135
139
} else if ( expected !== null && typeof expected !== "undefined" &&
136
140
expected . constructor == Object ) { // eslint-disable-line eqeqeq
137
141
for ( const key in expected ) {
142
+ if ( shouldIgnoreField ( key ) ) {
143
+ continue ;
144
+ }
138
145
if ( ! Object . prototype . hasOwnProperty . call ( expected , key ) ) {
139
146
continue ;
140
147
}
@@ -184,6 +191,9 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
184
191
const error_text = [ ] ;
185
192
186
193
for ( const key in expected ) {
194
+ if ( shouldIgnoreField ( key ) ) {
195
+ continue ;
196
+ }
187
197
if ( ! Object . prototype . hasOwnProperty . call ( expected , key ) ) {
188
198
continue ;
189
199
}
@@ -260,84 +270,83 @@ function checkResult(error_text, loadedFile, displaySuccess) {
260
270
return 1 ;
261
271
}
262
272
263
- function runCheck ( loadedFile , key , callback ) {
264
- const expected = loadedFile [ key ] ;
265
- const query = loadedFile . QUERY ;
266
-
267
- if ( Array . isArray ( query ) ) {
268
- if ( ! Array . isArray ( expected ) ) {
269
- console . log ( "FAILED" ) ;
270
- console . log ( `==> If QUERY variable is an array, ${ key } should be an array too` ) ;
271
- return 1 ;
272
- } else if ( query . length !== expected . length ) {
273
- console . log ( "FAILED" ) ;
274
- console . log ( `==> QUERY variable should have the same length as ${ key } ` ) ;
275
- return 1 ;
273
+ function runCheckInner ( callback , loadedFile , entry , getCorrections , extra ) {
274
+ if ( typeof entry . query !== "string" ) {
275
+ console . log ( "FAILED" ) ;
276
+ console . log ( "==> Missing `query` field" ) ;
277
+ return false ;
278
+ }
279
+ let error_text = callback ( entry . query , entry , extra ? "[ query `" + entry . query + "`]" : "" ) ;
280
+ if ( checkResult ( error_text , loadedFile , false ) !== 0 ) {
281
+ return false ;
282
+ }
283
+ if ( entry . correction !== undefined ) {
284
+ error_text = runCorrections ( entry . query , entry . correction , getCorrections , loadedFile ) ;
285
+ if ( checkResult ( error_text , loadedFile , false ) !== 0 ) {
286
+ return false ;
276
287
}
277
- for ( let i = 0 ; i < query . length ; ++ i ) {
278
- const error_text = callback ( query [ i ] , expected [ i ] , "[ query `" + query [ i ] + "`]" ) ;
279
- if ( checkResult ( error_text , loadedFile , false ) !== 0 ) {
288
+ }
289
+ return true ;
290
+ }
291
+
292
+ function runCheck ( loadedFile , key , getCorrections , callback ) {
293
+ const expected = loadedFile [ key ] ;
294
+
295
+ if ( Array . isArray ( expected ) ) {
296
+ for ( const entry of expected ) {
297
+ if ( ! runCheckInner ( callback , loadedFile , entry , getCorrections , true ) ) {
280
298
return 1 ;
281
299
}
282
300
}
283
- console . log ( "OK" ) ;
284
- } else {
285
- const error_text = callback ( query , expected , "" ) ;
286
- if ( checkResult ( error_text , loadedFile , true ) !== 0 ) {
287
- return 1 ;
288
- }
301
+ } else if ( ! runCheckInner ( callback , loadedFile , expected , getCorrections , false ) ) {
302
+ return 1 ;
289
303
}
304
+ console . log ( "OK" ) ;
290
305
return 0 ;
291
306
}
292
307
308
+ function hasCheck ( content , checkName ) {
309
+ return content . startsWith ( `const ${ checkName } ` ) || content . includes ( `\nconst ${ checkName } ` ) ;
310
+ }
311
+
293
312
function runChecks ( testFile , doSearch , parseQuery , getCorrections ) {
294
313
let checkExpected = false ;
295
314
let checkParsed = false ;
296
- let checkCorrections = false ;
297
- let testFileContent = readFile ( testFile ) + "exports.QUERY = QUERY;" ;
315
+ let testFileContent = readFile ( testFile ) ;
298
316
299
317
if ( testFileContent . indexOf ( "FILTER_CRATE" ) !== - 1 ) {
300
318
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;" ;
301
319
} else {
302
320
testFileContent += "exports.FILTER_CRATE = null;" ;
303
321
}
304
322
305
- if ( testFileContent . indexOf ( "\nconst EXPECTED") !== - 1 ) {
323
+ if ( hasCheck ( testFileContent , " EXPECTED") ) {
306
324
testFileContent += "exports.EXPECTED = EXPECTED;" ;
307
325
checkExpected = true ;
308
326
}
309
- if ( testFileContent . indexOf ( "\nconst PARSED") !== - 1 ) {
327
+ if ( hasCheck ( testFileContent , " PARSED") ) {
310
328
testFileContent += "exports.PARSED = PARSED;" ;
311
329
checkParsed = true ;
312
330
}
313
- if ( testFileContent . indexOf ( "\nconst CORRECTIONS" ) !== - 1 ) {
314
- testFileContent += "exports.CORRECTIONS = CORRECTIONS;" ;
315
- checkCorrections = true ;
316
- }
317
- if ( ! checkParsed && ! checkExpected && ! checkCorrections ) {
331
+ if ( ! checkParsed && ! checkExpected ) {
318
332
console . log ( "FAILED" ) ;
319
- console . log ( "==> At least `PARSED`, `EXPECTED`, or `CORRECTIONS ` is needed!" ) ;
333
+ console . log ( "==> At least `PARSED` or `EXPECTED ` is needed!" ) ;
320
334
return 1 ;
321
335
}
322
336
323
337
const loadedFile = loadContent ( testFileContent ) ;
324
338
let res = 0 ;
325
339
326
340
if ( checkExpected ) {
327
- res += runCheck ( loadedFile , "EXPECTED" , ( query , expected , text ) => {
341
+ res += runCheck ( loadedFile , "EXPECTED" , getCorrections , ( query , expected , text ) => {
328
342
return runSearch ( query , expected , doSearch , loadedFile , text ) ;
329
343
} ) ;
330
344
}
331
345
if ( checkParsed ) {
332
- res += runCheck ( loadedFile , "PARSED" , ( query , expected , text ) => {
346
+ res += runCheck ( loadedFile , "PARSED" , getCorrections , ( query , expected , text ) => {
333
347
return runParser ( query , expected , parseQuery , text ) ;
334
348
} ) ;
335
349
}
336
- if ( checkCorrections ) {
337
- res += runCheck ( loadedFile , "CORRECTIONS" , ( query , expected ) => {
338
- return runCorrections ( query , expected , getCorrections , loadedFile ) ;
339
- } ) ;
340
- }
341
350
return res ;
342
351
}
343
352
@@ -367,8 +376,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
367
376
} ,
368
377
getCorrections : function ( queryStr , filterCrate , currentCrate ) {
369
378
const parsedQuery = searchModule . parseQuery ( queryStr ) ;
370
- searchModule . execQuery ( parsedQuery , searchWords ,
371
- filterCrate , currentCrate ) ;
379
+ searchModule . execQuery ( parsedQuery , searchWords , filterCrate , currentCrate ) ;
372
380
return parsedQuery . correction ;
373
381
} ,
374
382
parseQuery : searchModule . parseQuery ,
0 commit comments