chai.js 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464
  1. !function (name, definition) {
  2. if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
  3. else this[name] = definition();
  4. }('chai', function () {
  5. // CommonJS require()
  6. function require(p){
  7. var path = require.resolve(p)
  8. , mod = require.modules[path];
  9. if (!mod) throw new Error('failed to require "' + p + '"');
  10. if (!mod.exports) {
  11. mod.exports = {};
  12. mod.call(mod.exports, mod, mod.exports, require.relative(path));
  13. }
  14. return mod.exports;
  15. }
  16. require.modules = {};
  17. require.resolve = function (path){
  18. var orig = path
  19. , reg = path + '.js'
  20. , index = path + '/index.js';
  21. return require.modules[reg] && reg
  22. || require.modules[index] && index
  23. || orig;
  24. };
  25. require.register = function (path, fn){
  26. require.modules[path] = fn;
  27. };
  28. require.relative = function (parent) {
  29. return function(p){
  30. if ('.' != p[0]) return require(p);
  31. var path = parent.split('/')
  32. , segs = p.split('/');
  33. path.pop();
  34. for (var i = 0; i < segs.length; i++) {
  35. var seg = segs[i];
  36. if ('..' == seg) path.pop();
  37. else if ('.' != seg) path.push(seg);
  38. }
  39. return require(path.join('/'));
  40. };
  41. };
  42. require.register("chai.js", function(module, exports, require){
  43. /*!
  44. * chai
  45. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  46. * MIT Licensed
  47. */
  48. var used = []
  49. , exports = module.exports = {};
  50. /*!
  51. * Chai version
  52. */
  53. exports.version = '1.1.0';
  54. /*!
  55. * Primary `Assertion` prototype
  56. */
  57. exports.Assertion = require('./chai/assertion');
  58. /*!
  59. * Assertion Error
  60. */
  61. exports.AssertionError = require('./chai/browser/error');
  62. /*!
  63. * Utils for plugins (not exported)
  64. */
  65. var util = require('./chai/utils');
  66. /**
  67. * # .use(function)
  68. *
  69. * Provides a way to extend the internals of Chai
  70. *
  71. * @param {Function}
  72. * @returns {this} for chaining
  73. * @api public
  74. */
  75. exports.use = function (fn) {
  76. if (!~used.indexOf(fn)) {
  77. fn(this, util);
  78. used.push(fn);
  79. }
  80. return this;
  81. };
  82. /*!
  83. * Core Assertions
  84. */
  85. var core = require('./chai/core/assertions');
  86. exports.use(core);
  87. /*!
  88. * Expect interface
  89. */
  90. var expect = require('./chai/interface/expect');
  91. exports.use(expect);
  92. /*!
  93. * Should interface
  94. */
  95. var should = require('./chai/interface/should');
  96. exports.use(should);
  97. /*!
  98. * Assert interface
  99. */
  100. var assert = require('./chai/interface/assert');
  101. exports.use(assert);
  102. }); // module: chai.js
  103. require.register("chai/assertion.js", function(module, exports, require){
  104. /*!
  105. * chai
  106. * http://chaijs.com
  107. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  108. * MIT Licensed
  109. */
  110. /*!
  111. * Module dependencies.
  112. */
  113. var AssertionError = require('./browser/error')
  114. , util = require('./utils')
  115. , flag = util.flag;
  116. /*!
  117. * Module export.
  118. */
  119. module.exports = Assertion;
  120. /*!
  121. * Assertion Constructor
  122. *
  123. * Creates object for chaining.
  124. *
  125. * @api private
  126. */
  127. function Assertion (obj, msg, stack) {
  128. flag(this, 'ssfi', stack || arguments.callee);
  129. flag(this, 'object', obj);
  130. flag(this, 'message', msg);
  131. }
  132. /*!
  133. * ### Assertion.includeStack
  134. *
  135. * User configurable property, influences whether stack trace
  136. * is included in Assertion error message. Default of false
  137. * suppresses stack trace in the error message
  138. *
  139. * Assertion.includeStack = true; // enable stack on error
  140. *
  141. * @api public
  142. */
  143. Assertion.includeStack = false;
  144. Assertion.addProperty = function (name, fn) {
  145. util.addProperty(this.prototype, name, fn);
  146. };
  147. Assertion.addMethod = function (name, fn) {
  148. util.addMethod(this.prototype, name, fn);
  149. };
  150. Assertion.addChainableMethod = function (name, fn, chainingBehavior) {
  151. util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
  152. };
  153. Assertion.overwriteProperty = function (name, fn) {
  154. util.overwriteProperty(this.prototype, name, fn);
  155. };
  156. Assertion.overwriteMethod = function (name, fn) {
  157. util.overwriteMethod(this.prototype, name, fn);
  158. };
  159. /*!
  160. * ### .assert(expression, message, negateMessage, expected, actual)
  161. *
  162. * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
  163. *
  164. * @name assert
  165. * @param {Philosophical} expression to be tested
  166. * @param {String} message to display if fails
  167. * @param {String} negatedMessage to display if negated expression fails
  168. * @param {Mixed} expected value (remember to check for negation)
  169. * @param {Mixed} actual (optional) will default to `this.obj`
  170. * @api private
  171. */
  172. Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual) {
  173. var msg = util.getMessage(this, arguments)
  174. , actual = util.getActual(this, arguments)
  175. , ok = util.test(this, arguments);
  176. if (!ok) {
  177. throw new AssertionError({
  178. message: msg
  179. , actual: actual
  180. , expected: expected
  181. , stackStartFunction: (Assertion.includeStack) ? this.assert : flag(this, 'ssfi')
  182. });
  183. }
  184. };
  185. /*!
  186. * ### ._obj
  187. *
  188. * Quick reference to stored `actual` value for plugin developers.
  189. *
  190. * @api private
  191. */
  192. Object.defineProperty(Assertion.prototype, '_obj',
  193. { get: function () {
  194. return flag(this, 'object');
  195. }
  196. , set: function (val) {
  197. flag(this, 'object', val);
  198. }
  199. });
  200. }); // module: chai/assertion.js
  201. require.register("chai/browser/error.js", function(module, exports, require){
  202. /*!
  203. * chai
  204. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  205. * MIT Licensed
  206. */
  207. module.exports = AssertionError;
  208. function AssertionError (options) {
  209. options = options || {};
  210. this.message = options.message;
  211. this.actual = options.actual;
  212. this.expected = options.expected;
  213. this.operator = options.operator;
  214. if (options.stackStartFunction && Error.captureStackTrace) {
  215. var stackStartFunction = options.stackStartFunction;
  216. Error.captureStackTrace(this, stackStartFunction);
  217. }
  218. }
  219. AssertionError.prototype = Object.create(Error.prototype);
  220. AssertionError.prototype.name = 'AssertionError';
  221. AssertionError.prototype.constructor = AssertionError;
  222. AssertionError.prototype.toString = function() {
  223. return this.message;
  224. };
  225. }); // module: chai/browser/error.js
  226. require.register("chai/core/assertions.js", function(module, exports, require){
  227. /*!
  228. * chai
  229. * http://chaijs.com
  230. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  231. * MIT Licensed
  232. */
  233. module.exports = function (chai, _) {
  234. var Assertion = chai.Assertion
  235. , toString = Object.prototype.toString
  236. , flag = _.flag;
  237. /**
  238. * ### Language Chains
  239. *
  240. * The following are provide as chainable getters to
  241. * improve the readability of your assertions. They
  242. * do not provide an testing capability unless they
  243. * have been overwritten by a plugin.
  244. *
  245. * **Chains**
  246. *
  247. * - to
  248. * - be
  249. * - been
  250. * - is
  251. * - that
  252. * - and
  253. * - have
  254. * - with
  255. *
  256. * @name language chains
  257. * @api public
  258. */
  259. [ 'to', 'be', 'been'
  260. , 'is', 'and', 'have'
  261. , 'with', 'that' ].forEach(function (chain) {
  262. Assertion.addProperty(chain, function () {
  263. return this;
  264. });
  265. });
  266. /**
  267. * ### .not
  268. *
  269. * Negates any of assertions following in the chain.
  270. *
  271. * expect(foo).to.not.equal('bar');
  272. * expect(goodFn).to.not.throw(Error);
  273. * expect({ foo: 'baz' }).to.have.property('foo')
  274. * .and.not.equal('bar');
  275. *
  276. * @name not
  277. * @api public
  278. */
  279. Assertion.addProperty('not', function () {
  280. flag(this, 'negate', true);
  281. });
  282. /**
  283. * ### .deep
  284. *
  285. * Sets the `deep` flag, later used by the `equal` and
  286. * `property` assertions.
  287. *
  288. * expect(foo).to.deep.equal({ bar: 'baz' });
  289. * expect({ foo: { bar: { baz: 'quux' } } })
  290. * .to.have.deep.property('foo.bar.baz', 'quux');
  291. *
  292. * @name deep
  293. * @api public
  294. */
  295. Assertion.addProperty('deep', function () {
  296. flag(this, 'deep', true);
  297. });
  298. /**
  299. * ### .a(type)
  300. *
  301. * The `a` and `an` assertions are aliases that can be
  302. * used either as language chains or to assert a value's
  303. * type (as revealed by `Object.prototype.toString`).
  304. *
  305. * // typeof
  306. * expect('test').to.be.a('string');
  307. * expect({ foo: 'bar' }).to.be.an('object');
  308. * expect(null).to.be.a('null');
  309. * expect(undefined).to.be.an('undefined');
  310. *
  311. * // language chain
  312. * expect(foo).to.be.an.instanceof(Foo);
  313. *
  314. * @name a
  315. * @alias an
  316. * @param {String} type
  317. * @api public
  318. */
  319. function an(type) {
  320. var obj = flag(this, 'object')
  321. , klassStart = type.charAt(0).toUpperCase()
  322. , klass = klassStart + type.slice(1)
  323. , article = ~[ 'A', 'E', 'I', 'O', 'U' ].indexOf(klassStart) ? 'an ' : 'a ';
  324. this.assert(
  325. '[object ' + klass + ']' === toString.call(obj)
  326. , 'expected #{this} to be ' + article + type
  327. , 'expected #{this} not to be ' + article + type
  328. );
  329. }
  330. Assertion.addChainableMethod('an', an);
  331. Assertion.addChainableMethod('a', an);
  332. /**
  333. * ### .include(value)
  334. *
  335. * The `include` and `contain` assertions can be used as either property
  336. * based language chains or as methods to assert the inclusion of an object
  337. * in an array or a substring in a string. When used as language chains,
  338. * they toggle the `contain` flag for the `keys` assertion.
  339. *
  340. * expect([1,2,3]).to.include(2);
  341. * expect('foobar').to.contain('foo');
  342. * expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');
  343. *
  344. * @name include
  345. * @alias contain
  346. * @param {Object|String|Number} obj
  347. * @api public
  348. */
  349. function includeChainingBehavior () {
  350. flag(this, 'contains', true);
  351. }
  352. function include (val) {
  353. var obj = flag(this, 'object')
  354. this.assert(
  355. ~obj.indexOf(val)
  356. , 'expected #{this} to include ' + _.inspect(val)
  357. , 'expected #{this} to not include ' + _.inspect(val));
  358. }
  359. Assertion.addChainableMethod('include', include, includeChainingBehavior);
  360. Assertion.addChainableMethod('contain', include, includeChainingBehavior);
  361. /**
  362. * ### .ok
  363. *
  364. * Asserts that the target is truthy.
  365. *
  366. * expect('everthing').to.be.ok;
  367. * expect(1).to.be.ok;
  368. * expect(false).to.not.be.ok;
  369. * expect(undefined).to.not.be.ok;
  370. * expect(null).to.not.be.ok;
  371. *
  372. * @name ok
  373. * @api public
  374. */
  375. Assertion.addProperty('ok', function () {
  376. this.assert(
  377. flag(this, 'object')
  378. , 'expected #{this} to be truthy'
  379. , 'expected #{this} to be falsy');
  380. });
  381. /**
  382. * ### .true
  383. *
  384. * Asserts that the target is `true`.
  385. *
  386. * expect(true).to.be.true;
  387. * expect(1).to.not.be.true;
  388. *
  389. * @name true
  390. * @api public
  391. */
  392. Assertion.addProperty('true', function () {
  393. this.assert(
  394. true === flag(this, 'object')
  395. , 'expected #{this} to be true'
  396. , 'expected #{this} to be false'
  397. , this.negate ? false : true
  398. );
  399. });
  400. /**
  401. * ### .false
  402. *
  403. * Asserts that the target is `false`.
  404. *
  405. * expect(false).to.be.false;
  406. * expect(0).to.not.be.false;
  407. *
  408. * @name false
  409. * @api public
  410. */
  411. Assertion.addProperty('false', function () {
  412. this.assert(
  413. false === flag(this, 'object')
  414. , 'expected #{this} to be false'
  415. , 'expected #{this} to be true'
  416. , this.negate ? true : false
  417. );
  418. });
  419. /**
  420. * ### .null
  421. *
  422. * Asserts that the target is `null`.
  423. *
  424. * expect(null).to.be.null;
  425. * expect(undefined).not.to.be.null;
  426. *
  427. * @name null
  428. * @api public
  429. */
  430. Assertion.addProperty('null', function () {
  431. this.assert(
  432. null === flag(this, 'object')
  433. , 'expected #{this} to be null'
  434. , 'expected #{this} not to be null'
  435. );
  436. });
  437. /**
  438. * ### .undefined
  439. *
  440. * Asserts that the target is `undefined`.
  441. *
  442. * expect(undefined).to.be.undefined;
  443. * expect(null).to.not.be.undefined;
  444. *
  445. * @name undefined
  446. * @api public
  447. */
  448. Assertion.addProperty('undefined', function () {
  449. this.assert(
  450. undefined === flag(this, 'object')
  451. , 'expected #{this} to be undefined'
  452. , 'expected #{this} not to be undefined'
  453. );
  454. });
  455. /**
  456. * ### .exist
  457. *
  458. * Asserts that the target is neither `null` nor `undefined`.
  459. *
  460. * var foo = 'hi'
  461. * , bar = null
  462. * , baz;
  463. *
  464. * expect(foo).to.exist;
  465. * expect(bar).to.not.exist;
  466. * expect(baz).to.not.exist;
  467. *
  468. * @name exist
  469. * @api public
  470. */
  471. Assertion.addProperty('exist', function () {
  472. this.assert(
  473. null != flag(this, 'object')
  474. , 'expected #{this} to exist'
  475. , 'expected #{this} to not exist'
  476. );
  477. });
  478. /**
  479. * ### .empty
  480. *
  481. * Asserts that the target's length is `0`. For arrays, it checks
  482. * the `length` property. For objects, it gets the count of
  483. * enumerable keys.
  484. *
  485. * expect([]).to.be.empty;
  486. * expect('').to.be.empty;
  487. * expect({}).to.be.empty;
  488. *
  489. * @name empty
  490. * @api public
  491. */
  492. Assertion.addProperty('empty', function () {
  493. var obj = flag(this, 'object')
  494. , expected = obj;
  495. if (Array.isArray(obj) || 'string' === typeof object) {
  496. expected = obj.length;
  497. } else if (typeof obj === 'object') {
  498. expected = Object.keys(obj).length;
  499. }
  500. this.assert(
  501. !expected
  502. , 'expected #{this} to be empty'
  503. , 'expected #{this} not to be empty'
  504. );
  505. });
  506. /**
  507. * ### .arguments
  508. *
  509. * Asserts that the target is an arguments object.
  510. *
  511. * function test () {
  512. * expect(arguments).to.be.arguments;
  513. * }
  514. *
  515. * @name arguments
  516. * @alias Arguments
  517. * @api public
  518. */
  519. function checkArguments () {
  520. var obj = flag(this, 'object')
  521. , type = Object.prototype.toString.call(obj);
  522. this.assert(
  523. '[object Arguments]' === type
  524. , 'expected #{this} to be arguments but got ' + type
  525. , 'expected #{this} to not be arguments'
  526. );
  527. }
  528. Assertion.addProperty('arguments', checkArguments);
  529. Assertion.addProperty('Arguments', checkArguments);
  530. /**
  531. * ### .equal(value)
  532. *
  533. * Asserts that the target is strictly equal (`===`) to `value`.
  534. * Alternately, if the `deep` flag is set, asserts that
  535. * the target is deeply equal to `value`.
  536. *
  537. * expect('hello').to.equal('hello');
  538. * expect(42).to.equal(42);
  539. * expect(1).to.not.equal(true);
  540. * expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' });
  541. * expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });
  542. *
  543. * @name equal
  544. * @alias eq
  545. * @alias deep.equal
  546. * @param {Mixed} value
  547. * @api public
  548. */
  549. function assertEqual (val) {
  550. var obj = flag(this, 'object');
  551. if (flag(this, 'deep')) {
  552. return this.eql(val);
  553. } else {
  554. this.assert(
  555. val === obj
  556. , 'expected #{this} to equal #{exp}'
  557. , 'expected #{this} to not equal #{exp}'
  558. , val
  559. );
  560. }
  561. }
  562. Assertion.addMethod('equal', assertEqual);
  563. Assertion.addMethod('eq', assertEqual);
  564. /**
  565. * ### .eql(value)
  566. *
  567. * Asserts that the target is deeply equal to `value`.
  568. *
  569. * expect({ foo: 'bar' }).to.eql({ foo: 'bar' });
  570. * expect([ 1, 2, 3 ]).to.eql([ 1, 2, 3 ]);
  571. *
  572. * @name eql
  573. * @param {Mixed} value
  574. * @api public
  575. */
  576. Assertion.addMethod('eql', function (obj) {
  577. this.assert(
  578. _.eql(obj, flag(this, 'object'))
  579. , 'expected #{this} to deeply equal #{exp}'
  580. , 'expected #{this} to not deeply equal #{exp}'
  581. , obj
  582. );
  583. });
  584. /**
  585. * ### .above(value)
  586. *
  587. * Asserts that the target is greater than `value`.
  588. *
  589. * expect(10).to.be.above(5);
  590. *
  591. * Can also be used in conjunction with `length` to
  592. * assert a minimum length. The benefit being a
  593. * more informative error message than if the length
  594. * was supplied directly.
  595. *
  596. * expect('foo').to.have.length.above(2);
  597. * expect([ 1, 2, 3 ]).to.have.length.above(2);
  598. *
  599. * @name above
  600. * @alias gt
  601. * @alias greaterThan
  602. * @param {Number} value
  603. * @api public
  604. */
  605. function assertAbove (n) {
  606. var obj = flag(this, 'object');
  607. if (flag(this, 'doLength')) {
  608. new Assertion(obj).to.have.property('length');
  609. var len = obj.length;
  610. this.assert(
  611. len > n
  612. , 'expected #{this} to have a length above #{exp} but got #{act}'
  613. , 'expected #{this} to not have a length above #{exp}'
  614. , n
  615. , len
  616. );
  617. } else {
  618. this.assert(
  619. obj > n
  620. , 'expected #{this} to be above ' + n
  621. , 'expected #{this} to be below ' + n
  622. );
  623. }
  624. }
  625. Assertion.addMethod('above', assertAbove);
  626. Assertion.addMethod('gt', assertAbove);
  627. Assertion.addMethod('greaterThan', assertAbove);
  628. /**
  629. * ### .below(value)
  630. *
  631. * Asserts that the target is less than `value`.
  632. *
  633. * expect(5).to.be.below(10);
  634. *
  635. * Can also be used in conjunction with `length` to
  636. * assert a maximum length. The benefit being a
  637. * more informative error message than if the length
  638. * was supplied directly.
  639. *
  640. * expect('foo').to.have.length.below(4);
  641. * expect([ 1, 2, 3 ]).to.have.length.below(4);
  642. *
  643. * @name below
  644. * @alias lt
  645. * @alias lessThan
  646. * @param {Number} value
  647. * @api public
  648. */
  649. function assertBelow (n) {
  650. var obj = flag(this, 'object');
  651. if (flag(this, 'doLength')) {
  652. new Assertion(obj).to.have.property('length');
  653. var len = obj.length;
  654. this.assert(
  655. len < n
  656. , 'expected #{this} to have a length below #{exp} but got #{act}'
  657. , 'expected #{this} to not have a length below #{exp}'
  658. , n
  659. , len
  660. );
  661. } else {
  662. this.assert(
  663. obj < n
  664. , 'expected #{this} to be below ' + n
  665. , 'expected #{this} to be above ' + n
  666. );
  667. }
  668. }
  669. Assertion.addMethod('below', assertBelow);
  670. Assertion.addMethod('lt', assertBelow);
  671. Assertion.addMethod('lessThan', assertBelow);
  672. /**
  673. * ### .within(start, finish)
  674. *
  675. * Asserts that the target is within a range.
  676. *
  677. * expect(7).to.be.within(5,10);
  678. *
  679. * Can also be used in conjunction with `length` to
  680. * assert a length range. The benefit being a
  681. * more informative error message than if the length
  682. * was supplied directly.
  683. *
  684. * expect('foo').to.have.length.within(2,4);
  685. * expect([ 1, 2, 3 ]).to.have.length.within(2,4);
  686. *
  687. * @name within
  688. * @param {Number} start lowerbound inclusive
  689. * @param {Number} finish upperbound inclusive
  690. * @api public
  691. */
  692. Assertion.addMethod('within', function (start, finish) {
  693. var obj = flag(this, 'object')
  694. , range = start + '..' + finish;
  695. if (flag(this, 'doLength')) {
  696. new Assertion(obj).to.have.property('length');
  697. var len = obj.length;
  698. this.assert(
  699. len >= start && len <= finish
  700. , 'expected #{this} to have a length within ' + range
  701. , 'expected #{this} to not have a length within ' + range
  702. );
  703. } else {
  704. this.assert(
  705. obj >= start && obj <= finish
  706. , 'expected #{this} to be within ' + range
  707. , 'expected #{this} to not be within ' + range
  708. );
  709. }
  710. });
  711. /**
  712. * ### .instanceof(constructor)
  713. *
  714. * Asserts that the target is an instance of `constructor`.
  715. *
  716. * var Tea = function (name) { this.name = name; }
  717. * , Chai = new Tea('chai');
  718. *
  719. * expect(Chai).to.be.an.instanceof(Tea);
  720. * expect([ 1, 2, 3 ]).to.be.instanceof(Array);
  721. *
  722. * @name instanceof
  723. * @param {Constructor} constructor
  724. * @alias instanceOf
  725. * @api public
  726. */
  727. function assertInstanceOf (constructor) {
  728. var name = _.getName(constructor);
  729. this.assert(
  730. flag(this, 'object') instanceof constructor
  731. , 'expected #{this} to be an instance of ' + name
  732. , 'expected #{this} to not be an instance of ' + name
  733. );
  734. };
  735. Assertion.addMethod('instanceof', assertInstanceOf);
  736. Assertion.addMethod('instanceOf', assertInstanceOf);
  737. /**
  738. * ### .property(name, [value])
  739. *
  740. * Asserts that the target has a property `name`, optionally asserting that
  741. * the value of that property is strictly equal to `value`.
  742. * If the `deep` flag is set, you can use dot- and bracket-notation for deep
  743. * references into objects and arrays.
  744. *
  745. * // simple referencing
  746. * var obj = { foo: 'bar' };
  747. * expect(obj).to.have.property('foo');
  748. * expect(obj).to.have.property('foo', 'bar');
  749. *
  750. * // deep referencing
  751. * var deepObj = {
  752. * green: { tea: 'matcha' }
  753. * , teas: [ 'chai', 'matcha', { tea: 'konacha' } ]
  754. * };
  755. * expect(deepObj).to.have.deep.property('green.tea', 'matcha');
  756. * expect(deepObj).to.have.deep.property('teas[1]', 'matcha');
  757. * expect(deepObj).to.have.deep.property('teas[2].tea', 'konacha');
  758. *
  759. * You can also use an array as the starting point of a `deep.property`
  760. * assertion, or traverse nested arrays.
  761. *
  762. * var arr = [
  763. * [ 'chai', 'matcha', 'konacha' ]
  764. * , [ { tea: 'chai' }
  765. * , { tea: 'matcha' }
  766. * , { tea: 'konacha' } ]
  767. * ];
  768. *
  769. * expect(arr).to.have.deep.property('[0][1]', 'matcha');
  770. * expect(arr).to.have.deep.property('[1][2].tea', 'konacha');
  771. *
  772. * Furthermore, `property` changes the subject of the assertion
  773. * to be the value of that property from the original object. This
  774. * permits for further chainable assertions on that property.
  775. *
  776. * expect(obj).to.have.property('foo')
  777. * .that.is.a('string');
  778. * expect(deepObj).to.have.property('green')
  779. * .that.is.an('object')
  780. * .that.deep.equals({ tea: 'matcha' });
  781. * expect(deepObj).to.have.property('teas')
  782. * .that.is.an('array')
  783. * .with.deep.property('[2]')
  784. * .that.deep.equals({ tea: 'konacha' });
  785. *
  786. * @name property
  787. * @alias deep.property
  788. * @param {String} name
  789. * @param {Mixed} value (optional)
  790. * @returns value of property for chaining
  791. * @api public
  792. */
  793. Assertion.addMethod('property', function (name, val) {
  794. var obj = flag(this, 'object')
  795. , value = flag(this, 'deep') ? _.getPathValue(name, obj) : obj[name]
  796. , descriptor = flag(this, 'deep') ? 'deep property ' : 'property '
  797. , negate = flag(this, 'negate');
  798. if (negate && undefined !== val) {
  799. if (undefined === value) {
  800. throw new Error(_.inspect(obj) + ' has no ' + descriptor + _.inspect(name));
  801. }
  802. } else {
  803. this.assert(
  804. undefined !== value
  805. , 'expected #{this} to have a ' + descriptor + _.inspect(name)
  806. , 'expected #{this} to not have ' + descriptor + _.inspect(name));
  807. }
  808. if (undefined !== val) {
  809. this.assert(
  810. val === value
  811. , 'expected #{this} to have a ' + descriptor + _.inspect(name) + ' of #{exp}, but got #{act}'
  812. , 'expected #{this} to not have a ' + descriptor + _.inspect(name) + ' of #{act}'
  813. , val
  814. , value
  815. );
  816. }
  817. flag(this, 'object', value);
  818. });
  819. /**
  820. * ### .ownProperty(name)
  821. *
  822. * Asserts that the target has an own property `name`.
  823. *
  824. * expect('test').to.have.ownProperty('length');
  825. *
  826. * @name ownProperty
  827. * @alias haveOwnProperty
  828. * @param {String} name
  829. * @api public
  830. */
  831. function assertOwnProperty (name) {
  832. var obj = flag(this, 'object');
  833. this.assert(
  834. obj.hasOwnProperty(name)
  835. , 'expected #{this} to have own property ' + _.inspect(name)
  836. , 'expected #{this} to not have own property ' + _.inspect(name)
  837. );
  838. }
  839. Assertion.addMethod('ownProperty', assertOwnProperty);
  840. Assertion.addMethod('haveOwnProperty', assertOwnProperty);
  841. /**
  842. * ### .length(value)
  843. *
  844. * Asserts that the target's `length` property has
  845. * the expected value.
  846. *
  847. * expect([ 1, 2, 3]).to.have.length(3);
  848. * expect('foobar').to.have.length(6);
  849. *
  850. * Can also be used as a chain precursor to a value
  851. * comparison for the length property.
  852. *
  853. * expect('foo').to.have.length.above(2);
  854. * expect([ 1, 2, 3 ]).to.have.length.above(2);
  855. * expect('foo').to.have.length.below(4);
  856. * expect([ 1, 2, 3 ]).to.have.length.below(4);
  857. * expect('foo').to.have.length.within(2,4);
  858. * expect([ 1, 2, 3 ]).to.have.length.within(2,4);
  859. *
  860. * @name length
  861. * @alias lengthOf
  862. * @param {Number} length
  863. * @api public
  864. */
  865. function assertLengthChain () {
  866. flag(this, 'doLength', true);
  867. }
  868. function assertLength (n) {
  869. var obj = flag(this, 'object');
  870. new Assertion(obj).to.have.property('length');
  871. var len = obj.length;
  872. this.assert(
  873. len == n
  874. , 'expected #{this} to have a length of #{exp} but got #{act}'
  875. , 'expected #{this} to not have a length of #{act}'
  876. , n
  877. , len
  878. );
  879. }
  880. Assertion.addChainableMethod('length', assertLength, assertLengthChain);
  881. Assertion.addMethod('lengthOf', assertLength, assertLengthChain);
  882. /**
  883. * ### .match(regexp)
  884. *
  885. * Asserts that the target matches a regular expression.
  886. *
  887. * expect('foobar').to.match(/^foo/);
  888. *
  889. * @name match
  890. * @param {RegExp} RegularExpression
  891. * @api public
  892. */
  893. Assertion.addMethod('match', function (re) {
  894. var obj = flag(this, 'object');
  895. this.assert(
  896. re.exec(obj)
  897. , 'expected #{this} to match ' + re
  898. , 'expected #{this} not to match ' + re
  899. );
  900. });
  901. /**
  902. * ### .string(string)
  903. *
  904. * Asserts that the string target contains another string.
  905. *
  906. * expect('foobar').to.have.string('bar');
  907. *
  908. * @name string
  909. * @param {String} string
  910. * @api public
  911. */
  912. Assertion.addMethod('string', function (str) {
  913. var obj = flag(this, 'object');
  914. new Assertion(obj).is.a('string');
  915. this.assert(
  916. ~obj.indexOf(str)
  917. , 'expected #{this} to contain ' + _.inspect(str)
  918. , 'expected #{this} to not contain ' + _.inspect(str)
  919. );
  920. });
  921. /**
  922. * ### .keys(key1, [key2], [...])
  923. *
  924. * Asserts that the target has exactly the given keys, or
  925. * asserts the inclusion of some keys when using the
  926. * `include` or `contain` modifiers.
  927. *
  928. * expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']);
  929. * expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('foo', 'bar');
  930. *
  931. * @name keys
  932. * @alias key
  933. * @param {String...|Array} keys
  934. * @api public
  935. */
  936. function assertKeys (keys) {
  937. var obj = flag(this, 'object')
  938. , str
  939. , ok = true;
  940. keys = keys instanceof Array
  941. ? keys
  942. : Array.prototype.slice.call(arguments);
  943. if (!keys.length) throw new Error('keys required');
  944. var actual = Object.keys(obj)
  945. , len = keys.length;
  946. // Inclusion
  947. ok = keys.every(function(key){
  948. return ~actual.indexOf(key);
  949. });
  950. // Strict
  951. if (!flag(this, 'negate') && !flag(this, 'contains')) {
  952. ok = ok && keys.length == actual.length;
  953. }
  954. // Key string
  955. if (len > 1) {
  956. keys = keys.map(function(key){
  957. return _.inspect(key);
  958. });
  959. var last = keys.pop();
  960. str = keys.join(', ') + ', and ' + last;
  961. } else {
  962. str = _.inspect(keys[0]);
  963. }
  964. // Form
  965. str = (len > 1 ? 'keys ' : 'key ') + str;
  966. // Have / include
  967. str = (flag(this, 'contains') ? 'contain ' : 'have ') + str;
  968. // Assertion
  969. this.assert(
  970. ok
  971. , 'expected #{this} to ' + str
  972. , 'expected #{this} to not ' + str
  973. );
  974. }
  975. Assertion.addMethod('keys', assertKeys);
  976. Assertion.addMethod('key', assertKeys);
  977. /**
  978. * ### .throw(constructor)
  979. *
  980. * Asserts that the function target will throw a specific error, or specific type of error
  981. * (as determined using `instanceof`), optionally with a RegExp or string inclusion test
  982. * for the error's message.
  983. *
  984. * var err = new ReferenceError('This is a bad function.');
  985. * var fn = function () { throw err; }
  986. * expect(fn).to.throw(ReferenceError);
  987. * expect(fn).to.throw(Error);
  988. * expect(fn).to.throw(/bad function/);
  989. * expect(fn).to.not.throw('good function');
  990. * expect(fn).to.throw(ReferenceError, /bad function/);
  991. * expect(fn).to.throw(err);
  992. * expect(fn).to.not.throw(new RangeError('Out of range.'));
  993. *
  994. * Please note that when a throw expectation is negated, it will check each
  995. * parameter independently, starting with error constructor type. The appropriate way
  996. * to check for the existence of a type of error but for a message that does not match
  997. * is to use `and`.
  998. *
  999. * expect(fn).to.throw(ReferenceError)
  1000. * .and.not.throw(/good function/);
  1001. *
  1002. * @name throw
  1003. * @alias throws
  1004. * @alias Throw
  1005. * @param {ErrorConstructor} constructor
  1006. * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
  1007. * @api public
  1008. */
  1009. function assertThrows (constructor, msg) {
  1010. var obj = flag(this, 'object');
  1011. new Assertion(obj).is.a('function');
  1012. var thrown = false
  1013. , desiredError = null
  1014. , name = null;
  1015. if (arguments.length === 0) {
  1016. msg = null;
  1017. constructor = null;
  1018. } else if (constructor && (constructor instanceof RegExp || 'string' === typeof constructor)) {
  1019. msg = constructor;
  1020. constructor = null;
  1021. } else if (constructor && constructor instanceof Error) {
  1022. desiredError = constructor;
  1023. constructor = null;
  1024. msg = null;
  1025. } else if (typeof constructor === 'function') {
  1026. name = (new constructor()).name;
  1027. } else {
  1028. constructor = null;
  1029. }
  1030. try {
  1031. obj();
  1032. } catch (err) {
  1033. // first, check desired error
  1034. if (desiredError) {
  1035. this.assert(
  1036. err === desiredError
  1037. , 'expected #{this} to throw ' + _.inspect(desiredError) + ' but ' + _.inspect(err) + ' was thrown'
  1038. , 'expected #{this} to not throw ' + _.inspect(desiredError)
  1039. );
  1040. return this;
  1041. }
  1042. // next, check constructor
  1043. if (constructor) {
  1044. this.assert(
  1045. err instanceof constructor
  1046. , 'expected #{this} to throw ' + name + ' but a ' + err.name + ' was thrown'
  1047. , 'expected #{this} to not throw ' + name );
  1048. if (!msg) return this;
  1049. }
  1050. // next, check message
  1051. if (err.message && msg && msg instanceof RegExp) {
  1052. this.assert(
  1053. msg.exec(err.message)
  1054. , 'expected #{this} to throw error matching ' + msg + ' but got ' + _.inspect(err.message)
  1055. , 'expected #{this} to throw error not matching ' + msg
  1056. );
  1057. return this;
  1058. } else if (err.message && msg && 'string' === typeof msg) {
  1059. this.assert(
  1060. ~err.message.indexOf(msg)
  1061. , 'expected #{this} to throw error including #{exp} but got #{act}'
  1062. , 'expected #{this} to throw error not including #{act}'
  1063. , msg
  1064. , err.message
  1065. );
  1066. return this;
  1067. } else {
  1068. thrown = true;
  1069. }
  1070. }
  1071. var expectedThrown = name ? name : desiredError ? _.inspect(desiredError) : 'an error';
  1072. this.assert(
  1073. thrown === true
  1074. , 'expected #{this} to throw ' + expectedThrown
  1075. , 'expected #{this} to not throw ' + expectedThrown
  1076. );
  1077. };
  1078. Assertion.addMethod('throw', assertThrows);
  1079. Assertion.addMethod('throws', assertThrows);
  1080. Assertion.addMethod('Throw', assertThrows);
  1081. /**
  1082. * ### .respondTo(method)
  1083. *
  1084. * Asserts that the object or class target will respond to a method.
  1085. *
  1086. * Klass.prototype.bar = function(){};
  1087. * expect(Klass).to.respondTo('bar');
  1088. * expect(obj).to.respondTo('bar');
  1089. *
  1090. * To check if a constructor will respond to a static function,
  1091. * set the `itself` flag.
  1092. *
  1093. * Klass.baz = function(){};
  1094. * expect(Klass).itself.to.respondTo('baz');
  1095. *
  1096. * @name respondTo
  1097. * @param {String} method
  1098. * @api public
  1099. */
  1100. Assertion.addMethod('respondTo', function (method) {
  1101. var obj = flag(this, 'object')
  1102. , itself = flag(this, 'itself')
  1103. , context = ('function' === typeof obj && !itself)
  1104. ? obj.prototype[method]
  1105. : obj[method];
  1106. this.assert(
  1107. 'function' === typeof context
  1108. , 'expected #{this} to respond to ' + _.inspect(method)
  1109. , 'expected #{this} to not respond to ' + _.inspect(method)
  1110. );
  1111. });
  1112. /**
  1113. * ### .itself
  1114. *
  1115. * Sets the `itself` flag, later used by the `respondTo` assertion.
  1116. *
  1117. * function Foo() {}
  1118. * Foo.bar = function() {}
  1119. * Foo.prototype.baz = function() {}
  1120. *
  1121. * expect(Foo).itself.to.respondTo('bar');
  1122. * expect(Foo).itself.not.to.respondTo('baz');
  1123. *
  1124. * @name itself
  1125. * @api public
  1126. */
  1127. Assertion.addProperty('itself', function () {
  1128. flag(this, 'itself', true);
  1129. });
  1130. /**
  1131. * ### .satisfy(method)
  1132. *
  1133. * Asserts that the target passes a given truth test.
  1134. *
  1135. * expect(1).to.satisfy(function(num) { return num > 0; });
  1136. *
  1137. * @name satisfy
  1138. * @param {Function} matcher
  1139. * @api public
  1140. */
  1141. Assertion.addMethod('satisfy', function (matcher) {
  1142. var obj = flag(this, 'object');
  1143. this.assert(
  1144. matcher(obj)
  1145. , 'expected #{this} to satisfy ' + _.inspect(matcher)
  1146. , 'expected #{this} to not satisfy' + _.inspect(matcher)
  1147. , this.negate ? false : true
  1148. , matcher(obj)
  1149. );
  1150. });
  1151. /**
  1152. * ### .closeTo(expected, delta)
  1153. *
  1154. * Asserts that the target is equal `expected`, to within a +/- `delta` range.
  1155. *
  1156. * expect(1.5).to.be.closeTo(1, 0.5);
  1157. *
  1158. * @name closeTo
  1159. * @param {Number} expected
  1160. * @param {Number} delta
  1161. * @api public
  1162. */
  1163. Assertion.addMethod('closeTo', function (expected, delta) {
  1164. var obj = flag(this, 'object');
  1165. this.assert(
  1166. Math.abs(obj - expected) <= delta
  1167. , 'expected #{this} to be close to ' + expected + ' +/- ' + delta
  1168. , 'expected #{this} not to be close to ' + expected + ' +/- ' + delta
  1169. );
  1170. });
  1171. };
  1172. }); // module: chai/core/assertions.js
  1173. require.register("chai/interface/assert.js", function(module, exports, require){
  1174. /*!
  1175. * chai
  1176. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  1177. * MIT Licensed
  1178. */
  1179. module.exports = function (chai, util) {
  1180. /*!
  1181. * Chai dependencies.
  1182. */
  1183. var Assertion = chai.Assertion
  1184. , flag = util.flag;
  1185. /*!
  1186. * Module export.
  1187. */
  1188. /**
  1189. * ### assert(expression, message)
  1190. *
  1191. * Write your own test expressions.
  1192. *
  1193. * assert('foo' !== 'bar', 'foo is not bar');
  1194. * assert(Array.isArray([]), 'empty arrays are arrays');
  1195. *
  1196. * @param {Mixed} expression to test for truthiness
  1197. * @param {String} message to display on error
  1198. * @name assert
  1199. * @api public
  1200. */
  1201. var assert = chai.assert = function (express, errmsg) {
  1202. var test = new Assertion(null);
  1203. test.assert(
  1204. express
  1205. , errmsg
  1206. , '[ negation message unavailable ]'
  1207. );
  1208. };
  1209. /**
  1210. * ### .fail(actual, expected, [message], [operator])
  1211. *
  1212. * Throw a failure. Node.js `assert` module-compatible.
  1213. *
  1214. * @name fail
  1215. * @param {Mixed} actual
  1216. * @param {Mixed} expected
  1217. * @param {String} message
  1218. * @param {String} operator
  1219. * @api public
  1220. */
  1221. assert.fail = function (actual, expected, message, operator) {
  1222. throw new chai.AssertionError({
  1223. actual: actual
  1224. , expected: expected
  1225. , message: message
  1226. , operator: operator
  1227. , stackStartFunction: assert.fail
  1228. });
  1229. };
  1230. /**
  1231. * ### .ok(object, [message])
  1232. *
  1233. * Asserts that `object` is truthy.
  1234. *
  1235. * assert.ok('everything', 'everything is ok');
  1236. * assert.ok(false, 'this will fail');
  1237. *
  1238. * @name ok
  1239. * @param {Mixed} object to test
  1240. * @param {String} message
  1241. * @api public
  1242. */
  1243. assert.ok = function (val, msg) {
  1244. new Assertion(val, msg).is.ok;
  1245. };
  1246. /**
  1247. * ### .equal(actual, expected, [message])
  1248. *
  1249. * Asserts non-strict equality (`==`) of `actual` and `expected`.
  1250. *
  1251. * assert.equal(3, '3', '== coerces values to strings');
  1252. *
  1253. * @name equal
  1254. * @param {Mixed} actual
  1255. * @param {Mixed} expected
  1256. * @param {String} message
  1257. * @api public
  1258. */
  1259. assert.equal = function (act, exp, msg) {
  1260. var test = new Assertion(act, msg);
  1261. test.assert(
  1262. exp == flag(test, 'object')
  1263. , 'expected #{this} to equal #{exp}'
  1264. , 'expected #{this} to not equal #{act}'
  1265. , exp
  1266. , act
  1267. );
  1268. };
  1269. /**
  1270. * ### .notEqual(actual, expected, [message])
  1271. *
  1272. * Asserts non-strict inequality (`!=`) of `actual` and `expected`.
  1273. *
  1274. * assert.notEqual(3, 4, 'these numbers are not equal');
  1275. *
  1276. * @name notEqual
  1277. * @param {Mixed} actual
  1278. * @param {Mixed} expected
  1279. * @param {String} message
  1280. * @api public
  1281. */
  1282. assert.notEqual = function (act, exp, msg) {
  1283. var test = new Assertion(act, msg);
  1284. test.assert(
  1285. exp != flag(test, 'object')
  1286. , 'expected #{this} to not equal #{exp}'
  1287. , 'expected #{this} to equal #{act}'
  1288. , exp
  1289. , act
  1290. );
  1291. };
  1292. /**
  1293. * ### .strictEqual(actual, expected, [message])
  1294. *
  1295. * Asserts strict equality (`===`) of `actual` and `expected`.
  1296. *
  1297. * assert.strictEqual(true, true, 'these booleans are strictly equal');
  1298. *
  1299. * @name strictEqual
  1300. * @param {Mixed} actual
  1301. * @param {Mixed} expected
  1302. * @param {String} message
  1303. * @api public
  1304. */
  1305. assert.strictEqual = function (act, exp, msg) {
  1306. new Assertion(act, msg).to.equal(exp);
  1307. };
  1308. /**
  1309. * ### .notStrictEqual(actual, expected, [message])
  1310. *
  1311. * Asserts strict inequality (`!==`) of `actual` and `expected`.
  1312. *
  1313. * assert.notStrictEqual(3, '3', 'no coercion for strict equality');
  1314. *
  1315. * @name notStrictEqual
  1316. * @param {Mixed} actual
  1317. * @param {Mixed} expected
  1318. * @param {String} message
  1319. * @api public
  1320. */
  1321. assert.notStrictEqual = function (act, exp, msg) {
  1322. new Assertion(act, msg).to.not.equal(exp);
  1323. };
  1324. /**
  1325. * ### .deepEqual(actual, expected, [message])
  1326. *
  1327. * Asserts that `actual` is deeply equal to `expected`.
  1328. *
  1329. * assert.deepEqual({ tea: 'green' }, { tea: 'green' });
  1330. *
  1331. * @name deepEqual
  1332. * @param {Mixed} actual
  1333. * @param {Mixed} expected
  1334. * @param {String} message
  1335. * @api public
  1336. */
  1337. assert.deepEqual = function (act, exp, msg) {
  1338. new Assertion(act, msg).to.eql(exp);
  1339. };
  1340. /**
  1341. * ### .notDeepEqual(actual, expected, [message])
  1342. *
  1343. * Assert that `actual` is not deeply equal to `expected`.
  1344. *
  1345. * assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });
  1346. *
  1347. * @name notDeepEqual
  1348. * @param {Mixed} actual
  1349. * @param {Mixed} expected
  1350. * @param {String} message
  1351. * @api public
  1352. */
  1353. assert.notDeepEqual = function (act, exp, msg) {
  1354. new Assertion(act, msg).to.not.eql(exp);
  1355. };
  1356. /**
  1357. * ### .isTrue(value, [message])
  1358. *
  1359. * Asserts that `value` is true.
  1360. *
  1361. * var teaServed = true;
  1362. * assert.isTrue(teaServed, 'the tea has been served');
  1363. *
  1364. * @name isTrue
  1365. * @param {Mixed} value
  1366. * @param {String} message
  1367. * @api public
  1368. */
  1369. assert.isTrue = function (val, msg) {
  1370. new Assertion(val, msg).is['true'];
  1371. };
  1372. /**
  1373. * ### .isFalse(value, [message])
  1374. *
  1375. * Asserts that `value` is false.
  1376. *
  1377. * var teaServed = false;
  1378. * assert.isFalse(teaServed, 'no tea yet? hmm...');
  1379. *
  1380. * @name isFalse
  1381. * @param {Mixed} value
  1382. * @param {String} message
  1383. * @api public
  1384. */
  1385. assert.isFalse = function (val, msg) {
  1386. new Assertion(val, msg).is['false'];
  1387. };
  1388. /**
  1389. * ### .isNull(value, [message])
  1390. *
  1391. * Asserts that `value` is null.
  1392. *
  1393. * assert.isNull(err, 'there was no error');
  1394. *
  1395. * @name isNull
  1396. * @param {Mixed} value
  1397. * @param {String} message
  1398. * @api public
  1399. */
  1400. assert.isNull = function (val, msg) {
  1401. new Assertion(val, msg).to.equal(null);
  1402. };
  1403. /**
  1404. * ### .isNotNull(value, [message])
  1405. *
  1406. * Asserts that `value` is not null.
  1407. *
  1408. * var tea = 'tasty chai';
  1409. * assert.isNotNull(tea, 'great, time for tea!');
  1410. *
  1411. * @name isNotNull
  1412. * @param {Mixed} value
  1413. * @param {String} message
  1414. * @api public
  1415. */
  1416. assert.isNotNull = function (val, msg) {
  1417. new Assertion(val, msg).to.not.equal(null);
  1418. };
  1419. /**
  1420. * ### .isUndefined(value, [message])
  1421. *
  1422. * Asserts that `value` is `undefined`.
  1423. *
  1424. * var tea;
  1425. * assert.isUndefined(tea, 'no tea defined');
  1426. *
  1427. * @name isUndefined
  1428. * @param {Mixed} value
  1429. * @param {String} message
  1430. * @api public
  1431. */
  1432. assert.isUndefined = function (val, msg) {
  1433. new Assertion(val, msg).to.equal(undefined);
  1434. };
  1435. /**
  1436. * ### .isDefined(value, [message])
  1437. *
  1438. * Asserts that `value` is not `undefined`.
  1439. *
  1440. * var tea = 'cup of chai';
  1441. * assert.isDefined(tea, 'tea has been defined');
  1442. *
  1443. * @name isUndefined
  1444. * @param {Mixed} value
  1445. * @param {String} message
  1446. * @api public
  1447. */
  1448. assert.isDefined = function (val, msg) {
  1449. new Assertion(val, msg).to.not.equal(undefined);
  1450. };
  1451. /**
  1452. * ### .isFunction(value, [message])
  1453. *
  1454. * Asserts that `value` is a function.
  1455. *
  1456. * function serveTea() { return 'cup of tea'; };
  1457. * assert.isFunction(serveTea, 'great, we can have tea now');
  1458. *
  1459. * @name isFunction
  1460. * @param {Mixed} value
  1461. * @param {String} message
  1462. * @api public
  1463. */
  1464. assert.isFunction = function (val, msg) {
  1465. new Assertion(val, msg).to.be.a('function');
  1466. };
  1467. /**
  1468. * ### .isNotFunction(value, [message])
  1469. *
  1470. * Asserts that `value` is _not_ a function.
  1471. *
  1472. * var serveTea = [ 'heat', 'pour', 'sip' ];
  1473. * assert.isNotFunction(serveTea, 'great, we have listed the steps');
  1474. *
  1475. * @name isNotFunction
  1476. * @param {Mixed} value
  1477. * @param {String} message
  1478. * @api public
  1479. */
  1480. assert.isNotFunction = function (val, msg) {
  1481. new Assertion(val, msg).to.not.be.a('function');
  1482. };
  1483. /**
  1484. * ### .isObject(value, [message])
  1485. *
  1486. * Asserts that `value` is an object (as revealed by
  1487. * `Object.prototype.toString`).
  1488. *
  1489. * var selection = { name: 'Chai', serve: 'with spices' };
  1490. * assert.isObject(selection, 'tea selection is an object');
  1491. *
  1492. * @name isObject
  1493. * @param {Mixed} value
  1494. * @param {String} message
  1495. * @api public
  1496. */
  1497. assert.isObject = function (val, msg) {
  1498. new Assertion(val, msg).to.be.a('object');
  1499. };
  1500. /**
  1501. * ### .isNotObject(value, [message])
  1502. *
  1503. * Asserts that `value` is _not_ an object.
  1504. *
  1505. * var selection = 'chai'
  1506. * assert.isObject(selection, 'tea selection is not an object');
  1507. * assert.isObject(null, 'null is not an object');
  1508. *
  1509. * @name isNotObject
  1510. * @param {Mixed} value
  1511. * @param {String} message
  1512. * @api public
  1513. */
  1514. assert.isNotObject = function (val, msg) {
  1515. new Assertion(val, msg).to.not.be.a('object');
  1516. };
  1517. /**
  1518. * ### .isArray(value, [message])
  1519. *
  1520. * Asserts that `value` is an array.
  1521. *
  1522. * var menu = [ 'green', 'chai', 'oolong' ];
  1523. * assert.isArray(menu, 'what kind of tea do we want?');
  1524. *
  1525. * @name isArray
  1526. * @param {Mixed} value
  1527. * @param {String} message
  1528. * @api public
  1529. */
  1530. assert.isArray = function (val, msg) {
  1531. new Assertion(val, msg).to.be.an('array');
  1532. };
  1533. /**
  1534. * ### .isNotArray(value, [message])
  1535. *
  1536. * Asserts that `value` is _not_ an array.
  1537. *
  1538. * var menu = 'green|chai|oolong';
  1539. * assert.isNotArray(menu, 'what kind of tea do we want?');
  1540. *
  1541. * @name isNotArray
  1542. * @param {Mixed} value
  1543. * @param {String} message
  1544. * @api public
  1545. */
  1546. assert.isNotArray = function (val, msg) {
  1547. new Assertion(val, msg).to.not.be.an('array');
  1548. };
  1549. /**
  1550. * ### .isString(value, [message])
  1551. *
  1552. * Asserts that `value` is a string.
  1553. *
  1554. * var teaOrder = 'chai';
  1555. * assert.isString(teaOrder, 'order placed');
  1556. *
  1557. * @name isString
  1558. * @param {Mixed} value
  1559. * @param {String} message
  1560. * @api public
  1561. */
  1562. assert.isString = function (val, msg) {
  1563. new Assertion(val, msg).to.be.a('string');
  1564. };
  1565. /**
  1566. * ### .isNotString(value, [message])
  1567. *
  1568. * Asserts that `value` is _not_ a string.
  1569. *
  1570. * var teaOrder = 4;
  1571. * assert.isNotString(teaOrder, 'order placed');
  1572. *
  1573. * @name isNotString
  1574. * @param {Mixed} value
  1575. * @param {String} message
  1576. * @api public
  1577. */
  1578. assert.isNotString = function (val, msg) {
  1579. new Assertion(val, msg).to.not.be.a('string');
  1580. };
  1581. /**
  1582. * ### .isNumber(value, [message])
  1583. *
  1584. * Asserts that `value` is a number.
  1585. *
  1586. * var cups = 2;
  1587. * assert.isNumber(cups, 'how many cups');
  1588. *
  1589. * @name isNumber
  1590. * @param {Number} value
  1591. * @param {String} message
  1592. * @api public
  1593. */
  1594. assert.isNumber = function (val, msg) {
  1595. new Assertion(val, msg).to.be.a('number');
  1596. };
  1597. /**
  1598. * ### .isNotNumber(value, [message])
  1599. *
  1600. * Asserts that `value` is _not_ a number.
  1601. *
  1602. * var cups = '2 cups please';
  1603. * assert.isNotNumber(cups, 'how many cups');
  1604. *
  1605. * @name isNotNumber
  1606. * @param {Mixed} value
  1607. * @param {String} message
  1608. * @api public
  1609. */
  1610. assert.isNotNumber = function (val, msg) {
  1611. new Assertion(val, msg).to.not.be.a('number');
  1612. };
  1613. /**
  1614. * ### .isBoolean(value, [message])
  1615. *
  1616. * Asserts that `value` is a boolean.
  1617. *
  1618. * var teaReady = true
  1619. * , teaServed = false;
  1620. *
  1621. * assert.isBoolean(teaReady, 'is the tea ready');
  1622. * assert.isBoolean(teaServed, 'has tea been served');
  1623. *
  1624. * @name isBoolean
  1625. * @param {Mixed} value
  1626. * @param {String} message
  1627. * @api public
  1628. */
  1629. assert.isBoolean = function (val, msg) {
  1630. new Assertion(val, msg).to.be.a('boolean');
  1631. };
  1632. /**
  1633. * ### .isNotBoolean(value, [message])
  1634. *
  1635. * Asserts that `value` is _not_ a boolean.
  1636. *
  1637. * var teaReady = 'yep'
  1638. * , teaServed = 'nope';
  1639. *
  1640. * assert.isNotBoolean(teaReady, 'is the tea ready');
  1641. * assert.isNotBoolean(teaServed, 'has tea been served');
  1642. *
  1643. * @name isNotBoolean
  1644. * @param {Mixed} value
  1645. * @param {String} message
  1646. * @api public
  1647. */
  1648. assert.isNotBoolean = function (val, msg) {
  1649. new Assertion(val, msg).to.not.be.a('boolean');
  1650. };
  1651. /**
  1652. * ### .typeOf(value, name, [message])
  1653. *
  1654. * Asserts that `value`'s type is `name`, as determined by
  1655. * `Object.prototype.toString`.
  1656. *
  1657. * assert.typeOf({ tea: 'chai' }, 'object', 'we have an object');
  1658. * assert.typeOf(['chai', 'jasmine'], 'array', 'we have an array');
  1659. * assert.typeOf('tea', 'string', 'we have a string');
  1660. * assert.typeOf(/tea/, 'regexp', 'we have a regular expression');
  1661. * assert.typeOf(null, 'null', 'we have a null');
  1662. * assert.typeOf(undefined, 'undefined', 'we have an undefined');
  1663. *
  1664. * @name typeOf
  1665. * @param {Mixed} value
  1666. * @param {String} name
  1667. * @param {String} message
  1668. * @api public
  1669. */
  1670. assert.typeOf = function (val, type, msg) {
  1671. new Assertion(val, msg).to.be.a(type);
  1672. };
  1673. /**
  1674. * ### .notTypeOf(value, name, [message])
  1675. *
  1676. * Asserts that `value`'s type is _not_ `name`, as determined by
  1677. * `Object.prototype.toString`.
  1678. *
  1679. * assert.notTypeOf('tea', 'number', 'strings are not numbers');
  1680. *
  1681. * @name notTypeOf
  1682. * @param {Mixed} value
  1683. * @param {String} typeof name
  1684. * @param {String} message
  1685. * @api public
  1686. */
  1687. assert.notTypeOf = function (val, type, msg) {
  1688. new Assertion(val, msg).to.not.be.a(type);
  1689. };
  1690. /**
  1691. * ### .instanceOf(object, constructor, [message])
  1692. *
  1693. * Asserts that `value` is an instance of `constructor`.
  1694. *
  1695. * var Tea = function (name) { this.name = name; }
  1696. * , chai = new Tea('chai');
  1697. *
  1698. * assert.instanceOf(chai, Tea, 'chai is an instance of tea');
  1699. *
  1700. * @name instanceOf
  1701. * @param {Object} object
  1702. * @param {Constructor} constructor
  1703. * @param {String} message
  1704. * @api public
  1705. */
  1706. assert.instanceOf = function (val, type, msg) {
  1707. new Assertion(val, msg).to.be.instanceOf(type);
  1708. };
  1709. /**
  1710. * ### .notInstanceOf(object, constructor, [message])
  1711. *
  1712. * Asserts `value` is not an instance of `constructor`.
  1713. *
  1714. * var Tea = function (name) { this.name = name; }
  1715. * , chai = new String('chai');
  1716. *
  1717. * assert.notInstanceOf(chai, Tea, 'chai is not an instance of tea');
  1718. *
  1719. * @name notInstanceOf
  1720. * @param {Object} object
  1721. * @param {Constructor} constructor
  1722. * @param {String} message
  1723. * @api public
  1724. */
  1725. assert.notInstanceOf = function (val, type, msg) {
  1726. new Assertion(val, msg).to.not.be.instanceOf(type);
  1727. };
  1728. /**
  1729. * ### .include(haystack, needle, [message])
  1730. *
  1731. * Asserts that `haystack` includes `needle`. Works
  1732. * for strings and arrays.
  1733. *
  1734. * assert.include('foobar', 'bar', 'foobar contains string "bar"');
  1735. * assert.include([ 1, 2, 3 ], 3, 'array contains value');
  1736. *
  1737. * @name include
  1738. * @param {Array|String} haystack
  1739. * @param {Mixed} needle
  1740. * @param {String} message
  1741. * @api public
  1742. */
  1743. assert.include = function (exp, inc, msg) {
  1744. var obj = new Assertion(exp, msg);
  1745. if (Array.isArray(exp)) {
  1746. obj.to.include(inc);
  1747. } else if ('string' === typeof exp) {
  1748. obj.to.contain.string(inc);
  1749. }
  1750. };
  1751. /**
  1752. * ### .match(value, regexp, [message])
  1753. *
  1754. * Asserts that `value` matches the regular expression `regexp`.
  1755. *
  1756. * assert.match('foobar', /^foo/, 'regexp matches');
  1757. *
  1758. * @name match
  1759. * @param {Mixed} value
  1760. * @param {RegExp} regexp
  1761. * @param {String} message
  1762. * @api public
  1763. */
  1764. assert.match = function (exp, re, msg) {
  1765. new Assertion(exp, msg).to.match(re);
  1766. };
  1767. /**
  1768. * ### .notMatch(value, regexp, [message])
  1769. *
  1770. * Asserts that `value` does not match the regular expression `regexp`.
  1771. *
  1772. * assert.notMatch('foobar', /^foo/, 'regexp does not match');
  1773. *
  1774. * @name notMatch
  1775. * @param {Mixed} value
  1776. * @param {RegExp} regexp
  1777. * @param {String} message
  1778. * @api public
  1779. */
  1780. assert.notMatch = function (exp, re, msg) {
  1781. new Assertion(exp, msg).to.not.match(re);
  1782. };
  1783. /**
  1784. * ### .property(object, property, [message])
  1785. *
  1786. * Asserts that `object` has a property named by `property`.
  1787. *
  1788. * assert.property({ tea: { green: 'matcha' }}, 'tea');
  1789. *
  1790. * @name property
  1791. * @param {Object} object
  1792. * @param {String} property
  1793. * @param {String} message
  1794. * @api public
  1795. */
  1796. assert.property = function (obj, prop, msg) {
  1797. new Assertion(obj, msg).to.have.property(prop);
  1798. };
  1799. /**
  1800. * ### .notProperty(object, property, [message])
  1801. *
  1802. * Asserts that `object` does _not_ have a property named by `property`.
  1803. *
  1804. * assert.notProperty({ tea: { green: 'matcha' }}, 'coffee');
  1805. *
  1806. * @name notProperty
  1807. * @param {Object} object
  1808. * @param {String} property
  1809. * @param {String} message
  1810. * @api public
  1811. */
  1812. assert.notProperty = function (obj, prop, msg) {
  1813. new Assertion(obj, msg).to.not.have.property(prop);
  1814. };
  1815. /**
  1816. * ### .deepProperty(object, property, [message])
  1817. *
  1818. * Asserts that `object` has a property named by `property`, which can be a
  1819. * string using dot- and bracket-notation for deep reference.
  1820. *
  1821. * assert.deepProperty({ tea: { green: 'matcha' }}, 'tea.green');
  1822. *
  1823. * @name deepProperty
  1824. * @param {Object} object
  1825. * @param {String} property
  1826. * @param {String} message
  1827. * @api public
  1828. */
  1829. assert.deepProperty = function (obj, prop, msg) {
  1830. new Assertion(obj, msg).to.have.deep.property(prop);
  1831. };
  1832. /**
  1833. * ### .notDeepProperty(object, property, [message])
  1834. *
  1835. * Asserts that `object` does _not_ have a property named by `property`, which
  1836. * can be a string using dot- and bracket-notation for deep reference.
  1837. *
  1838. * assert.notDeepProperty({ tea: { green: 'matcha' }}, 'tea.oolong');
  1839. *
  1840. * @name notDeepProperty
  1841. * @param {Object} object
  1842. * @param {String} property
  1843. * @param {String} message
  1844. * @api public
  1845. */
  1846. assert.notDeepProperty = function (obj, prop, msg) {
  1847. new Assertion(obj, msg).to.not.have.deep.property(prop);
  1848. };
  1849. /**
  1850. * ### .propertyVal(object, property, value, [message])
  1851. *
  1852. * Asserts that `object` has a property named by `property` with value given
  1853. * by `value`.
  1854. *
  1855. * assert.propertyVal({ tea: 'is good' }, 'tea', 'is good');
  1856. *
  1857. * @name propertyVal
  1858. * @param {Object} object
  1859. * @param {String} property
  1860. * @param {Mixed} value
  1861. * @param {String} message
  1862. * @api public
  1863. */
  1864. assert.propertyVal = function (obj, prop, val, msg) {
  1865. new Assertion(obj, msg).to.have.property(prop, val);
  1866. };
  1867. /**
  1868. * ### .propertyNotVal(object, property, value, [message])
  1869. *
  1870. * Asserts that `object` has a property named by `property`, but with a value
  1871. * different from that given by `value`.
  1872. *
  1873. * assert.propertyNotVal({ tea: 'is good' }, 'tea', 'is bad');
  1874. *
  1875. * @name propertyNotVal
  1876. * @param {Object} object
  1877. * @param {String} property
  1878. * @param {Mixed} value
  1879. * @param {String} message
  1880. * @api public
  1881. */
  1882. assert.propertyNotVal = function (obj, prop, val, msg) {
  1883. new Assertion(obj, msg).to.not.have.property(prop, val);
  1884. };
  1885. /**
  1886. * ### .deepPropertyVal(object, property, value, [message])
  1887. *
  1888. * Asserts that `object` has a property named by `property` with value given
  1889. * by `value`. `property` can use dot- and bracket-notation for deep
  1890. * reference.
  1891. *
  1892. * assert.deepPropertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'matcha');
  1893. *
  1894. * @name deepPropertyVal
  1895. * @param {Object} object
  1896. * @param {String} property
  1897. * @param {Mixed} value
  1898. * @param {String} message
  1899. * @api public
  1900. */
  1901. assert.deepPropertyVal = function (obj, prop, val, msg) {
  1902. new Assertion(obj, msg).to.have.deep.property(prop, val);
  1903. };
  1904. /**
  1905. * ### .deepPropertyNotVal(object, property, value, [message])
  1906. *
  1907. * Asserts that `object` has a property named by `property`, but with a value
  1908. * different from that given by `value`. `property` can use dot- and
  1909. * bracket-notation for deep reference.
  1910. *
  1911. * assert.deepPropertyNotVal({ tea: { green: 'matcha' }}, 'tea.green', 'konacha');
  1912. *
  1913. * @name deepPropertyNotVal
  1914. * @param {Object} object
  1915. * @param {String} property
  1916. * @param {Mixed} value
  1917. * @param {String} message
  1918. * @api public
  1919. */
  1920. assert.deepPropertyNotVal = function (obj, prop, val, msg) {
  1921. new Assertion(obj, msg).to.not.have.deep.property(prop, val);
  1922. };
  1923. /**
  1924. * ### .lengthOf(object, length, [message])
  1925. *
  1926. * Asserts that `object` has a `length` property with the expected value.
  1927. *
  1928. * assert.lengthOf([1,2,3], 3, 'array has length of 3');
  1929. * assert.lengthOf('foobar', 5, 'string has length of 6');
  1930. *
  1931. * @name lengthOf
  1932. * @param {Mixed} object
  1933. * @param {Number} length
  1934. * @param {String} message
  1935. * @api public
  1936. */
  1937. assert.lengthOf = function (exp, len, msg) {
  1938. new Assertion(exp, msg).to.have.length(len);
  1939. };
  1940. /**
  1941. * ### .throws(function, [constructor/regexp], [message])
  1942. *
  1943. * Asserts that `function` will throw an error that is an instance of
  1944. * `constructor`, or alternately that it will throw an error with message
  1945. * matching `regexp`.
  1946. *
  1947. * assert.throw(fn, ReferenceError, 'function throws a reference error');
  1948. *
  1949. * @name throws
  1950. * @alias throw
  1951. * @alias Throw
  1952. * @param {Function} function
  1953. * @param {ErrorConstructor} constructor
  1954. * @param {RegExp} regexp
  1955. * @param {String} message
  1956. * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
  1957. * @api public
  1958. */
  1959. assert.Throw = function (fn, type, msg) {
  1960. if ('string' === typeof type) {
  1961. msg = type;
  1962. type = null;
  1963. }
  1964. new Assertion(fn, msg).to.Throw(type);
  1965. };
  1966. /**
  1967. * ### .doesNotThrow(function, [constructor/regexp], [message])
  1968. *
  1969. * Asserts that `function` will _not_ throw an error that is an instance of
  1970. * `constructor`, or alternately that it will not throw an error with message
  1971. * matching `regexp`.
  1972. *
  1973. * assert.doesNotThrow(fn, Error, 'function does not throw');
  1974. *
  1975. * @name doesNotThrow
  1976. * @param {Function} function
  1977. * @param {ErrorConstructor} constructor
  1978. * @param {RegExp} regexp
  1979. * @param {String} message
  1980. * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
  1981. * @api public
  1982. */
  1983. assert.doesNotThrow = function (fn, type, msg) {
  1984. if ('string' === typeof type) {
  1985. msg = type;
  1986. type = null;
  1987. }
  1988. new Assertion(fn, msg).to.not.Throw(type);
  1989. };
  1990. /**
  1991. * ### .operator(val1, operator, val2, [message])
  1992. *
  1993. * Compares two values using `operator`.
  1994. *
  1995. * assert.operator(1, '<', 2, 'everything is ok');
  1996. * assert.operator(1, '>', 2, 'this will fail');
  1997. *
  1998. * @name operator
  1999. * @param {Mixed} val1
  2000. * @param {String} operator
  2001. * @param {Mixed} val2
  2002. * @param {String} message
  2003. * @api public
  2004. */
  2005. assert.operator = function (val, operator, val2, msg) {
  2006. if (!~['==', '===', '>', '>=', '<', '<=', '!=', '!=='].indexOf(operator)) {
  2007. throw new Error('Invalid operator "' + operator + '"');
  2008. }
  2009. var test = new Assertion(eval(val + operator + val2), msg);
  2010. test.assert(
  2011. true === flag(test, 'object')
  2012. , 'expected ' + util.inspect(val) + ' to be ' + operator + ' ' + util.inspect(val2)
  2013. , 'expected ' + util.inspect(val) + ' to not be ' + operator + ' ' + util.inspect(val2) );
  2014. };
  2015. /*!
  2016. * Undocumented / untested
  2017. */
  2018. assert.ifError = function (val, msg) {
  2019. new Assertion(val, msg).to.not.be.ok;
  2020. };
  2021. /*!
  2022. * Aliases.
  2023. */
  2024. (function alias(name, as){
  2025. assert[as] = assert[name];
  2026. return alias;
  2027. })
  2028. ('Throw', 'throw')
  2029. ('Throw', 'throws');
  2030. };
  2031. }); // module: chai/interface/assert.js
  2032. require.register("chai/interface/expect.js", function(module, exports, require){
  2033. /*!
  2034. * chai
  2035. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  2036. * MIT Licensed
  2037. */
  2038. module.exports = function (chai, util) {
  2039. chai.expect = function (val, message) {
  2040. return new chai.Assertion(val, message);
  2041. };
  2042. };
  2043. }); // module: chai/interface/expect.js
  2044. require.register("chai/interface/should.js", function(module, exports, require){
  2045. /*!
  2046. * chai
  2047. * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
  2048. * MIT Licensed
  2049. */
  2050. module.exports = function (chai, util) {
  2051. var Assertion = chai.Assertion;
  2052. function loadShould () {
  2053. // modify Object.prototype to have `should`
  2054. Object.defineProperty(Object.prototype, 'should',
  2055. { set: function () {}
  2056. , get: function(){
  2057. if (this instanceof String || this instanceof Number) {
  2058. return new Assertion(this.constructor(this));
  2059. } else if (this instanceof Boolean) {
  2060. return new Assertion(this == true);
  2061. }
  2062. return new Assertion(this);
  2063. }
  2064. , configurable: true
  2065. });
  2066. var should = {};
  2067. should.equal = function (val1, val2) {
  2068. new Assertion(val1).to.equal(val2);
  2069. };
  2070. should.Throw = function (fn, errt, errs) {
  2071. new Assertion(fn).to.Throw(errt, errs);
  2072. };
  2073. should.exist = function (val) {
  2074. new Assertion(val).to.exist;
  2075. }
  2076. // negation
  2077. should.not = {}
  2078. should.not.equal = function (val1, val2) {
  2079. new Assertion(val1).to.not.equal(val2);
  2080. };
  2081. should.not.Throw = function (fn, errt, errs) {
  2082. new Assertion(fn).to.not.Throw(errt, errs);
  2083. };
  2084. should.not.exist = function (val) {
  2085. new Assertion(val).to.not.exist;
  2086. }
  2087. should['throw'] = should['Throw'];
  2088. should.not['throw'] = should.not['Throw'];
  2089. return should;
  2090. };
  2091. chai.should = loadShould;
  2092. chai.Should = loadShould;
  2093. };
  2094. }); // module: chai/interface/should.js
  2095. require.register("chai/utils/addChainableMethod.js", function(module, exports, require){
  2096. /*!
  2097. * Chai - addChainingMethod utility
  2098. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2099. * MIT Licensed
  2100. */
  2101. /*!
  2102. * Module dependencies
  2103. */
  2104. var transferFlags = require('./transferFlags');
  2105. /**
  2106. * ### addChainableMethod (ctx, name, method, chainingBehavior)
  2107. *
  2108. * Adds a method to an object, such that the method can also be chained.
  2109. *
  2110. * utils.addChainableMethod(chai.Assertion.prototype, 'foo', function (str) {
  2111. * var obj = utils.flag(this, 'object');
  2112. * new chai.Assertion(obj).to.be.equal(str);
  2113. * });
  2114. *
  2115. * Can also be accessed directly from `chai.Assertion`.
  2116. *
  2117. * chai.Assertion.addChainableMethod('foo', fn, chainingBehavior);
  2118. *
  2119. * The result can then be used as both a method assertion, executing both `method` and
  2120. * `chainingBehavior`, or as a language chain, which only executes `chainingBehavior`.
  2121. *
  2122. * expect(fooStr).to.be.foo('bar');
  2123. * expect(fooStr).to.be.foo.equal('foo');
  2124. *
  2125. * @param {Object} ctx object to which the method is added
  2126. * @param {String} name of method to add
  2127. * @param {Function} method function to be used for `name`, when called
  2128. * @param {Function} chainingBehavior function to be called every time the property is accessed
  2129. * @name addChainableMethod
  2130. * @api public
  2131. */
  2132. module.exports = function (ctx, name, method, chainingBehavior) {
  2133. if (typeof chainingBehavior !== 'function')
  2134. chainingBehavior = function () { };
  2135. Object.defineProperty(ctx, name,
  2136. { get: function () {
  2137. chainingBehavior.call(this);
  2138. var assert = function () {
  2139. var result = method.apply(this, arguments);
  2140. return result === undefined ? this : result;
  2141. };
  2142. // Re-enumerate every time to better accomodate plugins.
  2143. var asserterNames = Object.getOwnPropertyNames(ctx);
  2144. asserterNames.forEach(function (asserterName) {
  2145. var pd = Object.getOwnPropertyDescriptor(ctx, asserterName)
  2146. , functionProtoPD = Object.getOwnPropertyDescriptor(Function.prototype, asserterName);
  2147. // Avoid trying to overwrite things that we can't, like `length` and `arguments`.
  2148. if (functionProtoPD && !functionProtoPD.configurable) return;
  2149. if (asserterName === 'arguments') return; // @see chaijs/chai/issues/69
  2150. Object.defineProperty(assert, asserterName, pd);
  2151. });
  2152. transferFlags(this, assert);
  2153. return assert;
  2154. }
  2155. , configurable: true
  2156. });
  2157. };
  2158. }); // module: chai/utils/addChainableMethod.js
  2159. require.register("chai/utils/addMethod.js", function(module, exports, require){
  2160. /*!
  2161. * Chai - addMethod utility
  2162. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2163. * MIT Licensed
  2164. */
  2165. /**
  2166. * ### addMethod (ctx, name, method)
  2167. *
  2168. * Adds a method to the prototype of an object.
  2169. *
  2170. * utils.addMethod(chai.Assertion.prototype, 'foo', function (str) {
  2171. * var obj = utils.flag(this, 'object');
  2172. * new chai.Assertion(obj).to.be.equal(str);
  2173. * });
  2174. *
  2175. * Can also be accessed directly from `chai.Assertion`.
  2176. *
  2177. * chai.Assertion.addMethod('foo', fn);
  2178. *
  2179. * Then can be used as any other assertion.
  2180. *
  2181. * expect(fooStr).to.be.foo('bar');
  2182. *
  2183. * @param {Object} ctx object to which the method is added
  2184. * @param {String} name of method to add
  2185. * @param {Function} method function to be used for name
  2186. * @name addMethod
  2187. * @api public
  2188. */
  2189. module.exports = function (ctx, name, method) {
  2190. ctx[name] = function () {
  2191. var result = method.apply(this, arguments);
  2192. return result === undefined ? this : result;
  2193. };
  2194. };
  2195. }); // module: chai/utils/addMethod.js
  2196. require.register("chai/utils/addProperty.js", function(module, exports, require){
  2197. /*!
  2198. * Chai - addProperty utility
  2199. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2200. * MIT Licensed
  2201. */
  2202. /**
  2203. * ### addProperty (ctx, name, getter)
  2204. *
  2205. * Adds a property to the prototype of an object.
  2206. *
  2207. * utils.addProperty(chai.Assertion.prototype, 'foo', function () {
  2208. * var obj = utils.flag(this, 'object');
  2209. * new chai.Assertion(obj).to.be.instanceof(Foo);
  2210. * });
  2211. *
  2212. * Can also be accessed directly from `chai.Assertion`.
  2213. *
  2214. * chai.Assertion.addProperty('foo', fn);
  2215. *
  2216. * Then can be used as any other assertion.
  2217. *
  2218. * expect(myFoo).to.be.foo;
  2219. *
  2220. * @param {Object} ctx object to which the property is added
  2221. * @param {String} name of property to add
  2222. * @param {Function} getter function to be used for name
  2223. * @name addProperty
  2224. * @api public
  2225. */
  2226. module.exports = function (ctx, name, getter) {
  2227. Object.defineProperty(ctx, name,
  2228. { get: function () {
  2229. var result = getter.call(this);
  2230. return result === undefined ? this : result;
  2231. }
  2232. , configurable: true
  2233. });
  2234. };
  2235. }); // module: chai/utils/addProperty.js
  2236. require.register("chai/utils/eql.js", function(module, exports, require){
  2237. // This is directly from Node.js assert
  2238. // https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/assert.js
  2239. module.exports = _deepEqual;
  2240. // For browser implementation
  2241. if (!Buffer) {
  2242. var Buffer = {
  2243. isBuffer: function () {
  2244. return false;
  2245. }
  2246. };
  2247. }
  2248. function _deepEqual(actual, expected) {
  2249. // 7.1. All identical values are equivalent, as determined by ===.
  2250. if (actual === expected) {
  2251. return true;
  2252. } else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
  2253. if (actual.length != expected.length) return false;
  2254. for (var i = 0; i < actual.length; i++) {
  2255. if (actual[i] !== expected[i]) return false;
  2256. }
  2257. return true;
  2258. // 7.2. If the expected value is a Date object, the actual value is
  2259. // equivalent if it is also a Date object that refers to the same time.
  2260. } else if (actual instanceof Date && expected instanceof Date) {
  2261. return actual.getTime() === expected.getTime();
  2262. // 7.3. Other pairs that do not both pass typeof value == 'object',
  2263. // equivalence is determined by ==.
  2264. } else if (typeof actual != 'object' && typeof expected != 'object') {
  2265. return actual === expected;
  2266. // 7.4. For all other Object pairs, including Array objects, equivalence is
  2267. // determined by having the same number of owned properties (as verified
  2268. // with Object.prototype.hasOwnProperty.call), the same set of keys
  2269. // (although not necessarily the same order), equivalent values for every
  2270. // corresponding key, and an identical 'prototype' property. Note: this
  2271. // accounts for both named and indexed properties on Arrays.
  2272. } else {
  2273. return objEquiv(actual, expected);
  2274. }
  2275. }
  2276. function isUndefinedOrNull(value) {
  2277. return value === null || value === undefined;
  2278. }
  2279. function isArguments(object) {
  2280. return Object.prototype.toString.call(object) == '[object Arguments]';
  2281. }
  2282. function objEquiv(a, b) {
  2283. if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
  2284. return false;
  2285. // an identical 'prototype' property.
  2286. if (a.prototype !== b.prototype) return false;
  2287. //~~~I've managed to break Object.keys through screwy arguments passing.
  2288. // Converting to array solves the problem.
  2289. if (isArguments(a)) {
  2290. if (!isArguments(b)) {
  2291. return false;
  2292. }
  2293. a = pSlice.call(a);
  2294. b = pSlice.call(b);
  2295. return _deepEqual(a, b);
  2296. }
  2297. try {
  2298. var ka = Object.keys(a),
  2299. kb = Object.keys(b),
  2300. key, i;
  2301. } catch (e) {//happens when one is a string literal and the other isn't
  2302. return false;
  2303. }
  2304. // having the same number of owned properties (keys incorporates
  2305. // hasOwnProperty)
  2306. if (ka.length != kb.length)
  2307. return false;
  2308. //the same set of keys (although not necessarily the same order),
  2309. ka.sort();
  2310. kb.sort();
  2311. //~~~cheap key test
  2312. for (i = ka.length - 1; i >= 0; i--) {
  2313. if (ka[i] != kb[i])
  2314. return false;
  2315. }
  2316. //equivalent values for every corresponding key, and
  2317. //~~~possibly expensive deep test
  2318. for (i = ka.length - 1; i >= 0; i--) {
  2319. key = ka[i];
  2320. if (!_deepEqual(a[key], b[key])) return false;
  2321. }
  2322. return true;
  2323. }
  2324. }); // module: chai/utils/eql.js
  2325. require.register("chai/utils/flag.js", function(module, exports, require){
  2326. /*!
  2327. * Chai - flag utility
  2328. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2329. * MIT Licensed
  2330. */
  2331. /**
  2332. * ### flag(object ,key, [value])
  2333. *
  2334. * Get or set a flag value on an object. If a
  2335. * value is provided it will be set, else it will
  2336. * return the currently set value or `undefined` if
  2337. * the value is not set.
  2338. *
  2339. * utils.flag(this, 'foo', 'bar'); // setter
  2340. * utils.flag(this, 'foo'); // getter, returns `bar`
  2341. *
  2342. * @param {Object} object (constructed Assertion
  2343. * @param {String} key
  2344. * @param {Mixed} value (optional)
  2345. * @name flag
  2346. * @api private
  2347. */
  2348. module.exports = function (obj, key, value) {
  2349. var flags = obj.__flags || (obj.__flags = Object.create(null));
  2350. if (arguments.length === 3) {
  2351. flags[key] = value;
  2352. } else {
  2353. return flags[key];
  2354. }
  2355. };
  2356. }); // module: chai/utils/flag.js
  2357. require.register("chai/utils/getActual.js", function(module, exports, require){
  2358. /*!
  2359. * Chai - getActual utility
  2360. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2361. * MIT Licensed
  2362. */
  2363. /**
  2364. * # getActual(object, [actual])
  2365. *
  2366. * Returns the `actual` value for an Assertion
  2367. *
  2368. * @param {Object} object (constructed Assertion)
  2369. * @param {Arguments} chai.Assertion.prototype.assert arguments
  2370. */
  2371. module.exports = function (obj, args) {
  2372. var actual = args[4];
  2373. return 'undefined' !== actual ? actual : obj.obj;
  2374. };
  2375. }); // module: chai/utils/getActual.js
  2376. require.register("chai/utils/getMessage.js", function(module, exports, require){
  2377. /*!
  2378. * Chai - message composition utility
  2379. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2380. * MIT Licensed
  2381. */
  2382. /*!
  2383. * Module dependancies
  2384. */
  2385. var flag = require('./flag')
  2386. , getActual = require('./getActual')
  2387. , inspect = require('./inspect');
  2388. /**
  2389. * # getMessage(object, message, negateMessage)
  2390. *
  2391. * Construct the error message based on flags
  2392. * and template tags. Template tags will return
  2393. * a stringified inspection of the object referenced.
  2394. *
  2395. * Messsage template tags:
  2396. * - `#{this}` current asserted object
  2397. * - `#{act}` actual value
  2398. * - `#{exp}` expected value
  2399. *
  2400. * @param {Object} object (constructed Assertion)
  2401. * @param {Arguments} chai.Assertion.prototype.assert arguments
  2402. */
  2403. module.exports = function (obj, args) {
  2404. var negate = flag(obj, 'negate')
  2405. , val = flag(obj, 'object')
  2406. , expected = args[3]
  2407. , actual = getActual(obj, args)
  2408. , msg = negate ? args[2] : args[1]
  2409. , flagMsg = flag(obj, 'message');
  2410. msg = msg || '';
  2411. msg = msg
  2412. .replace(/#{this}/g, inspect(val))
  2413. .replace(/#{act}/g, inspect(actual))
  2414. .replace(/#{exp}/g, inspect(expected));
  2415. return flagMsg ? flagMsg + ': ' + msg : msg;
  2416. };
  2417. }); // module: chai/utils/getMessage.js
  2418. require.register("chai/utils/getName.js", function(module, exports, require){
  2419. /*!
  2420. * Chai - getName utility
  2421. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2422. * MIT Licensed
  2423. */
  2424. /**
  2425. * # getName(func)
  2426. *
  2427. * Gets the name of a function, in a cross-browser way.
  2428. *
  2429. * @param {Function} a function (usually a constructor)
  2430. */
  2431. module.exports = function (func) {
  2432. if (func.name) return func.name;
  2433. var match = /^\s?function ([^(]*)\(/.exec(func);
  2434. return match && match[1] ? match[1] : "";
  2435. };
  2436. }); // module: chai/utils/getName.js
  2437. require.register("chai/utils/getPathValue.js", function(module, exports, require){
  2438. /*!
  2439. * Chai - getPathValue utility
  2440. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2441. * @see https://github.com/logicalparadox/filtr
  2442. * MIT Licensed
  2443. */
  2444. /**
  2445. * ### .getPathValue(path, object)
  2446. *
  2447. * This allows the retrieval of values in an
  2448. * object given a string path.
  2449. *
  2450. * var obj = {
  2451. * prop1: {
  2452. * arr: ['a', 'b', 'c']
  2453. * , str: 'Hello'
  2454. * }
  2455. * , prop2: {
  2456. * arr: [ { nested: 'Universe' } ]
  2457. * , str: 'Hello again!'
  2458. * }
  2459. * }
  2460. *
  2461. * The following would be the results.
  2462. *
  2463. * getPathValue('prop1.str', obj); // Hello
  2464. * getPathValue('prop1.att[2]', obj); // b
  2465. * getPathValue('prop2.arr[0].nested', obj); // Universe
  2466. *
  2467. * @param {String} path
  2468. * @param {Object} object
  2469. * @returns {Object} value or `undefined`
  2470. * @name getPathValue
  2471. * @api public
  2472. */
  2473. var getPathValue = module.exports = function (path, obj) {
  2474. var parsed = parsePath(path);
  2475. return _getPathValue(parsed, obj);
  2476. };
  2477. /*!
  2478. * ## parsePath(path)
  2479. *
  2480. * Helper function used to parse string object
  2481. * paths. Use in conjunction with `_getPathValue`.
  2482. *
  2483. * var parsed = parsePath('myobject.property.subprop');
  2484. *
  2485. * ### Paths:
  2486. *
  2487. * * Can be as near infinitely deep and nested
  2488. * * Arrays are also valid using the formal `myobject.document[3].property`.
  2489. *
  2490. * @param {String} path
  2491. * @returns {Object} parsed
  2492. * @api private
  2493. */
  2494. function parsePath (path) {
  2495. var str = path.replace(/\[/g, '.[')
  2496. , parts = str.match(/(\\\.|[^.]+?)+/g);
  2497. return parts.map(function (value) {
  2498. var re = /\[(\d+)\]$/
  2499. , mArr = re.exec(value)
  2500. if (mArr) return { i: parseFloat(mArr[1]) };
  2501. else return { p: value };
  2502. });
  2503. };
  2504. /*!
  2505. * ## _getPathValue(parsed, obj)
  2506. *
  2507. * Helper companion function for `.parsePath` that returns
  2508. * the value located at the parsed address.
  2509. *
  2510. * var value = getPathValue(parsed, obj);
  2511. *
  2512. * @param {Object} parsed definition from `parsePath`.
  2513. * @param {Object} object to search against
  2514. * @returns {Object|Undefined} value
  2515. * @api private
  2516. */
  2517. function _getPathValue (parsed, obj) {
  2518. var tmp = obj
  2519. , res;
  2520. for (var i = 0, l = parsed.length; i < l; i++) {
  2521. var part = parsed[i];
  2522. if (tmp) {
  2523. if ('undefined' !== typeof part.p)
  2524. tmp = tmp[part.p];
  2525. else if ('undefined' !== typeof part.i)
  2526. tmp = tmp[part.i];
  2527. if (i == (l - 1)) res = tmp;
  2528. } else {
  2529. res = undefined;
  2530. }
  2531. }
  2532. return res;
  2533. };
  2534. }); // module: chai/utils/getPathValue.js
  2535. require.register("chai/utils/index.js", function(module, exports, require){
  2536. /*!
  2537. * chai
  2538. * Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
  2539. * MIT Licensed
  2540. */
  2541. /*!
  2542. * Main exports
  2543. */
  2544. var exports = module.exports = {};
  2545. /*!
  2546. * test utility
  2547. */
  2548. exports.test = require('./test');
  2549. /*!
  2550. * message utility
  2551. */
  2552. exports.getMessage = require('./getMessage');
  2553. /*!
  2554. * actual utility
  2555. */
  2556. exports.getActual = require('./getActual');
  2557. /*!
  2558. * Inspect util
  2559. */
  2560. exports.inspect = require('./inspect');
  2561. /*!
  2562. * Flag utility
  2563. */
  2564. exports.flag = require('./flag');
  2565. /*!
  2566. * Flag transferring utility
  2567. */
  2568. exports.transferFlags = require('./transferFlags');
  2569. /*!
  2570. * Deep equal utility
  2571. */
  2572. exports.eql = require('./eql');
  2573. /*!
  2574. * Deep path value
  2575. */
  2576. exports.getPathValue = require('./getPathValue');
  2577. /*!
  2578. * Function name
  2579. */
  2580. exports.getName = require('./getName');
  2581. /*!
  2582. * add Property
  2583. */
  2584. exports.addProperty = require('./addProperty');
  2585. /*!
  2586. * add Method
  2587. */
  2588. exports.addMethod = require('./addMethod');
  2589. /*!
  2590. * overwrite Property
  2591. */
  2592. exports.overwriteProperty = require('./overwriteProperty');
  2593. /*!
  2594. * overwrite Method
  2595. */
  2596. exports.overwriteMethod = require('./overwriteMethod');
  2597. /*!
  2598. * Add a chainable method
  2599. */
  2600. exports.addChainableMethod = require('./addChainableMethod');
  2601. }); // module: chai/utils/index.js
  2602. require.register("chai/utils/inspect.js", function(module, exports, require){
  2603. // This is (almost) directly from Node.js utils
  2604. // https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/util.js
  2605. var getName = require('./getName');
  2606. module.exports = inspect;
  2607. /**
  2608. * Echos the value of a value. Trys to print the value out
  2609. * in the best way possible given the different types.
  2610. *
  2611. * @param {Object} obj The object to print out.
  2612. * @param {Boolean} showHidden Flag that shows hidden (not enumerable)
  2613. * properties of objects.
  2614. * @param {Number} depth Depth in which to descend in object. Default is 2.
  2615. * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
  2616. * output. Default is false (no coloring).
  2617. */
  2618. function inspect(obj, showHidden, depth, colors) {
  2619. var ctx = {
  2620. showHidden: showHidden,
  2621. seen: [],
  2622. stylize: function (str) { return str; }
  2623. };
  2624. return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth));
  2625. }
  2626. function formatValue(ctx, value, recurseTimes) {
  2627. // Provide a hook for user-specified inspect functions.
  2628. // Check that value is an object with an inspect function on it
  2629. if (value && typeof value.inspect === 'function' &&
  2630. // Filter out the util module, it's inspect function is special
  2631. value.inspect !== exports.inspect &&
  2632. // Also filter out any prototype objects using the circular check.
  2633. !(value.constructor && value.constructor.prototype === value)) {
  2634. return value.inspect(recurseTimes);
  2635. }
  2636. // Primitive types cannot have properties
  2637. var primitive = formatPrimitive(ctx, value);
  2638. if (primitive) {
  2639. return primitive;
  2640. }
  2641. // Look up the keys of the object.
  2642. var visibleKeys = Object.keys(value);
  2643. var keys = ctx.showHidden ? Object.getOwnPropertyNames(value) : visibleKeys;
  2644. // Some type of object without properties can be shortcutted.
  2645. // In IE, errors have a single `stack` property, or if they are vanilla `Error`,
  2646. // a `stack` plus `description` property; ignore those for consistency.
  2647. if (keys.length === 0 || (isError(value) && (
  2648. (keys.length === 1 && keys[0] === 'stack') ||
  2649. (keys.length === 2 && keys[0] === 'description' && keys[1] === 'stack')
  2650. ))) {
  2651. if (typeof value === 'function') {
  2652. var name = getName(value);
  2653. var nameSuffix = name ? ': ' + name : '';
  2654. return ctx.stylize('[Function' + nameSuffix + ']', 'special');
  2655. }
  2656. if (isRegExp(value)) {
  2657. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2658. }
  2659. if (isDate(value)) {
  2660. return ctx.stylize(Date.prototype.toUTCString.call(value), 'date');
  2661. }
  2662. if (isError(value)) {
  2663. return formatError(value);
  2664. }
  2665. }
  2666. var base = '', array = false, braces = ['{', '}'];
  2667. // Make Array say that they are Array
  2668. if (isArray(value)) {
  2669. array = true;
  2670. braces = ['[', ']'];
  2671. }
  2672. // Make functions say that they are functions
  2673. if (typeof value === 'function') {
  2674. var name = getName(value);
  2675. var nameSuffix = name ? ': ' + name : '';
  2676. base = ' [Function' + nameSuffix + ']';
  2677. }
  2678. // Make RegExps say that they are RegExps
  2679. if (isRegExp(value)) {
  2680. base = ' ' + RegExp.prototype.toString.call(value);
  2681. }
  2682. // Make dates with properties first say the date
  2683. if (isDate(value)) {
  2684. base = ' ' + Date.prototype.toUTCString.call(value);
  2685. }
  2686. // Make error with message first say the error
  2687. if (isError(value)) {
  2688. return formatError(value);
  2689. }
  2690. if (keys.length === 0 && (!array || value.length == 0)) {
  2691. return braces[0] + base + braces[1];
  2692. }
  2693. if (recurseTimes < 0) {
  2694. if (isRegExp(value)) {
  2695. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2696. } else {
  2697. return ctx.stylize('[Object]', 'special');
  2698. }
  2699. }
  2700. ctx.seen.push(value);
  2701. var output;
  2702. if (array) {
  2703. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  2704. } else {
  2705. output = keys.map(function(key) {
  2706. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  2707. });
  2708. }
  2709. ctx.seen.pop();
  2710. return reduceToSingleString(output, base, braces);
  2711. }
  2712. function formatPrimitive(ctx, value) {
  2713. switch (typeof value) {
  2714. case 'undefined':
  2715. return ctx.stylize('undefined', 'undefined');
  2716. case 'string':
  2717. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  2718. .replace(/'/g, "\\'")
  2719. .replace(/\\"/g, '"') + '\'';
  2720. return ctx.stylize(simple, 'string');
  2721. case 'number':
  2722. return ctx.stylize('' + value, 'number');
  2723. case 'boolean':
  2724. return ctx.stylize('' + value, 'boolean');
  2725. }
  2726. // For some reason typeof null is "object", so special case here.
  2727. if (value === null) {
  2728. return ctx.stylize('null', 'null');
  2729. }
  2730. }
  2731. function formatError(value) {
  2732. return '[' + Error.prototype.toString.call(value) + ']';
  2733. }
  2734. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  2735. var output = [];
  2736. for (var i = 0, l = value.length; i < l; ++i) {
  2737. if (Object.prototype.hasOwnProperty.call(value, String(i))) {
  2738. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2739. String(i), true));
  2740. } else {
  2741. output.push('');
  2742. }
  2743. }
  2744. keys.forEach(function(key) {
  2745. if (!key.match(/^\d+$/)) {
  2746. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2747. key, true));
  2748. }
  2749. });
  2750. return output;
  2751. }
  2752. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  2753. var name, str;
  2754. if (value.__lookupGetter__) {
  2755. if (value.__lookupGetter__(key)) {
  2756. if (value.__lookupSetter__(key)) {
  2757. str = ctx.stylize('[Getter/Setter]', 'special');
  2758. } else {
  2759. str = ctx.stylize('[Getter]', 'special');
  2760. }
  2761. } else {
  2762. if (value.__lookupSetter__(key)) {
  2763. str = ctx.stylize('[Setter]', 'special');
  2764. }
  2765. }
  2766. }
  2767. if (visibleKeys.indexOf(key) < 0) {
  2768. name = '[' + key + ']';
  2769. }
  2770. if (!str) {
  2771. if (ctx.seen.indexOf(value[key]) < 0) {
  2772. if (recurseTimes === null) {
  2773. str = formatValue(ctx, value[key], null);
  2774. } else {
  2775. str = formatValue(ctx, value[key], recurseTimes - 1);
  2776. }
  2777. if (str.indexOf('\n') > -1) {
  2778. if (array) {
  2779. str = str.split('\n').map(function(line) {
  2780. return ' ' + line;
  2781. }).join('\n').substr(2);
  2782. } else {
  2783. str = '\n' + str.split('\n').map(function(line) {
  2784. return ' ' + line;
  2785. }).join('\n');
  2786. }
  2787. }
  2788. } else {
  2789. str = ctx.stylize('[Circular]', 'special');
  2790. }
  2791. }
  2792. if (typeof name === 'undefined') {
  2793. if (array && key.match(/^\d+$/)) {
  2794. return str;
  2795. }
  2796. name = JSON.stringify('' + key);
  2797. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  2798. name = name.substr(1, name.length - 2);
  2799. name = ctx.stylize(name, 'name');
  2800. } else {
  2801. name = name.replace(/'/g, "\\'")
  2802. .replace(/\\"/g, '"')
  2803. .replace(/(^"|"$)/g, "'");
  2804. name = ctx.stylize(name, 'string');
  2805. }
  2806. }
  2807. return name + ': ' + str;
  2808. }
  2809. function reduceToSingleString(output, base, braces) {
  2810. var numLinesEst = 0;
  2811. var length = output.reduce(function(prev, cur) {
  2812. numLinesEst++;
  2813. if (cur.indexOf('\n') >= 0) numLinesEst++;
  2814. return prev + cur.length + 1;
  2815. }, 0);
  2816. if (length > 60) {
  2817. return braces[0] +
  2818. (base === '' ? '' : base + '\n ') +
  2819. ' ' +
  2820. output.join(',\n ') +
  2821. ' ' +
  2822. braces[1];
  2823. }
  2824. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  2825. }
  2826. function isArray(ar) {
  2827. return Array.isArray(ar) ||
  2828. (typeof ar === 'object' && objectToString(ar) === '[object Array]');
  2829. }
  2830. function isRegExp(re) {
  2831. return typeof re === 'object' && objectToString(re) === '[object RegExp]';
  2832. }
  2833. function isDate(d) {
  2834. return typeof d === 'object' && objectToString(d) === '[object Date]';
  2835. }
  2836. function isError(e) {
  2837. return typeof e === 'object' && objectToString(e) === '[object Error]';
  2838. }
  2839. function objectToString(o) {
  2840. return Object.prototype.toString.call(o);
  2841. }
  2842. }); // module: chai/utils/inspect.js
  2843. require.register("chai/utils/overwriteMethod.js", function(module, exports, require){
  2844. /*!
  2845. * Chai - overwriteMethod utility
  2846. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2847. * MIT Licensed
  2848. */
  2849. /**
  2850. * ### overwriteMethod (ctx, name, fn)
  2851. *
  2852. * Overwites an already existing method and provides
  2853. * access to previous function. Must return function
  2854. * to be used for name.
  2855. *
  2856. * utils.overwriteMethod(chai.Assertion.prototype, 'equal', function (_super) {
  2857. * return function (str) {
  2858. * var obj = utils.flag(this, 'object');
  2859. * if (obj instanceof Foo) {
  2860. * new chai.Assertion(obj.value).to.equal(str);
  2861. * } else {
  2862. * _super.apply(this, arguments);
  2863. * }
  2864. * }
  2865. * });
  2866. *
  2867. * Can also be accessed directly from `chai.Assertion`.
  2868. *
  2869. * chai.Assertion.overwriteMethod('foo', fn);
  2870. *
  2871. * Then can be used as any other assertion.
  2872. *
  2873. * expect(myFoo).to.equal('bar');
  2874. *
  2875. * @param {Object} ctx object whose method is to be overwritten
  2876. * @param {String} name of method to overwrite
  2877. * @param {Function} method function that returns a function to be used for name
  2878. * @name overwriteMethod
  2879. * @api public
  2880. */
  2881. module.exports = function (ctx, name, method) {
  2882. var _method = ctx[name]
  2883. , _super = function () { return this; };
  2884. if (_method && 'function' === typeof _method)
  2885. _super = _method;
  2886. ctx[name] = function () {
  2887. var result = method(_super).apply(this, arguments);
  2888. return result === undefined ? this : result;
  2889. }
  2890. };
  2891. }); // module: chai/utils/overwriteMethod.js
  2892. require.register("chai/utils/overwriteProperty.js", function(module, exports, require){
  2893. /*!
  2894. * Chai - overwriteProperty utility
  2895. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2896. * MIT Licensed
  2897. */
  2898. /**
  2899. * ### overwriteProperty (ctx, name, fn)
  2900. *
  2901. * Overwites an already existing property getter and provides
  2902. * access to previous value. Must return function to use as getter.
  2903. *
  2904. * utils.overwriteProperty(chai.Assertion.prototype, 'ok', function (_super) {
  2905. * return function () {
  2906. * var obj = utils.flag(this, 'object');
  2907. * if (obj instanceof Foo) {
  2908. * new chai.Assertion(obj.name).to.equal('bar');
  2909. * } else {
  2910. * _super.call(this);
  2911. * }
  2912. * }
  2913. * });
  2914. *
  2915. *
  2916. * Can also be accessed directly from `chai.Assertion`.
  2917. *
  2918. * chai.Assertion.overwriteProperty('foo', fn);
  2919. *
  2920. * Then can be used as any other assertion.
  2921. *
  2922. * expect(myFoo).to.be.ok;
  2923. *
  2924. * @param {Object} ctx object whose property is to be overwritten
  2925. * @param {String} name of property to overwrite
  2926. * @param {Function} getter function that returns a getter function to be used for name
  2927. * @name overwriteProperty
  2928. * @api public
  2929. */
  2930. module.exports = function (ctx, name, getter) {
  2931. var _get = Object.getOwnPropertyDescriptor(ctx, name)
  2932. , _super = function () {};
  2933. if (_get && 'function' === typeof _get.get)
  2934. _super = _get.get
  2935. Object.defineProperty(ctx, name,
  2936. { get: function () {
  2937. var result = getter(_super).call(this);
  2938. return result === undefined ? this : result;
  2939. }
  2940. , configurable: true
  2941. });
  2942. };
  2943. }); // module: chai/utils/overwriteProperty.js
  2944. require.register("chai/utils/test.js", function(module, exports, require){
  2945. /*!
  2946. * Chai - test utility
  2947. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2948. * MIT Licensed
  2949. */
  2950. /*!
  2951. * Module dependancies
  2952. */
  2953. var flag = require('./flag');
  2954. /**
  2955. * # test(object, expression)
  2956. *
  2957. * Test and object for expression.
  2958. *
  2959. * @param {Object} object (constructed Assertion)
  2960. * @param {Arguments} chai.Assertion.prototype.assert arguments
  2961. */
  2962. module.exports = function (obj, args) {
  2963. var negate = flag(obj, 'negate')
  2964. , expr = args[0];
  2965. return negate ? !expr : expr;
  2966. };
  2967. }); // module: chai/utils/test.js
  2968. require.register("chai/utils/transferFlags.js", function(module, exports, require){
  2969. /*!
  2970. * Chai - transferFlags utility
  2971. * Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
  2972. * MIT Licensed
  2973. */
  2974. /**
  2975. * ### transferFlags(assertion, object, includeAll = true)
  2976. *
  2977. * Transfer all the flags for `assertion` to `object`. If
  2978. * `includeAll` is set to `false`, then the base Chai
  2979. * assertion flags (namely `object`, `ssfi`, and `message`)
  2980. * will not be transferred.
  2981. *
  2982. *
  2983. * var newAssertion = new Assertion();
  2984. * utils.transferFlags(assertion, newAssertion);
  2985. *
  2986. * var anotherAsseriton = new Assertion(myObj);
  2987. * utils.transferFlags(assertion, anotherAssertion, false);
  2988. *
  2989. * @param {Assertion} assertion the assertion to transfer the flags from
  2990. * @param {Object} object the object to transfer the flags too; usually a new assertion
  2991. * @param {Boolean} includeAll
  2992. * @name getAllFlags
  2993. * @api private
  2994. */
  2995. module.exports = function (assertion, object, includeAll) {
  2996. var flags = assertion.__flags || (assertion.__flags = Object.create(null));
  2997. if (!object.__flags) {
  2998. object.__flags = Object.create(null);
  2999. }
  3000. includeAll = arguments.length === 3 ? includeAll : true;
  3001. for (var flag in flags) {
  3002. if (includeAll ||
  3003. (flag !== 'object' && flag !== 'ssfi' && flag != 'message')) {
  3004. object.__flags[flag] = flags[flag];
  3005. }
  3006. }
  3007. };
  3008. }); // module: chai/utils/transferFlags.js
  3009. return require('chai');
  3010. });