Skip to content

Commit

Permalink
- Release 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 21, 2009
1 parent 19887d4 commit a4acfed
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 65 deletions.
8 changes: 7 additions & 1 deletion History.txt
@@ -1,9 +1,15 @@

=== 0.4.0 / 2009-02-20

* Added comment literal (//)
* Added pre-processor for convering matchers.
For example 'test'.should_be_true becomes JSpec.match('test', 'should_be', 'true'),
preventing pollution of core prototypes.

=== 0.3.2 / 2009-02-19

* Added TM bundle (go checkout my jspec.tmbundle repo on github)
* Renamed have_length_of to have_length
*

=== 0.3.1 / 2009-02-19

Expand Down
2 changes: 1 addition & 1 deletion lib/jspec.js
Expand Up @@ -3,7 +3,7 @@

var JSpec = {

version : '0.3.2',
version : '0.4.0',
main : this,
suites : {},
stats : { specs : 0, assertions : 0, failures : 0, passes : 0 },
Expand Down
2 changes: 2 additions & 0 deletions pkg/jspec.jquery.js
@@ -1,4 +1,6 @@

// JSpec - jQuery - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)

// TODO: fix stupid sizzle bug
// TODO: have_attr('attr', value)
// TODO: ajax
Expand Down
79 changes: 25 additions & 54 deletions pkg/jspec.js
Expand Up @@ -3,7 +3,7 @@

var JSpec = {

version : '0.3.2',
version : '0.4.0',
main : this,
suites : {},
stats : { specs : 0, assertions : 0, failures : 0, passes : 0 },
Expand Down Expand Up @@ -283,23 +283,23 @@ var JSpec = {
// --- Methods

/**
* Invoke a matcher. Useful when creating custom matchers
* so that you may utilize, or negate others when creating your own.
* Invoke a matcher.
*
* this.match('test', 'be_a', String)
* this.match('test', 'should', 'be_a', String)
*
* @param {object} actual
* @param {bool, string} negate
* @param {string} name
* @param {object} expected
* @param {bool} negate
* @return {bool}
* @api public
*/

match : function(actual, name, expected, negate) {
negate = negate || false
match : function(actual, negate, name, expected) {
if (typeof negate == 'string') negate = negate == 'should' ? false : true
var matcher = new JSpec.Matcher(name, this.matchers[name], expected, actual, negate)
return matcher.passes()
JSpec.currentSpec.assertions.push(matcher.exec())
return matcher.result
},

/**
Expand Down Expand Up @@ -331,47 +331,13 @@ var JSpec = {
*/

addMatchers : function(matchers) {
// TODO: this.extend(o, o)
this.each(matchers, function(name, body){
this.addMatcher(name, body)
this.matchers[name] = body
})
return this
},

/**
* Add a matcher.
*
* @param {string} name
* @param {string, hash} body
* @return {JSpec}
* @api public
*/

addMatcher : function(name, body) {
this._addMatcher(name, body, true)._addMatcher(name, body, false)
},

/**
* Add raw matcher, this requires that you specify the negate
* parameter, use addMatcher instead.
*
* @param {string} name
* @param {string, hash} body
* @param {bool} negate
* @return {JSpec}
* @api private
*/

_addMatcher : function(name, body, negate) {
Object.prototype['should_' + (negate ? 'not_' : '') + name] = function(other) {
var matcher = new JSpec.Matcher(name, body, other, this, negate)
if (JSpec.currentSpec) {
JSpec.currentSpec.assertions.push(matcher.exec())
return matcher.result
}
}
return this
},

/**
* Evaluate a JSpec capture body.
*
Expand Down Expand Up @@ -400,7 +366,12 @@ var JSpec = {
*/

preProcessBody : function(body) {
// Allow optional parens for matchers
body = body.replace(/\.should_(\w+)(?: |$)(.*)$/gm, '.should_$1($2)')
// Convert to non-polluting match() invocation
body = body.replace(/(.+?)\.(should(?:_not)?)_(\w+)\((.*)\)$/gm, 'JSpec.match($1, "$2", "$3", $4)')
// Remove expected arg ', )' left when not set
body = body.replace(/, \)$/gm, ')')
return body
},

Expand All @@ -413,13 +384,15 @@ var JSpec = {
*/

parse : function(input) {
var describing, specing, capturing
var describing, specing, capturing, commenting
var token, describe, spec, capture, body = []
var tokens = this.tokenize(input)

while (tokens.length) {
token = tokens.shift()

if (commenting && token != "\n") continue

switch (token) {
case 'end':
if (describing) this.suites[describe] = this.suites[describe] || new JSpec.Suite(describe)
Expand All @@ -438,14 +411,12 @@ var JSpec = {
}
break

case 'before':
case 'after':
case 'before_each':
case 'after_each': capturing = true; break

case 'describe': describing = true; break
case 'it' : specing = true; break
case '__END__' : return this; break
case 'before': case 'after': case 'before_each': case 'after_each': capturing = true; break
case "\n" : commenting = false; break
case '//' : commenting = true; break
case 'describe' : describing = true; break
case 'it' : specing = true; break
case '__END__' : return this; break
}

if (spec || capture) {
Expand Down Expand Up @@ -473,7 +444,7 @@ var JSpec = {

tokenize : function(input) {
if (input.constructor == Array) return input
var regexp = /(?:__END__|end|before_each|after_each|before|after|it|describe|'.*?')(?= |\n|$)|\n|./gm
var regexp = /(?:__END__|end|before_each|after_each|before|after|it|describe|'.*?')(?= |\n|$)|\/\/|\n|./gm
return input.match(regexp)
},

Expand Down
21 changes: 12 additions & 9 deletions pkg/jspec.min.js
@@ -1,5 +1,5 @@

var JSpec={version:'0.3.2',main:this,suites:{},stats:{specs:0,assertions:0,failures:0,passes:0},matchers:{eql:"==",be:"alias eql",equal:"===",be_greater_than:">",be_less_than:"<",be_at_least:">=",be_at_most:"<=",be_a:"actual.constructor == expected",be_an:"alias be_a",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",have_length:"actual.length == expected",respond_to:"typeof actual[expected] == 'function'",include:{match:function(expected,actual){if(actual.constructor==String)return actual.match(expected)
var JSpec={version:'0.4.0',main:this,suites:{},stats:{specs:0,assertions:0,failures:0,passes:0},matchers:{eql:"==",be:"alias eql",equal:"===",be_greater_than:">",be_less_than:"<",be_at_least:">=",be_at_most:"<=",be_a:"actual.constructor == expected",be_an:"alias be_a",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",have_length:"actual.length == expected",respond_to:"typeof actual[expected] == 'function'",include:{match:function(expected,actual){if(actual.constructor==String)return actual.match(expected)
else return expected in actual}},throw_error:{match:function(expected,actual){try{actual()}
catch(e){return true}}}},defaultContext:{sandbox:function(name){var sandbox=document.createElement('div')
sandbox.setAttribute('class','jspec-sandbox')
Expand Down Expand Up @@ -48,24 +48,25 @@ this.failure=function(){var failure
JSpec.each(this.assertions,function(assertion){if(!assertion.passed&&!failure)failure=assertion})
return failure}
this.passed=function(){return!this.failure()}
this.requiresImplementation=function(){return this.assertions.length==0}},match:function(actual,name,expected,negate){negate=negate||false
this.requiresImplementation=function(){return this.assertions.length==0}},match:function(actual,negate,name,expected){if(typeof negate=='string')negate=negate=='should'?false:true
var matcher=new JSpec.Matcher(name,this.matchers[name],expected,actual,negate)
return matcher.passes()},each:function(object,callback){for(var key in object){if(typeof object[key]=='function')continue
JSpec.currentSpec.assertions.push(matcher.exec())
return matcher.result},each:function(object,callback){for(var key in object){if(typeof object[key]=='function')continue
if(callback.length==1)
callback.call(this,object[key])
else
callback.call(this,key,object[key])}
return this},addMatchers:function(matchers){this.each(matchers,function(name,body){this.addMatcher(name,body)})
return this},addMatcher:function(name,body){this._addMatcher(name,body,true)._addMatcher(name,body,false)},_addMatcher:function(name,body,negate){Object.prototype['should_'+(negate?'not_':'')+name]=function(other){var matcher=new JSpec.Matcher(name,body,other,this,negate)
if(JSpec.currentSpec){JSpec.currentSpec.assertions.push(matcher.exec())
return matcher.result}}
return this},addMatchers:function(matchers){this.each(matchers,function(name,body){this.matchers[name]=body})
return this},evalBody:function(body,errorMessage){try{var runner=function(){eval(JSpec.preProcessBody(body))}
runner.call(this.context||this.defaultContext)}
catch(e){throw(errorMessage||'Error: ')+e}},preProcessBody:function(body){body=body.replace(/\.should_(\w+)(?: |$)(.*)$/gm,'.should_$1($2)')
return body},parse:function(input){var describing,specing,capturing
body=body.replace(/(.+?)\.(should(?:_not)?)_(\w+)\((.*)\)$/gm,'JSpec.match($1, "$2", "$3", $4)')
body=body.replace(/, \)$/gm,')')
return body},parse:function(input){var describing,specing,capturing,commenting
var token,describe,spec,capture,body=[]
var tokens=this.tokenize(input)
while(tokens.length){token=tokens.shift()
if(commenting&&token!="\n")continue
switch(token){case'end':if(describing)this.suites[describe]=this.suites[describe]||new JSpec.Suite(describe)
if(specing){var newSpec=new JSpec.Spec(spec,body.join(''))
this.suites[describe].addSpec(newSpec)
Expand All @@ -76,6 +77,8 @@ body=[],capturing=capture=null}
else if(describing){describing=describe=null}
break
case'before':case'after':case'before_each':case'after_each':capturing=true;break
case"\n":commenting=false;break
case'//':commenting=true;break
case'describe':describing=true;break
case'it':specing=true;break
case'__END__':return this;break}
Expand All @@ -84,7 +87,7 @@ else{if(capturing)capture=token
if(/'.*?'/.test(token)){if(specing)spec=token.replace(/'/g,'')
else if(describing)describe=token.replace(/'/g,'')}}}
return this},tokenize:function(input){if(input.constructor==Array)return input
var regexp=/(?:__END__|end|before_each|after_each|before|after|it|describe|'.*?')(?= |\n|$)|\n|./gm
var regexp=/(?:__END__|end|before_each|after_each|before|after|it|describe|'.*?')(?= |\n|$)|\/\/|\n|./gm
return input.match(regexp)},report:function(options){options=options||{}
this.formatter?new this.formatter(this,options):new JSpec.DOMFormatter(this,options)
return this},run:function(){this.each(this.suites,function(suite){this.runSuite(suite)})
Expand Down

0 comments on commit a4acfed

Please sign in to comment.