dwf/rva: Refactored async spec logic.

This commit is contained in:
pivotal 2008-12-01 15:15:34 -08:00
parent 3e02023d9f
commit 0ccb6dfc90
3 changed files with 103 additions and 36 deletions

View File

@ -92,7 +92,7 @@
<file leaf-file-name="bootstrap.js" pinned="false" current="true" current-in-tab="true"> <file leaf-file-name="bootstrap.js" pinned="false" current="true" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/test/bootstrap.js"> <entry file="file://$PROJECT_DIR$/test/bootstrap.js">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state line="54" column="17" selection-start="1594" selection-end="1594" vertical-scroll-proportion="0.46705997"> <state line="238" column="6" selection-start="7001" selection-end="7001" vertical-scroll-proportion="0.6903164">
<folding /> <folding />
</state> </state>
</provider> </provider>
@ -110,7 +110,7 @@
<file leaf-file-name="jasmine.js" pinned="false" current="false" current-in-tab="false"> <file leaf-file-name="jasmine.js" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/lib/jasmine.js"> <entry file="file://$PROJECT_DIR$/lib/jasmine.js">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state line="100" column="0" selection-start="2241" selection-end="2241" vertical-scroll-proportion="0.53793776"> <state line="74" column="0" selection-start="1733" selection-end="1733" vertical-scroll-proportion="0.13132295">
<folding /> <folding />
</state> </state>
</provider> </provider>
@ -515,13 +515,6 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/lib/jasmine.js">
<provider selected="true" editor-type-id="text-editor">
<state line="100" column="0" selection-start="2241" selection-end="2241" vertical-scroll-proportion="0.53793776">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test/bootstrap.html"> <entry file="file://$PROJECT_DIR$/test/bootstrap.html">
<provider editor-type-id="HtmlPreview"> <provider editor-type-id="HtmlPreview">
<state /> <state />
@ -539,9 +532,16 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/lib/jasmine.js">
<provider selected="true" editor-type-id="text-editor">
<state line="74" column="0" selection-start="1733" selection-end="1733" vertical-scroll-proportion="0.13132295">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test/bootstrap.js"> <entry file="file://$PROJECT_DIR$/test/bootstrap.js">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state line="54" column="17" selection-start="1594" selection-end="1594" vertical-scroll-proportion="0.46705997"> <state line="238" column="6" selection-start="7001" selection-end="7001" vertical-scroll-proportion="0.6903164">
<folding /> <folding />
</state> </state>
</provider> </provider>

View File

@ -61,22 +61,24 @@ var expects_that = function (actual) {
/* /*
* Jasmine spec constructor * Jasmine spec constructor
*/ */
var it = function (description, func) { //var it = function (description, func) {
var that = { // var that = {
description: description, // description: description,
func: func, // func: func,
done: false, // done: false,
execute: function() { // execute: function() {
that.func.apply(that); // that.func.apply(that);
} // }
} // }
return that; // return that;
} //}
var queuedFunction = function(func, timeout, spec) { var queuedFunction = function(func, timeout, spec) {
var that = { var that = {
func: func, func: func,
next: function () {spec.finish()}, next: function () {
spec.finish(); // default value is to be done after one function
},
execute: function () { execute: function () {
if (timeout > 0) { if (timeout > 0) {
setTimeout(function () { setTimeout(function () {
@ -92,22 +94,26 @@ var queuedFunction = function(func, timeout, spec) {
return that; return that;
} }
var it_async = function (description) { var it = function (description) {
var that = { var that = {
description: description, description: description,
queue: [], queue: [],
currentTimeout: 0, currentTimeout: 0,
done: false,
waits: function (timeout) { waits: function (timeout) {
that.currentTimeout = timeout; that.currentTimeout = timeout;
return that; return that;
}, },
resetTimeout: function() { resetTimeout: function() {
that.currentTimeout = 0; that.currentTimeout = 0;
}, },
finish: function() { finish: function() {
that.done = true; that.done = true;
}, },
done: false,
execute: function () { execute: function () {
if (that.queue[0]) { if (that.queue[0]) {
that.queue[0].execute(); that.queue[0].execute();
@ -116,15 +122,18 @@ var it_async = function (description) {
}; };
var addToQueue = function(func) { var addToQueue = function(func) {
currentFunction = queuedFunction(func, that.currentTimeout, that); var currentFunction = queuedFunction(func, that.currentTimeout, that);
that.queue.push(currentFunction); that.queue.push(currentFunction);
if (that.previousFunction) {
that.previousFunction.next = function () { if (that.queue.length > 1) {
var previousFunction = that.queue[that.queue.length - 2];
previousFunction.next = function () {
currentFunction.execute(); currentFunction.execute();
} }
} }
that.resetTimeout(); that.resetTimeout();
that.previousFunction = currentFunction;
return that; return that;
} }

72
test/bootstrap.js vendored
View File

@ -93,7 +93,7 @@ var testSpecs = function () {
"Spec did not have a description"); "Spec did not have a description");
Jasmine = jasmine_init(); Jasmine = jasmine_init();
var another_spec = it('spec with an expectation', function () { var another_spec = it('spec with an expectation').runs(function () {
var foo = 'bar'; var foo = 'bar';
expects_that(foo).should_equal('bar'); expects_that(foo).should_equal('bar');
}); });
@ -106,7 +106,7 @@ var testSpecs = function () {
"Results has a result, but it's true"); "Results has a result, but it's true");
Jasmine = jasmine_init(); Jasmine = jasmine_init();
var yet_another_spec = it('spec with failing expectation', function () { var yet_another_spec = it('spec with failing expectation').runs(function () {
var foo = 'bar'; var foo = 'bar';
expects_that(foo).should_equal('baz'); expects_that(foo).should_equal('baz');
}); });
@ -117,7 +117,7 @@ var testSpecs = function () {
"Expectation that failed, passed"); "Expectation that failed, passed");
Jasmine = jasmine_init(); Jasmine = jasmine_init();
var yet_yet_another_spec = it('spec with multiple assertions', function () { var yet_yet_another_spec = it('spec with multiple assertions').runs( function () {
var foo = 'bar'; var foo = 'bar';
var baz = 'quux'; var baz = 'quux';
@ -135,7 +135,7 @@ var testAsyncSpecs = function () {
Jasmine = jasmine_init(); Jasmine = jasmine_init();
var foo = 0; var foo = 0;
var a_spec = it_async('simple queue test'). var a_spec = it('simple queue test').
runs(function () { runs(function () {
foo++; foo++;
}).then(function() { }).then(function() {
@ -147,7 +147,7 @@ var testAsyncSpecs = function () {
Jasmine = jasmine_init(); Jasmine = jasmine_init();
foo = 0; foo = 0;
a_spec = it_async('spec w/ queued statments'). a_spec = it('spec w/ queued statments').
runs(function () { runs(function () {
foo++; foo++;
}).then(function() { }).then(function() {
@ -163,7 +163,7 @@ var testAsyncSpecs = function () {
Jasmine = jasmine_init(); Jasmine = jasmine_init();
foo = 0; foo = 0;
a_spec = it_async('spec w/ queued statments'). a_spec = it('spec w/ queued statments').
runs(function () { runs(function () {
setTimeout(function() { setTimeout(function() {
foo++ foo++
@ -181,6 +181,64 @@ var testAsyncSpecs = function () {
reporter.test((Jasmine.results[0].passed === true), reporter.test((Jasmine.results[0].passed === true),
'Calling waits(): Queued expectation failed'); 'Calling waits(): Queued expectation failed');
}, 1250); }, 1250);
setTimeout(function() {
Jasmine = jasmine_init();
var bar = 0;
var another_spec = it('spec w/ queued statments').
runs(function () {
setTimeout(function() {
bar++;
}, 250);
}).
waits(500).
then(function () {
setTimeout(function() {
bar++;
}, 250);
}).
waits(1500).
then(function() {
expects_that(bar).should_equal(2);
});
another_spec.execute();
setTimeout(function(){
reporter.test((another_spec.queue.length === 3),
'Calling 2 waits(): Spec queue was less than expected length');
reporter.test((Jasmine.results.length === 1),
'Calling 2 waits(): Spec queue did not run all functions');
reporter.test((Jasmine.results[0].passed === true),
'Calling 2 waits(): Queued expectation failed');
}, 2500);
}, 1500);
setTimeout(function() {
Jasmine = jasmine_init();
var baz = 0;
var yet_another_spec = it('spec w/ async fail').
runs(function () {
setTimeout(function() {
baz++;
}, 250);
}).
waits(100).
then(function() {
expects_that(baz).should_equal(1);
});
yet_another_spec.execute();
setTimeout(function(){
reporter.test((yet_another_spec.queue.length === 2),
'Calling 2 waits(): Spec queue was less than expected length');
reporter.test((Jasmine.results.length === 1),
'Calling 2 waits(): Spec queue did not run all functions');
reporter.test((Jasmine.results[0].passed === false),
'Calling 2 waits(): Queued expectation failed');
}, 2500);
}, 5000);
} }
var runTests = function () { var runTests = function () {
@ -194,7 +252,7 @@ var runTests = function () {
setTimeout(function() { setTimeout(function() {
$('spinner').hide(); $('spinner').hide();
reporter.summary(); reporter.summary();
}, 1500); }, 10000);
} }
//it('should be an async test') { //it('should be an async test') {