nodeunit.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /*!
  2. * Nodeunit
  3. * https://github.com/caolan/nodeunit
  4. * Copyright (c) 2010 Caolan McMahon
  5. * MIT Licensed
  6. *
  7. * json2.js
  8. * http://www.JSON.org/json2.js
  9. * Public Domain.
  10. * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  11. */
  12. nodeunit = (function(){
  13. /*
  14. http://www.JSON.org/json2.js
  15. 2010-11-17
  16. Public Domain.
  17. NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  18. See http://www.JSON.org/js.html
  19. This code should be minified before deployment.
  20. See http://javascript.crockford.com/jsmin.html
  21. USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
  22. NOT CONTROL.
  23. This file creates a global JSON object containing two methods: stringify
  24. and parse.
  25. JSON.stringify(value, replacer, space)
  26. value any JavaScript value, usually an object or array.
  27. replacer an optional parameter that determines how object
  28. values are stringified for objects. It can be a
  29. function or an array of strings.
  30. space an optional parameter that specifies the indentation
  31. of nested structures. If it is omitted, the text will
  32. be packed without extra whitespace. If it is a number,
  33. it will specify the number of spaces to indent at each
  34. level. If it is a string (such as '\t' or ' '),
  35. it contains the characters used to indent at each level.
  36. This method produces a JSON text from a JavaScript value.
  37. When an object value is found, if the object contains a toJSON
  38. method, its toJSON method will be called and the result will be
  39. stringified. A toJSON method does not serialize: it returns the
  40. value represented by the name/value pair that should be serialized,
  41. or undefined if nothing should be serialized. The toJSON method
  42. will be passed the key associated with the value, and this will be
  43. bound to the value
  44. For example, this would serialize Dates as ISO strings.
  45. Date.prototype.toJSON = function (key) {
  46. function f(n) {
  47. // Format integers to have at least two digits.
  48. return n < 10 ? '0' + n : n;
  49. }
  50. return this.getUTCFullYear() + '-' +
  51. f(this.getUTCMonth() + 1) + '-' +
  52. f(this.getUTCDate()) + 'T' +
  53. f(this.getUTCHours()) + ':' +
  54. f(this.getUTCMinutes()) + ':' +
  55. f(this.getUTCSeconds()) + 'Z';
  56. };
  57. You can provide an optional replacer method. It will be passed the
  58. key and value of each member, with this bound to the containing
  59. object. The value that is returned from your method will be
  60. serialized. If your method returns undefined, then the member will
  61. be excluded from the serialization.
  62. If the replacer parameter is an array of strings, then it will be
  63. used to select the members to be serialized. It filters the results
  64. such that only members with keys listed in the replacer array are
  65. stringified.
  66. Values that do not have JSON representations, such as undefined or
  67. functions, will not be serialized. Such values in objects will be
  68. dropped; in arrays they will be replaced with null. You can use
  69. a replacer function to replace those with JSON values.
  70. JSON.stringify(undefined) returns undefined.
  71. The optional space parameter produces a stringification of the
  72. value that is filled with line breaks and indentation to make it
  73. easier to read.
  74. If the space parameter is a non-empty string, then that string will
  75. be used for indentation. If the space parameter is a number, then
  76. the indentation will be that many spaces.
  77. Example:
  78. text = JSON.stringify(['e', {pluribus: 'unum'}]);
  79. // text is '["e",{"pluribus":"unum"}]'
  80. text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
  81. // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
  82. text = JSON.stringify([new Date()], function (key, value) {
  83. return this[key] instanceof Date ?
  84. 'Date(' + this[key] + ')' : value;
  85. });
  86. // text is '["Date(---current time---)"]'
  87. JSON.parse(text, reviver)
  88. This method parses a JSON text to produce an object or array.
  89. It can throw a SyntaxError exception.
  90. The optional reviver parameter is a function that can filter and
  91. transform the results. It receives each of the keys and values,
  92. and its return value is used instead of the original value.
  93. If it returns what it received, then the structure is not modified.
  94. If it returns undefined then the member is deleted.
  95. Example:
  96. // Parse the text. Values that look like ISO date strings will
  97. // be converted to Date objects.
  98. myData = JSON.parse(text, function (key, value) {
  99. var a;
  100. if (typeof value === 'string') {
  101. a =
  102. /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
  103. if (a) {
  104. return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
  105. +a[5], +a[6]));
  106. }
  107. }
  108. return value;
  109. });
  110. myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
  111. var d;
  112. if (typeof value === 'string' &&
  113. value.slice(0, 5) === 'Date(' &&
  114. value.slice(-1) === ')') {
  115. d = new Date(value.slice(5, -1));
  116. if (d) {
  117. return d;
  118. }
  119. }
  120. return value;
  121. });
  122. This is a reference implementation. You are free to copy, modify, or
  123. redistribute.
  124. */
  125. /*jslint evil: true, strict: false, regexp: false */
  126. /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
  127. call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
  128. getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
  129. lastIndex, length, parse, prototype, push, replace, slice, stringify,
  130. test, toJSON, toString, valueOf
  131. */
  132. // Create a JSON object only if one does not already exist. We create the
  133. // methods in a closure to avoid creating global variables.
  134. var JSON = {};
  135. (function () {
  136. "use strict";
  137. function f(n) {
  138. // Format integers to have at least two digits.
  139. return n < 10 ? '0' + n : n;
  140. }
  141. if (typeof Date.prototype.toJSON !== 'function') {
  142. Date.prototype.toJSON = function (key) {
  143. return isFinite(this.valueOf()) ?
  144. this.getUTCFullYear() + '-' +
  145. f(this.getUTCMonth() + 1) + '-' +
  146. f(this.getUTCDate()) + 'T' +
  147. f(this.getUTCHours()) + ':' +
  148. f(this.getUTCMinutes()) + ':' +
  149. f(this.getUTCSeconds()) + 'Z' : null;
  150. };
  151. String.prototype.toJSON =
  152. Number.prototype.toJSON =
  153. Boolean.prototype.toJSON = function (key) {
  154. return this.valueOf();
  155. };
  156. }
  157. var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
  158. escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
  159. gap,
  160. indent,
  161. meta = { // table of character substitutions
  162. '\b': '\\b',
  163. '\t': '\\t',
  164. '\n': '\\n',
  165. '\f': '\\f',
  166. '\r': '\\r',
  167. '"' : '\\"',
  168. '\\': '\\\\'
  169. },
  170. rep;
  171. function quote(string) {
  172. // If the string contains no control characters, no quote characters, and no
  173. // backslash characters, then we can safely slap some quotes around it.
  174. // Otherwise we must also replace the offending characters with safe escape
  175. // sequences.
  176. escapable.lastIndex = 0;
  177. return escapable.test(string) ?
  178. '"' + string.replace(escapable, function (a) {
  179. var c = meta[a];
  180. return typeof c === 'string' ? c :
  181. '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  182. }) + '"' :
  183. '"' + string + '"';
  184. }
  185. function str(key, holder) {
  186. // Produce a string from holder[key].
  187. var i, // The loop counter.
  188. k, // The member key.
  189. v, // The member value.
  190. length,
  191. mind = gap,
  192. partial,
  193. value = holder[key];
  194. // If the value has a toJSON method, call it to obtain a replacement value.
  195. if (value && typeof value === 'object' &&
  196. typeof value.toJSON === 'function') {
  197. value = value.toJSON(key);
  198. }
  199. // If we were called with a replacer function, then call the replacer to
  200. // obtain a replacement value.
  201. if (typeof rep === 'function') {
  202. value = rep.call(holder, key, value);
  203. }
  204. // What happens next depends on the value's type.
  205. switch (typeof value) {
  206. case 'string':
  207. return quote(value);
  208. case 'number':
  209. // JSON numbers must be finite. Encode non-finite numbers as null.
  210. return isFinite(value) ? String(value) : 'null';
  211. case 'boolean':
  212. case 'null':
  213. // If the value is a boolean or null, convert it to a string. Note:
  214. // typeof null does not produce 'null'. The case is included here in
  215. // the remote chance that this gets fixed someday.
  216. return String(value);
  217. // If the type is 'object', we might be dealing with an object or an array or
  218. // null.
  219. case 'object':
  220. // Due to a specification blunder in ECMAScript, typeof null is 'object',
  221. // so watch out for that case.
  222. if (!value) {
  223. return 'null';
  224. }
  225. // Make an array to hold the partial results of stringifying this object value.
  226. gap += indent;
  227. partial = [];
  228. // Is the value an array?
  229. if (Object.prototype.toString.apply(value) === '[object Array]') {
  230. // The value is an array. Stringify every element. Use null as a placeholder
  231. // for non-JSON values.
  232. length = value.length;
  233. for (i = 0; i < length; i += 1) {
  234. partial[i] = str(i, value) || 'null';
  235. }
  236. // Join all of the elements together, separated with commas, and wrap them in
  237. // brackets.
  238. v = partial.length === 0 ? '[]' :
  239. gap ? '[\n' + gap +
  240. partial.join(',\n' + gap) + '\n' +
  241. mind + ']' :
  242. '[' + partial.join(',') + ']';
  243. gap = mind;
  244. return v;
  245. }
  246. // If the replacer is an array, use it to select the members to be stringified.
  247. if (rep && typeof rep === 'object') {
  248. length = rep.length;
  249. for (i = 0; i < length; i += 1) {
  250. k = rep[i];
  251. if (typeof k === 'string') {
  252. v = str(k, value);
  253. if (v) {
  254. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  255. }
  256. }
  257. }
  258. } else {
  259. // Otherwise, iterate through all of the keys in the object.
  260. for (k in value) {
  261. if (Object.hasOwnProperty.call(value, k)) {
  262. v = str(k, value);
  263. if (v) {
  264. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  265. }
  266. }
  267. }
  268. }
  269. // Join all of the member texts together, separated with commas,
  270. // and wrap them in braces.
  271. v = partial.length === 0 ? '{}' :
  272. gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
  273. mind + '}' : '{' + partial.join(',') + '}';
  274. gap = mind;
  275. return v;
  276. }
  277. }
  278. // If the JSON object does not yet have a stringify method, give it one.
  279. if (typeof JSON.stringify !== 'function') {
  280. JSON.stringify = function (value, replacer, space) {
  281. // The stringify method takes a value and an optional replacer, and an optional
  282. // space parameter, and returns a JSON text. The replacer can be a function
  283. // that can replace values, or an array of strings that will select the keys.
  284. // A default replacer method can be provided. Use of the space parameter can
  285. // produce text that is more easily readable.
  286. var i;
  287. gap = '';
  288. indent = '';
  289. // If the space parameter is a number, make an indent string containing that
  290. // many spaces.
  291. if (typeof space === 'number') {
  292. for (i = 0; i < space; i += 1) {
  293. indent += ' ';
  294. }
  295. // If the space parameter is a string, it will be used as the indent string.
  296. } else if (typeof space === 'string') {
  297. indent = space;
  298. }
  299. // If there is a replacer, it must be a function or an array.
  300. // Otherwise, throw an error.
  301. rep = replacer;
  302. if (replacer && typeof replacer !== 'function' &&
  303. (typeof replacer !== 'object' ||
  304. typeof replacer.length !== 'number')) {
  305. throw new Error('JSON.stringify');
  306. }
  307. // Make a fake root object containing our value under the key of ''.
  308. // Return the result of stringifying the value.
  309. return str('', {'': value});
  310. };
  311. }
  312. // If the JSON object does not yet have a parse method, give it one.
  313. if (typeof JSON.parse !== 'function') {
  314. JSON.parse = function (text, reviver) {
  315. // The parse method takes a text and an optional reviver function, and returns
  316. // a JavaScript value if the text is a valid JSON text.
  317. var j;
  318. function walk(holder, key) {
  319. // The walk method is used to recursively walk the resulting structure so
  320. // that modifications can be made.
  321. var k, v, value = holder[key];
  322. if (value && typeof value === 'object') {
  323. for (k in value) {
  324. if (Object.hasOwnProperty.call(value, k)) {
  325. v = walk(value, k);
  326. if (v !== undefined) {
  327. value[k] = v;
  328. } else {
  329. delete value[k];
  330. }
  331. }
  332. }
  333. }
  334. return reviver.call(holder, key, value);
  335. }
  336. // Parsing happens in four stages. In the first stage, we replace certain
  337. // Unicode characters with escape sequences. JavaScript handles many characters
  338. // incorrectly, either silently deleting them, or treating them as line endings.
  339. text = String(text);
  340. cx.lastIndex = 0;
  341. if (cx.test(text)) {
  342. text = text.replace(cx, function (a) {
  343. return '\\u' +
  344. ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  345. });
  346. }
  347. // In the second stage, we run the text against regular expressions that look
  348. // for non-JSON patterns. We are especially concerned with '()' and 'new'
  349. // because they can cause invocation, and '=' because it can cause mutation.
  350. // But just to be safe, we want to reject all unexpected forms.
  351. // We split the second stage into 4 regexp operations in order to work around
  352. // crippling inefficiencies in IE's and Safari's regexp engines. First we
  353. // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
  354. // replace all simple value tokens with ']' characters. Third, we delete all
  355. // open brackets that follow a colon or comma or that begin the text. Finally,
  356. // we look to see that the remaining characters are only whitespace or ']' or
  357. // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
  358. if (/^[\],:{}\s]*$/
  359. .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
  360. .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
  361. .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  362. // In the third stage we use the eval function to compile the text into a
  363. // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
  364. // in JavaScript: it can begin a block or an object literal. We wrap the text
  365. // in parens to eliminate the ambiguity.
  366. j = eval('(' + text + ')');
  367. // In the optional fourth stage, we recursively walk the new structure, passing
  368. // each name/value pair to a reviver function for possible transformation.
  369. return typeof reviver === 'function' ?
  370. walk({'': j}, '') : j;
  371. }
  372. // If the text is not JSON parseable, then a SyntaxError is thrown.
  373. throw new SyntaxError('JSON.parse');
  374. };
  375. }
  376. }());
  377. var assert = this.assert = {};
  378. var types = {};
  379. var core = {};
  380. var nodeunit = {};
  381. var reporter = {};
  382. /*global setTimeout: false, console: false */
  383. (function () {
  384. var async = {};
  385. // global on the server, window in the browser
  386. var root = this,
  387. previous_async = root.async;
  388. if (typeof module !== 'undefined' && module.exports) {
  389. module.exports = async;
  390. }
  391. else {
  392. root.async = async;
  393. }
  394. async.noConflict = function () {
  395. root.async = previous_async;
  396. return async;
  397. };
  398. //// cross-browser compatiblity functions ////
  399. var _forEach = function (arr, iterator) {
  400. if (arr.forEach) {
  401. return arr.forEach(iterator);
  402. }
  403. for (var i = 0; i < arr.length; i += 1) {
  404. iterator(arr[i], i, arr);
  405. }
  406. };
  407. var _map = function (arr, iterator) {
  408. if (arr.map) {
  409. return arr.map(iterator);
  410. }
  411. var results = [];
  412. _forEach(arr, function (x, i, a) {
  413. results.push(iterator(x, i, a));
  414. });
  415. return results;
  416. };
  417. var _reduce = function (arr, iterator, memo) {
  418. if (arr.reduce) {
  419. return arr.reduce(iterator, memo);
  420. }
  421. _forEach(arr, function (x, i, a) {
  422. memo = iterator(memo, x, i, a);
  423. });
  424. return memo;
  425. };
  426. var _keys = function (obj) {
  427. if (Object.keys) {
  428. return Object.keys(obj);
  429. }
  430. var keys = [];
  431. for (var k in obj) {
  432. if (obj.hasOwnProperty(k)) {
  433. keys.push(k);
  434. }
  435. }
  436. return keys;
  437. };
  438. var _indexOf = function (arr, item) {
  439. if (arr.indexOf) {
  440. return arr.indexOf(item);
  441. }
  442. for (var i = 0; i < arr.length; i += 1) {
  443. if (arr[i] === item) {
  444. return i;
  445. }
  446. }
  447. return -1;
  448. };
  449. //// exported async module functions ////
  450. //// nextTick implementation with browser-compatible fallback ////
  451. if (typeof process === 'undefined' || !(process.nextTick)) {
  452. async.nextTick = function (fn) {
  453. setTimeout(fn, 0);
  454. };
  455. }
  456. else {
  457. async.nextTick = process.nextTick;
  458. }
  459. async.forEach = function (arr, iterator, callback) {
  460. if (!arr.length) {
  461. return callback();
  462. }
  463. var completed = 0;
  464. _forEach(arr, function (x) {
  465. iterator(x, function (err) {
  466. if (err) {
  467. callback(err);
  468. callback = function () {};
  469. }
  470. else {
  471. completed += 1;
  472. if (completed === arr.length) {
  473. callback();
  474. }
  475. }
  476. });
  477. });
  478. };
  479. async.forEachSeries = function (arr, iterator, callback) {
  480. if (!arr.length) {
  481. return callback();
  482. }
  483. var completed = 0;
  484. var iterate = function () {
  485. iterator(arr[completed], function (err) {
  486. if (err) {
  487. callback(err);
  488. callback = function () {};
  489. }
  490. else {
  491. completed += 1;
  492. if (completed === arr.length) {
  493. callback();
  494. }
  495. else {
  496. iterate();
  497. }
  498. }
  499. });
  500. };
  501. iterate();
  502. };
  503. var doParallel = function (fn) {
  504. return function () {
  505. var args = Array.prototype.slice.call(arguments);
  506. return fn.apply(null, [async.forEach].concat(args));
  507. };
  508. };
  509. var doSeries = function (fn) {
  510. return function () {
  511. var args = Array.prototype.slice.call(arguments);
  512. return fn.apply(null, [async.forEachSeries].concat(args));
  513. };
  514. };
  515. var _asyncMap = function (eachfn, arr, iterator, callback) {
  516. var results = [];
  517. arr = _map(arr, function (x, i) {
  518. return {index: i, value: x};
  519. });
  520. eachfn(arr, function (x, callback) {
  521. iterator(x.value, function (err, v) {
  522. results[x.index] = v;
  523. callback(err);
  524. });
  525. }, function (err) {
  526. callback(err, results);
  527. });
  528. };
  529. async.map = doParallel(_asyncMap);
  530. async.mapSeries = doSeries(_asyncMap);
  531. // reduce only has a series version, as doing reduce in parallel won't
  532. // work in many situations.
  533. async.reduce = function (arr, memo, iterator, callback) {
  534. async.forEachSeries(arr, function (x, callback) {
  535. iterator(memo, x, function (err, v) {
  536. memo = v;
  537. callback(err);
  538. });
  539. }, function (err) {
  540. callback(err, memo);
  541. });
  542. };
  543. // inject alias
  544. async.inject = async.reduce;
  545. // foldl alias
  546. async.foldl = async.reduce;
  547. async.reduceRight = function (arr, memo, iterator, callback) {
  548. var reversed = _map(arr, function (x) {
  549. return x;
  550. }).reverse();
  551. async.reduce(reversed, memo, iterator, callback);
  552. };
  553. // foldr alias
  554. async.foldr = async.reduceRight;
  555. var _filter = function (eachfn, arr, iterator, callback) {
  556. var results = [];
  557. arr = _map(arr, function (x, i) {
  558. return {index: i, value: x};
  559. });
  560. eachfn(arr, function (x, callback) {
  561. iterator(x.value, function (v) {
  562. if (v) {
  563. results.push(x);
  564. }
  565. callback();
  566. });
  567. }, function (err) {
  568. callback(_map(results.sort(function (a, b) {
  569. return a.index - b.index;
  570. }), function (x) {
  571. return x.value;
  572. }));
  573. });
  574. };
  575. async.filter = doParallel(_filter);
  576. async.filterSeries = doSeries(_filter);
  577. // select alias
  578. async.select = async.filter;
  579. async.selectSeries = async.filterSeries;
  580. var _reject = function (eachfn, arr, iterator, callback) {
  581. var results = [];
  582. arr = _map(arr, function (x, i) {
  583. return {index: i, value: x};
  584. });
  585. eachfn(arr, function (x, callback) {
  586. iterator(x.value, function (v) {
  587. if (!v) {
  588. results.push(x);
  589. }
  590. callback();
  591. });
  592. }, function (err) {
  593. callback(_map(results.sort(function (a, b) {
  594. return a.index - b.index;
  595. }), function (x) {
  596. return x.value;
  597. }));
  598. });
  599. };
  600. async.reject = doParallel(_reject);
  601. async.rejectSeries = doSeries(_reject);
  602. var _detect = function (eachfn, arr, iterator, main_callback) {
  603. eachfn(arr, function (x, callback) {
  604. iterator(x, function (result) {
  605. if (result) {
  606. main_callback(x);
  607. }
  608. else {
  609. callback();
  610. }
  611. });
  612. }, function (err) {
  613. main_callback();
  614. });
  615. };
  616. async.detect = doParallel(_detect);
  617. async.detectSeries = doSeries(_detect);
  618. async.some = function (arr, iterator, main_callback) {
  619. async.forEach(arr, function (x, callback) {
  620. iterator(x, function (v) {
  621. if (v) {
  622. main_callback(true);
  623. main_callback = function () {};
  624. }
  625. callback();
  626. });
  627. }, function (err) {
  628. main_callback(false);
  629. });
  630. };
  631. // any alias
  632. async.any = async.some;
  633. async.every = function (arr, iterator, main_callback) {
  634. async.forEach(arr, function (x, callback) {
  635. iterator(x, function (v) {
  636. if (!v) {
  637. main_callback(false);
  638. main_callback = function () {};
  639. }
  640. callback();
  641. });
  642. }, function (err) {
  643. main_callback(true);
  644. });
  645. };
  646. // all alias
  647. async.all = async.every;
  648. async.sortBy = function (arr, iterator, callback) {
  649. async.map(arr, function (x, callback) {
  650. iterator(x, function (err, criteria) {
  651. if (err) {
  652. callback(err);
  653. }
  654. else {
  655. callback(null, {value: x, criteria: criteria});
  656. }
  657. });
  658. }, function (err, results) {
  659. if (err) {
  660. return callback(err);
  661. }
  662. else {
  663. var fn = function (left, right) {
  664. var a = left.criteria, b = right.criteria;
  665. return a < b ? -1 : a > b ? 1 : 0;
  666. };
  667. callback(null, _map(results.sort(fn), function (x) {
  668. return x.value;
  669. }));
  670. }
  671. });
  672. };
  673. async.auto = function (tasks, callback) {
  674. callback = callback || function () {};
  675. var keys = _keys(tasks);
  676. if (!keys.length) {
  677. return callback(null);
  678. }
  679. var completed = [];
  680. var listeners = [];
  681. var addListener = function (fn) {
  682. listeners.unshift(fn);
  683. };
  684. var removeListener = function (fn) {
  685. for (var i = 0; i < listeners.length; i += 1) {
  686. if (listeners[i] === fn) {
  687. listeners.splice(i, 1);
  688. return;
  689. }
  690. }
  691. };
  692. var taskComplete = function () {
  693. _forEach(listeners, function (fn) {
  694. fn();
  695. });
  696. };
  697. addListener(function () {
  698. if (completed.length === keys.length) {
  699. callback(null);
  700. }
  701. });
  702. _forEach(keys, function (k) {
  703. var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
  704. var taskCallback = function (err) {
  705. if (err) {
  706. callback(err);
  707. // stop subsequent errors hitting callback multiple times
  708. callback = function () {};
  709. }
  710. else {
  711. completed.push(k);
  712. taskComplete();
  713. }
  714. };
  715. var requires = task.slice(0, Math.abs(task.length - 1)) || [];
  716. var ready = function () {
  717. return _reduce(requires, function (a, x) {
  718. return (a && _indexOf(completed, x) !== -1);
  719. }, true);
  720. };
  721. if (ready()) {
  722. task[task.length - 1](taskCallback);
  723. }
  724. else {
  725. var listener = function () {
  726. if (ready()) {
  727. removeListener(listener);
  728. task[task.length - 1](taskCallback);
  729. }
  730. };
  731. addListener(listener);
  732. }
  733. });
  734. };
  735. async.waterfall = function (tasks, callback) {
  736. if (!tasks.length) {
  737. return callback();
  738. }
  739. callback = callback || function () {};
  740. var wrapIterator = function (iterator) {
  741. return function (err) {
  742. if (err) {
  743. callback(err);
  744. callback = function () {};
  745. }
  746. else {
  747. var args = Array.prototype.slice.call(arguments, 1);
  748. var next = iterator.next();
  749. if (next) {
  750. args.push(wrapIterator(next));
  751. }
  752. else {
  753. args.push(callback);
  754. }
  755. async.nextTick(function () {
  756. iterator.apply(null, args);
  757. });
  758. }
  759. };
  760. };
  761. wrapIterator(async.iterator(tasks))();
  762. };
  763. async.parallel = function (tasks, callback) {
  764. callback = callback || function () {};
  765. if (tasks.constructor === Array) {
  766. async.map(tasks, function (fn, callback) {
  767. if (fn) {
  768. fn(function (err) {
  769. var args = Array.prototype.slice.call(arguments, 1);
  770. if (args.length <= 1) {
  771. args = args[0];
  772. }
  773. callback.call(null, err, args || null);
  774. });
  775. }
  776. }, callback);
  777. }
  778. else {
  779. var results = {};
  780. async.forEach(_keys(tasks), function (k, callback) {
  781. tasks[k](function (err) {
  782. var args = Array.prototype.slice.call(arguments, 1);
  783. if (args.length <= 1) {
  784. args = args[0];
  785. }
  786. results[k] = args;
  787. callback(err);
  788. });
  789. }, function (err) {
  790. callback(err, results);
  791. });
  792. }
  793. };
  794. async.series = function (tasks, callback) {
  795. callback = callback || function () {};
  796. if (tasks.constructor === Array) {
  797. async.mapSeries(tasks, function (fn, callback) {
  798. if (fn) {
  799. fn(function (err) {
  800. var args = Array.prototype.slice.call(arguments, 1);
  801. if (args.length <= 1) {
  802. args = args[0];
  803. }
  804. callback.call(null, err, args || null);
  805. });
  806. }
  807. }, callback);
  808. }
  809. else {
  810. var results = {};
  811. async.forEachSeries(_keys(tasks), function (k, callback) {
  812. tasks[k](function (err) {
  813. var args = Array.prototype.slice.call(arguments, 1);
  814. if (args.length <= 1) {
  815. args = args[0];
  816. }
  817. results[k] = args;
  818. callback(err);
  819. });
  820. }, function (err) {
  821. callback(err, results);
  822. });
  823. }
  824. };
  825. async.iterator = function (tasks) {
  826. var makeCallback = function (index) {
  827. var fn = function () {
  828. if (tasks.length) {
  829. tasks[index].apply(null, arguments);
  830. }
  831. return fn.next();
  832. };
  833. fn.next = function () {
  834. return (index < tasks.length - 1) ? makeCallback(index + 1): null;
  835. };
  836. return fn;
  837. };
  838. return makeCallback(0);
  839. };
  840. async.apply = function (fn) {
  841. var args = Array.prototype.slice.call(arguments, 1);
  842. return function () {
  843. return fn.apply(
  844. null, args.concat(Array.prototype.slice.call(arguments))
  845. );
  846. };
  847. };
  848. var _concat = function (eachfn, arr, fn, callback) {
  849. var r = [];
  850. eachfn(arr, function (x, cb) {
  851. fn(x, function (err, y) {
  852. r = r.concat(y || []);
  853. cb(err);
  854. });
  855. }, function (err) {
  856. callback(err, r);
  857. });
  858. };
  859. async.concat = doParallel(_concat);
  860. async.concatSeries = doSeries(_concat);
  861. async.whilst = function (test, iterator, callback) {
  862. if (test()) {
  863. iterator(function (err) {
  864. if (err) {
  865. return callback(err);
  866. }
  867. async.whilst(test, iterator, callback);
  868. });
  869. }
  870. else {
  871. callback();
  872. }
  873. };
  874. async.until = function (test, iterator, callback) {
  875. if (!test()) {
  876. iterator(function (err) {
  877. if (err) {
  878. return callback(err);
  879. }
  880. async.until(test, iterator, callback);
  881. });
  882. }
  883. else {
  884. callback();
  885. }
  886. };
  887. async.queue = function (worker, concurrency) {
  888. var workers = 0;
  889. var tasks = [];
  890. var q = {
  891. concurrency: concurrency,
  892. push: function (data, callback) {
  893. tasks.push({data: data, callback: callback});
  894. async.nextTick(q.process);
  895. },
  896. process: function () {
  897. if (workers < q.concurrency && tasks.length) {
  898. var task = tasks.splice(0, 1)[0];
  899. workers += 1;
  900. worker(task.data, function () {
  901. workers -= 1;
  902. if (task.callback) {
  903. task.callback.apply(task, arguments);
  904. }
  905. q.process();
  906. });
  907. }
  908. },
  909. length: function () {
  910. return tasks.length;
  911. }
  912. };
  913. return q;
  914. };
  915. var _console_fn = function (name) {
  916. return function (fn) {
  917. var args = Array.prototype.slice.call(arguments, 1);
  918. fn.apply(null, args.concat([function (err) {
  919. var args = Array.prototype.slice.call(arguments, 1);
  920. if (typeof console !== 'undefined') {
  921. if (err) {
  922. if (console.error) {
  923. console.error(err);
  924. }
  925. }
  926. else if (console[name]) {
  927. _forEach(args, function (x) {
  928. console[name](x);
  929. });
  930. }
  931. }
  932. }]));
  933. };
  934. };
  935. async.log = _console_fn('log');
  936. async.dir = _console_fn('dir');
  937. /*async.info = _console_fn('info');
  938. async.warn = _console_fn('warn');
  939. async.error = _console_fn('error');*/
  940. async.memoize = function (fn, hasher) {
  941. var memo = {};
  942. hasher = hasher || function (x) {
  943. return x;
  944. };
  945. return function () {
  946. var args = Array.prototype.slice.call(arguments);
  947. var callback = args.pop();
  948. var key = hasher.apply(null, args);
  949. if (key in memo) {
  950. callback.apply(null, memo[key]);
  951. }
  952. else {
  953. fn.apply(null, args.concat([function () {
  954. memo[key] = arguments;
  955. callback.apply(null, arguments);
  956. }]));
  957. }
  958. };
  959. };
  960. }());
  961. (function(exports){
  962. /**
  963. * This file is based on the node.js assert module, but with some small
  964. * changes for browser-compatibility
  965. * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
  966. */
  967. /**
  968. * Added for browser compatibility
  969. */
  970. var _keys = function(obj){
  971. if(Object.keys) return Object.keys(obj);
  972. if (typeof obj != 'object' && typeof obj != 'function') {
  973. throw new TypeError('-');
  974. }
  975. var keys = [];
  976. for(var k in obj){
  977. if(obj.hasOwnProperty(k)) keys.push(k);
  978. }
  979. return keys;
  980. };
  981. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  982. //
  983. // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
  984. //
  985. // Originally from narwhal.js (http://narwhaljs.org)
  986. // Copyright (c) 2009 Thomas Robinson <280north.com>
  987. //
  988. // Permission is hereby granted, free of charge, to any person obtaining a copy
  989. // of this software and associated documentation files (the 'Software'), to
  990. // deal in the Software without restriction, including without limitation the
  991. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  992. // sell copies of the Software, and to permit persons to whom the Software is
  993. // furnished to do so, subject to the following conditions:
  994. //
  995. // The above copyright notice and this permission notice shall be included in
  996. // all copies or substantial portions of the Software.
  997. //
  998. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  999. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1000. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1001. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1002. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  1003. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  1004. var pSlice = Array.prototype.slice;
  1005. // 1. The assert module provides functions that throw
  1006. // AssertionError's when particular conditions are not met. The
  1007. // assert module must conform to the following interface.
  1008. var assert = exports;
  1009. // 2. The AssertionError is defined in assert.
  1010. // new assert.AssertionError({message: message, actual: actual, expected: expected})
  1011. assert.AssertionError = function AssertionError (options) {
  1012. this.name = "AssertionError";
  1013. this.message = options.message;
  1014. this.actual = options.actual;
  1015. this.expected = options.expected;
  1016. this.operator = options.operator;
  1017. var stackStartFunction = options.stackStartFunction || fail;
  1018. if (Error.captureStackTrace) {
  1019. Error.captureStackTrace(this, stackStartFunction);
  1020. }
  1021. };
  1022. // code from util.inherits in node
  1023. assert.AssertionError.super_ = Error;
  1024. // EDITED FOR BROWSER COMPATIBILITY: replaced Object.create call
  1025. // TODO: test what effect this may have
  1026. var ctor = function () { this.constructor = assert.AssertionError; };
  1027. ctor.prototype = Error.prototype;
  1028. assert.AssertionError.prototype = new ctor();
  1029. assert.AssertionError.prototype.toString = function() {
  1030. if (this.message) {
  1031. return [this.name+":", this.message].join(' ');
  1032. } else {
  1033. return [ this.name+":"
  1034. , JSON.stringify(this.expected )
  1035. , this.operator
  1036. , JSON.stringify(this.actual)
  1037. ].join(" ");
  1038. }
  1039. };
  1040. // assert.AssertionError instanceof Error
  1041. assert.AssertionError.__proto__ = Error.prototype;
  1042. // At present only the three keys mentioned above are used and
  1043. // understood by the spec. Implementations or sub modules can pass
  1044. // other keys to the AssertionError's constructor - they will be
  1045. // ignored.
  1046. // 3. All of the following functions must throw an AssertionError
  1047. // when a corresponding condition is not met, with a message that
  1048. // may be undefined if not provided. All assertion methods provide
  1049. // both the actual and expected values to the assertion error for
  1050. // display purposes.
  1051. function fail(actual, expected, message, operator, stackStartFunction) {
  1052. throw new assert.AssertionError({
  1053. message: message,
  1054. actual: actual,
  1055. expected: expected,
  1056. operator: operator,
  1057. stackStartFunction: stackStartFunction
  1058. });
  1059. }
  1060. // EXTENSION! allows for well behaved errors defined elsewhere.
  1061. assert.fail = fail;
  1062. // 4. Pure assertion tests whether a value is truthy, as determined
  1063. // by !!guard.
  1064. // assert.ok(guard, message_opt);
  1065. // This statement is equivalent to assert.equal(true, guard,
  1066. // message_opt);. To test strictly for the value true, use
  1067. // assert.strictEqual(true, guard, message_opt);.
  1068. assert.ok = function ok(value, message) {
  1069. if (!!!value) fail(value, true, message, "==", assert.ok);
  1070. };
  1071. // 5. The equality assertion tests shallow, coercive equality with
  1072. // ==.
  1073. // assert.equal(actual, expected, message_opt);
  1074. assert.equal = function equal(actual, expected, message) {
  1075. if (actual != expected) fail(actual, expected, message, "==", assert.equal);
  1076. };
  1077. // 6. The non-equality assertion tests for whether two objects are not equal
  1078. // with != assert.notEqual(actual, expected, message_opt);
  1079. assert.notEqual = function notEqual(actual, expected, message) {
  1080. if (actual == expected) {
  1081. fail(actual, expected, message, "!=", assert.notEqual);
  1082. }
  1083. };
  1084. // 7. The equivalence assertion tests a deep equality relation.
  1085. // assert.deepEqual(actual, expected, message_opt);
  1086. assert.deepEqual = function deepEqual(actual, expected, message) {
  1087. if (!_deepEqual(actual, expected)) {
  1088. fail(actual, expected, message, "deepEqual", assert.deepEqual);
  1089. }
  1090. };
  1091. var Buffer = null;
  1092. if (typeof require !== 'undefined' && typeof process !== 'undefined') {
  1093. try {
  1094. Buffer = require('buffer').Buffer;
  1095. }
  1096. catch (e) {
  1097. // May be a CommonJS environment other than Node.js
  1098. Buffer = null;
  1099. }
  1100. }
  1101. function _deepEqual(actual, expected) {
  1102. // 7.1. All identical values are equivalent, as determined by ===.
  1103. if (actual === expected) {
  1104. return true;
  1105. // 7.2. If the expected value is a Date object, the actual value is
  1106. // equivalent if it is also a Date object that refers to the same time.
  1107. } else if (actual instanceof Date && expected instanceof Date) {
  1108. return actual.getTime() === expected.getTime();
  1109. // 7.2.1 If the expcted value is a RegExp object, the actual value is
  1110. // equivalent if it is also a RegExp object that refers to the same source and options
  1111. } else if (actual instanceof RegExp && expected instanceof RegExp) {
  1112. return actual.source === expected.source &&
  1113. actual.global === expected.global &&
  1114. actual.ignoreCase === expected.ignoreCase &&
  1115. actual.multiline === expected.multiline;
  1116. } else if (Buffer && actual instanceof Buffer && expected instanceof Buffer) {
  1117. return (function() {
  1118. var i, len;
  1119. for (i = 0, len = expected.length; i < len; i++) {
  1120. if (actual[i] !== expected[i]) {
  1121. return false;
  1122. }
  1123. }
  1124. return actual.length === expected.length;
  1125. })();
  1126. // 7.3. Other pairs that do not both pass typeof value == "object",
  1127. // equivalence is determined by ==.
  1128. } else if (typeof actual != 'object' && typeof expected != 'object') {
  1129. return actual == expected;
  1130. // 7.4. For all other Object pairs, including Array objects, equivalence is
  1131. // determined by having the same number of owned properties (as verified
  1132. // with Object.prototype.hasOwnProperty.call), the same set of keys
  1133. // (although not necessarily the same order), equivalent values for every
  1134. // corresponding key, and an identical "prototype" property. Note: this
  1135. // accounts for both named and indexed properties on Arrays.
  1136. } else {
  1137. return objEquiv(actual, expected);
  1138. }
  1139. }
  1140. function isUndefinedOrNull (value) {
  1141. return value === null || value === undefined;
  1142. }
  1143. function isArguments (object) {
  1144. return Object.prototype.toString.call(object) == '[object Arguments]';
  1145. }
  1146. function objEquiv (a, b) {
  1147. if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
  1148. return false;
  1149. // an identical "prototype" property.
  1150. if (a.prototype !== b.prototype) return false;
  1151. //~~~I've managed to break Object.keys through screwy arguments passing.
  1152. // Converting to array solves the problem.
  1153. if (isArguments(a)) {
  1154. if (!isArguments(b)) {
  1155. return false;
  1156. }
  1157. a = pSlice.call(a);
  1158. b = pSlice.call(b);
  1159. return _deepEqual(a, b);
  1160. }
  1161. try{
  1162. var ka = _keys(a),
  1163. kb = _keys(b),
  1164. key, i;
  1165. } catch (e) {//happens when one is a string literal and the other isn't
  1166. return false;
  1167. }
  1168. // having the same number of owned properties (keys incorporates hasOwnProperty)
  1169. if (ka.length != kb.length)
  1170. return false;
  1171. //the same set of keys (although not necessarily the same order),
  1172. ka.sort();
  1173. kb.sort();
  1174. //~~~cheap key test
  1175. for (i = ka.length - 1; i >= 0; i--) {
  1176. if (ka[i] != kb[i])
  1177. return false;
  1178. }
  1179. //equivalent values for every corresponding key, and
  1180. //~~~possibly expensive deep test
  1181. for (i = ka.length - 1; i >= 0; i--) {
  1182. key = ka[i];
  1183. if (!_deepEqual(a[key], b[key] ))
  1184. return false;
  1185. }
  1186. return true;
  1187. }
  1188. // 8. The non-equivalence assertion tests for any deep inequality.
  1189. // assert.notDeepEqual(actual, expected, message_opt);
  1190. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  1191. if (_deepEqual(actual, expected)) {
  1192. fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual);
  1193. }
  1194. };
  1195. // 9. The strict equality assertion tests strict equality, as determined by ===.
  1196. // assert.strictEqual(actual, expected, message_opt);
  1197. assert.strictEqual = function strictEqual(actual, expected, message) {
  1198. if (actual !== expected) {
  1199. fail(actual, expected, message, "===", assert.strictEqual);
  1200. }
  1201. };
  1202. // 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
  1203. // assert.notStrictEqual(actual, expected, message_opt);
  1204. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  1205. if (actual === expected) {
  1206. fail(actual, expected, message, "!==", assert.notStrictEqual);
  1207. }
  1208. };
  1209. function expectedException(actual, expected) {
  1210. if (!actual || !expected) {
  1211. return false;
  1212. }
  1213. if (expected instanceof RegExp) {
  1214. return expected.test(actual.message || actual);
  1215. } else if (actual instanceof expected) {
  1216. return true;
  1217. } else if (expected.call({}, actual) === true) {
  1218. return true;
  1219. }
  1220. return false;
  1221. }
  1222. function _throws(shouldThrow, block, expected, message) {
  1223. var actual;
  1224. if (typeof expected === 'string') {
  1225. message = expected;
  1226. expected = null;
  1227. }
  1228. try {
  1229. block();
  1230. } catch (e) {
  1231. actual = e;
  1232. }
  1233. message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
  1234. (message ? ' ' + message : '.');
  1235. if (shouldThrow && !actual) {
  1236. fail('Missing expected exception' + message);
  1237. }
  1238. if (!shouldThrow && expectedException(actual, expected)) {
  1239. fail('Got unwanted exception' + message);
  1240. }
  1241. if ((shouldThrow && actual && expected &&
  1242. !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  1243. throw actual;
  1244. }
  1245. }
  1246. // 11. Expected to throw an error:
  1247. // assert.throws(block, Error_opt, message_opt);
  1248. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  1249. _throws.apply(this, [true].concat(pSlice.call(arguments)));
  1250. };
  1251. // EXTENSION! This is annoying to write outside this module.
  1252. assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
  1253. _throws.apply(this, [false].concat(pSlice.call(arguments)));
  1254. };
  1255. assert.ifError = function (err) { if (err) {throw err;}};
  1256. })(assert);
  1257. (function(exports){
  1258. /*!
  1259. * Nodeunit
  1260. * Copyright (c) 2010 Caolan McMahon
  1261. * MIT Licensed
  1262. *
  1263. * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
  1264. * Only code on that line will be removed, it's mostly to avoid requiring code
  1265. * that is node specific
  1266. */
  1267. /**
  1268. * Module dependencies
  1269. */
  1270. /**
  1271. * Creates assertion objects representing the result of an assert call.
  1272. * Accepts an object or AssertionError as its argument.
  1273. *
  1274. * @param {object} obj
  1275. * @api public
  1276. */
  1277. exports.assertion = function (obj) {
  1278. return {
  1279. method: obj.method || '',
  1280. message: obj.message || (obj.error && obj.error.message) || '',
  1281. error: obj.error,
  1282. passed: function () {
  1283. return !this.error;
  1284. },
  1285. failed: function () {
  1286. return Boolean(this.error);
  1287. }
  1288. };
  1289. };
  1290. /**
  1291. * Creates an assertion list object representing a group of assertions.
  1292. * Accepts an array of assertion objects.
  1293. *
  1294. * @param {Array} arr
  1295. * @param {Number} duration
  1296. * @api public
  1297. */
  1298. exports.assertionList = function (arr, duration) {
  1299. var that = arr || [];
  1300. that.failures = function () {
  1301. var failures = 0;
  1302. for (var i = 0; i < this.length; i += 1) {
  1303. if (this[i].failed()) {
  1304. failures += 1;
  1305. }
  1306. }
  1307. return failures;
  1308. };
  1309. that.passes = function () {
  1310. return that.length - that.failures();
  1311. };
  1312. that.duration = duration || 0;
  1313. return that;
  1314. };
  1315. /**
  1316. * Create a wrapper function for assert module methods. Executes a callback
  1317. * after it's complete with an assertion object representing the result.
  1318. *
  1319. * @param {Function} callback
  1320. * @api private
  1321. */
  1322. var assertWrapper = function (callback) {
  1323. return function (new_method, assert_method, arity) {
  1324. return function () {
  1325. var message = arguments[arity - 1];
  1326. var a = exports.assertion({method: new_method, message: message});
  1327. try {
  1328. assert[assert_method].apply(null, arguments);
  1329. }
  1330. catch (e) {
  1331. a.error = e;
  1332. }
  1333. callback(a);
  1334. };
  1335. };
  1336. };
  1337. /**
  1338. * Creates the 'test' object that gets passed to every test function.
  1339. * Accepts the name of the test function as its first argument, followed by
  1340. * the start time in ms, the options object and a callback function.
  1341. *
  1342. * @param {String} name
  1343. * @param {Number} start
  1344. * @param {Object} options
  1345. * @param {Function} callback
  1346. * @api public
  1347. */
  1348. exports.test = function (name, start, options, callback) {
  1349. var expecting;
  1350. var a_list = [];
  1351. var wrapAssert = assertWrapper(function (a) {
  1352. a_list.push(a);
  1353. if (options.log) {
  1354. async.nextTick(function () {
  1355. options.log(a);
  1356. });
  1357. }
  1358. });
  1359. var test = {
  1360. done: function (err) {
  1361. if (expecting !== undefined && expecting !== a_list.length) {
  1362. var e = new Error(
  1363. 'Expected ' + expecting + ' assertions, ' +
  1364. a_list.length + ' ran'
  1365. );
  1366. var a1 = exports.assertion({method: 'expect', error: e});
  1367. a_list.push(a1);
  1368. if (options.log) {
  1369. async.nextTick(function () {
  1370. options.log(a1);
  1371. });
  1372. }
  1373. }
  1374. if (err) {
  1375. var a2 = exports.assertion({error: err});
  1376. a_list.push(a2);
  1377. if (options.log) {
  1378. async.nextTick(function () {
  1379. options.log(a2);
  1380. });
  1381. }
  1382. }
  1383. var end = new Date().getTime();
  1384. async.nextTick(function () {
  1385. var assertion_list = exports.assertionList(a_list, end - start);
  1386. options.testDone(name, assertion_list);
  1387. callback(null, a_list);
  1388. });
  1389. },
  1390. ok: wrapAssert('ok', 'ok', 2),
  1391. same: wrapAssert('same', 'deepEqual', 3),
  1392. equals: wrapAssert('equals', 'equal', 3),
  1393. expect: function (num) {
  1394. expecting = num;
  1395. },
  1396. _assertion_list: a_list
  1397. };
  1398. // add all functions from the assert module
  1399. for (var k in assert) {
  1400. if (assert.hasOwnProperty(k)) {
  1401. test[k] = wrapAssert(k, k, assert[k].length);
  1402. }
  1403. }
  1404. return test;
  1405. };
  1406. /**
  1407. * Ensures an options object has all callbacks, adding empty callback functions
  1408. * if any are missing.
  1409. *
  1410. * @param {Object} opt
  1411. * @return {Object}
  1412. * @api public
  1413. */
  1414. exports.options = function (opt) {
  1415. var optionalCallback = function (name) {
  1416. opt[name] = opt[name] || function () {};
  1417. };
  1418. optionalCallback('moduleStart');
  1419. optionalCallback('moduleDone');
  1420. optionalCallback('testStart');
  1421. optionalCallback('testDone');
  1422. //optionalCallback('log');
  1423. // 'done' callback is not optional.
  1424. return opt;
  1425. };
  1426. })(types);
  1427. (function(exports){
  1428. /*!
  1429. * Nodeunit
  1430. * Copyright (c) 2010 Caolan McMahon
  1431. * MIT Licensed
  1432. *
  1433. * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
  1434. * Only code on that line will be removed, it's mostly to avoid requiring code
  1435. * that is node specific
  1436. */
  1437. /**
  1438. * Module dependencies
  1439. */
  1440. /**
  1441. * Added for browser compatibility
  1442. */
  1443. var _keys = function (obj) {
  1444. if (Object.keys) {
  1445. return Object.keys(obj);
  1446. }
  1447. var keys = [];
  1448. for (var k in obj) {
  1449. if (obj.hasOwnProperty(k)) {
  1450. keys.push(k);
  1451. }
  1452. }
  1453. return keys;
  1454. };
  1455. var _copy = function (obj) {
  1456. var nobj = {};
  1457. var keys = _keys(obj);
  1458. for (var i = 0; i < keys.length; i += 1) {
  1459. nobj[keys[i]] = obj[keys[i]];
  1460. }
  1461. return nobj;
  1462. };
  1463. /**
  1464. * Runs a test function (fn) from a loaded module. After the test function
  1465. * calls test.done(), the callback is executed with an assertionList as its
  1466. * second argument.
  1467. *
  1468. * @param {String} name
  1469. * @param {Function} fn
  1470. * @param {Object} opt
  1471. * @param {Function} callback
  1472. * @api public
  1473. */
  1474. exports.runTest = function (name, fn, opt, callback) {
  1475. var options = types.options(opt);
  1476. options.testStart(name);
  1477. var start = new Date().getTime();
  1478. var test = types.test(name, start, options, callback);
  1479. try {
  1480. fn(test);
  1481. }
  1482. catch (e) {
  1483. test.done(e);
  1484. }
  1485. };
  1486. /**
  1487. * Takes an object containing test functions or other test suites as properties
  1488. * and runs each in series. After all tests have completed, the callback is
  1489. * called with a list of all assertions as the second argument.
  1490. *
  1491. * If a name is passed to this function it is prepended to all test and suite
  1492. * names that run within it.
  1493. *
  1494. * @param {String} name
  1495. * @param {Object} suite
  1496. * @param {Object} opt
  1497. * @param {Function} callback
  1498. * @api public
  1499. */
  1500. exports.runSuite = function (name, suite, opt, callback) {
  1501. suite = wrapGroup(suite);
  1502. var keys = _keys(suite);
  1503. async.concatSeries(keys, function (k, cb) {
  1504. var prop = suite[k], _name;
  1505. _name = name ? [].concat(name, k) : [k];
  1506. _name.toString = function () {
  1507. // fallback for old one
  1508. return this.join(' - ');
  1509. };
  1510. if (typeof prop === 'function') {
  1511. var in_name = false,
  1512. in_specific_test = (_name.toString() === opt.testFullSpec) ? true : false;
  1513. for (var i = 0; i < _name.length; i += 1) {
  1514. if (_name[i] === opt.testspec) {
  1515. in_name = true;
  1516. }
  1517. }
  1518. if ((!opt.testFullSpec || in_specific_test) && (!opt.testspec || in_name)) {
  1519. if (opt.moduleStart) {
  1520. opt.moduleStart();
  1521. }
  1522. exports.runTest(_name, suite[k], opt, cb);
  1523. }
  1524. else {
  1525. return cb();
  1526. }
  1527. }
  1528. else {
  1529. exports.runSuite(_name, suite[k], opt, cb);
  1530. }
  1531. }, callback);
  1532. };
  1533. /**
  1534. * Run each exported test function or test suite from a loaded module.
  1535. *
  1536. * @param {String} name
  1537. * @param {Object} mod
  1538. * @param {Object} opt
  1539. * @param {Function} callback
  1540. * @api public
  1541. */
  1542. exports.runModule = function (name, mod, opt, callback) {
  1543. var options = _copy(types.options(opt));
  1544. var _run = false;
  1545. var _moduleStart = options.moduleStart;
  1546. mod = wrapGroup(mod);
  1547. function run_once() {
  1548. if (!_run) {
  1549. _run = true;
  1550. _moduleStart(name);
  1551. }
  1552. }
  1553. options.moduleStart = run_once;
  1554. var start = new Date().getTime();
  1555. exports.runSuite(null, mod, options, function (err, a_list) {
  1556. var end = new Date().getTime();
  1557. var assertion_list = types.assertionList(a_list, end - start);
  1558. options.moduleDone(name, assertion_list);
  1559. if (nodeunit.complete) {
  1560. nodeunit.complete(name, assertion_list);
  1561. }
  1562. callback(null, a_list);
  1563. });
  1564. };
  1565. /**
  1566. * Treats an object literal as a list of modules keyed by name. Runs each
  1567. * module and finished with calling 'done'. You can think of this as a browser
  1568. * safe alternative to runFiles in the nodeunit module.
  1569. *
  1570. * @param {Object} modules
  1571. * @param {Object} opt
  1572. * @api public
  1573. */
  1574. // TODO: add proper unit tests for this function
  1575. exports.runModules = function (modules, opt) {
  1576. var all_assertions = [];
  1577. var options = types.options(opt);
  1578. var start = new Date().getTime();
  1579. async.concatSeries(_keys(modules), function (k, cb) {
  1580. exports.runModule(k, modules[k], options, cb);
  1581. },
  1582. function (err, all_assertions) {
  1583. var end = new Date().getTime();
  1584. options.done(types.assertionList(all_assertions, end - start));
  1585. });
  1586. };
  1587. /**
  1588. * Wraps a test function with setUp and tearDown functions.
  1589. * Used by testCase.
  1590. *
  1591. * @param {Function} setUp
  1592. * @param {Function} tearDown
  1593. * @param {Function} fn
  1594. * @api private
  1595. */
  1596. var wrapTest = function (setUp, tearDown, fn) {
  1597. return function (test) {
  1598. var context = {};
  1599. if (tearDown) {
  1600. var done = test.done;
  1601. test.done = function (err) {
  1602. try {
  1603. tearDown.call(context, function (err2) {
  1604. if (err && err2) {
  1605. test._assertion_list.push(
  1606. types.assertion({error: err})
  1607. );
  1608. return done(err2);
  1609. }
  1610. done(err || err2);
  1611. });
  1612. }
  1613. catch (e) {
  1614. done(e);
  1615. }
  1616. };
  1617. }
  1618. if (setUp) {
  1619. setUp.call(context, function (err) {
  1620. if (err) {
  1621. return test.done(err);
  1622. }
  1623. fn.call(context, test);
  1624. });
  1625. }
  1626. else {
  1627. fn.call(context, test);
  1628. }
  1629. };
  1630. };
  1631. /**
  1632. * Returns a serial callback from two functions.
  1633. *
  1634. * @param {Function} funcFirst
  1635. * @param {Function} funcSecond
  1636. * @api private
  1637. */
  1638. var getSerialCallback = function (fns) {
  1639. if (!fns.length) {
  1640. return null;
  1641. }
  1642. return function (callback) {
  1643. var that = this;
  1644. var bound_fns = [];
  1645. for (var i = 0, len = fns.length; i < len; i++) {
  1646. (function (j) {
  1647. bound_fns.push(function () {
  1648. return fns[j].apply(that, arguments);
  1649. });
  1650. })(i);
  1651. }
  1652. return async.series(bound_fns, callback);
  1653. };
  1654. };
  1655. /**
  1656. * Wraps a group of tests with setUp and tearDown functions.
  1657. * Used by testCase.
  1658. *
  1659. * @param {Object} group
  1660. * @param {Array} setUps - parent setUp functions
  1661. * @param {Array} tearDowns - parent tearDown functions
  1662. * @api private
  1663. */
  1664. var wrapGroup = function (group, setUps, tearDowns) {
  1665. var tests = {};
  1666. var setUps = setUps ? setUps.slice(): [];
  1667. var tearDowns = tearDowns ? tearDowns.slice(): [];
  1668. if (group.setUp) {
  1669. setUps.push(group.setUp);
  1670. delete group.setUp;
  1671. }
  1672. if (group.tearDown) {
  1673. tearDowns.unshift(group.tearDown);
  1674. delete group.tearDown;
  1675. }
  1676. var keys = _keys(group);
  1677. for (var i = 0; i < keys.length; i += 1) {
  1678. var k = keys[i];
  1679. if (typeof group[k] === 'function') {
  1680. tests[k] = wrapTest(
  1681. getSerialCallback(setUps),
  1682. getSerialCallback(tearDowns),
  1683. group[k]
  1684. );
  1685. }
  1686. else if (typeof group[k] === 'object') {
  1687. tests[k] = wrapGroup(group[k], setUps, tearDowns);
  1688. }
  1689. }
  1690. return tests;
  1691. };
  1692. /**
  1693. * Backwards compatibility for test suites using old testCase API
  1694. */
  1695. exports.testCase = function (suite) {
  1696. return suite;
  1697. };
  1698. })(core);
  1699. (function(exports){
  1700. /*!
  1701. * Nodeunit
  1702. * Copyright (c) 2010 Caolan McMahon
  1703. * MIT Licensed
  1704. *
  1705. * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
  1706. * Only code on that line will be removed, its mostly to avoid requiring code
  1707. * that is node specific
  1708. */
  1709. /**
  1710. * NOTE: this test runner is not listed in index.js because it cannot be
  1711. * used with the command-line tool, only inside the browser.
  1712. */
  1713. /**
  1714. * Reporter info string
  1715. */
  1716. exports.info = "Browser-based test reporter";
  1717. /**
  1718. * Run all tests within each module, reporting the results
  1719. *
  1720. * @param {Array} files
  1721. * @api public
  1722. */
  1723. exports.run = function (modules, options) {
  1724. var start = new Date().getTime(), div;
  1725. options = options || {};
  1726. div = options.div || document.body;
  1727. function setText(el, txt) {
  1728. if ('innerText' in el) {
  1729. el.innerText = txt;
  1730. }
  1731. else if ('textContent' in el){
  1732. el.textContent = txt;
  1733. }
  1734. }
  1735. function getOrCreate(tag, id) {
  1736. var el = document.getElementById(id);
  1737. if (!el) {
  1738. el = document.createElement(tag);
  1739. el.id = id;
  1740. div.appendChild(el);
  1741. }
  1742. return el;
  1743. };
  1744. var header = getOrCreate('h1', 'nodeunit-header');
  1745. var banner = getOrCreate('h2', 'nodeunit-banner');
  1746. var userAgent = getOrCreate('h2', 'nodeunit-userAgent');
  1747. var tests = getOrCreate('ol', 'nodeunit-tests');
  1748. var result = getOrCreate('p', 'nodeunit-testresult');
  1749. setText(userAgent, navigator.userAgent);
  1750. nodeunit.runModules(modules, {
  1751. moduleStart: function (name) {
  1752. /*var mheading = document.createElement('h2');
  1753. mheading.innerText = name;
  1754. results.appendChild(mheading);
  1755. module = document.createElement('ol');
  1756. results.appendChild(module);*/
  1757. },
  1758. testDone: function (name, assertions) {
  1759. var test = document.createElement('li');
  1760. var strong = document.createElement('strong');
  1761. strong.innerHTML = name + ' <b style="color: black;">(' +
  1762. '<b class="fail">' + assertions.failures() + '</b>, ' +
  1763. '<b class="pass">' + assertions.passes() + '</b>, ' +
  1764. assertions.length +
  1765. ')</b>';
  1766. test.className = assertions.failures() ? 'fail': 'pass';
  1767. test.appendChild(strong);
  1768. var aList = document.createElement('ol');
  1769. aList.style.display = 'none';
  1770. test.onclick = function () {
  1771. var d = aList.style.display;
  1772. aList.style.display = (d == 'none') ? 'block': 'none';
  1773. };
  1774. for (var i=0; i<assertions.length; i++) {
  1775. var li = document.createElement('li');
  1776. var a = assertions[i];
  1777. if (a.failed()) {
  1778. li.innerHTML = (a.message || a.method || 'no message') +
  1779. '<pre>' + (a.error.stack || a.error) + '</pre>';
  1780. li.className = 'fail';
  1781. }
  1782. else {
  1783. li.innerHTML = a.message || a.method || 'no message';
  1784. li.className = 'pass';
  1785. }
  1786. aList.appendChild(li);
  1787. }
  1788. test.appendChild(aList);
  1789. tests.appendChild(test);
  1790. },
  1791. done: function (assertions) {
  1792. var end = new Date().getTime();
  1793. var duration = end - start;
  1794. var failures = assertions.failures();
  1795. banner.className = failures ? 'fail': 'pass';
  1796. result.innerHTML = 'Tests completed in ' + duration +
  1797. ' milliseconds.<br/><span class="passed">' +
  1798. assertions.passes() + '</span> assertions of ' +
  1799. '<span class="all">' + assertions.length + '<span> passed, ' +
  1800. assertions.failures() + ' failed.';
  1801. }
  1802. });
  1803. };
  1804. })(reporter);
  1805. nodeunit = core;
  1806. nodeunit.assert = assert;
  1807. nodeunit.reporter = reporter;
  1808. nodeunit.run = reporter.run;
  1809. return nodeunit; })();