1+ var page = require ( 'webpage' ) . create ( ) ;
2+ var fs = require ( 'fs' ) ;
3+ var Resurrect = require ( './lib/resurrect' ) ;
4+ var compare = require ( 'fast-json-patch' ) . compare ;
5+ var system = require ( 'system' ) ;
6+
7+ var demo ,
8+ frames = 10 ,
9+ testUrl = 'http://localhost:9000/demo/dev.html' ,
10+ refsPath = 'test/browser/refs' ,
11+ diffsPath = 'test/browser/diffs' ;
12+
13+ var update = arg ( '--update' ) ,
14+ updateAll = typeof arg ( '--updateAll' ) !== 'undefined' ,
15+ diff = arg ( '--diff' ) ;
16+
17+ var resurrect = new Resurrect ( { cleanup : true } ) ,
18+ created = [ ] ,
19+ changed = [ ] ;
20+
21+ var test = function ( status ) {
22+ if ( status === 'fail' ) {
23+ console . log ( 'failed to load' , testUrl ) ;
24+ console . log ( 'check dev server is running!' ) ;
25+ console . log ( 'use `grunt dev`' ) ;
26+ phantom . exit ( 1 ) ;
27+ return ;
28+ }
29+
30+ var demos = page . evaluate ( function ( ) {
31+ var demoSelect = document . getElementById ( 'demo-select' ) ,
32+ options = Array . prototype . slice . call ( demoSelect ) ;
33+ return options . map ( function ( o ) { return o . value ; } ) ;
34+ } ) ;
35+
36+ fs . removeTree ( diffsPath ) ;
37+
38+ if ( diff ) {
39+ fs . makeDirectory ( diffsPath ) ;
40+ }
41+
42+ for ( var i = 0 ; i < demos . length ; i += 1 ) {
43+ demo = demos [ i ] ;
44+
45+ var hasChanged = false ,
46+ hasCreated = false ,
47+ forceUpdate = update === demo || updateAll ,
48+ worldStartPath = refsPath + '/' + demo + '/' + demo + '-0.json' ,
49+ worldEndPath = refsPath + '/' + demo + '/' + demo + '-' + frames + '.json' ,
50+ worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json' ,
51+ worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json' ;
52+
53+ var worldStart = page . evaluate ( function ( demo ) {
54+ var engine = Matter . Demo . _engine ;
55+ Matter . Runner . stop ( engine ) ;
56+ if ( ! ( demo in Matter . Demo ) ) {
57+ throw '\'' + demo + '\' is not defined in Matter.Demo' ;
58+ }
59+ Matter . Demo [ demo ] ( ) ;
60+ return engine . world ;
61+ } , demo ) ;
62+
63+ var worldEnd = page . evaluate ( function ( demo , frames ) {
64+ var engine = Matter . Demo . _engine ;
65+
66+ for ( var j = 0 ; j <= frames ; j += 1 ) {
67+ Matter . Events . trigger ( engine , 'tick' , { timestamp : engine . timing . timestamp } ) ;
68+ Matter . Engine . update ( engine , engine . timing . delta ) ;
69+ Matter . Events . trigger ( engine , 'afterTick' , { timestamp : engine . timing . timestamp } ) ;
70+ }
71+
72+ return engine . world ;
73+ } , demo , frames ) ;
74+
75+ if ( fs . exists ( worldStartPath ) ) {
76+ var worldStartRef = resurrect . resurrect ( fs . read ( worldStartPath ) ) ;
77+ var worldStartDiff = compare ( worldStartRef , worldStart ) ;
78+
79+ if ( worldStartDiff . length !== 0 ) {
80+ if ( diff ) {
81+ fs . write ( worldStartDiffPath , JSON . stringify ( worldStartDiff , null , 2 ) , 'w' ) ;
82+ }
83+
84+ if ( forceUpdate ) {
85+ hasCreated = true ;
86+ fs . write ( worldStartPath , resurrect . stringify ( worldStart , null , 2 ) , 'w' ) ;
87+ } else {
88+ hasChanged = true ;
89+ }
90+ }
91+ } else {
92+ hasCreated = true ;
93+ fs . write ( worldStartPath , resurrect . stringify ( worldStart , null , 2 ) , 'w' ) ;
94+ }
95+
96+ if ( fs . exists ( worldEndPath ) ) {
97+ var worldEndRef = resurrect . resurrect ( fs . read ( worldEndPath ) ) ;
98+ var worldEndDiff = compare ( worldEndRef , worldEnd ) ;
99+
100+ if ( worldEndDiff . length !== 0 ) {
101+ if ( diff ) {
102+ fs . write ( worldEndDiffPath , JSON . stringify ( worldEndDiff , null , 2 ) , 'w' ) ;
103+ }
104+
105+ if ( forceUpdate ) {
106+ hasCreated = true ;
107+ fs . write ( worldEndPath , resurrect . stringify ( worldEnd , null , 2 ) , 'w' ) ;
108+ } else {
109+ hasChanged = true ;
110+ }
111+ }
112+ } else {
113+ hasCreated = true ;
114+ fs . write ( worldEndPath , resurrect . stringify ( worldEnd , null , 2 ) , 'w' ) ;
115+ }
116+
117+ if ( hasChanged ) {
118+ changed . push ( "'" + demo + "'" ) ;
119+ system . stdout . write ( 'x' ) ;
120+ } else if ( hasCreated ) {
121+ created . push ( "'" + demo + "'" ) ;
122+ system . stdout . write ( '+' ) ;
123+ } else {
124+ system . stdout . write ( '.' ) ;
125+ }
126+ }
127+
128+ if ( created . length > 0 ) {
129+ console . log ( '\nupdated' , created . join ( ', ' ) ) ;
130+ }
131+
132+ var isOk = changed . length === 0 ? 1 : 0 ;
133+
134+ console . log ( '' ) ;
135+
136+ if ( isOk ) {
137+ console . log ( 'ok' ) ;
138+ } else {
139+ console . log ( '\nchanges detected on:' ) ;
140+ console . log ( changed . join ( ', ' ) ) ;
141+ console . log ( '\nreview, then --update [name] or --updateAll' ) ;
142+ console . log ( 'use --diff for diff log' ) ;
143+ }
144+
145+ phantom . exit ( ! isOk ) ;
146+ } ;
147+
148+ function arg ( name ) {
149+ var index = system . args . indexOf ( name ) ;
150+ if ( index >= 0 ) {
151+ return system . args [ index + 1 ] || true ;
152+ }
153+ return undefined ;
154+ }
155+
156+ page . onError = function ( msg , trace ) {
157+ setTimeout ( function ( ) {
158+ var msgStack = [ 'testing \'' + demo + '\'' , msg ] ;
159+
160+ if ( trace && trace . length ) {
161+ trace . forEach ( function ( t ) {
162+ msgStack . push ( ' -> ' + ( t . file || t . sourceURL ) + ': ' + t . line + ( t . function ? ' (fn: ' + t . function + ')' : '' ) ) ;
163+ } ) ;
164+ }
165+
166+ console . log ( msgStack . join ( '\n' ) ) ;
167+ phantom . exit ( 1 ) ;
168+ } , 0 ) ;
169+ } ;
170+
171+
172+ page . onResourceReceived = function ( res ) {
173+ setTimeout ( function ( ) {
174+ if ( res . stage === 'end'
175+ && ( res . status !== 304 && res . status !== 200 && res . status !== null ) ) {
176+ console . log ( 'error' , res . status , res . url ) ;
177+ phantom . exit ( 1 ) ;
178+ }
179+ } , 0 ) ;
180+ } ;
181+
182+ phantom . onError = page . onError ;
183+
184+ page . open ( testUrl , test ) ;
0 commit comments