@@ -22,6 +22,10 @@ function contentToDiffLine(key, value) {
2222 return `"${ key } ": "${ value } ",` ;
2323}
2424
25+ function shouldIgnoreField ( fieldName ) {
26+ return fieldName === "query" || fieldName === "correction" ;
27+ }
28+
2529// This function is only called when no matching result was found and therefore will only display
2630// the diff between the two items.
2731function betterLookingDiff ( entry , data ) {
@@ -135,6 +139,9 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
135139 } else if ( expected !== null && typeof expected !== "undefined" &&
136140 expected . constructor == Object ) { // eslint-disable-line eqeqeq
137141 for ( const key in expected ) {
142+ if ( shouldIgnoreField ( key ) ) {
143+ continue ;
144+ }
138145 if ( ! Object . prototype . hasOwnProperty . call ( expected , key ) ) {
139146 continue ;
140147 }
@@ -184,6 +191,9 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
184191 const error_text = [ ] ;
185192
186193 for ( const key in expected ) {
194+ if ( shouldIgnoreField ( key ) ) {
195+ continue ;
196+ }
187197 if ( ! Object . prototype . hasOwnProperty . call ( expected , key ) ) {
188198 continue ;
189199 }
@@ -260,84 +270,83 @@ function checkResult(error_text, loadedFile, displaySuccess) {
260270 return 1 ;
261271}
262272
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 ;
276287 }
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 ) ) {
280298 return 1 ;
281299 }
282300 }
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 ;
289303 }
304+ console . log ( "OK" ) ;
290305 return 0 ;
291306}
292307
308+ function hasCheck ( content , checkName ) {
309+ return content . startsWith ( `const ${ checkName } ` ) || content . includes ( `\nconst ${ checkName } ` ) ;
310+ }
311+
293312function runChecks ( testFile , doSearch , parseQuery , getCorrections ) {
294313 let checkExpected = false ;
295314 let checkParsed = false ;
296- let checkCorrections = false ;
297- let testFileContent = readFile ( testFile ) + "exports.QUERY = QUERY;" ;
315+ let testFileContent = readFile ( testFile ) ;
298316
299317 if ( testFileContent . indexOf ( "FILTER_CRATE" ) !== - 1 ) {
300318 testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;" ;
301319 } else {
302320 testFileContent += "exports.FILTER_CRATE = null;" ;
303321 }
304322
305- if ( testFileContent . indexOf ( "\nconst EXPECTED") !== - 1 ) {
323+ if ( hasCheck ( testFileContent , " EXPECTED") ) {
306324 testFileContent += "exports.EXPECTED = EXPECTED;" ;
307325 checkExpected = true ;
308326 }
309- if ( testFileContent . indexOf ( "\nconst PARSED") !== - 1 ) {
327+ if ( hasCheck ( testFileContent , " PARSED") ) {
310328 testFileContent += "exports.PARSED = PARSED;" ;
311329 checkParsed = true ;
312330 }
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 ) {
318332 console . log ( "FAILED" ) ;
319- console . log ( "==> At least `PARSED`, `EXPECTED`, or `CORRECTIONS ` is needed!" ) ;
333+ console . log ( "==> At least `PARSED` or `EXPECTED ` is needed!" ) ;
320334 return 1 ;
321335 }
322336
323337 const loadedFile = loadContent ( testFileContent ) ;
324338 let res = 0 ;
325339
326340 if ( checkExpected ) {
327- res += runCheck ( loadedFile , "EXPECTED" , ( query , expected , text ) => {
341+ res += runCheck ( loadedFile , "EXPECTED" , getCorrections , ( query , expected , text ) => {
328342 return runSearch ( query , expected , doSearch , loadedFile , text ) ;
329343 } ) ;
330344 }
331345 if ( checkParsed ) {
332- res += runCheck ( loadedFile , "PARSED" , ( query , expected , text ) => {
346+ res += runCheck ( loadedFile , "PARSED" , getCorrections , ( query , expected , text ) => {
333347 return runParser ( query , expected , parseQuery , text ) ;
334348 } ) ;
335349 }
336- if ( checkCorrections ) {
337- res += runCheck ( loadedFile , "CORRECTIONS" , ( query , expected ) => {
338- return runCorrections ( query , expected , getCorrections , loadedFile ) ;
339- } ) ;
340- }
341350 return res ;
342351}
343352
@@ -367,8 +376,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
367376 } ,
368377 getCorrections : function ( queryStr , filterCrate , currentCrate ) {
369378 const parsedQuery = searchModule . parseQuery ( queryStr ) ;
370- searchModule . execQuery ( parsedQuery , searchWords ,
371- filterCrate , currentCrate ) ;
379+ searchModule . execQuery ( parsedQuery , searchWords , filterCrate , currentCrate ) ;
372380 return parsedQuery . correction ;
373381 } ,
374382 parseQuery : searchModule . parseQuery ,
0 commit comments