Permalink
Browse files

added 'async' wrapper for describing asynchronous behavior, fixed pau…

…se/resume
  • Loading branch information...
mmonteleone committed Jun 4, 2011
1 parent d7687a8 commit 667443b61298b4ff92561d9839142c533f17f845
Showing with 94 additions and 31 deletions.
  1. +8 −0 example/example.specs.js
  2. +34 −7 pavlov.js
  3. +52 −24 spec/pavlov.specs.js
View
@@ -29,6 +29,14 @@ pavlov.specify("Pavlov Example", function(){
}).throwsException();
});
+ it("can specify asynchronous features", async(function(){
+ // an async spec implementation will pause the test runner until 'resume()'
+ setTimeout(function(){
+ assert.pass();
+ resume();
+ }, 500);
+ }));
+
describe("can also have nested examples", function(){
before(function(){
View
@@ -1,5 +1,5 @@
/**
- * Pavlov - Framework-independent Behavioral JavaScript Testing API
+ * Pavlov - Test framework-independent behavioral API
*
* version 0.3.0pre
*
@@ -53,10 +53,7 @@
* @returns array
*/
makeArray: function(array) {
- var ret = [];
- var i = array.length;
- while(i) { ret[--i] = array[i]; }
- return ret;
+ return Array.prototype.slice.call(array);
},
/**
* returns whether or not an object is an array
@@ -358,13 +355,30 @@
throw "'specification' argument is required";
}
if(fn) {
+ if(fn.async) {
+ specification += " asynchronously";
+ }
currentExample.specs.push([specification, fn]);
} else {
// if not passed an implementation, create an implementation that simply asserts fail
api.it(specification, function(){api.assert.fail('Not Implemented');});
}
},
+ /**
+ * wraps a spec (test) implementation with an initial call to pause() the test runner
+ * The spec must call resume() when ready
+ * @param {Function} fn Function containing a test to assert that it does indeed do it (optional)
+ */
+ async: function(fn) {
+ var implementation = function(){
+ adapter.pause();
+ fn();
+ };
+ implementation.async = true;
+ return implementation;
+ },
+
/**
* Generates a row spec for each argument passed, applying
* each argument to a new call against the spec
@@ -421,6 +435,20 @@
fn();
adapter.resume();
}, ms);
+ },
+
+ /**
+ * specifies test framework to pause test runner
+ */
+ pause: function() {
+ adapter.pause();
+ },
+
+ /**
+ * specifies test framework to resume test runner
+ */
+ resume: function() {
+ adapter.resume();
}
};
@@ -571,8 +599,7 @@
},
util: {
each: util.each,
- extend: util.extend,
- makeArray: util.makeArray
+ extend: util.extend
},
globalApi: false, // when true, adds api to global scope
extendAssertions: addAssertions // function for adding custom assertions
View
@@ -6,7 +6,6 @@ test("should be able to run before Pavlov's QUnit Adapter", function() {
ok(firstStandardQUnitTestRan);
});
-
// intercept calls to adapter methods for testing they were run
var capturedSuiteName;
var adapterMethods = {
@@ -73,16 +72,19 @@ pavlov.specify("Pavlov", function() {
try {
originalMethod = object[methodName];
object[methodName] = function(){
- args = pavlov.util.makeArray(arguments);
+ args = makeArray(arguments);
};
if(callback.apply(this, args) === true) {
- originalMethod.apply(this, args);
+ originalMethod.apply(object, args);
}
} finally {
object[methodName] = originalMethod;
}
return args;
};
+ var makeArray = function(args) {
+ return Array.prototype.slice.call(args);
+ };
describe("version", function(){
it("should return the current version", function(){
@@ -171,15 +173,6 @@ pavlov.specify("Pavlov", function() {
assert(target.d).equals(6);
});
});
- describe("makeArray()", function(){
- it("should convert array-like items to arrays", function(){
- var result;
- (function(){
- result = pavlov.util.makeArray(arguments);
- }(1,2,3,4));
- assert(result).contentsEqual([1,2,3,4]);
- });
- });
});
describe("specify()", function() {
@@ -329,7 +322,7 @@ pavlov.specify("Pavlov", function() {
// later, will verify the correct behavior happened with 1 arg.
it = function() {
if(arguments.length === 2) {
- args = pavlov.util.makeArray(arguments);
+ args = makeArray(arguments);
} else {
originalIt.apply(this,arguments);
}
@@ -451,9 +444,44 @@ pavlov.specify("Pavlov", function() {
});
});
- });
+ describe("with a pause()", function(){
+ it("should proxy adapter's pause()", function(){
+ var originalPause = pavlov.adapter.pause;
+ var paused = false;
+ pavlov.adapter.pause = function() { paused = true; }
+ pause();
+ pavlov.adapter.pause = originalPause;
+ assert(paused).isTrue();
+ });
+ });
+
+ describe("with a resume()", function(){
+ it("should proxy adapter's resume()", function(){
+ var originalResume = pavlov.adapter.resume;
+ var resumed = false;
+ pavlov.adapter.resume = function() { resumed = true; }
+ resume();
+ pavlov.adapter.resume = originalResume;
+ assert(resumed).isTrue();
+ });
+ });
+
+ describe("with an async()", function(){
+ it("should return a function which calls pause and then the original function", function(){
+ var calls = [];
+ var specImplementation = function() { calls.push('spec'); };
+ var originalPause = pavlov.adapter.pause;
+ pavlov.adapter.pause = function(){ calls.push('pause'); };
+ var asyncSpecImplementation = async(specImplementation);
+ asyncSpecImplementation();
+
+ pavlov.adapter.pause = originalPause;
+ assert(calls).contentsEqual(['pause','spec']);
+ });
+ });
+ });
describe("assertions", function() {
@@ -825,10 +853,10 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
isGreaterThan: function(actual, expected, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
},
isLessThan: function(actual, expected, message) {
- ltArgs = pavlov.util.makeArray(arguments);
+ ltArgs = makeArray(arguments);
}
});
@@ -843,10 +871,10 @@ pavlov.specify("Pavlov", function() {
var purpleArgs, yellowArgs;
pavlov.extendAssertions({
isPurple: function(actual, message) {
- purpleArgs = pavlov.util.makeArray(arguments);
+ purpleArgs = makeArray(arguments);
},
isYellow: function(actual, message) {
- yellowArgs = pavlov.util.makeArray(arguments);
+ yellowArgs = makeArray(arguments);
}
});
@@ -864,7 +892,7 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
isGreaterThan: function(actual, expected, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
}
});
assert(4).isGreaterThan(2,"some message");
@@ -877,7 +905,7 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
isGreaterThan: function(actual, expected, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
}
});
assert(4).isGreaterThan(2);
@@ -889,7 +917,7 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
hasLengthOf: function(actual, expected, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
}
});
assert(['a','b','c']).hasLengthOf(3);
@@ -902,7 +930,7 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
isAFunction: function(actual, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
}
});
var helloFn = function() { alert('hello'); };
@@ -916,7 +944,7 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
isAStringWithLengthOf: function(actual, expected, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
}
});
assert("test string").isAStringWithLengthOf(11);
@@ -930,7 +958,7 @@ pavlov.specify("Pavlov", function() {
var gtArgs, ltArgs;
pavlov.extendAssertions({
isNotTheSameLiteralValueAs: function(actual, expected, message) {
- gtArgs = pavlov.util.makeArray(arguments);
+ gtArgs = makeArray(arguments);
}
});
assert(a).isNotTheSameLiteralValueAs(b);

0 comments on commit 667443b

Please sign in to comment.