@@ -19,22 +19,71 @@ var errorFn = function $ERROR(err) {
1919 else $LOG ( "test262/error Error: " + err ) ;
2020} . toString ( )
2121
22+ var batchDoneFn = function $DONE ( err ) {
23+ if ( err ) { if ( typeof err === "object" && err !== null && "name" in err )
24+ $LOG ( "test262/error " + err . name + ": " + err . message )
25+ else $LOG ( "test262/error Error: " + err ) ;
26+ }
27+ $LOG ( 'test262/done' ) ;
28+ $LOG ( 'test262/test-end' ) ;
29+ if ( tests . length > 0 ) runNext ( ) ;
30+ } . toString ( )
31+
32+
2233function ConsoleRunner ( args ) {
2334 this . command = args . consoleCommand ;
2435 this . printCommand = args . consolePrintCommand || "console.log" ;
25- this . deps = [
26- doneFn ,
27- errorFn ,
28- 'function $LOG(str) { ' + this . _print ( 'str' ) + '}' ,
29- ]
36+
37+ if ( args . batch ) {
38+ // Done comes from the parent context
39+ this . deps = [
40+ errorFn ,
41+ this . logFn
42+ ]
43+ } else {
44+ this . deps = [
45+ doneFn ,
46+ errorFn ,
47+ this . logFn
48+ ]
49+ }
3050 if ( ! this . command ) throw "--consoleCommand option required for console runner" ;
3151
52+
3253 Runner . apply ( this , arguments ) ;
3354}
3455ConsoleRunner . prototype = Object . create ( Runner . prototype ) ;
3556ConsoleRunner . prototype . _print = function ( str ) {
3657 return this . printCommand + '(' + str + ');\n' ;
3758}
59+ Object . defineProperty ( ConsoleRunner . prototype , 'logFn' , {
60+ get : memoize ( function ( ) {
61+ return 'function $LOG(str) { ' + this . _print ( 'str' ) + '}' ;
62+ } )
63+ } ) ;
64+
65+ Object . defineProperty ( ConsoleRunner . prototype , 'runNextFn' , {
66+ get : memoize ( function ( ) {
67+ if ( ! this . _createEnv ) throw "Don't know how to create an environment" ;
68+ if ( ! this . _runBatched ) throw "Don't know how to run a batched tests" ;
69+
70+ var runNextFn = function runNext ( ) {
71+ var test = tests . shift ( ) ;
72+ var env = $1 ;
73+ env . $DONE = $DONE ;
74+
75+ try {
76+ $LOG ( 'test262/test-start' )
77+ $2 ;
78+ } catch ( e ) {
79+ $DONE ( e ) ;
80+ }
81+ } . toString ( ) ;
82+
83+ return runNextFn . replace ( "$1" , this . _createEnv )
84+ . replace ( "$2" , this . _runBatched )
85+ } )
86+ } ) ;
3887
3988ConsoleRunner . prototype . execute = function ( test , cb ) {
4089 var runner = this ;
@@ -58,3 +107,53 @@ ConsoleRunner.prototype.execute = function(test, cb) {
58107 } ) ;
59108}
60109
110+ ConsoleRunner . prototype . executeBatch = function ( batch , batchDone ) {
111+ var runner = this ;
112+ var scriptFile = '__tmp' + counter ++ + '.js' ;
113+ var script = this . logFn + '\n' +
114+ batchDoneFn + '\n' +
115+ this . runNextFn + '\n' ;
116+
117+ script += 'var tests = ' + JSON . stringify ( batch . map ( function ( test , i ) {
118+ return test . contents
119+ } ) ) + '\n' ;
120+
121+ script += 'runNext();'
122+
123+ fs . writeFileSync ( scriptFile , script ) ;
124+
125+ cp . exec ( this . command + " " + scriptFile , function ( err , stdout , stderr ) {
126+ var results = { log : [ ] } ;
127+ var lines = stdout . split ( / \r ? \n / ) ;
128+ var index = 0 ;
129+
130+ lines . forEach ( function ( line ) {
131+ switch ( line ) {
132+ case 'test262/test-start' :
133+ break ;
134+ case 'test262/test-end' :
135+ var test = batch [ index ++ ] ;
136+ runner . validateResult ( test , results ) ;
137+ results = { log : [ ] } ;
138+
139+ break ;
140+ default :
141+ results . log . push ( line ) ;
142+ }
143+ } )
144+
145+ //fs.unlink(scriptFile);
146+ batchDone ( ) ;
147+ } ) ;
148+ }
149+
150+
151+
152+ function memoize ( getter ) {
153+ var val = null ;
154+
155+ return function ( ) {
156+ if ( val ) return val ;
157+ return val = getter . apply ( this ) ;
158+ }
159+ }
0 commit comments