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
115- function traverse ( specs , data ) {
116- Object . keys ( specs ) . forEach ( function ( key ) {
118+ function traverse ( specs , data ) {
119+ Object . keys ( specs ) . forEach ( function ( key ) {
117120 // bail on default
118121 if ( key === 'defaults' && ! path . length ) {
119122 return ;
@@ -128,14 +131,26 @@ function specsRunner(specs, reporter, callback, err, data) {
128131 path . pop ( ) ;
129132 return ;
130133 }
131-
134+ if ( Array . isArray ( data [ key ] ) && specs [ key ] . find ) {
135+ var arrData = data [ key ] ;
136+ var filteredData = arrData . find ( function ( item ) {
137+ var regex = new RegExp ( specs [ key ] . find . pattern ) ;
138+ return regex . test ( item [ specs [ key ] . find . key ] ) ;
139+ } ) ;
140+ if ( filteredData ) {
141+ traverse ( specs [ key ] . find . spec , filteredData ) ;
142+ }
143+ path . pop ( ) ;
144+ return ;
145+ }
132146 var spec = specs [ key ] ;
133147 if ( typeof spec === 'object' && ! spec . min && ! spec . max ) {
134148 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 ( ) ;
149+ path . pop ( ) ;
150+ } else if ( typeof spec === 'number' && Array . isArray ( data [ key ] ) ) {
151+ tests . push (
152+ buildTest ( path . join ( '.' ) , spec , data [ key ] . length , defaults ) ) ;
153+ path . pop ( ) ;
139154 } else {
140155 tests . push ( buildTest ( path . join ( '.' ) , spec , data [ key ] , defaults ) ) ;
141156 path . pop ( ) ;
@@ -154,19 +169,24 @@ function specsRunner(specs, reporter, callback, err, data) {
154169 }
155170
156171 if ( err ) {
157- tests . push ( { text : err . message , result : err } ) ;
172+ tests . push ( {
173+ text : err . message ,
174+ result : err
175+ } ) ;
158176 } else {
159177 defaults = specs . defaults || { } ;
160178 traverse ( specs , data . data ) ;
161179 }
162180
163181 // run mocha suite
164- var mocha = new Mocha ( { reporter : reporter } ) ;
182+ var mocha = new Mocha ( {
183+ reporter : reporter
184+ } ) ;
165185 mocha . suite . emit ( 'pre-require' , global , null , mocha ) ;
166186 buildSuite ( defaults , tests ) ;
167- mocha . run ( callback || function ( failures ) {
187+ mocha . run ( callback || function ( failures ) {
168188 process . exit ( failures ) ;
169189 } ) ;
170190}
171191
172- module . exports = specsRunner ;
192+ module . exports = specsRunner ;
0 commit comments