| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 var after; | 34 var after; |
| 35 var beforeEach; | 35 var beforeEach; |
| 36 var afterEach; | 36 var afterEach; |
| 37 var RunAllTests; | 37 var RunAllTests; |
| 38 | 38 |
| 39 var assert = require('assert'); | 39 var assert = require('assert'); |
| 40 | 40 |
| 41 (function() { | 41 (function() { |
| 42 var TIMEOUT = 1000; | 42 var TIMEOUT = 1000; |
| 43 | 43 |
| 44 function PostMicrotask(fn) { | |
| 45 var o = {}; | |
| 46 Object.observe(o, function() { | |
| 47 fn(); | |
| 48 }); | |
| 49 // Change something to enqueue a microtask. | |
| 50 o.x = 'hello'; | |
| 51 } | |
| 52 | |
| 53 var context = { | 44 var context = { |
| 54 beingDescribed: undefined, | 45 beingDescribed: undefined, |
| 55 currentSuiteIndex: 0, | 46 currentSuiteIndex: 0, |
| 56 suites: [] | 47 suites: [] |
| 57 }; | 48 }; |
| 58 | 49 |
| 59 function Run() { | 50 function Run() { |
| 60 function current() { | 51 function current() { |
| 61 while (context.currentSuiteIndex < context.suites.length && | 52 while (context.currentSuiteIndex < context.suites.length && |
| 62 context.suites[context.currentSuiteIndex].hasRun) { | 53 context.suites[context.currentSuiteIndex].hasRun) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 if (e instanceof Error) { | 146 if (e instanceof Error) { |
| 156 return suite.ReportError(this, e); | 147 return suite.ReportError(this, e); |
| 157 } | 148 } |
| 158 if (assert.fails.length > 0) { | 149 if (assert.fails.length > 0) { |
| 159 return suite.ReportError(this, assert.fails[0]); | 150 return suite.ReportError(this, assert.fails[0]); |
| 160 } | 151 } |
| 161 this.MarkAsDone(); | 152 this.MarkAsDone(); |
| 162 if (this.isRegular) { | 153 if (this.isRegular) { |
| 163 print('PASS: ' + suite.description + '#' + this.name); | 154 print('PASS: ' + suite.description + '#' + this.name); |
| 164 } | 155 } |
| 165 PostMicrotask(postAction); | 156 %EnqueueMicrotask(postAction); |
| 166 }.bind(this)); | 157 }.bind(this)); |
| 167 }.bind(this)); | 158 }.bind(this)); |
| 168 }.bind(this)); | 159 }.bind(this)); |
| 169 }; | 160 }; |
| 170 | 161 |
| 171 function TestSuite(described) { | 162 function TestSuite(described) { |
| 172 this.description = described.description; | 163 this.description = described.description; |
| 173 this.cases = []; | 164 this.cases = []; |
| 174 this.currentIndex = 0; | 165 this.currentIndex = 0; |
| 175 this.hasRun = false; | 166 this.hasRun = false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 187 } | 178 } |
| 188 if (described.after) { | 179 if (described.after) { |
| 189 this.cases.push(new TestCase(this.description + ' :after', | 180 this.cases.push(new TestCase(this.description + ' :after', |
| 190 undefined, described.after, undefined, false)); | 181 undefined, described.after, undefined, false)); |
| 191 } | 182 } |
| 192 } | 183 } |
| 193 | 184 |
| 194 TestSuite.prototype.Run = function() { | 185 TestSuite.prototype.Run = function() { |
| 195 this.hasRun = this.currentIndex === this.cases.length; | 186 this.hasRun = this.currentIndex === this.cases.length; |
| 196 if (this.hasRun) { | 187 if (this.hasRun) { |
| 197 PostMicrotask(Run); | 188 %EnqueueMicrotask(Run); |
| 198 return; | 189 return; |
| 199 } | 190 } |
| 200 | 191 |
| 201 // TestCase.prototype.Run cannot throw an exception. | 192 // TestCase.prototype.Run cannot throw an exception. |
| 202 this.cases[this.currentIndex].Run(this, function() { | 193 this.cases[this.currentIndex].Run(this, function() { |
| 203 ++this.currentIndex; | 194 ++this.currentIndex; |
| 204 PostMicrotask(Run); | 195 %EnqueueMicrotask(Run); |
| 205 }.bind(this)); | 196 }.bind(this)); |
| 206 }; | 197 }; |
| 207 | 198 |
| 208 TestSuite.prototype.numRegularTestCases = function() { | 199 TestSuite.prototype.numRegularTestCases = function() { |
| 209 var n = 0; | 200 var n = 0; |
| 210 for (var i = 0; i < this.cases.length; ++i) { | 201 for (var i = 0; i < this.cases.length; ++i) { |
| 211 if (this.cases[i].isRegular) { | 202 if (this.cases[i].isRegular) { |
| 212 ++n; | 203 ++n; |
| 213 } | 204 } |
| 214 } | 205 } |
| 215 return n; | 206 return n; |
| 216 } | 207 } |
| 217 | 208 |
| 218 TestSuite.prototype.ReportError = function(testCase, e) { | 209 TestSuite.prototype.ReportError = function(testCase, e) { |
| 219 if (testCase.hasDone) { | 210 if (testCase.hasDone) { |
| 220 return; | 211 return; |
| 221 } | 212 } |
| 222 testCase.MarkAsDone(); | 213 testCase.MarkAsDone(); |
| 223 this.hasRun = this.currentIndex === this.cases.length; | 214 this.hasRun = this.currentIndex === this.cases.length; |
| 224 print('FAIL: ' + this.description + '#' + testCase.name + ': ' + | 215 print('FAIL: ' + this.description + '#' + testCase.name + ': ' + |
| 225 e.name + ' (' + e.message + ')'); | 216 e.name + ' (' + e.message + ')'); |
| 226 ++this.currentIndex; | 217 ++this.currentIndex; |
| 227 PostMicrotask(Run); | 218 %EnqueueMicrotask(Run); |
| 228 }; | 219 }; |
| 229 | 220 |
| 230 describe = function(description, fn) { | 221 describe = function(description, fn) { |
| 231 var parent = context.beingDescribed; | 222 var parent = context.beingDescribed; |
| 232 var incomplete = { | 223 var incomplete = { |
| 233 cases: [], | 224 cases: [], |
| 234 description: parent ? parent.description + ' ' + description : description, | 225 description: parent ? parent.description + ' ' + description : description, |
| 235 parent: parent, | 226 parent: parent, |
| 236 }; | 227 }; |
| 237 context.beingDescribed = incomplete; | 228 context.beingDescribed = incomplete; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 255 | 246 |
| 256 beforeEach = function(fn) { | 247 beforeEach = function(fn) { |
| 257 context.beingDescribed.beforeEach = fn; | 248 context.beingDescribed.beforeEach = fn; |
| 258 } | 249 } |
| 259 | 250 |
| 260 afterEach = function(fn) { | 251 afterEach = function(fn) { |
| 261 context.beingDescribed.afterEach = fn; | 252 context.beingDescribed.afterEach = fn; |
| 262 } | 253 } |
| 263 | 254 |
| 264 }()); | 255 }()); |
| OLD | NEW |