lib/jspec.js in visionmedia-jspec-2.2.0 vs lib/jspec.js in visionmedia-jspec-2.2.1
- old
+ new
@@ -3,11 +3,11 @@
(function(){
JSpec = {
- version : '2.2.0',
+ version : '2.2.1',
suites : [],
allSuites : [],
matchers : {},
stats : { specs : 0, assertions : 0, failures : 0, passes : 0, specsFinished : 0, suitesFinished : 0 },
options : { profile : false },
@@ -42,29 +42,10 @@
* @api public
*/
an_instance_of : function(constructor) {
return { an_instance_of : constructor }
- },
-
- /**
- * Sets the current spec's wait duration to _n_.
- *
- * wait(3000)
- * wait(1, 'second')
- * wait(3, 'seconds')
- *
- * @param {number} n
- * @param {string} unit
- * @api public
- */
-
- wait : function(n, unit) {
- JSpec.currentSpec.wait = {
- 'second' : n * 1000,
- 'seconds' : n * 1000
- }[unit] || n
}
},
// --- Objects
@@ -1043,18 +1024,18 @@
* @api private
*/
preprocess : function(input) {
return input.
- replace(/describe (.*?)$/gm, 'describe($1, function(){').
- replace(/ it (.*?)$/gm, ' it($1, function(){').
+ replace(/describe\s+(.*?)$/gm, 'describe($1, function(){').
+ replace(/\sit\s+(.*?)$/gm, ' it($1, function(){').
replace(/^(?: *)(before_each|after_each|before|after)(?= |\n|$)/gm, 'JSpec.currentSuite.addHook("$1", function(){').
- replace(/end(?= |\n|$)/gm, '});').
+ replace(/end(?=\s|$)/gm, '});').
replace(/-\{/g, 'function(){').
replace(/(\d+)\.\.(\d+)/g, function(_, a, b){ return range(a, b) }).
replace(/\.should([_\.]not)?[_\.](\w+)(?: |$)(.*)$/gm, '.should$1_$2($3)').
- replace(/([\/ ]*)(.+?)\.(should(?:[_\.]not)?)[_\.](\w+)\((.*)\)$/gm, '$1 expect($2).$3($4, $5)').
+ replace(/([\/\s]*)(.+?)\.(should(?:[_\.]not)?)[_\.](\w+)\((.*)\)$/gm, '$1 expect($2).$3($4, $5)').
replace(/, \)/gm, ')').
replace(/should\.not/gm, 'should_not')
},
/**
@@ -1070,35 +1051,21 @@
var current = parseInt(start), end = parseInt(end), values = [current]
if (end > current) while (++current <= end) values.push(current)
else while (--current >= end) values.push(current)
return '[' + values + ']'
},
-
- /**
- * Call _callback_ when all specs have finished.
- *
- * @param {function} callback
- * @api public
- */
-
- whenFinished : function(callback) {
- if (this.stats.specsFinished >= this.stats.specs) callback()
- else setTimeout(function(){ JSpec.whenFinished(callback) }, 50)
- },
/**
* Report on the results.
*
* @api public
*/
report : function() {
- this.whenFinished(function() {
- JSpec.options.formatter ?
- new JSpec.options.formatter(JSpec, JSpec.options):
- new JSpec.formatters.DOM(JSpec, JSpec.options)
- })
+ JSpec.options.formatter ?
+ new JSpec.options.formatter(JSpec, JSpec.options):
+ new JSpec.formatters.DOM(JSpec, JSpec.options)
},
/**
* Run the spec suites. Options are merged
* with JSpec options when present.
@@ -1115,24 +1082,10 @@
if (option('profile')) console.groupEnd()
return this
},
/**
- * When the current spec's wait duration has passed
- * the _callback_ will be called.
- *
- * @param {function} callback
- * @api public
- */
-
- whenCurrentSpecIsFinished : function(callback) {
- if (this.currentSpec && this.currentSpec.wait)
- setTimeout(callback, this.currentSpec.wait)
- else callback()
- },
-
- /**
* Run a suite.
*
* @param {Suite} suite
* @api public
*/
@@ -1141,15 +1094,13 @@
this.currentSuite = suite
this.evalBody(suite.body)
suite.ran = true
suite.hook('before')
each(suite.specs, function(spec) {
- JSpec.whenCurrentSpecIsFinished(function(){
- suite.hook('before_each')
- JSpec.runSpec(spec)
- suite.hook('after_each')
- })
+ suite.hook('before_each')
+ JSpec.runSpec(spec)
+ suite.hook('after_each')
})
if (suite.hasSuites()) {
each(suite.suites, function(suite) {
JSpec.runSuite(suite)
})
@@ -1246,20 +1197,19 @@
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
request.send(data)
},
/**
- * Report back to server with statistics.
+ * Report to server with statistics.
*
+ * @param {string} url
* @api private
*/
- reportToServer : function() {
- this.whenFinished(function(){
- JSpec.post('http://localhost:4444', 'passes=' + JSpec.stats.passes + '&failures=' + JSpec.stats.failures)
- if ('close' in main) main.close()
- })
+ reportToServer : function(url) {
+ JSpec.post(url || 'http://localhost:4444', 'passes=' + JSpec.stats.passes + '&failures=' + JSpec.stats.failures)
+ if ('close' in main) main.close()
},
/**
* Instantiate an XMLHttpRequest.
*
@@ -1357,11 +1307,10 @@
be_at_most : "<=",
be_a : "actual.constructor == expected",
be_an : "alias be_a",
be_an_instance_of : "actual instanceof expected",
be_null : "actual == null",
- be_empty : "actual.length == 0",
be_true : "actual == true",
be_false : "actual == false",
be_type : "typeof actual == expected",
match : "typeof actual == 'string' ? actual.match(expected) : false",
respond_to : "typeof actual[expected] == 'function'",
@@ -1379,10 +1328,17 @@
receive : { defer : true, match : function(actual, method, times) {
proxy = new JSpec.ProxyAssertion(actual, method, times)
JSpec.currentSpec.assertions.push(proxy)
return proxy
}},
+
+ be_empty : function(actual) {
+ if (actual.constructor == Object && actual.length == undefined)
+ for (var key in actual)
+ return false;
+ return !actual.length
+ },
include : function(actual) {
for (state = true, i = 1; i < arguments.length; i++) {
arg = arguments[i]
switch (actual.constructor) {
@@ -1409,15 +1365,14 @@
throw_error : { match : function(actual, expected, message) {
try { actual() }
catch (e) {
this.e = e
var assert = function(arg) {
- console.log(e.constructor);
switch (arg.constructor) {
case RegExp : return arg.test(e)
- case Function : return e.constructor == arg
case String : return arg == (e.message || e.toString())
+ case Function : return (e.name || 'Error') == arg.name
}
}
return message ? assert(expected) && assert(message) :
expected ? assert(expected) :
true
@@ -1426,17 +1381,14 @@
// TODO: refactor when actual is not in expected [0]
var message_for = function(i) {
if (expected[i] == undefined) return 'exception'
switch (expected[i].constructor) {
case RegExp : return 'exception matching ' + puts(expected[i])
- case Function : return expected[i].name
case String : return 'exception of ' + puts(expected[i])
+ case Function : return expected[i].name || 'Error'
}
}
- if (expected[1] == Error && expected[2] == 'oh no!') {
- console.log(expected);
- }
exception = message_for(1) + (expected[2] ? ' and ' + message_for(2) : '')
return 'expected ' + exception + (negate ? ' not ' : '' ) +
' to be thrown, but ' + (this.e ? 'got ' + puts(this.e) : 'nothing was')
}},
@@ -1470,6 +1422,7 @@
value == null ? true:
value === actual[property]
}
})
-})()
\ No newline at end of file
+})()
+