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">
<entry file="file://$PROJECT_DIR$/test/bootstrap.js">
<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 />
</state>
</provider>
@ -110,7 +110,7 @@
<file leaf-file-name="jasmine.js" pinned="false" current="false" current-in-tab="false">
<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">
<state line="74" column="0" selection-start="1733" selection-end="1733" vertical-scroll-proportion="0.13132295">
<folding />
</state>
</provider>
@ -515,13 +515,6 @@
</state>
</provider>
</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">
<provider editor-type-id="HtmlPreview">
<state />
@ -539,9 +532,16 @@
</state>
</provider>
</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">
<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 />
</state>
</provider>

View File

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

72
test/bootstrap.js vendored
View File

@ -93,7 +93,7 @@ var testSpecs = function () {
"Spec did not have a description");
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';
expects_that(foo).should_equal('bar');
});
@ -106,7 +106,7 @@ var testSpecs = function () {
"Results has a result, but it's true");
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';
expects_that(foo).should_equal('baz');
});
@ -117,7 +117,7 @@ var testSpecs = function () {
"Expectation that failed, passed");
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 baz = 'quux';
@ -135,7 +135,7 @@ var testAsyncSpecs = function () {
Jasmine = jasmine_init();
var foo = 0;
var a_spec = it_async('simple queue test').
var a_spec = it('simple queue test').
runs(function () {
foo++;
}).then(function() {
@ -147,7 +147,7 @@ var testAsyncSpecs = function () {
Jasmine = jasmine_init();
foo = 0;
a_spec = it_async('spec w/ queued statments').
a_spec = it('spec w/ queued statments').
runs(function () {
foo++;
}).then(function() {
@ -163,7 +163,7 @@ var testAsyncSpecs = function () {
Jasmine = jasmine_init();
foo = 0;
a_spec = it_async('spec w/ queued statments').
a_spec = it('spec w/ queued statments').
runs(function () {
setTimeout(function() {
foo++
@ -181,6 +181,64 @@ var testAsyncSpecs = function () {
reporter.test((Jasmine.results[0].passed === true),
'Calling waits(): Queued expectation failed');
}, 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 () {
@ -194,7 +252,7 @@ var runTests = function () {
setTimeout(function() {
$('spinner').hide();
reporter.summary();
}, 1500);
}, 10000);
}
//it('should be an async test') {