From 3e02023d9fe4cd0f1a8f0f69702a8d85c7c4a8dc Mon Sep 17 00:00:00 2001 From: pivotal Date: Mon, 1 Dec 2008 14:24:13 -0800 Subject: [PATCH] dwf/rva: async working with .waits() syntax; simple case --- jasmine.iws | 20 ++++++++-------- lib/jasmine.js | 57 +++++++++++++++++++++++++++++++++++--------- test/bootstrap.html | 3 ++- test/bootstrap.js | 58 ++++++++++++++++++++++++--------------------- test/test.css | 4 ---- 5 files changed, 89 insertions(+), 53 deletions(-) diff --git a/jasmine.iws b/jasmine.iws index 646707e..15e0c66 100644 --- a/jasmine.iws +++ b/jasmine.iws @@ -83,7 +83,7 @@ - + @@ -92,7 +92,7 @@ - + @@ -101,7 +101,7 @@ - + @@ -110,7 +110,7 @@ - + @@ -515,9 +515,9 @@ - + - + @@ -527,21 +527,21 @@ - + - + - + - + diff --git a/lib/jasmine.js b/lib/jasmine.js index b46c2cd..beea03c 100755 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -3,9 +3,10 @@ // Object.create instead of new Object if (typeof Object.create !== 'function') { Object.create = function (o) { - var F = function () {}; - F.prototype = o; - return new F(); + var F = function () { + }; + F.prototype = o; + return new F(); }; } @@ -41,13 +42,13 @@ Matchers.method('report', function (result, failing_message) { Matchers.method('should_equal', function (expected) { return this.report((this.actual === expected), - 'Expected ' + expected + ' but got ' + this.actual + '.'); - + 'Expected ' + expected + ' but got ' + this.actual + '.'); + }); Matchers.method('should_not_equal', function (expected) { return this.report((this.actual !== expected), - 'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.'); + 'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.'); }); /* @@ -72,24 +73,58 @@ var it = function (description, func) { return that; } +var queuedFunction = function(func, timeout, spec) { + var that = { + func: func, + next: function () {spec.finish()}, + execute: function () { + if (timeout > 0) { + setTimeout(function () { + that.func(); + that.next(); + }, timeout); + } else { + that.func(); + that.next(); + } + } + } + return that; +} + var it_async = function (description) { var that = { description: description, queue: [], + currentTimeout: 0, waits: function (timeout) { + that.currentTimeout = timeout; return that; }, + resetTimeout: function() { + that.currentTimeout = 0; + }, + finish: function() { + that.done = true; + }, done: false, execute: function () { - for(i = 0; i < that.queue.length; i++) { - that.queue[i](); + if (that.queue[0]) { + that.queue[0].execute(); } } - -}; + }; var addToQueue = function(func) { - that.queue.push(func); + currentFunction = queuedFunction(func, that.currentTimeout, that); + that.queue.push(currentFunction); + if (that.previousFunction) { + that.previousFunction.next = function () { + currentFunction.execute(); + } + } + that.resetTimeout(); + that.previousFunction = currentFunction; return that; } diff --git a/test/bootstrap.html b/test/bootstrap.html index 054c6c7..8e42156 100755 --- a/test/bootstrap.html +++ b/test/bootstrap.html @@ -12,7 +12,8 @@

Running all Jasmine Test Suites

-
+
+
diff --git a/test/bootstrap.js b/test/bootstrap.js index 87bfb55..d7e60c9 100755 --- a/test/bootstrap.js +++ b/test/bootstrap.js @@ -151,46 +151,50 @@ var testAsyncSpecs = function () { runs(function () { foo++; }).then(function() { - expects_that(foo).should_equal(1) - }); - - a_spec.execute(); - - reporter.test((Jasmine.results.length === 1), - 'Spec queue did not run all functions'); - reporter.test((Jasmine.results[0].passed === true), - 'Queued expectation failed'); - - Jasmine = jasmine_init(); - foo = 0; - a_spec = it_async('spec w/ queued statments'). - runs(function () { - setTimeout(function() { - foo++ - }, 500); - }).waits(1000).then(function() { expects_that(foo).should_equal(1); }); a_spec.execute(); reporter.test((Jasmine.results.length === 1), - 'Spec queue did not run all functions'); - + 'No call to waits(): Spec queue did not run all functions'); reporter.test((Jasmine.results[0].passed === true), - 'Queued expectation failed'); + 'No call to waits(): Queued expectation failed'); + + Jasmine = jasmine_init(); + foo = 0; + a_spec = it_async('spec w/ queued statments'). + runs(function () { + setTimeout(function() { + foo++ + }, 500); + }).waits(1000). + then(function() { + expects_that(foo).should_equal(1); + }); + + a_spec.execute(); + setTimeout(function(){ + reporter.test((Jasmine.results.length === 1), + 'Calling waits(): Spec queue did not run all functions'); + + reporter.test((Jasmine.results[0].passed === true), + 'Calling waits(): Queued expectation failed'); + }, 1250); } var runTests = function () { $('spinner').show(); -// testMatchersComparisons(); - // testMatchersReporting(); - // testSpecs(); - + testMatchersComparisons(); + testMatchersReporting(); + testSpecs(); testAsyncSpecs(); - $('spinner').hide(); - reporter.summary(); + + setTimeout(function() { + $('spinner').hide(); + reporter.summary(); + }, 1500); } //it('should be an async test') { diff --git a/test/test.css b/test/test.css index 2e7b33c..421dcc6 100755 --- a/test/test.css +++ b/test/test.css @@ -3,10 +3,6 @@ body { padding-left: 40px; } -img#spinner { - display: block; -} - h1 { padding-top: 20px; font-weight: bold;