2009-07-09 00:55:25 +00:00
/ * *
* Environment for Jasmine
2009-07-09 01:18:17 +00:00
*
* @ constructor
2009-07-09 00:55:25 +00:00
* /
2009-05-29 03:02:15 +00:00
jasmine . Env = function ( ) {
this . currentSpec = null ;
this . currentSuite = null ;
2009-09-28 23:23:21 +00:00
this . currentRunner _ = new jasmine . Runner ( this ) ;
2009-05-29 03:02:15 +00:00
2009-07-09 00:55:25 +00:00
this . reporter = new jasmine . MultiReporter ( ) ;
2009-11-13 19:32:08 +00:00
this . updateInterval = jasmine . DEFAULT _UPDATE _INTERVAL ;
2009-05-29 03:02:15 +00:00
this . lastUpdate = 0 ;
this . specFilter = function ( ) {
return true ;
} ;
this . nextSpecId _ = 0 ;
2009-07-29 00:27:52 +00:00
this . nextSuiteId _ = 0 ;
2009-05-29 03:02:15 +00:00
this . equalityTesters _ = [ ] ;
2009-11-13 19:32:08 +00:00
// wrap matchers
this . matchersClass = function ( ) {
jasmine . Matchers . apply ( this , arguments ) ;
} ;
jasmine . util . inherit ( this . matchersClass , jasmine . Matchers ) ;
2009-12-24 17:15:18 +00:00
jasmine . Matchers . wrapInto _ ( jasmine . Matchers . prototype , this . matchersClass ) ;
2009-05-29 03:02:15 +00:00
} ;
jasmine . Env . prototype . setTimeout = jasmine . setTimeout ;
jasmine . Env . prototype . clearTimeout = jasmine . clearTimeout ;
jasmine . Env . prototype . setInterval = jasmine . setInterval ;
jasmine . Env . prototype . clearInterval = jasmine . clearInterval ;
2009-09-28 18:13:44 +00:00
/ * *
* @ returns an object containing jasmine version build info , if set .
* /
2009-08-21 05:16:14 +00:00
jasmine . Env . prototype . version = function ( ) {
if ( jasmine . version _ ) {
2009-08-21 14:00:54 +00:00
return jasmine . version _ ;
2009-08-21 05:16:14 +00:00
} else {
2009-08-21 14:00:54 +00:00
throw new Error ( 'Version not set' ) ;
2009-08-21 05:16:14 +00:00
}
} ;
2010-04-01 22:56:29 +00:00
/ * *
* @ returns string containing jasmine version build info , if set .
* /
jasmine . Env . prototype . versionString = function ( ) {
if ( jasmine . version _ ) {
var version = this . version ( ) ;
return version . major + "." + version . minor + "." + version . build + " revision " + version . revision ;
} else {
return "version unknown" ;
}
} ;
2009-09-28 18:13:44 +00:00
/ * *
* @ returns a sequential integer starting at 0
* /
jasmine . Env . prototype . nextSpecId = function ( ) {
return this . nextSpecId _ ++ ;
} ;
/ * *
* @ returns a sequential integer starting at 0
* /
jasmine . Env . prototype . nextSuiteId = function ( ) {
return this . nextSuiteId _ ++ ;
} ;
2009-07-09 00:55:25 +00:00
/ * *
* Register a reporter to receive status updates from Jasmine .
2009-07-09 01:18:17 +00:00
* @ param { jasmine . Reporter } reporter An object which will receive status updates .
2009-07-09 00:55:25 +00:00
* /
jasmine . Env . prototype . addReporter = function ( reporter ) {
this . reporter . addReporter ( reporter ) ;
} ;
2009-05-29 03:02:15 +00:00
jasmine . Env . prototype . execute = function ( ) {
2009-09-28 23:23:21 +00:00
this . currentRunner _ . execute ( ) ;
2009-05-29 03:02:15 +00:00
} ;
jasmine . Env . prototype . describe = function ( description , specDefinitions ) {
var suite = new jasmine . Suite ( this , description , specDefinitions , this . currentSuite ) ;
var parentSuite = this . currentSuite ;
if ( parentSuite ) {
2009-08-01 22:28:39 +00:00
parentSuite . add ( suite ) ;
2009-05-29 03:02:15 +00:00
} else {
2009-09-28 23:23:21 +00:00
this . currentRunner _ . add ( suite ) ;
2009-05-29 03:02:15 +00:00
}
this . currentSuite = suite ;
2010-06-04 18:14:31 +00:00
var declarationError = null ;
try {
specDefinitions . call ( suite ) ;
} catch ( e ) {
declarationError = e ;
}
2009-05-29 03:02:15 +00:00
this . currentSuite = parentSuite ;
2010-06-04 18:14:31 +00:00
if ( declarationError ) {
this . it ( "encountered a declaration exception" , function ( ) {
throw declarationError ;
} ) ;
}
2009-05-29 03:02:15 +00:00
return suite ;
} ;
jasmine . Env . prototype . beforeEach = function ( beforeEachFunction ) {
2009-09-28 23:23:21 +00:00
if ( this . currentSuite ) {
this . currentSuite . beforeEach ( beforeEachFunction ) ;
} else {
this . currentRunner _ . beforeEach ( beforeEachFunction ) ;
}
} ;
jasmine . Env . prototype . currentRunner = function ( ) {
return this . currentRunner _ ;
2009-05-29 03:02:15 +00:00
} ;
jasmine . Env . prototype . afterEach = function ( afterEachFunction ) {
2009-09-28 23:23:21 +00:00
if ( this . currentSuite ) {
this . currentSuite . afterEach ( afterEachFunction ) ;
} else {
this . currentRunner _ . afterEach ( afterEachFunction ) ;
}
2009-05-29 03:02:15 +00:00
} ;
jasmine . Env . prototype . xdescribe = function ( desc , specDefinitions ) {
return {
execute : function ( ) {
}
} ;
} ;
jasmine . Env . prototype . it = function ( description , func ) {
var spec = new jasmine . Spec ( this , this . currentSuite , description ) ;
2009-08-01 22:28:39 +00:00
this . currentSuite . add ( spec ) ;
2009-05-29 03:02:15 +00:00
this . currentSpec = spec ;
if ( func ) {
2009-07-31 05:31:57 +00:00
spec . runs ( func ) ;
2009-05-29 03:02:15 +00:00
}
return spec ;
} ;
jasmine . Env . prototype . xit = function ( desc , func ) {
return {
2009-09-28 18:13:44 +00:00
id : this . nextSpecId ( ) ,
2009-05-29 03:02:15 +00:00
runs : function ( ) {
}
} ;
} ;
jasmine . Env . prototype . compareObjects _ = function ( a , b , mismatchKeys , mismatchValues ) {
if ( a . _ _Jasmine _been _here _before _ _ === b && b . _ _Jasmine _been _here _before _ _ === a ) {
return true ;
}
a . _ _Jasmine _been _here _before _ _ = b ;
b . _ _Jasmine _been _here _before _ _ = a ;
var hasKey = function ( obj , keyName ) {
2009-11-26 16:12:06 +00:00
return obj != null && obj [ keyName ] !== jasmine . undefined ;
2009-05-29 03:02:15 +00:00
} ;
for ( var property in b ) {
if ( ! hasKey ( a , property ) && hasKey ( b , property ) ) {
2009-10-30 00:03:24 +00:00
mismatchKeys . push ( "expected has key '" + property + "', but missing from actual." ) ;
2009-05-29 03:02:15 +00:00
}
}
for ( property in a ) {
if ( ! hasKey ( b , property ) && hasKey ( a , property ) ) {
2009-10-30 00:03:24 +00:00
mismatchKeys . push ( "expected missing key '" + property + "', but present in actual." ) ;
2009-05-29 03:02:15 +00:00
}
}
for ( property in b ) {
if ( property == '__Jasmine_been_here_before__' ) continue ;
if ( ! this . equals _ ( a [ property ] , b [ property ] , mismatchKeys , mismatchValues ) ) {
2009-10-30 00:03:24 +00:00
mismatchValues . push ( "'" + property + "' was '" + ( b [ property ] ? jasmine . util . htmlEscape ( b [ property ] . toString ( ) ) : b [ property ] ) + "' in expected, but was '" + ( a [ property ] ? jasmine . util . htmlEscape ( a [ property ] . toString ( ) ) : a [ property ] ) + "' in actual." ) ;
2009-05-29 03:02:15 +00:00
}
}
if ( jasmine . isArray _ ( a ) && jasmine . isArray _ ( b ) && a . length != b . length ) {
mismatchValues . push ( "arrays were not the same length" ) ;
}
delete a . _ _Jasmine _been _here _before _ _ ;
delete b . _ _Jasmine _been _here _before _ _ ;
return ( mismatchKeys . length == 0 && mismatchValues . length == 0 ) ;
} ;
jasmine . Env . prototype . equals _ = function ( a , b , mismatchKeys , mismatchValues ) {
mismatchKeys = mismatchKeys || [ ] ;
mismatchValues = mismatchValues || [ ] ;
2010-03-09 05:56:04 +00:00
for ( var i = 0 ; i < this . equalityTesters _ . length ; i ++ ) {
var equalityTester = this . equalityTesters _ [ i ] ;
var result = equalityTester ( a , b , this , mismatchKeys , mismatchValues ) ;
if ( result !== jasmine . undefined ) return result ;
}
2009-05-29 03:02:15 +00:00
if ( a === b ) return true ;
2009-11-26 16:12:06 +00:00
if ( a === jasmine . undefined || a === null || b === jasmine . undefined || b === null ) {
return ( a == jasmine . undefined && b == jasmine . undefined ) ;
2009-05-29 03:02:15 +00:00
}
if ( jasmine . isDomNode ( a ) && jasmine . isDomNode ( b ) ) {
return a === b ;
}
if ( a instanceof Date && b instanceof Date ) {
return a . getTime ( ) == b . getTime ( ) ;
}
if ( a instanceof jasmine . Matchers . Any ) {
return a . matches ( b ) ;
}
if ( b instanceof jasmine . Matchers . Any ) {
return b . matches ( a ) ;
}
2010-03-17 15:19:29 +00:00
if ( jasmine . isString _ ( a ) && jasmine . isString _ ( b ) ) {
return ( a == b ) ;
}
2010-03-19 03:32:40 +00:00
if ( jasmine . isNumber _ ( a ) && jasmine . isNumber _ ( b ) ) {
return ( a == b ) ;
}
2009-05-29 03:02:15 +00:00
if ( typeof a === "object" && typeof b === "object" ) {
return this . compareObjects _ ( a , b , mismatchKeys , mismatchValues ) ;
}
//Straight check
return ( a === b ) ;
} ;
jasmine . Env . prototype . contains _ = function ( haystack , needle ) {
if ( jasmine . isArray _ ( haystack ) ) {
for ( var i = 0 ; i < haystack . length ; i ++ ) {
if ( this . equals _ ( haystack [ i ] , needle ) ) return true ;
}
return false ;
}
return haystack . indexOf ( needle ) >= 0 ;
} ;
jasmine . Env . prototype . addEqualityTester = function ( equalityTester ) {
this . equalityTesters _ . push ( equalityTester ) ;
} ;