@@ -181,7 +181,7 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
181
181
for ( var i = 0 ; i < thingsToLoad . length ; ++ i ) {
182
182
var tmp = funcToCall ( fileContent , thingsToLoad [ i ] ) ;
183
183
if ( tmp === null ) {
184
- console . error ( 'unable to find ' + kindOfLoad + ' "' + thingsToLoad [ i ] + '"' ) ;
184
+ console . log ( 'unable to find ' + kindOfLoad + ' "' + thingsToLoad [ i ] + '"' ) ;
185
185
process . exit ( 1 ) ;
186
186
}
187
187
content += tmp ;
@@ -223,7 +223,8 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
223
223
searchIndex . pop ( ) ;
224
224
}
225
225
searchIndex . pop ( ) ;
226
- searchIndex = loadContent ( searchIndex . join ( "\n" ) + '\nexports.searchIndex = searchIndex;' ) ;
226
+ var fullSearchIndex = searchIndex . join ( "\n" ) + '\nexports.rawSearchIndex = searchIndex;' ;
227
+ searchIndex = loadContent ( fullSearchIndex ) ;
227
228
var finalJS = "" ;
228
229
229
230
var arraysToLoad = [ "itemTypes" ] ;
@@ -235,7 +236,7 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
235
236
// execQuery last parameter is built in buildIndex.
236
237
// buildIndex requires the hashmap from search-index.
237
238
var functionsToLoad = [ "buildHrefAndPath" , "pathSplitter" , "levenshtein" , "validateResult" ,
238
- "getQuery" , "buildIndex" , "execQuery" , "execSearch" ] ;
239
+ "handleAliases" , " getQuery", "buildIndex" , "execQuery" , "execSearch" ] ;
239
240
240
241
finalJS += 'window = { "currentCrate": "' + crate + '" };\n' ;
241
242
finalJS += 'var rootPath = "../";\n' ;
@@ -245,24 +246,19 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
245
246
finalJS += loadThings ( functionsToLoad , 'function' , extractFunction , mainJs ) ;
246
247
247
248
var loaded = loadContent ( finalJS ) ;
248
- var index = loaded . buildIndex ( searchIndex . searchIndex ) ;
249
+ var index = loaded . buildIndex ( searchIndex . rawSearchIndex ) ;
250
+ // We make it "global" so that the "loaded.execSearch" function will find it.
251
+ rawSearchIndex = searchIndex . rawSearchIndex ;
249
252
250
253
return [ loaded , index ] ;
251
254
}
252
255
253
- function runChecks ( testFile , loaded , index ) {
254
- var errors = 0 ;
255
- var loadedFile = loadContent (
256
- readFile ( testFile ) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;' ) ;
257
-
258
- const expected = loadedFile . EXPECTED ;
259
- const query = loadedFile . QUERY ;
256
+ function runSearch ( query , expected , index , loaded , loadedFile , queryName ) {
260
257
const filter_crate = loadedFile . FILTER_CRATE ;
261
258
const ignore_order = loadedFile . ignore_order ;
262
259
const exact_check = loadedFile . exact_check ;
263
- const should_fail = loadedFile . should_fail ;
264
260
265
- var results = loaded . execSearch ( loaded . getQuery ( query ) , index ) ;
261
+ var results = loaded . execSearch ( loaded . getQuery ( query ) , index , filter_crate ) ;
266
262
var error_text = [ ] ;
267
263
268
264
for ( var key in expected ) {
@@ -278,32 +274,68 @@ function runChecks(testFile, loaded, index) {
278
274
for ( var i = 0 ; i < entry . length ; ++ i ) {
279
275
var entry_pos = lookForEntry ( entry [ i ] , results [ key ] ) ;
280
276
if ( entry_pos === null ) {
281
- error_text . push ( "==> Result not found in '" + key + "': '" +
277
+ error_text . push ( queryName + "==> Result not found in '" + key + "': '" +
282
278
JSON . stringify ( entry [ i ] ) + "'" ) ;
283
279
} else if ( exact_check === true && prev_pos + 1 !== entry_pos ) {
284
- error_text . push ( "==> Exact check failed at position " + ( prev_pos + 1 ) + ": " +
285
- "expected '" + JSON . stringify ( entry [ i ] ) + "' but found '" +
280
+ error_text . push ( queryName + "==> Exact check failed at position " + ( prev_pos + 1 ) +
281
+ ": expected '" + JSON . stringify ( entry [ i ] ) + "' but found '" +
286
282
JSON . stringify ( results [ key ] [ i ] ) + "'" ) ;
287
283
} else if ( ignore_order === false && entry_pos < prev_pos ) {
288
- error_text . push ( "==> '" + JSON . stringify ( entry [ i ] ) + "' was supposed to be " +
289
- " before '" + JSON . stringify ( results [ key ] [ entry_pos ] ) + "'" ) ;
284
+ error_text . push ( queryName + "==> '" + JSON . stringify ( entry [ i ] ) + "' was supposed " +
285
+ "to be before '" + JSON . stringify ( results [ key ] [ entry_pos ] ) + "'" ) ;
290
286
} else {
291
287
prev_pos = entry_pos ;
292
288
}
293
289
}
294
290
}
295
- if ( error_text . length === 0 && should_fail === true ) {
296
- errors += 1 ;
297
- console . error ( "FAILED" ) ;
298
- console . error ( "==> Test was supposed to fail but all items were found..." ) ;
299
- } else if ( error_text . length !== 0 && should_fail === false ) {
300
- errors += 1 ;
301
- console . error ( "FAILED" ) ;
302
- console . error ( error_text . join ( "\n" ) ) ;
291
+ return error_text ;
292
+ }
293
+
294
+ function checkResult ( error_text , loadedFile , displaySuccess ) {
295
+ if ( error_text . length === 0 && loadedFile . should_fail === true ) {
296
+ console . log ( "FAILED" ) ;
297
+ console . log ( "==> Test was supposed to fail but all items were found..." ) ;
298
+ } else if ( error_text . length !== 0 && loadedFile . should_fail === false ) {
299
+ console . log ( "FAILED" ) ;
300
+ console . log ( error_text . join ( "\n" ) ) ;
303
301
} else {
302
+ if ( displaySuccess ) {
303
+ console . log ( "OK" ) ;
304
+ }
305
+ return 0 ;
306
+ }
307
+ return 1 ;
308
+ }
309
+
310
+ function runChecks ( testFile , loaded , index ) {
311
+ var loadedFile = loadContent (
312
+ readFile ( testFile ) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;' ) ;
313
+
314
+ const expected = loadedFile . EXPECTED ;
315
+ const query = loadedFile . QUERY ;
316
+
317
+ if ( Array . isArray ( query ) ) {
318
+ if ( ! Array . isArray ( expected ) ) {
319
+ console . log ( "FAILED" ) ;
320
+ console . log ( "==> If QUERY variable is an array, EXPECTED should be an array too" ) ;
321
+ return 1 ;
322
+ } else if ( query . length !== expected . length ) {
323
+ console . log ( "FAILED" ) ;
324
+ console . log ( "==> QUERY variable should have the same length as EXPECTED" ) ;
325
+ return 1 ;
326
+ }
327
+ for ( var i = 0 ; i < query . length ; ++ i ) {
328
+ var error_text = runSearch ( query [ i ] , expected [ i ] , index , loaded , loadedFile ,
329
+ "[ query `" + query [ i ] + "`]" ) ;
330
+ if ( checkResult ( error_text , loadedFile , false ) !== 0 ) {
331
+ return 1 ;
332
+ }
333
+ }
304
334
console . log ( "OK" ) ;
335
+ return 0 ;
305
336
}
306
- return errors ;
337
+ var error_text = runSearch ( query , expected , index , loaded , loadedFile , "" ) ;
338
+ return checkResult ( error_text , loadedFile , true ) ;
307
339
}
308
340
309
341
function load_files ( doc_folder , resource_suffix , crate ) {
@@ -349,25 +381,25 @@ function parseOptions(args) {
349
381
|| args [ i ] === "--crate-name" ) {
350
382
i += 1 ;
351
383
if ( i >= args . length ) {
352
- console . error ( "Missing argument after `" + args [ i - 1 ] + "` option." ) ;
384
+ console . log ( "Missing argument after `" + args [ i - 1 ] + "` option." ) ;
353
385
return null ;
354
386
}
355
387
opts [ correspondances [ args [ i - 1 ] ] ] = args [ i ] ;
356
388
} else if ( args [ i ] === "--help" ) {
357
389
showHelp ( ) ;
358
390
process . exit ( 0 ) ;
359
391
} else {
360
- console . error ( "Unknown option `" + args [ i ] + "`." ) ;
361
- console . error ( "Use `--help` to see the list of options" ) ;
392
+ console . log ( "Unknown option `" + args [ i ] + "`." ) ;
393
+ console . log ( "Use `--help` to see the list of options" ) ;
362
394
return null ;
363
395
}
364
396
}
365
397
if ( opts [ "doc_folder" ] . length < 1 ) {
366
- console . error ( "Missing `--doc-folder` option." ) ;
398
+ console . log ( "Missing `--doc-folder` option." ) ;
367
399
} else if ( opts [ "crate_name" ] . length < 1 ) {
368
- console . error ( "Missing `--crate-name` option." ) ;
400
+ console . log ( "Missing `--crate-name` option." ) ;
369
401
} else if ( opts [ "test_folder" ] . length < 1 && opts [ "test_file" ] . length < 1 ) {
370
- console . error ( "At least one of `--test-folder` or `--test-file` option is required." ) ;
402
+ console . log ( "At least one of `--test-folder` or `--test-file` option is required." ) ;
371
403
} else {
372
404
return opts ;
373
405
}
0 commit comments