66 */
77
88var fs = require ( 'fs' ) ,
9- Mocha = require ( 'mocha' ) ,
10- assert = require ( 'assert' ) ;
9+ Mocha = require ( 'mocha' ) ,
10+ assert = require ( 'assert' ) ;
1111
1212function buildSuite ( defaults , tests ) {
13- describe ( defaults . suiteName || 'WebPageTest' , function ( ) {
14- tests . forEach ( function ( test ) {
15- it ( test . text , function ( ) {
13+ describe ( defaults . suiteName || 'WebPageTest' , function ( ) {
14+ tests . forEach ( function ( test ) {
15+ it ( test . text , function ( ) {
1616 if ( test . result instanceof Error ) {
1717 return assert . ifError ( test . result ) ;
1818 }
@@ -99,21 +99,24 @@ function buildTest(metric, spec, actual, defaults) {
9999 . replace ( '{operation}' , operation )
100100 . replace ( '{expected}' , expected ) ;
101101
102- return { text : text , result : result } ;
102+ return {
103+ text : text ,
104+ result : result
105+ } ;
103106}
104107
105108function specsRunner ( specs , reporter , callback , err , data ) {
106109 var defaults = { } ,
107- tests = [ ] ,
108- path = [ ] ;
110+ tests = [ ] ,
111+ path = [ ] ;
109112
110113 // bail if test not complete
111114 if ( ! err && ( ! data || data . statusCode !== 200 ) ) {
112115 return callback ( err , data ) ;
113116 }
114117
115118 function traverse ( specs , data ) {
116- Object . keys ( specs ) . forEach ( function ( key ) {
119+ Object . keys ( specs ) . forEach ( function ( key ) {
117120 // bail on default
118121 if ( key === 'defaults' && ! path . length ) {
119122 return ;
@@ -128,14 +131,37 @@ function specsRunner(specs, reporter, callback, err, data) {
128131 path . pop ( ) ;
129132 return ;
130133 }
131-
134+ if ( Array . isArray ( data [ key ] ) && specs [ key ] . find && specs [ key ] . find . length ) {
135+ specs [ key ] . find . forEach ( find => {
136+ if ( find . key && find . pattern && find . spec ) {
137+ var arrData = data [ key ] ;
138+ var filteredData = arrData . find ( function ( item ) {
139+ var regex = new RegExp ( find . pattern ) ;
140+ return regex . test ( item [ find . key ] ) ;
141+ } ) ;
142+ if ( filteredData ) {
143+ path . push ( find . key + "." + filteredData [ find . key ] ) ;
144+ traverse ( find . spec , filteredData ) ;
145+ path . pop ( ) ;
146+ } else if ( find . required ) {
147+ tests . push ( {
148+ text : path . join ( '.' ) + "." + find . key + ".match(" + find . pattern + ")" ,
149+ result : new Error ( 'not found' )
150+ } ) ;
151+ }
152+ }
153+ } ) ;
154+ path . pop ( ) ;
155+ return ;
156+ }
132157 var spec = specs [ key ] ;
133158 if ( typeof spec === 'object' && ! spec . min && ! spec . max ) {
134159 traverse ( spec , data [ key ] ) ;
135- } else if ( typeof spec === 'number' && Array . isArray ( data [ key ] ) ) {
136- tests . push (
137- buildTest ( path . join ( '.' ) , spec , data [ key ] . length , defaults ) ) ;
138- path . pop ( ) ;
160+ path . pop ( ) ;
161+ } else if ( typeof spec === 'number' && Array . isArray ( data [ key ] ) ) {
162+ tests . push (
163+ buildTest ( path . join ( '.' ) , spec , data [ key ] . length , defaults ) ) ;
164+ path . pop ( ) ;
139165 } else {
140166 tests . push ( buildTest ( path . join ( '.' ) , spec , data [ key ] , defaults ) ) ;
141167 path . pop ( ) ;
@@ -154,19 +180,24 @@ function specsRunner(specs, reporter, callback, err, data) {
154180 }
155181
156182 if ( err ) {
157- tests . push ( { text : err . message , result : err } ) ;
183+ tests . push ( {
184+ text : err . message ,
185+ result : err
186+ } ) ;
158187 } else {
159188 defaults = specs . defaults || { } ;
160189 traverse ( specs , data . data ) ;
161190 }
162191
163192 // run mocha suite
164- var mocha = new Mocha ( { reporter : reporter } ) ;
193+ var mocha = new Mocha ( {
194+ reporter : reporter
195+ } ) ;
165196 mocha . suite . emit ( 'pre-require' , global , null , mocha ) ;
166197 buildSuite ( defaults , tests ) ;
167- mocha . run ( callback || function ( failures ) {
198+ mocha . run ( callback || function ( failures ) {
168199 process . exit ( failures ) ;
169200 } ) ;
170201}
171202
172- module . exports = specsRunner ;
203+ module . exports = specsRunner ;
0 commit comments