| | 1 | /** |
| | 2 | * Sinon.JS 1.8.2, 2014/02/11 |
| | 3 | * |
| | 4 | * @author Christian Johansen (christian@cjohansen.no) |
| | 5 | * @author Contributors: https://github.com/cjohansen/Sinon.JS/blob/master/AUTHORS |
| | 6 | * |
| | 7 | * (The BSD License) |
| | 8 | * |
| | 9 | * Copyright (c) 2010-2014, Christian Johansen, christian@cjohansen.no |
| | 10 | * All rights reserved. |
| | 11 | * |
| | 12 | * Redistribution and use in source and binary forms, with or without modification, |
| | 13 | * are permitted provided that the following conditions are met: |
| | 14 | * |
| | 15 | * * Redistributions of source code must retain the above copyright notice, |
| | 16 | * this list of conditions and the following disclaimer. |
| | 17 | * * Redistributions in binary form must reproduce the above copyright notice, |
| | 18 | * this list of conditions and the following disclaimer in the documentation |
| | 19 | * and/or other materials provided with the distribution. |
| | 20 | * * Neither the name of Christian Johansen nor the names of his contributors |
| | 21 | * may be used to endorse or promote products derived from this software |
| | 22 | * without specific prior written permission. |
| | 23 | * |
| | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| | 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| | 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| | 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| | 34 | */ |
| | 35 | |
| | 36 | this.sinon = (function () { |
| | 37 | var samsam, formatio; |
| | 38 | function define(mod, deps, fn) { if (mod == "samsam") { samsam = deps(); } else { formatio = fn(samsam); } } |
| | 39 | define.amd = true; |
| | 40 | ((typeof define === "function" && define.amd && function (m) { define("samsam", m); }) || |
| | 41 | (typeof module === "object" && |
| | 42 | function (m) { module.exports = m(); }) || // Node |
| | 43 | function (m) { this.samsam = m(); } // Browser globals |
| | 44 | )(function () { |
| | 45 | var o = Object.prototype; |
| | 46 | var div = typeof document !== "undefined" && document.createElement("div"); |
| | 47 | |
| | 48 | function isNaN(value) { |
| | 49 | // Unlike global isNaN, this avoids type coercion |
| | 50 | // typeof check avoids IE host object issues, hat tip to |
| | 51 | // lodash |
| | 52 | var val = value; // JsLint thinks value !== value is "weird" |
| | 53 | return typeof value === "number" && value !== val; |
| | 54 | } |
| | 55 | |
| | 56 | function getClass(value) { |
| | 57 | // Returns the internal [[Class]] by calling Object.prototype.toString |
| | 58 | // with the provided value as this. Return value is a string, naming the |
| | 59 | // internal class, e.g. "Array" |
| | 60 | return o.toString.call(value).split(/[ \]]/)[1]; |
| | 61 | } |
| | 62 | |
| | 63 | /** |
| | 64 | * @name samsam.isArguments |
| | 65 | * @param Object object |
| | 66 | * |
| | 67 | * Returns ``true`` if ``object`` is an ``arguments`` object, |
| | 68 | * ``false`` otherwise. |
| | 69 | */ |
| | 70 | function isArguments(object) { |
| | 71 | if (typeof object !== "object" || typeof object.length !== "number" || |
| | 72 | getClass(object) === "Array") { |
| | 73 | return false; |
| | 74 | } |
| | 75 | if (typeof object.callee == "function") { return true; } |
| | 76 | try { |
| | 77 | object[object.length] = 6; |
| | 78 | delete object[object.length]; |
| | 79 | } catch (e) { |
| | 80 | return true; |
| | 81 | } |
| | 82 | return false; |
| | 83 | } |
| | 84 | |
| | 85 | /** |
| | 86 | * @name samsam.isElement |
| | 87 | * @param Object object |
| | 88 | * |
| | 89 | * Returns ``true`` if ``object`` is a DOM element node. Unlike |
| | 90 | * Underscore.js/lodash, this function will return ``false`` if ``object`` |
| | 91 | * is an *element-like* object, i.e. a regular object with a ``nodeType`` |
| | 92 | * property that holds the value ``1``. |
| | 93 | */ |
| | 94 | function isElement(object) { |
| | 95 | if (!object || object.nodeType !== 1 || !div) { return false; } |
| | 96 | try { |
| | 97 | object.appendChild(div); |
| | 98 | object.removeChild(div); |
| | 99 | } catch (e) { |
| | 100 | return false; |
| | 101 | } |
| | 102 | return true; |
| | 103 | } |
| | 104 | |
| | 105 | /** |
| | 106 | * @name samsam.keys |
| | 107 | * @param Object object |
| | 108 | * |
| | 109 | * Return an array of own property names. |
| | 110 | */ |
| | 111 | function keys(object) { |
| | 112 | var ks = [], prop; |
| | 113 | for (prop in object) { |
| | 114 | if (o.hasOwnProperty.call(object, prop)) { ks.push(prop); } |
| | 115 | } |
| | 116 | return ks; |
| | 117 | } |
| | 118 | |
| | 119 | /** |
| | 120 | * @name samsam.isDate |
| | 121 | * @param Object value |
| | 122 | * |
| | 123 | * Returns true if the object is a ``Date``, or *date-like*. Duck typing |
| | 124 | * of date objects work by checking that the object has a ``getTime`` |
| | 125 | * function whose return value equals the return value from the object's |
| | 126 | * ``valueOf``. |
| | 127 | */ |
| | 128 | function isDate(value) { |
| | 129 | return typeof value.getTime == "function" && |
| | 130 | value.getTime() == value.valueOf(); |
| | 131 | } |
| | 132 | |
| | 133 | /** |
| | 134 | * @name samsam.isNegZero |
| | 135 | * @param Object value |
| | 136 | * |
| | 137 | * Returns ``true`` if ``value`` is ``-0``. |
| | 138 | */ |
| | 139 | function isNegZero(value) { |
| | 140 | return value === 0 && 1 / value === -Infinity; |
| | 141 | } |
| | 142 | |
| | 143 | /** |
| | 144 | * @name samsam.equal |
| | 145 | * @param Object obj1 |
| | 146 | * @param Object obj2 |
| | 147 | * |
| | 148 | * Returns ``true`` if two objects are strictly equal. Compared to |
| | 149 | * ``===`` there are two exceptions: |
| | 150 | * |
| | 151 | * - NaN is considered equal to NaN |
| | 152 | * - -0 and +0 are not considered equal |
| | 153 | */ |
| | 154 | function identical(obj1, obj2) { |
| | 155 | if (obj1 === obj2 || (isNaN(obj1) && isNaN(obj2))) { |
| | 156 | return obj1 !== 0 || isNegZero(obj1) === isNegZero(obj2); |
| | 157 | } |
| | 158 | } |
| | 159 | |
| | 160 | |
| | 161 | /** |
| | 162 | * @name samsam.deepEqual |
| | 163 | * @param Object obj1 |
| | 164 | * @param Object obj2 |
| | 165 | * |
| | 166 | * Deep equal comparison. Two values are "deep equal" if: |
| | 167 | * |
| | 168 | * - They are equal, according to samsam.identical |
| | 169 | * - They are both date objects representing the same time |
| | 170 | * - They are both arrays containing elements that are all deepEqual |
| | 171 | * - They are objects with the same set of properties, and each property |
| | 172 | * in ``obj1`` is deepEqual to the corresponding property in ``obj2`` |
| | 173 | * |
| | 174 | * Supports cyclic objects. |
| | 175 | */ |
| | 176 | function deepEqualCyclic(obj1, obj2) { |
| | 177 | |
| | 178 | // used for cyclic comparison |
| | 179 | // contain already visited objects |
| | 180 | var objects1 = [], |
| | 181 | objects2 = [], |
| | 182 | // contain pathes (position in the object structure) |
| | 183 | // of the already visited objects |
| | 184 | // indexes same as in objects arrays |
| | 185 | paths1 = [], |
| | 186 | paths2 = [], |
| | 187 | // contains combinations of already compared objects |
| | 188 | // in the manner: { "$1['ref']$2['ref']": true } |
| | 189 | compared = {}; |
| | 190 | |
| | 191 | /** |
| | 192 | * used to check, if the value of a property is an object |
| | 193 | * (cyclic logic is only needed for objects) |
| | 194 | * only needed for cyclic logic |
| | 195 | */ |
| | 196 | function isObject(value) { |
| | 197 | |
| | 198 | if (typeof value === 'object' && value !== null && |
| | 199 | !(value instanceof Boolean) && |
| | 200 | !(value instanceof Date) && |
| | 201 | !(value instanceof Number) && |
| | 202 | !(value instanceof RegExp) && |
| | 203 | !(value instanceof String)) { |
| | 204 | |
| | 205 | return true; |
| | 206 | } |
| | 207 | |
| | 208 | return false; |
| | 209 | } |
| | 210 | |
| | 211 | /** |
| | 212 | * returns the index of the given object in the |
| | 213 | * given objects array, -1 if not contained |
| | 214 | * only needed for cyclic logic |
| | 215 | */ |
| | 216 | function getIndex(objects, obj) { |
| | 217 | |
| | 218 | var i; |
| | 219 | for (i = 0; i < objects.length; i++) { |
| | 220 | if (objects[i] === obj) { |
| | 221 | return i; |
| | 222 | } |
| | 223 | } |
| | 224 | |
| | 225 | return -1; |
| | 226 | } |
| | 227 | |
| | 228 | // does the recursion for the deep equal check |
| | 229 | return (function deepEqual(obj1, obj2, path1, path2) { |
| | 230 | var type1 = typeof obj1; |
| | 231 | var type2 = typeof obj2; |
| | 232 | |
| | 233 | // == null also matches undefined |
| | 234 | if (obj1 === obj2 || |
| | 235 | isNaN(obj1) || isNaN(obj2) || |
| | 236 | obj1 == null || obj2 == null || |
| | 237 | type1 !== "object" || type2 !== "object") { |
| | 238 | |
| | 239 | return identical(obj1, obj2); |
| | 240 | } |
| | 241 | |
| | 242 | // Elements are only equal if identical(expected, actual) |
| | 243 | if (isElement(obj1) || isElement(obj2)) { return false; } |
| | 244 | |
| | 245 | var isDate1 = isDate(obj1), isDate2 = isDate(obj2); |
| | 246 | if (isDate1 || isDate2) { |
| | 247 | if (!isDate1 || !isDate2 || obj1.getTime() !== obj2.getTime()) { |
| | 248 | return false; |
| | 249 | } |
| | 250 | } |
| | 251 | |
| | 252 | if (obj1 instanceof RegExp && obj2 instanceof RegExp) { |
| | 253 | if (obj1.toString() !== obj2.toString()) { return false; } |
| | 254 | } |
| | 255 | |
| | 256 | var class1 = getClass(obj1); |
| | 257 | var class2 = getClass(obj2); |
| | 258 | var keys1 = keys(obj1); |
| | 259 | var keys2 = keys(obj2); |
| | 260 | |
| | 261 | if (isArguments(obj1) || isArguments(obj2)) { |
| | 262 | if (obj1.length !== obj2.length) { return false; } |
| | 263 | } else { |
| | 264 | if (type1 !== type2 || class1 !== class2 || |
| | 265 | keys1.length !== keys2.length) { |
| | 266 | return false; |
| | 267 | } |
| | 268 | } |
| | 269 | |
| | 270 | var key, i, l, |
| | 271 | // following vars are used for the cyclic logic |
| | 272 | value1, value2, |
| | 273 | isObject1, isObject2, |
| | 274 | index1, index2, |
| | 275 | newPath1, newPath2; |
| | 276 | |
| | 277 | for (i = 0, l = keys1.length; i < l; i++) { |
| | 278 | key = keys1[i]; |
| | 279 | if (!o.hasOwnProperty.call(obj2, key)) { |
| | 280 | return false; |
| | 281 | } |
| | 282 | |
| | 283 | // Start of the cyclic logic |
| | 284 | |
| | 285 | value1 = obj1[key]; |
| | 286 | value2 = obj2[key]; |
| | 287 | |
| | 288 | isObject1 = isObject(value1); |
| | 289 | isObject2 = isObject(value2); |
| | 290 | |
| | 291 | // determine, if the objects were already visited |
| | 292 | // (it's faster to check for isObject first, than to |
| | 293 | // get -1 from getIndex for non objects) |
| | 294 | index1 = isObject1 ? getIndex(objects1, value1) : -1; |
| | 295 | index2 = isObject2 ? getIndex(objects2, value2) : -1; |
| | 296 | |
| | 297 | // determine the new pathes of the objects |
| | 298 | // - for non cyclic objects the current path will be extended |
| | 299 | // by current property name |
| | 300 | // - for cyclic objects the stored path is taken |
| | 301 | newPath1 = index1 !== -1 |
| | 302 | ? paths1[index1] |
| | 303 | : path1 + '[' + JSON.stringify(key) + ']'; |
| | 304 | newPath2 = index2 !== -1 |
| | 305 | ? paths2[index2] |
| | 306 | : path2 + '[' + JSON.stringify(key) + ']'; |
| | 307 | |
| | 308 | // stop recursion if current objects are already compared |
| | 309 | if (compared[newPath1 + newPath2]) { |
| | 310 | return true; |
| | 311 | } |
| | 312 | |
| | 313 | // remember the current objects and their pathes |
| | 314 | if (index1 === -1 && isObject1) { |
| | 315 | objects1.push(value1); |
| | 316 | paths1.push(newPath1); |
| | 317 | } |
| | 318 | if (index2 === -1 && isObject2) { |
| | 319 | objects2.push(value2); |
| | 320 | paths2.push(newPath2); |
| | 321 | } |
| | 322 | |
| | 323 | // remember that the current objects are already compared |
| | 324 | if (isObject1 && isObject2) { |
| | 325 | compared[newPath1 + newPath2] = true; |
| | 326 | } |
| | 327 | |
| | 328 | // End of cyclic logic |
| | 329 | |
| | 330 | // neither value1 nor value2 is a cycle |
| | 331 | // continue with next level |
| | 332 | if (!deepEqual(value1, value2, newPath1, newPath2)) { |
| | 333 | return false; |
| | 334 | } |
| | 335 | } |
| | 336 | |
| | 337 | return true; |
| | 338 | |
| | 339 | }(obj1, obj2, '$1', '$2')); |
| | 340 | } |
| | 341 | |
| | 342 | var match; |
| | 343 | |
| | 344 | function arrayContains(array, subset) { |
| | 345 | if (subset.length === 0) { return true; } |
| | 346 | var i, l, j, k; |
| | 347 | for (i = 0, l = array.length; i < l; ++i) { |
| | 348 | if (match(array[i], subset[0])) { |
| | 349 | for (j = 0, k = subset.length; j < k; ++j) { |
| | 350 | if (!match(array[i + j], subset[j])) { return false; } |
| | 351 | } |
| | 352 | return true; |
| | 353 | } |
| | 354 | } |
| | 355 | return false; |
| | 356 | } |
| | 357 | |
| | 358 | /** |
| | 359 | * @name samsam.match |
| | 360 | * @param Object object |
| | 361 | * @param Object matcher |
| | 362 | * |
| | 363 | * Compare arbitrary value ``object`` with matcher. |
| | 364 | */ |
| | 365 | match = function match(object, matcher) { |
| | 366 | if (matcher && typeof matcher.test === "function") { |
| | 367 | return matcher.test(object); |
| | 368 | } |
| | 369 | |
| | 370 | if (typeof matcher === "function") { |
| | 371 | return matcher(object) === true; |
| | 372 | } |
| | 373 | |
| | 374 | if (typeof matcher === "string") { |
| | 375 | matcher = matcher.toLowerCase(); |
| | 376 | var notNull = typeof object === "string" || !!object; |
| | 377 | return notNull && |
| | 378 | (String(object)).toLowerCase().indexOf(matcher) >= 0; |
| | 379 | } |
| | 380 | |
| | 381 | if (typeof matcher === "number") { |
| | 382 | return matcher === object; |
| | 383 | } |
| | 384 | |
| | 385 | if (typeof matcher === "boolean") { |
| | 386 | return matcher === object; |
| | 387 | } |
| | 388 | |
| | 389 | if (getClass(object) === "Array" && getClass(matcher) === "Array") { |
| | 390 | return arrayContains(object, matcher); |
| | 391 | } |
| | 392 | |
| | 393 | if (matcher && typeof matcher === "object") { |
| | 394 | var prop; |
| | 395 | for (prop in matcher) { |
| | 396 | if (!match(object[prop], matcher[prop])) { |
| | 397 | return false; |
| | 398 | } |
| | 399 | } |
| | 400 | return true; |
| | 401 | } |
| | 402 | |
| | 403 | throw new Error("Matcher was not a string, a number, a " + |
| | 404 | "function, a boolean or an object"); |
| | 405 | }; |
| | 406 | |
| | 407 | return { |
| | 408 | isArguments: isArguments, |
| | 409 | isElement: isElement, |
| | 410 | isDate: isDate, |
| | 411 | isNegZero: isNegZero, |
| | 412 | identical: identical, |
| | 413 | deepEqual: deepEqualCyclic, |
| | 414 | match: match, |
| | 415 | keys: keys |
| | 416 | }; |
| | 417 | }); |
| | 418 | ((typeof define === "function" && define.amd && function (m) { |
| | 419 | define("formatio", ["samsam"], m); |
| | 420 | }) || (typeof module === "object" && function (m) { |
| | 421 | module.exports = m(require("samsam")); |
| | 422 | }) || function (m) { this.formatio = m(this.samsam); } |
| | 423 | )(function (samsam) { |
| | 424 | |
| | 425 | var formatio = { |
| | 426 | excludeConstructors: ["Object", /^.$/], |
| | 427 | quoteStrings: true |
| | 428 | }; |
| | 429 | |
| | 430 | var hasOwn = Object.prototype.hasOwnProperty; |
| | 431 | |
| | 432 | var specialObjects = []; |
| | 433 | if (typeof global !== "undefined") { |
| | 434 | specialObjects.push({ object: global, value: "[object global]" }); |
| | 435 | } |
| | 436 | if (typeof document !== "undefined") { |
| | 437 | specialObjects.push({ |
| | 438 | object: document, |
| | 439 | value: "[object HTMLDocument]" |
| | 440 | }); |
| | 441 | } |
| | 442 | if (typeof window !== "undefined") { |
| | 443 | specialObjects.push({ object: window, value: "[object Window]" }); |
| | 444 | } |
| | 445 | |
| | 446 | function functionName(func) { |
| | 447 | if (!func) { return ""; } |
| | 448 | if (func.displayName) { return func.displayName; } |
| | 449 | if (func.name) { return func.name; } |
| | 450 | var matches = func.toString().match(/function\s+([^\(]+)/m); |
| | 451 | return (matches && matches[1]) || ""; |
| | 452 | } |
| | 453 | |
| | 454 | function constructorName(f, object) { |
| | 455 | var name = functionName(object && object.constructor); |
| | 456 | var excludes = f.excludeConstructors || |
| | 457 | formatio.excludeConstructors || []; |
| | 458 | |
| | 459 | var i, l; |
| | 460 | for (i = 0, l = excludes.length; i < l; ++i) { |
| | 461 | if (typeof excludes[i] === "string" && excludes[i] === name) { |
| | 462 | return ""; |
| | 463 | } else if (excludes[i].test && excludes[i].test(name)) { |
| | 464 | return ""; |
| | 465 | } |
| | 466 | } |
| | 467 | |
| | 468 | return name; |
| | 469 | } |
| | 470 | |
| | 471 | function isCircular(object, objects) { |
| | 472 | if (typeof object !== "object") { return false; } |
| | 473 | var i, l; |
| | 474 | for (i = 0, l = objects.length; i < l; ++i) { |
| | 475 | if (objects[i] === object) { return true; } |
| | 476 | } |
| | 477 | return false; |
| | 478 | } |
| | 479 | |
| | 480 | function ascii(f, object, processed, indent) { |
| | 481 | if (typeof object === "string") { |
| | 482 | var qs = f.quoteStrings; |
| | 483 | var quote = typeof qs !== "boolean" || qs; |
| | 484 | return processed || quote ? '"' + object + '"' : object; |
| | 485 | } |
| | 486 | |
| | 487 | if (typeof object === "function" && !(object instanceof RegExp)) { |
| | 488 | return ascii.func(object); |
| | 489 | } |
| | 490 | |
| | 491 | processed = processed || []; |
| | 492 | |
| | 493 | if (isCircular(object, processed)) { return "[Circular]"; } |
| | 494 | |
| | 495 | if (Object.prototype.toString.call(object) === "[object Array]") { |
| | 496 | return ascii.array.call(f, object, processed); |
| | 497 | } |
| | 498 | |
| | 499 | if (!object) { return String((1/object) === -Infinity ? "-0" : object); } |
| | 500 | if (samsam.isElement(object)) { return ascii.element(object); } |
| | 501 | |
| | 502 | if (typeof object.toString === "function" && |
| | 503 | object.toString !== Object.prototype.toString) { |
| | 504 | return object.toString(); |
| | 505 | } |
| | 506 | |
| | 507 | var i, l; |
| | 508 | for (i = 0, l = specialObjects.length; i < l; i++) { |
| | 509 | if (object === specialObjects[i].object) { |
| | 510 | return specialObjects[i].value; |
| | 511 | } |
| | 512 | } |
| | 513 | |
| | 514 | return ascii.object.call(f, object, processed, indent); |
| | 515 | } |
| | 516 | |
| | 517 | ascii.func = function (func) { |
| | 518 | return "function " + functionName(func) + "() {}"; |
| | 519 | }; |
| | 520 | |
| | 521 | ascii.array = function (array, processed) { |
| | 522 | processed = processed || []; |
| | 523 | processed.push(array); |
| | 524 | var i, l, pieces = []; |
| | 525 | for (i = 0, l = array.length; i < l; ++i) { |
| | 526 | pieces.push(ascii(this, array[i], processed)); |
| | 527 | } |
| | 528 | return "[" + pieces.join(", ") + "]"; |
| | 529 | }; |
| | 530 | |
| | 531 | ascii.object = function (object, processed, indent) { |
| | 532 | processed = processed || []; |
| | 533 | processed.push(object); |
| | 534 | indent = indent || 0; |
| | 535 | var pieces = [], properties = samsam.keys(object).sort(); |
| | 536 | var length = 3; |
| | 537 | var prop, str, obj, i, l; |
| | 538 | |
| | 539 | for (i = 0, l = properties.length; i < l; ++i) { |
| | 540 | prop = properties[i]; |
| | 541 | obj = object[prop]; |
| | 542 | |
| | 543 | if (isCircular(obj, processed)) { |
| | 544 | str = "[Circular]"; |
| | 545 | } else { |
| | 546 | str = ascii(this, obj, processed, indent + 2); |
| | 547 | } |
| | 548 | |
| | 549 | str = (/\s/.test(prop) ? '"' + prop + '"' : prop) + ": " + str; |
| | 550 | length += str.length; |
| | 551 | pieces.push(str); |
| | 552 | } |
| | 553 | |
| | 554 | var cons = constructorName(this, object); |
| | 555 | var prefix = cons ? "[" + cons + "] " : ""; |
| | 556 | var is = ""; |
| | 557 | for (i = 0, l = indent; i < l; ++i) { is += " "; } |
| | 558 | |
| | 559 | if (length + indent > 80) { |
| | 560 | return prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" + |
| | 561 | is + "}"; |
| | 562 | } |
| | 563 | return prefix + "{ " + pieces.join(", ") + " }"; |
| | 564 | }; |
| | 565 | |
| | 566 | ascii.element = function (element) { |
| | 567 | var tagName = element.tagName.toLowerCase(); |
| | 568 | var attrs = element.attributes, attr, pairs = [], attrName, i, l, val; |
| | 569 | |
| | 570 | for (i = 0, l = attrs.length; i < l; ++i) { |
| | 571 | attr = attrs.item(i); |
| | 572 | attrName = attr.nodeName.toLowerCase().replace("html:", ""); |
| | 573 | val = attr.nodeValue; |
| | 574 | if (attrName !== "contenteditable" || val !== "inherit") { |
| | 575 | if (!!val) { pairs.push(attrName + "=\"" + val + "\""); } |
| | 576 | } |
| | 577 | } |
| | 578 | |
| | 579 | var formatted = "<" + tagName + (pairs.length > 0 ? " " : ""); |
| | 580 | var content = element.innerHTML; |
| | 581 | |
| | 582 | if (content.length > 20) { |
| | 583 | content = content.substr(0, 20) + "[...]"; |
| | 584 | } |
| | 585 | |
| | 586 | var res = formatted + pairs.join(" ") + ">" + content + |
| | 587 | "</" + tagName + ">"; |
| | 588 | |
| | 589 | return res.replace(/ contentEditable="inherit"/, ""); |
| | 590 | }; |
| | 591 | |
| | 592 | function Formatio(options) { |
| | 593 | for (var opt in options) { |
| | 594 | this[opt] = options[opt]; |
| | 595 | } |
| | 596 | } |
| | 597 | |
| | 598 | Formatio.prototype = { |
| | 599 | functionName: functionName, |
| | 600 | |
| | 601 | configure: function (options) { |
| | 602 | return new Formatio(options); |
| | 603 | }, |
| | 604 | |
| | 605 | constructorName: function (object) { |
| | 606 | return constructorName(this, object); |
| | 607 | }, |
| | 608 | |
| | 609 | ascii: function (object, processed, indent) { |
| | 610 | return ascii(this, object, processed, indent); |
| | 611 | } |
| | 612 | }; |
| | 613 | |
| | 614 | return Formatio.prototype; |
| | 615 | }); |
| | 616 | /*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/ |
| | 617 | /*global module, require, __dirname, document*/ |
| | 618 | /** |
| | 619 | * Sinon core utilities. For internal use only. |
| | 620 | * |
| | 621 | * @author Christian Johansen (christian@cjohansen.no) |
| | 622 | * @license BSD |
| | 623 | * |
| | 624 | * Copyright (c) 2010-2013 Christian Johansen |
| | 625 | */ |
| | 626 | |
| | 627 | var sinon = (function (formatio) { |
| | 628 | var div = typeof document != "undefined" && document.createElement("div"); |
| | 629 | var hasOwn = Object.prototype.hasOwnProperty; |
| | 630 | |
| | 631 | function isDOMNode(obj) { |
| | 632 | var success = false; |
| | 633 | |
| | 634 | try { |
| | 635 | obj.appendChild(div); |
| | 636 | success = div.parentNode == obj; |
| | 637 | } catch (e) { |
| | 638 | return false; |
| | 639 | } finally { |
| | 640 | try { |
| | 641 | obj.removeChild(div); |
| | 642 | } catch (e) { |
| | 643 | // Remove failed, not much we can do about that |
| | 644 | } |
| | 645 | } |
| | 646 | |
| | 647 | return success; |
| | 648 | } |
| | 649 | |
| | 650 | function isElement(obj) { |
| | 651 | return div && obj && obj.nodeType === 1 && isDOMNode(obj); |
| | 652 | } |
| | 653 | |
| | 654 | function isFunction(obj) { |
| | 655 | return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply); |
| | 656 | } |
| | 657 | |
| | 658 | function mirrorProperties(target, source) { |
| | 659 | for (var prop in source) { |
| | 660 | if (!hasOwn.call(target, prop)) { |
| | 661 | target[prop] = source[prop]; |
| | 662 | } |
| | 663 | } |
| | 664 | } |
| | 665 | |
| | 666 | function isRestorable (obj) { |
| | 667 | return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon; |
| | 668 | } |
| | 669 | |
| | 670 | var sinon = { |
| | 671 | wrapMethod: function wrapMethod(object, property, method) { |
| | 672 | if (!object) { |
| | 673 | throw new TypeError("Should wrap property of object"); |
| | 674 | } |
| | 675 | |
| | 676 | if (typeof method != "function") { |
| | 677 | throw new TypeError("Method wrapper should be function"); |
| | 678 | } |
| | 679 | |
| | 680 | var wrappedMethod = object[property], |
| | 681 | error; |
| | 682 | |
| | 683 | if (!isFunction(wrappedMethod)) { |
| | 684 | error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " + |
| | 685 | property + " as function"); |
| | 686 | } |
| | 687 | |
| | 688 | if (wrappedMethod.restore && wrappedMethod.restore.sinon) { |
| | 689 | error = new TypeError("Attempted to wrap " + property + " which is already wrapped"); |
| | 690 | } |
| | 691 | |
| | 692 | if (wrappedMethod.calledBefore) { |
| | 693 | var verb = !!wrappedMethod.returns ? "stubbed" : "spied on"; |
| | 694 | error = new TypeError("Attempted to wrap " + property + " which is already " + verb); |
| | 695 | } |
| | 696 | |
| | 697 | if (error) { |
| | 698 | if (wrappedMethod._stack) { |
| | 699 | error.stack += '\n--------------\n' + wrappedMethod._stack; |
| | 700 | } |
| | 701 | throw error; |
| | 702 | } |
| | 703 | |
| | 704 | // IE 8 does not support hasOwnProperty on the window object and Firefox has a problem |
| | 705 | // when using hasOwn.call on objects from other frames. |
| | 706 | var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property); |
| | 707 | object[property] = method; |
| | 708 | method.displayName = property; |
| | 709 | // Set up a stack trace which can be used later to find what line of |
| | 710 | // code the original method was created on. |
| | 711 | method._stack = (new Error('Stack Trace for original')).stack; |
| | 712 | |
| | 713 | method.restore = function () { |
| | 714 | // For prototype properties try to reset by delete first. |
| | 715 | // If this fails (ex: localStorage on mobile safari) then force a reset |
| | 716 | // via direct assignment. |
| | 717 | if (!owned) { |
| | 718 | delete object[property]; |
| | 719 | } |
| | 720 | if (object[property] === method) { |
| | 721 | object[property] = wrappedMethod; |
| | 722 | } |
| | 723 | }; |
| | 724 | |
| | 725 | method.restore.sinon = true; |
| | 726 | mirrorProperties(method, wrappedMethod); |
| | 727 | |
| | 728 | return method; |
| | 729 | }, |
| | 730 | |
| | 731 | extend: function extend(target) { |
| | 732 | for (var i = 1, l = arguments.length; i < l; i += 1) { |
| | 733 | for (var prop in arguments[i]) { |
| | 734 | if (arguments[i].hasOwnProperty(prop)) { |
| | 735 | target[prop] = arguments[i][prop]; |
| | 736 | } |
| | 737 | |
| | 738 | // DONT ENUM bug, only care about toString |
| | 739 | if (arguments[i].hasOwnProperty("toString") && |
| | 740 | arguments[i].toString != target.toString) { |
| | 741 | target.toString = arguments[i].toString; |
| | 742 | } |
| | 743 | } |
| | 744 | } |
| | 745 | |
| | 746 | return target; |
| | 747 | }, |
| | 748 | |
| | 749 | create: function create(proto) { |
| | 750 | var F = function () {}; |
| | 751 | F.prototype = proto; |
| | 752 | return new F(); |
| | 753 | }, |
| | 754 | |
| | 755 | deepEqual: function deepEqual(a, b) { |
| | 756 | if (sinon.match && sinon.match.isMatcher(a)) { |
| | 757 | return a.test(b); |
| | 758 | } |
| | 759 | if (typeof a != "object" || typeof b != "object") { |
| | 760 | return a === b; |
| | 761 | } |
| | 762 | |
| | 763 | if (isElement(a) || isElement(b)) { |
| | 764 | return a === b; |
| | 765 | } |
| | 766 | |
| | 767 | if (a === b) { |
| | 768 | return true; |
| | 769 | } |
| | 770 | |
| | 771 | if ((a === null && b !== null) || (a !== null && b === null)) { |
| | 772 | return false; |
| | 773 | } |
| | 774 | |
| | 775 | var aString = Object.prototype.toString.call(a); |
| | 776 | if (aString != Object.prototype.toString.call(b)) { |
| | 777 | return false; |
| | 778 | } |
| | 779 | |
| | 780 | if (aString == "[object Date]") { |
| | 781 | return a.valueOf() === b.valueOf(); |
| | 782 | } |
| | 783 | |
| | 784 | var prop, aLength = 0, bLength = 0; |
| | 785 | |
| | 786 | if (aString == "[object Array]" && a.length !== b.length) { |
| | 787 | return false; |
| | 788 | } |
| | 789 | |
| | 790 | for (prop in a) { |
| | 791 | aLength += 1; |
| | 792 | |
| | 793 | if (!deepEqual(a[prop], b[prop])) { |
| | 794 | return false; |
| | 795 | } |
| | 796 | } |
| | 797 | |
| | 798 | for (prop in b) { |
| | 799 | bLength += 1; |
| | 800 | } |
| | 801 | |
| | 802 | return aLength == bLength; |
| | 803 | }, |
| | 804 | |
| | 805 | functionName: function functionName(func) { |
| | 806 | var name = func.displayName || func.name; |
| | 807 | |
| | 808 | // Use function decomposition as a last resort to get function |
| | 809 | // name. Does not rely on function decomposition to work - if it |
| | 810 | // doesn't debugging will be slightly less informative |
| | 811 | // (i.e. toString will say 'spy' rather than 'myFunc'). |
| | 812 | if (!name) { |
| | 813 | var matches = func.toString().match(/function ([^\s\(]+)/); |
| | 814 | name = matches && matches[1]; |
| | 815 | } |
| | 816 | |
| | 817 | return name; |
| | 818 | }, |
| | 819 | |
| | 820 | functionToString: function toString() { |
| | 821 | if (this.getCall && this.callCount) { |
| | 822 | var thisValue, prop, i = this.callCount; |
| | 823 | |
| | 824 | while (i--) { |
| | 825 | thisValue = this.getCall(i).thisValue; |
| | 826 | |
| | 827 | for (prop in thisValue) { |
| | 828 | if (thisValue[prop] === this) { |
| | 829 | return prop; |
| | 830 | } |
| | 831 | } |
| | 832 | } |
| | 833 | } |
| | 834 | |
| | 835 | return this.displayName || "sinon fake"; |
| | 836 | }, |
| | 837 | |
| | 838 | getConfig: function (custom) { |
| | 839 | var config = {}; |
| | 840 | custom = custom || {}; |
| | 841 | var defaults = sinon.defaultConfig; |
| | 842 | |
| | 843 | for (var prop in defaults) { |
| | 844 | if (defaults.hasOwnProperty(prop)) { |
| | 845 | config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop]; |
| | 846 | } |
| | 847 | } |
| | 848 | |
| | 849 | return config; |
| | 850 | }, |
| | 851 | |
| | 852 | format: function (val) { |
| | 853 | return "" + val; |
| | 854 | }, |
| | 855 | |
| | 856 | defaultConfig: { |
| | 857 | injectIntoThis: true, |
| | 858 | injectInto: null, |
| | 859 | properties: ["spy", "stub", "mock", "clock", "server", "requests"], |
| | 860 | useFakeTimers: true, |
| | 861 | useFakeServer: true |
| | 862 | }, |
| | 863 | |
| | 864 | timesInWords: function timesInWords(count) { |
| | 865 | return count == 1 && "once" || |
| | 866 | count == 2 && "twice" || |
| | 867 | count == 3 && "thrice" || |
| | 868 | (count || 0) + " times"; |
| | 869 | }, |
| | 870 | |
| | 871 | calledInOrder: function (spies) { |
| | 872 | for (var i = 1, l = spies.length; i < l; i++) { |
| | 873 | if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) { |
| | 874 | return false; |
| | 875 | } |
| | 876 | } |
| | 877 | |
| | 878 | return true; |
| | 879 | }, |
| | 880 | |
| | 881 | orderByFirstCall: function (spies) { |
| | 882 | return spies.sort(function (a, b) { |
| | 883 | // uuid, won't ever be equal |
| | 884 | var aCall = a.getCall(0); |
| | 885 | var bCall = b.getCall(0); |
| | 886 | var aId = aCall && aCall.callId || -1; |
| | 887 | var bId = bCall && bCall.callId || -1; |
| | 888 | |
| | 889 | return aId < bId ? -1 : 1; |
| | 890 | }); |
| | 891 | }, |
| | 892 | |
| | 893 | log: function () {}, |
| | 894 | |
| | 895 | logError: function (label, err) { |
| | 896 | var msg = label + " threw exception: "; |
| | 897 | sinon.log(msg + "[" + err.name + "] " + err.message); |
| | 898 | if (err.stack) { sinon.log(err.stack); } |
| | 899 | |
| | 900 | setTimeout(function () { |
| | 901 | err.message = msg + err.message; |
| | 902 | throw err; |
| | 903 | }, 0); |
| | 904 | }, |
| | 905 | |
| | 906 | typeOf: function (value) { |
| | 907 | if (value === null) { |
| | 908 | return "null"; |
| | 909 | } |
| | 910 | else if (value === undefined) { |
| | 911 | return "undefined"; |
| | 912 | } |
| | 913 | var string = Object.prototype.toString.call(value); |
| | 914 | return string.substring(8, string.length - 1).toLowerCase(); |
| | 915 | }, |
| | 916 | |
| | 917 | createStubInstance: function (constructor) { |
| | 918 | if (typeof constructor !== "function") { |
| | 919 | throw new TypeError("The constructor should be a function."); |
| | 920 | } |
| | 921 | return sinon.stub(sinon.create(constructor.prototype)); |
| | 922 | }, |
| | 923 | |
| | 924 | restore: function (object) { |
| | 925 | if (object !== null && typeof object === "object") { |
| | 926 | for (var prop in object) { |
| | 927 | if (isRestorable(object[prop])) { |
| | 928 | object[prop].restore(); |
| | 929 | } |
| | 930 | } |
| | 931 | } |
| | 932 | else if (isRestorable(object)) { |
| | 933 | object.restore(); |
| | 934 | } |
| | 935 | } |
| | 936 | }; |
| | 937 | |
| | 938 | var isNode = typeof module !== "undefined" && module.exports; |
| | 939 | var isAMD = typeof define === 'function' && typeof define.amd === 'object' && define.amd; |
| | 940 | |
| | 941 | if (isAMD) { |
| | 942 | define(function(){ |
| | 943 | return sinon; |
| | 944 | }); |
| | 945 | } else if (isNode) { |
| | 946 | try { |
| | 947 | formatio = require("formatio"); |
| | 948 | } catch (e) {} |
| | 949 | module.exports = sinon; |
| | 950 | module.exports.spy = require("./sinon/spy"); |
| | 951 | module.exports.spyCall = require("./sinon/call"); |
| | 952 | module.exports.behavior = require("./sinon/behavior"); |
| | 953 | module.exports.stub = require("./sinon/stub"); |
| | 954 | module.exports.mock = require("./sinon/mock"); |
| | 955 | module.exports.collection = require("./sinon/collection"); |
| | 956 | module.exports.assert = require("./sinon/assert"); |
| | 957 | module.exports.sandbox = require("./sinon/sandbox"); |
| | 958 | module.exports.test = require("./sinon/test"); |
| | 959 | module.exports.testCase = require("./sinon/test_case"); |
| | 960 | module.exports.assert = require("./sinon/assert"); |
| | 961 | module.exports.match = require("./sinon/match"); |
| | 962 | } |
| | 963 | |
| | 964 | if (formatio) { |
| | 965 | var formatter = formatio.configure({ quoteStrings: false }); |
| | 966 | sinon.format = function () { |
| | 967 | return formatter.ascii.apply(formatter, arguments); |
| | 968 | }; |
| | 969 | } else if (isNode) { |
| | 970 | try { |
| | 971 | var util = require("util"); |
| | 972 | sinon.format = function (value) { |
| | 973 | return typeof value == "object" && value.toString === Object.prototype.toString ? util.inspect(value) : value; |
| | 974 | }; |
| | 975 | } catch (e) { |
| | 976 | /* Node, but no util module - would be very old, but better safe than |
| | 977 | sorry */ |
| | 978 | } |
| | 979 | } |
| | 980 | |
| | 981 | return sinon; |
| | 982 | }(typeof formatio == "object" && formatio)); |
| | 983 | |
| | 984 | /* @depend ../sinon.js */ |
| | 985 | /*jslint eqeqeq: false, onevar: false, plusplus: false*/ |
| | 986 | /*global module, require, sinon*/ |
| | 987 | /** |
| | 988 | * Match functions |
| | 989 | * |
| | 990 | * @author Maximilian Antoni (mail@maxantoni.de) |
| | 991 | * @license BSD |
| | 992 | * |
| | 993 | * Copyright (c) 2012 Maximilian Antoni |
| | 994 | */ |
| | 995 | |
| | 996 | (function (sinon) { |
| | 997 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 998 | |
| | 999 | if (!sinon && commonJSModule) { |
| | 1000 | sinon = require("../sinon"); |
| | 1001 | } |
| | 1002 | |
| | 1003 | if (!sinon) { |
| | 1004 | return; |
| | 1005 | } |
| | 1006 | |
| | 1007 | function assertType(value, type, name) { |
| | 1008 | var actual = sinon.typeOf(value); |
| | 1009 | if (actual !== type) { |
| | 1010 | throw new TypeError("Expected type of " + name + " to be " + |
| | 1011 | type + ", but was " + actual); |
| | 1012 | } |
| | 1013 | } |
| | 1014 | |
| | 1015 | var matcher = { |
| | 1016 | toString: function () { |
| | 1017 | return this.message; |
| | 1018 | } |
| | 1019 | }; |
| | 1020 | |
| | 1021 | function isMatcher(object) { |
| | 1022 | return matcher.isPrototypeOf(object); |
| | 1023 | } |
| | 1024 | |
| | 1025 | function matchObject(expectation, actual) { |
| | 1026 | if (actual === null || actual === undefined) { |
| | 1027 | return false; |
| | 1028 | } |
| | 1029 | for (var key in expectation) { |
| | 1030 | if (expectation.hasOwnProperty(key)) { |
| | 1031 | var exp = expectation[key]; |
| | 1032 | var act = actual[key]; |
| | 1033 | if (match.isMatcher(exp)) { |
| | 1034 | if (!exp.test(act)) { |
| | 1035 | return false; |
| | 1036 | } |
| | 1037 | } else if (sinon.typeOf(exp) === "object") { |
| | 1038 | if (!matchObject(exp, act)) { |
| | 1039 | return false; |
| | 1040 | } |
| | 1041 | } else if (!sinon.deepEqual(exp, act)) { |
| | 1042 | return false; |
| | 1043 | } |
| | 1044 | } |
| | 1045 | } |
| | 1046 | return true; |
| | 1047 | } |
| | 1048 | |
| | 1049 | matcher.or = function (m2) { |
| | 1050 | if (!isMatcher(m2)) { |
| | 1051 | throw new TypeError("Matcher expected"); |
| | 1052 | } |
| | 1053 | var m1 = this; |
| | 1054 | var or = sinon.create(matcher); |
| | 1055 | or.test = function (actual) { |
| | 1056 | return m1.test(actual) || m2.test(actual); |
| | 1057 | }; |
| | 1058 | or.message = m1.message + ".or(" + m2.message + ")"; |
| | 1059 | return or; |
| | 1060 | }; |
| | 1061 | |
| | 1062 | matcher.and = function (m2) { |
| | 1063 | if (!isMatcher(m2)) { |
| | 1064 | throw new TypeError("Matcher expected"); |
| | 1065 | } |
| | 1066 | var m1 = this; |
| | 1067 | var and = sinon.create(matcher); |
| | 1068 | and.test = function (actual) { |
| | 1069 | return m1.test(actual) && m2.test(actual); |
| | 1070 | }; |
| | 1071 | and.message = m1.message + ".and(" + m2.message + ")"; |
| | 1072 | return and; |
| | 1073 | }; |
| | 1074 | |
| | 1075 | var match = function (expectation, message) { |
| | 1076 | var m = sinon.create(matcher); |
| | 1077 | var type = sinon.typeOf(expectation); |
| | 1078 | switch (type) { |
| | 1079 | case "object": |
| | 1080 | if (typeof expectation.test === "function") { |
| | 1081 | m.test = function (actual) { |
| | 1082 | return expectation.test(actual) === true; |
| | 1083 | }; |
| | 1084 | m.message = "match(" + sinon.functionName(expectation.test) + ")"; |
| | 1085 | return m; |
| | 1086 | } |
| | 1087 | var str = []; |
| | 1088 | for (var key in expectation) { |
| | 1089 | if (expectation.hasOwnProperty(key)) { |
| | 1090 | str.push(key + ": " + expectation[key]); |
| | 1091 | } |
| | 1092 | } |
| | 1093 | m.test = function (actual) { |
| | 1094 | return matchObject(expectation, actual); |
| | 1095 | }; |
| | 1096 | m.message = "match(" + str.join(", ") + ")"; |
| | 1097 | break; |
| | 1098 | case "number": |
| | 1099 | m.test = function (actual) { |
| | 1100 | return expectation == actual; |
| | 1101 | }; |
| | 1102 | break; |
| | 1103 | case "string": |
| | 1104 | m.test = function (actual) { |
| | 1105 | if (typeof actual !== "string") { |
| | 1106 | return false; |
| | 1107 | } |
| | 1108 | return actual.indexOf(expectation) !== -1; |
| | 1109 | }; |
| | 1110 | m.message = "match(\"" + expectation + "\")"; |
| | 1111 | break; |
| | 1112 | case "regexp": |
| | 1113 | m.test = function (actual) { |
| | 1114 | if (typeof actual !== "string") { |
| | 1115 | return false; |
| | 1116 | } |
| | 1117 | return expectation.test(actual); |
| | 1118 | }; |
| | 1119 | break; |
| | 1120 | case "function": |
| | 1121 | m.test = expectation; |
| | 1122 | if (message) { |
| | 1123 | m.message = message; |
| | 1124 | } else { |
| | 1125 | m.message = "match(" + sinon.functionName(expectation) + ")"; |
| | 1126 | } |
| | 1127 | break; |
| | 1128 | default: |
| | 1129 | m.test = function (actual) { |
| | 1130 | return sinon.deepEqual(expectation, actual); |
| | 1131 | }; |
| | 1132 | } |
| | 1133 | if (!m.message) { |
| | 1134 | m.message = "match(" + expectation + ")"; |
| | 1135 | } |
| | 1136 | return m; |
| | 1137 | }; |
| | 1138 | |
| | 1139 | match.isMatcher = isMatcher; |
| | 1140 | |
| | 1141 | match.any = match(function () { |
| | 1142 | return true; |
| | 1143 | }, "any"); |
| | 1144 | |
| | 1145 | match.defined = match(function (actual) { |
| | 1146 | return actual !== null && actual !== undefined; |
| | 1147 | }, "defined"); |
| | 1148 | |
| | 1149 | match.truthy = match(function (actual) { |
| | 1150 | return !!actual; |
| | 1151 | }, "truthy"); |
| | 1152 | |
| | 1153 | match.falsy = match(function (actual) { |
| | 1154 | return !actual; |
| | 1155 | }, "falsy"); |
| | 1156 | |
| | 1157 | match.same = function (expectation) { |
| | 1158 | return match(function (actual) { |
| | 1159 | return expectation === actual; |
| | 1160 | }, "same(" + expectation + ")"); |
| | 1161 | }; |
| | 1162 | |
| | 1163 | match.typeOf = function (type) { |
| | 1164 | assertType(type, "string", "type"); |
| | 1165 | return match(function (actual) { |
| | 1166 | return sinon.typeOf(actual) === type; |
| | 1167 | }, "typeOf(\"" + type + "\")"); |
| | 1168 | }; |
| | 1169 | |
| | 1170 | match.instanceOf = function (type) { |
| | 1171 | assertType(type, "function", "type"); |
| | 1172 | return match(function (actual) { |
| | 1173 | return actual instanceof type; |
| | 1174 | }, "instanceOf(" + sinon.functionName(type) + ")"); |
| | 1175 | }; |
| | 1176 | |
| | 1177 | function createPropertyMatcher(propertyTest, messagePrefix) { |
| | 1178 | return function (property, value) { |
| | 1179 | assertType(property, "string", "property"); |
| | 1180 | var onlyProperty = arguments.length === 1; |
| | 1181 | var message = messagePrefix + "(\"" + property + "\""; |
| | 1182 | if (!onlyProperty) { |
| | 1183 | message += ", " + value; |
| | 1184 | } |
| | 1185 | message += ")"; |
| | 1186 | return match(function (actual) { |
| | 1187 | if (actual === undefined || actual === null || |
| | 1188 | !propertyTest(actual, property)) { |
| | 1189 | return false; |
| | 1190 | } |
| | 1191 | return onlyProperty || sinon.deepEqual(value, actual[property]); |
| | 1192 | }, message); |
| | 1193 | }; |
| | 1194 | } |
| | 1195 | |
| | 1196 | match.has = createPropertyMatcher(function (actual, property) { |
| | 1197 | if (typeof actual === "object") { |
| | 1198 | return property in actual; |
| | 1199 | } |
| | 1200 | return actual[property] !== undefined; |
| | 1201 | }, "has"); |
| | 1202 | |
| | 1203 | match.hasOwn = createPropertyMatcher(function (actual, property) { |
| | 1204 | return actual.hasOwnProperty(property); |
| | 1205 | }, "hasOwn"); |
| | 1206 | |
| | 1207 | match.bool = match.typeOf("boolean"); |
| | 1208 | match.number = match.typeOf("number"); |
| | 1209 | match.string = match.typeOf("string"); |
| | 1210 | match.object = match.typeOf("object"); |
| | 1211 | match.func = match.typeOf("function"); |
| | 1212 | match.array = match.typeOf("array"); |
| | 1213 | match.regexp = match.typeOf("regexp"); |
| | 1214 | match.date = match.typeOf("date"); |
| | 1215 | |
| | 1216 | if (commonJSModule) { |
| | 1217 | module.exports = match; |
| | 1218 | } else { |
| | 1219 | sinon.match = match; |
| | 1220 | } |
| | 1221 | }(typeof sinon == "object" && sinon || null)); |
| | 1222 | |
| | 1223 | /** |
| | 1224 | * @depend ../sinon.js |
| | 1225 | * @depend match.js |
| | 1226 | */ |
| | 1227 | /*jslint eqeqeq: false, onevar: false, plusplus: false*/ |
| | 1228 | /*global module, require, sinon*/ |
| | 1229 | /** |
| | 1230 | * Spy calls |
| | 1231 | * |
| | 1232 | * @author Christian Johansen (christian@cjohansen.no) |
| | 1233 | * @author Maximilian Antoni (mail@maxantoni.de) |
| | 1234 | * @license BSD |
| | 1235 | * |
| | 1236 | * Copyright (c) 2010-2013 Christian Johansen |
| | 1237 | * Copyright (c) 2013 Maximilian Antoni |
| | 1238 | */ |
| | 1239 | |
| | 1240 | (function (sinon) { |
| | 1241 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 1242 | if (!sinon && commonJSModule) { |
| | 1243 | sinon = require("../sinon"); |
| | 1244 | } |
| | 1245 | |
| | 1246 | if (!sinon) { |
| | 1247 | return; |
| | 1248 | } |
| | 1249 | |
| | 1250 | function throwYieldError(proxy, text, args) { |
| | 1251 | var msg = sinon.functionName(proxy) + text; |
| | 1252 | if (args.length) { |
| | 1253 | msg += " Received [" + slice.call(args).join(", ") + "]"; |
| | 1254 | } |
| | 1255 | throw new Error(msg); |
| | 1256 | } |
| | 1257 | |
| | 1258 | var slice = Array.prototype.slice; |
| | 1259 | |
| | 1260 | var callProto = { |
| | 1261 | calledOn: function calledOn(thisValue) { |
| | 1262 | if (sinon.match && sinon.match.isMatcher(thisValue)) { |
| | 1263 | return thisValue.test(this.thisValue); |
| | 1264 | } |
| | 1265 | return this.thisValue === thisValue; |
| | 1266 | }, |
| | 1267 | |
| | 1268 | calledWith: function calledWith() { |
| | 1269 | for (var i = 0, l = arguments.length; i < l; i += 1) { |
| | 1270 | if (!sinon.deepEqual(arguments[i], this.args[i])) { |
| | 1271 | return false; |
| | 1272 | } |
| | 1273 | } |
| | 1274 | |
| | 1275 | return true; |
| | 1276 | }, |
| | 1277 | |
| | 1278 | calledWithMatch: function calledWithMatch() { |
| | 1279 | for (var i = 0, l = arguments.length; i < l; i += 1) { |
| | 1280 | var actual = this.args[i]; |
| | 1281 | var expectation = arguments[i]; |
| | 1282 | if (!sinon.match || !sinon.match(expectation).test(actual)) { |
| | 1283 | return false; |
| | 1284 | } |
| | 1285 | } |
| | 1286 | return true; |
| | 1287 | }, |
| | 1288 | |
| | 1289 | calledWithExactly: function calledWithExactly() { |
| | 1290 | return arguments.length == this.args.length && |
| | 1291 | this.calledWith.apply(this, arguments); |
| | 1292 | }, |
| | 1293 | |
| | 1294 | notCalledWith: function notCalledWith() { |
| | 1295 | return !this.calledWith.apply(this, arguments); |
| | 1296 | }, |
| | 1297 | |
| | 1298 | notCalledWithMatch: function notCalledWithMatch() { |
| | 1299 | return !this.calledWithMatch.apply(this, arguments); |
| | 1300 | }, |
| | 1301 | |
| | 1302 | returned: function returned(value) { |
| | 1303 | return sinon.deepEqual(value, this.returnValue); |
| | 1304 | }, |
| | 1305 | |
| | 1306 | threw: function threw(error) { |
| | 1307 | if (typeof error === "undefined" || !this.exception) { |
| | 1308 | return !!this.exception; |
| | 1309 | } |
| | 1310 | |
| | 1311 | return this.exception === error || this.exception.name === error; |
| | 1312 | }, |
| | 1313 | |
| | 1314 | calledWithNew: function calledWithNew() { |
| | 1315 | return this.proxy.prototype && this.thisValue instanceof this.proxy; |
| | 1316 | }, |
| | 1317 | |
| | 1318 | calledBefore: function (other) { |
| | 1319 | return this.callId < other.callId; |
| | 1320 | }, |
| | 1321 | |
| | 1322 | calledAfter: function (other) { |
| | 1323 | return this.callId > other.callId; |
| | 1324 | }, |
| | 1325 | |
| | 1326 | callArg: function (pos) { |
| | 1327 | this.args[pos](); |
| | 1328 | }, |
| | 1329 | |
| | 1330 | callArgOn: function (pos, thisValue) { |
| | 1331 | this.args[pos].apply(thisValue); |
| | 1332 | }, |
| | 1333 | |
| | 1334 | callArgWith: function (pos) { |
| | 1335 | this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1))); |
| | 1336 | }, |
| | 1337 | |
| | 1338 | callArgOnWith: function (pos, thisValue) { |
| | 1339 | var args = slice.call(arguments, 2); |
| | 1340 | this.args[pos].apply(thisValue, args); |
| | 1341 | }, |
| | 1342 | |
| | 1343 | "yield": function () { |
| | 1344 | this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0))); |
| | 1345 | }, |
| | 1346 | |
| | 1347 | yieldOn: function (thisValue) { |
| | 1348 | var args = this.args; |
| | 1349 | for (var i = 0, l = args.length; i < l; ++i) { |
| | 1350 | if (typeof args[i] === "function") { |
| | 1351 | args[i].apply(thisValue, slice.call(arguments, 1)); |
| | 1352 | return; |
| | 1353 | } |
| | 1354 | } |
| | 1355 | throwYieldError(this.proxy, " cannot yield since no callback was passed.", args); |
| | 1356 | }, |
| | 1357 | |
| | 1358 | yieldTo: function (prop) { |
| | 1359 | this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1))); |
| | 1360 | }, |
| | 1361 | |
| | 1362 | yieldToOn: function (prop, thisValue) { |
| | 1363 | var args = this.args; |
| | 1364 | for (var i = 0, l = args.length; i < l; ++i) { |
| | 1365 | if (args[i] && typeof args[i][prop] === "function") { |
| | 1366 | args[i][prop].apply(thisValue, slice.call(arguments, 2)); |
| | 1367 | return; |
| | 1368 | } |
| | 1369 | } |
| | 1370 | throwYieldError(this.proxy, " cannot yield to '" + prop + |
| | 1371 | "' since no callback was passed.", args); |
| | 1372 | }, |
| | 1373 | |
| | 1374 | toString: function () { |
| | 1375 | var callStr = this.proxy.toString() + "("; |
| | 1376 | var args = []; |
| | 1377 | |
| | 1378 | for (var i = 0, l = this.args.length; i < l; ++i) { |
| | 1379 | args.push(sinon.format(this.args[i])); |
| | 1380 | } |
| | 1381 | |
| | 1382 | callStr = callStr + args.join(", ") + ")"; |
| | 1383 | |
| | 1384 | if (typeof this.returnValue != "undefined") { |
| | 1385 | callStr += " => " + sinon.format(this.returnValue); |
| | 1386 | } |
| | 1387 | |
| | 1388 | if (this.exception) { |
| | 1389 | callStr += " !" + this.exception.name; |
| | 1390 | |
| | 1391 | if (this.exception.message) { |
| | 1392 | callStr += "(" + this.exception.message + ")"; |
| | 1393 | } |
| | 1394 | } |
| | 1395 | |
| | 1396 | return callStr; |
| | 1397 | } |
| | 1398 | }; |
| | 1399 | |
| | 1400 | callProto.invokeCallback = callProto.yield; |
| | 1401 | |
| | 1402 | function createSpyCall(spy, thisValue, args, returnValue, exception, id) { |
| | 1403 | if (typeof id !== "number") { |
| | 1404 | throw new TypeError("Call id is not a number"); |
| | 1405 | } |
| | 1406 | var proxyCall = sinon.create(callProto); |
| | 1407 | proxyCall.proxy = spy; |
| | 1408 | proxyCall.thisValue = thisValue; |
| | 1409 | proxyCall.args = args; |
| | 1410 | proxyCall.returnValue = returnValue; |
| | 1411 | proxyCall.exception = exception; |
| | 1412 | proxyCall.callId = id; |
| | 1413 | |
| | 1414 | return proxyCall; |
| | 1415 | } |
| | 1416 | createSpyCall.toString = callProto.toString; // used by mocks |
| | 1417 | |
| | 1418 | if (commonJSModule) { |
| | 1419 | module.exports = createSpyCall; |
| | 1420 | } else { |
| | 1421 | sinon.spyCall = createSpyCall; |
| | 1422 | } |
| | 1423 | }(typeof sinon == "object" && sinon || null)); |
| | 1424 | |
| | 1425 | |
| | 1426 | /** |
| | 1427 | * @depend ../sinon.js |
| | 1428 | * @depend call.js |
| | 1429 | */ |
| | 1430 | /*jslint eqeqeq: false, onevar: false, plusplus: false*/ |
| | 1431 | /*global module, require, sinon*/ |
| | 1432 | /** |
| | 1433 | * Spy functions |
| | 1434 | * |
| | 1435 | * @author Christian Johansen (christian@cjohansen.no) |
| | 1436 | * @license BSD |
| | 1437 | * |
| | 1438 | * Copyright (c) 2010-2013 Christian Johansen |
| | 1439 | */ |
| | 1440 | |
| | 1441 | (function (sinon) { |
| | 1442 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 1443 | var push = Array.prototype.push; |
| | 1444 | var slice = Array.prototype.slice; |
| | 1445 | var callId = 0; |
| | 1446 | |
| | 1447 | if (!sinon && commonJSModule) { |
| | 1448 | sinon = require("../sinon"); |
| | 1449 | } |
| | 1450 | |
| | 1451 | if (!sinon) { |
| | 1452 | return; |
| | 1453 | } |
| | 1454 | |
| | 1455 | function spy(object, property) { |
| | 1456 | if (!property && typeof object == "function") { |
| | 1457 | return spy.create(object); |
| | 1458 | } |
| | 1459 | |
| | 1460 | if (!object && !property) { |
| | 1461 | return spy.create(function () { }); |
| | 1462 | } |
| | 1463 | |
| | 1464 | var method = object[property]; |
| | 1465 | return sinon.wrapMethod(object, property, spy.create(method)); |
| | 1466 | } |
| | 1467 | |
| | 1468 | function matchingFake(fakes, args, strict) { |
| | 1469 | if (!fakes) { |
| | 1470 | return; |
| | 1471 | } |
| | 1472 | |
| | 1473 | for (var i = 0, l = fakes.length; i < l; i++) { |
| | 1474 | if (fakes[i].matches(args, strict)) { |
| | 1475 | return fakes[i]; |
| | 1476 | } |
| | 1477 | } |
| | 1478 | } |
| | 1479 | |
| | 1480 | function incrementCallCount() { |
| | 1481 | this.called = true; |
| | 1482 | this.callCount += 1; |
| | 1483 | this.notCalled = false; |
| | 1484 | this.calledOnce = this.callCount == 1; |
| | 1485 | this.calledTwice = this.callCount == 2; |
| | 1486 | this.calledThrice = this.callCount == 3; |
| | 1487 | } |
| | 1488 | |
| | 1489 | function createCallProperties() { |
| | 1490 | this.firstCall = this.getCall(0); |
| | 1491 | this.secondCall = this.getCall(1); |
| | 1492 | this.thirdCall = this.getCall(2); |
| | 1493 | this.lastCall = this.getCall(this.callCount - 1); |
| | 1494 | } |
| | 1495 | |
| | 1496 | var vars = "a,b,c,d,e,f,g,h,i,j,k,l"; |
| | 1497 | function createProxy(func) { |
| | 1498 | // Retain the function length: |
| | 1499 | var p; |
| | 1500 | if (func.length) { |
| | 1501 | eval("p = (function proxy(" + vars.substring(0, func.length * 2 - 1) + |
| | 1502 | ") { return p.invoke(func, this, slice.call(arguments)); });"); |
| | 1503 | } |
| | 1504 | else { |
| | 1505 | p = function proxy() { |
| | 1506 | return p.invoke(func, this, slice.call(arguments)); |
| | 1507 | }; |
| | 1508 | } |
| | 1509 | return p; |
| | 1510 | } |
| | 1511 | |
| | 1512 | var uuid = 0; |
| | 1513 | |
| | 1514 | // Public API |
| | 1515 | var spyApi = { |
| | 1516 | reset: function () { |
| | 1517 | this.called = false; |
| | 1518 | this.notCalled = true; |
| | 1519 | this.calledOnce = false; |
| | 1520 | this.calledTwice = false; |
| | 1521 | this.calledThrice = false; |
| | 1522 | this.callCount = 0; |
| | 1523 | this.firstCall = null; |
| | 1524 | this.secondCall = null; |
| | 1525 | this.thirdCall = null; |
| | 1526 | this.lastCall = null; |
| | 1527 | this.args = []; |
| | 1528 | this.returnValues = []; |
| | 1529 | this.thisValues = []; |
| | 1530 | this.exceptions = []; |
| | 1531 | this.callIds = []; |
| | 1532 | if (this.fakes) { |
| | 1533 | for (var i = 0; i < this.fakes.length; i++) { |
| | 1534 | this.fakes[i].reset(); |
| | 1535 | } |
| | 1536 | } |
| | 1537 | }, |
| | 1538 | |
| | 1539 | create: function create(func) { |
| | 1540 | var name; |
| | 1541 | |
| | 1542 | if (typeof func != "function") { |
| | 1543 | func = function () { }; |
| | 1544 | } else { |
| | 1545 | name = sinon.functionName(func); |
| | 1546 | } |
| | 1547 | |
| | 1548 | var proxy = createProxy(func); |
| | 1549 | |
| | 1550 | sinon.extend(proxy, spy); |
| | 1551 | delete proxy.create; |
| | 1552 | sinon.extend(proxy, func); |
| | 1553 | |
| | 1554 | proxy.reset(); |
| | 1555 | proxy.prototype = func.prototype; |
| | 1556 | proxy.displayName = name || "spy"; |
| | 1557 | proxy.toString = sinon.functionToString; |
| | 1558 | proxy._create = sinon.spy.create; |
| | 1559 | proxy.id = "spy#" + uuid++; |
| | 1560 | |
| | 1561 | return proxy; |
| | 1562 | }, |
| | 1563 | |
| | 1564 | invoke: function invoke(func, thisValue, args) { |
| | 1565 | var matching = matchingFake(this.fakes, args); |
| | 1566 | var exception, returnValue; |
| | 1567 | |
| | 1568 | incrementCallCount.call(this); |
| | 1569 | push.call(this.thisValues, thisValue); |
| | 1570 | push.call(this.args, args); |
| | 1571 | push.call(this.callIds, callId++); |
| | 1572 | |
| | 1573 | try { |
| | 1574 | if (matching) { |
| | 1575 | returnValue = matching.invoke(func, thisValue, args); |
| | 1576 | } else { |
| | 1577 | returnValue = (this.func || func).apply(thisValue, args); |
| | 1578 | } |
| | 1579 | |
| | 1580 | var thisCall = this.getCall(this.callCount - 1); |
| | 1581 | if (thisCall.calledWithNew() && typeof returnValue !== 'object') { |
| | 1582 | returnValue = thisValue; |
| | 1583 | } |
| | 1584 | } catch (e) { |
| | 1585 | exception = e; |
| | 1586 | } |
| | 1587 | |
| | 1588 | push.call(this.exceptions, exception); |
| | 1589 | push.call(this.returnValues, returnValue); |
| | 1590 | |
| | 1591 | createCallProperties.call(this); |
| | 1592 | |
| | 1593 | if (exception !== undefined) { |
| | 1594 | throw exception; |
| | 1595 | } |
| | 1596 | |
| | 1597 | return returnValue; |
| | 1598 | }, |
| | 1599 | |
| | 1600 | getCall: function getCall(i) { |
| | 1601 | if (i < 0 || i >= this.callCount) { |
| | 1602 | return null; |
| | 1603 | } |
| | 1604 | |
| | 1605 | return sinon.spyCall(this, this.thisValues[i], this.args[i], |
| | 1606 | this.returnValues[i], this.exceptions[i], |
| | 1607 | this.callIds[i]); |
| | 1608 | }, |
| | 1609 | |
| | 1610 | getCalls: function () { |
| | 1611 | var calls = []; |
| | 1612 | var i; |
| | 1613 | |
| | 1614 | for (i = 0; i < this.callCount; i++) { |
| | 1615 | calls.push(this.getCall(i)); |
| | 1616 | } |
| | 1617 | |
| | 1618 | return calls; |
| | 1619 | }, |
| | 1620 | |
| | 1621 | calledBefore: function calledBefore(spyFn) { |
| | 1622 | if (!this.called) { |
| | 1623 | return false; |
| | 1624 | } |
| | 1625 | |
| | 1626 | if (!spyFn.called) { |
| | 1627 | return true; |
| | 1628 | } |
| | 1629 | |
| | 1630 | return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1]; |
| | 1631 | }, |
| | 1632 | |
| | 1633 | calledAfter: function calledAfter(spyFn) { |
| | 1634 | if (!this.called || !spyFn.called) { |
| | 1635 | return false; |
| | 1636 | } |
| | 1637 | |
| | 1638 | return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1]; |
| | 1639 | }, |
| | 1640 | |
| | 1641 | withArgs: function () { |
| | 1642 | var args = slice.call(arguments); |
| | 1643 | |
| | 1644 | if (this.fakes) { |
| | 1645 | var match = matchingFake(this.fakes, args, true); |
| | 1646 | |
| | 1647 | if (match) { |
| | 1648 | return match; |
| | 1649 | } |
| | 1650 | } else { |
| | 1651 | this.fakes = []; |
| | 1652 | } |
| | 1653 | |
| | 1654 | var original = this; |
| | 1655 | var fake = this._create(); |
| | 1656 | fake.matchingAguments = args; |
| | 1657 | fake.parent = this; |
| | 1658 | push.call(this.fakes, fake); |
| | 1659 | |
| | 1660 | fake.withArgs = function () { |
| | 1661 | return original.withArgs.apply(original, arguments); |
| | 1662 | }; |
| | 1663 | |
| | 1664 | for (var i = 0; i < this.args.length; i++) { |
| | 1665 | if (fake.matches(this.args[i])) { |
| | 1666 | incrementCallCount.call(fake); |
| | 1667 | push.call(fake.thisValues, this.thisValues[i]); |
| | 1668 | push.call(fake.args, this.args[i]); |
| | 1669 | push.call(fake.returnValues, this.returnValues[i]); |
| | 1670 | push.call(fake.exceptions, this.exceptions[i]); |
| | 1671 | push.call(fake.callIds, this.callIds[i]); |
| | 1672 | } |
| | 1673 | } |
| | 1674 | createCallProperties.call(fake); |
| | 1675 | |
| | 1676 | return fake; |
| | 1677 | }, |
| | 1678 | |
| | 1679 | matches: function (args, strict) { |
| | 1680 | var margs = this.matchingAguments; |
| | 1681 | |
| | 1682 | if (margs.length <= args.length && |
| | 1683 | sinon.deepEqual(margs, args.slice(0, margs.length))) { |
| | 1684 | return !strict || margs.length == args.length; |
| | 1685 | } |
| | 1686 | }, |
| | 1687 | |
| | 1688 | printf: function (format) { |
| | 1689 | var spy = this; |
| | 1690 | var args = slice.call(arguments, 1); |
| | 1691 | var formatter; |
| | 1692 | |
| | 1693 | return (format || "").replace(/%(.)/g, function (match, specifyer) { |
| | 1694 | formatter = spyApi.formatters[specifyer]; |
| | 1695 | |
| | 1696 | if (typeof formatter == "function") { |
| | 1697 | return formatter.call(null, spy, args); |
| | 1698 | } else if (!isNaN(parseInt(specifyer, 10))) { |
| | 1699 | return sinon.format(args[specifyer - 1]); |
| | 1700 | } |
| | 1701 | |
| | 1702 | return "%" + specifyer; |
| | 1703 | }); |
| | 1704 | } |
| | 1705 | }; |
| | 1706 | |
| | 1707 | function delegateToCalls(method, matchAny, actual, notCalled) { |
| | 1708 | spyApi[method] = function () { |
| | 1709 | if (!this.called) { |
| | 1710 | if (notCalled) { |
| | 1711 | return notCalled.apply(this, arguments); |
| | 1712 | } |
| | 1713 | return false; |
| | 1714 | } |
| | 1715 | |
| | 1716 | var currentCall; |
| | 1717 | var matches = 0; |
| | 1718 | |
| | 1719 | for (var i = 0, l = this.callCount; i < l; i += 1) { |
| | 1720 | currentCall = this.getCall(i); |
| | 1721 | |
| | 1722 | if (currentCall[actual || method].apply(currentCall, arguments)) { |
| | 1723 | matches += 1; |
| | 1724 | |
| | 1725 | if (matchAny) { |
| | 1726 | return true; |
| | 1727 | } |
| | 1728 | } |
| | 1729 | } |
| | 1730 | |
| | 1731 | return matches === this.callCount; |
| | 1732 | }; |
| | 1733 | } |
| | 1734 | |
| | 1735 | delegateToCalls("calledOn", true); |
| | 1736 | delegateToCalls("alwaysCalledOn", false, "calledOn"); |
| | 1737 | delegateToCalls("calledWith", true); |
| | 1738 | delegateToCalls("calledWithMatch", true); |
| | 1739 | delegateToCalls("alwaysCalledWith", false, "calledWith"); |
| | 1740 | delegateToCalls("alwaysCalledWithMatch", false, "calledWithMatch"); |
| | 1741 | delegateToCalls("calledWithExactly", true); |
| | 1742 | delegateToCalls("alwaysCalledWithExactly", false, "calledWithExactly"); |
| | 1743 | delegateToCalls("neverCalledWith", false, "notCalledWith", |
| | 1744 | function () { return true; }); |
| | 1745 | delegateToCalls("neverCalledWithMatch", false, "notCalledWithMatch", |
| | 1746 | function () { return true; }); |
| | 1747 | delegateToCalls("threw", true); |
| | 1748 | delegateToCalls("alwaysThrew", false, "threw"); |
| | 1749 | delegateToCalls("returned", true); |
| | 1750 | delegateToCalls("alwaysReturned", false, "returned"); |
| | 1751 | delegateToCalls("calledWithNew", true); |
| | 1752 | delegateToCalls("alwaysCalledWithNew", false, "calledWithNew"); |
| | 1753 | delegateToCalls("callArg", false, "callArgWith", function () { |
| | 1754 | throw new Error(this.toString() + " cannot call arg since it was not yet invoked."); |
| | 1755 | }); |
| | 1756 | spyApi.callArgWith = spyApi.callArg; |
| | 1757 | delegateToCalls("callArgOn", false, "callArgOnWith", function () { |
| | 1758 | throw new Error(this.toString() + " cannot call arg since it was not yet invoked."); |
| | 1759 | }); |
| | 1760 | spyApi.callArgOnWith = spyApi.callArgOn; |
| | 1761 | delegateToCalls("yield", false, "yield", function () { |
| | 1762 | throw new Error(this.toString() + " cannot yield since it was not yet invoked."); |
| | 1763 | }); |
| | 1764 | // "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode. |
| | 1765 | spyApi.invokeCallback = spyApi.yield; |
| | 1766 | delegateToCalls("yieldOn", false, "yieldOn", function () { |
| | 1767 | throw new Error(this.toString() + " cannot yield since it was not yet invoked."); |
| | 1768 | }); |
| | 1769 | delegateToCalls("yieldTo", false, "yieldTo", function (property) { |
| | 1770 | throw new Error(this.toString() + " cannot yield to '" + property + |
| | 1771 | "' since it was not yet invoked."); |
| | 1772 | }); |
| | 1773 | delegateToCalls("yieldToOn", false, "yieldToOn", function (property) { |
| | 1774 | throw new Error(this.toString() + " cannot yield to '" + property + |
| | 1775 | "' since it was not yet invoked."); |
| | 1776 | }); |
| | 1777 | |
| | 1778 | spyApi.formatters = { |
| | 1779 | "c": function (spy) { |
| | 1780 | return sinon.timesInWords(spy.callCount); |
| | 1781 | }, |
| | 1782 | |
| | 1783 | "n": function (spy) { |
| | 1784 | return spy.toString(); |
| | 1785 | }, |
| | 1786 | |
| | 1787 | "C": function (spy) { |
| | 1788 | var calls = []; |
| | 1789 | |
| | 1790 | for (var i = 0, l = spy.callCount; i < l; ++i) { |
| | 1791 | var stringifiedCall = " " + spy.getCall(i).toString(); |
| | 1792 | if (/\n/.test(calls[i - 1])) { |
| | 1793 | stringifiedCall = "\n" + stringifiedCall; |
| | 1794 | } |
| | 1795 | push.call(calls, stringifiedCall); |
| | 1796 | } |
| | 1797 | |
| | 1798 | return calls.length > 0 ? "\n" + calls.join("\n") : ""; |
| | 1799 | }, |
| | 1800 | |
| | 1801 | "t": function (spy) { |
| | 1802 | var objects = []; |
| | 1803 | |
| | 1804 | for (var i = 0, l = spy.callCount; i < l; ++i) { |
| | 1805 | push.call(objects, sinon.format(spy.thisValues[i])); |
| | 1806 | } |
| | 1807 | |
| | 1808 | return objects.join(", "); |
| | 1809 | }, |
| | 1810 | |
| | 1811 | "*": function (spy, args) { |
| | 1812 | var formatted = []; |
| | 1813 | |
| | 1814 | for (var i = 0, l = args.length; i < l; ++i) { |
| | 1815 | push.call(formatted, sinon.format(args[i])); |
| | 1816 | } |
| | 1817 | |
| | 1818 | return formatted.join(", "); |
| | 1819 | } |
| | 1820 | }; |
| | 1821 | |
| | 1822 | sinon.extend(spy, spyApi); |
| | 1823 | |
| | 1824 | spy.spyCall = sinon.spyCall; |
| | 1825 | |
| | 1826 | if (commonJSModule) { |
| | 1827 | module.exports = spy; |
| | 1828 | } else { |
| | 1829 | sinon.spy = spy; |
| | 1830 | } |
| | 1831 | }(typeof sinon == "object" && sinon || null)); |
| | 1832 | |
| | 1833 | /** |
| | 1834 | * @depend ../sinon.js |
| | 1835 | */ |
| | 1836 | /*jslint eqeqeq: false, onevar: false*/ |
| | 1837 | /*global module, require, sinon, process, setImmediate, setTimeout*/ |
| | 1838 | /** |
| | 1839 | * Stub behavior |
| | 1840 | * |
| | 1841 | * @author Christian Johansen (christian@cjohansen.no) |
| | 1842 | * @author Tim Fischbach (mail@timfischbach.de) |
| | 1843 | * @license BSD |
| | 1844 | * |
| | 1845 | * Copyright (c) 2010-2013 Christian Johansen |
| | 1846 | */ |
| | 1847 | |
| | 1848 | (function (sinon) { |
| | 1849 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 1850 | |
| | 1851 | if (!sinon && commonJSModule) { |
| | 1852 | sinon = require("../sinon"); |
| | 1853 | } |
| | 1854 | |
| | 1855 | if (!sinon) { |
| | 1856 | return; |
| | 1857 | } |
| | 1858 | |
| | 1859 | var slice = Array.prototype.slice; |
| | 1860 | var join = Array.prototype.join; |
| | 1861 | var proto; |
| | 1862 | |
| | 1863 | var nextTick = (function () { |
| | 1864 | if (typeof process === "object" && typeof process.nextTick === "function") { |
| | 1865 | return process.nextTick; |
| | 1866 | } else if (typeof setImmediate === "function") { |
| | 1867 | return setImmediate; |
| | 1868 | } else { |
| | 1869 | return function (callback) { |
| | 1870 | setTimeout(callback, 0); |
| | 1871 | }; |
| | 1872 | } |
| | 1873 | })(); |
| | 1874 | |
| | 1875 | function throwsException(error, message) { |
| | 1876 | if (typeof error == "string") { |
| | 1877 | this.exception = new Error(message || ""); |
| | 1878 | this.exception.name = error; |
| | 1879 | } else if (!error) { |
| | 1880 | this.exception = new Error("Error"); |
| | 1881 | } else { |
| | 1882 | this.exception = error; |
| | 1883 | } |
| | 1884 | |
| | 1885 | return this; |
| | 1886 | } |
| | 1887 | |
| | 1888 | function getCallback(behavior, args) { |
| | 1889 | var callArgAt = behavior.callArgAt; |
| | 1890 | |
| | 1891 | if (callArgAt < 0) { |
| | 1892 | var callArgProp = behavior.callArgProp; |
| | 1893 | |
| | 1894 | for (var i = 0, l = args.length; i < l; ++i) { |
| | 1895 | if (!callArgProp && typeof args[i] == "function") { |
| | 1896 | return args[i]; |
| | 1897 | } |
| | 1898 | |
| | 1899 | if (callArgProp && args[i] && |
| | 1900 | typeof args[i][callArgProp] == "function") { |
| | 1901 | return args[i][callArgProp]; |
| | 1902 | } |
| | 1903 | } |
| | 1904 | |
| | 1905 | return null; |
| | 1906 | } |
| | 1907 | |
| | 1908 | return args[callArgAt]; |
| | 1909 | } |
| | 1910 | |
| | 1911 | function getCallbackError(behavior, func, args) { |
| | 1912 | if (behavior.callArgAt < 0) { |
| | 1913 | var msg; |
| | 1914 | |
| | 1915 | if (behavior.callArgProp) { |
| | 1916 | msg = sinon.functionName(behavior.stub) + |
| | 1917 | " expected to yield to '" + behavior.callArgProp + |
| | 1918 | "', but no object with such a property was passed."; |
| | 1919 | } else { |
| | 1920 | msg = sinon.functionName(behavior.stub) + |
| | 1921 | " expected to yield, but no callback was passed."; |
| | 1922 | } |
| | 1923 | |
| | 1924 | if (args.length > 0) { |
| | 1925 | msg += " Received [" + join.call(args, ", ") + "]"; |
| | 1926 | } |
| | 1927 | |
| | 1928 | return msg; |
| | 1929 | } |
| | 1930 | |
| | 1931 | return "argument at index " + behavior.callArgAt + " is not a function: " + func; |
| | 1932 | } |
| | 1933 | |
| | 1934 | function callCallback(behavior, args) { |
| | 1935 | if (typeof behavior.callArgAt == "number") { |
| | 1936 | var func = getCallback(behavior, args); |
| | 1937 | |
| | 1938 | if (typeof func != "function") { |
| | 1939 | throw new TypeError(getCallbackError(behavior, func, args)); |
| | 1940 | } |
| | 1941 | |
| | 1942 | if (behavior.callbackAsync) { |
| | 1943 | nextTick(function() { |
| | 1944 | func.apply(behavior.callbackContext, behavior.callbackArguments); |
| | 1945 | }); |
| | 1946 | } else { |
| | 1947 | func.apply(behavior.callbackContext, behavior.callbackArguments); |
| | 1948 | } |
| | 1949 | } |
| | 1950 | } |
| | 1951 | |
| | 1952 | proto = { |
| | 1953 | create: function(stub) { |
| | 1954 | var behavior = sinon.extend({}, sinon.behavior); |
| | 1955 | delete behavior.create; |
| | 1956 | behavior.stub = stub; |
| | 1957 | |
| | 1958 | return behavior; |
| | 1959 | }, |
| | 1960 | |
| | 1961 | isPresent: function() { |
| | 1962 | return (typeof this.callArgAt == 'number' || |
| | 1963 | this.exception || |
| | 1964 | typeof this.returnArgAt == 'number' || |
| | 1965 | this.returnThis || |
| | 1966 | this.returnValueDefined); |
| | 1967 | }, |
| | 1968 | |
| | 1969 | invoke: function(context, args) { |
| | 1970 | callCallback(this, args); |
| | 1971 | |
| | 1972 | if (this.exception) { |
| | 1973 | throw this.exception; |
| | 1974 | } else if (typeof this.returnArgAt == 'number') { |
| | 1975 | return args[this.returnArgAt]; |
| | 1976 | } else if (this.returnThis) { |
| | 1977 | return context; |
| | 1978 | } |
| | 1979 | |
| | 1980 | return this.returnValue; |
| | 1981 | }, |
| | 1982 | |
| | 1983 | onCall: function(index) { |
| | 1984 | return this.stub.onCall(index); |
| | 1985 | }, |
| | 1986 | |
| | 1987 | onFirstCall: function() { |
| | 1988 | return this.stub.onFirstCall(); |
| | 1989 | }, |
| | 1990 | |
| | 1991 | onSecondCall: function() { |
| | 1992 | return this.stub.onSecondCall(); |
| | 1993 | }, |
| | 1994 | |
| | 1995 | onThirdCall: function() { |
| | 1996 | return this.stub.onThirdCall(); |
| | 1997 | }, |
| | 1998 | |
| | 1999 | withArgs: function(/* arguments */) { |
| | 2000 | throw new Error('Defining a stub by invoking "stub.onCall(...).withArgs(...)" is not supported. ' + |
| | 2001 | 'Use "stub.withArgs(...).onCall(...)" to define sequential behavior for calls with certain arguments.'); |
| | 2002 | }, |
| | 2003 | |
| | 2004 | callsArg: function callsArg(pos) { |
| | 2005 | if (typeof pos != "number") { |
| | 2006 | throw new TypeError("argument index is not number"); |
| | 2007 | } |
| | 2008 | |
| | 2009 | this.callArgAt = pos; |
| | 2010 | this.callbackArguments = []; |
| | 2011 | this.callbackContext = undefined; |
| | 2012 | this.callArgProp = undefined; |
| | 2013 | this.callbackAsync = false; |
| | 2014 | |
| | 2015 | return this; |
| | 2016 | }, |
| | 2017 | |
| | 2018 | callsArgOn: function callsArgOn(pos, context) { |
| | 2019 | if (typeof pos != "number") { |
| | 2020 | throw new TypeError("argument index is not number"); |
| | 2021 | } |
| | 2022 | if (typeof context != "object") { |
| | 2023 | throw new TypeError("argument context is not an object"); |
| | 2024 | } |
| | 2025 | |
| | 2026 | this.callArgAt = pos; |
| | 2027 | this.callbackArguments = []; |
| | 2028 | this.callbackContext = context; |
| | 2029 | this.callArgProp = undefined; |
| | 2030 | this.callbackAsync = false; |
| | 2031 | |
| | 2032 | return this; |
| | 2033 | }, |
| | 2034 | |
| | 2035 | callsArgWith: function callsArgWith(pos) { |
| | 2036 | if (typeof pos != "number") { |
| | 2037 | throw new TypeError("argument index is not number"); |
| | 2038 | } |
| | 2039 | |
| | 2040 | this.callArgAt = pos; |
| | 2041 | this.callbackArguments = slice.call(arguments, 1); |
| | 2042 | this.callbackContext = undefined; |
| | 2043 | this.callArgProp = undefined; |
| | 2044 | this.callbackAsync = false; |
| | 2045 | |
| | 2046 | return this; |
| | 2047 | }, |
| | 2048 | |
| | 2049 | callsArgOnWith: function callsArgWith(pos, context) { |
| | 2050 | if (typeof pos != "number") { |
| | 2051 | throw new TypeError("argument index is not number"); |
| | 2052 | } |
| | 2053 | if (typeof context != "object") { |
| | 2054 | throw new TypeError("argument context is not an object"); |
| | 2055 | } |
| | 2056 | |
| | 2057 | this.callArgAt = pos; |
| | 2058 | this.callbackArguments = slice.call(arguments, 2); |
| | 2059 | this.callbackContext = context; |
| | 2060 | this.callArgProp = undefined; |
| | 2061 | this.callbackAsync = false; |
| | 2062 | |
| | 2063 | return this; |
| | 2064 | }, |
| | 2065 | |
| | 2066 | yields: function () { |
| | 2067 | this.callArgAt = -1; |
| | 2068 | this.callbackArguments = slice.call(arguments, 0); |
| | 2069 | this.callbackContext = undefined; |
| | 2070 | this.callArgProp = undefined; |
| | 2071 | this.callbackAsync = false; |
| | 2072 | |
| | 2073 | return this; |
| | 2074 | }, |
| | 2075 | |
| | 2076 | yieldsOn: function (context) { |
| | 2077 | if (typeof context != "object") { |
| | 2078 | throw new TypeError("argument context is not an object"); |
| | 2079 | } |
| | 2080 | |
| | 2081 | this.callArgAt = -1; |
| | 2082 | this.callbackArguments = slice.call(arguments, 1); |
| | 2083 | this.callbackContext = context; |
| | 2084 | this.callArgProp = undefined; |
| | 2085 | this.callbackAsync = false; |
| | 2086 | |
| | 2087 | return this; |
| | 2088 | }, |
| | 2089 | |
| | 2090 | yieldsTo: function (prop) { |
| | 2091 | this.callArgAt = -1; |
| | 2092 | this.callbackArguments = slice.call(arguments, 1); |
| | 2093 | this.callbackContext = undefined; |
| | 2094 | this.callArgProp = prop; |
| | 2095 | this.callbackAsync = false; |
| | 2096 | |
| | 2097 | return this; |
| | 2098 | }, |
| | 2099 | |
| | 2100 | yieldsToOn: function (prop, context) { |
| | 2101 | if (typeof context != "object") { |
| | 2102 | throw new TypeError("argument context is not an object"); |
| | 2103 | } |
| | 2104 | |
| | 2105 | this.callArgAt = -1; |
| | 2106 | this.callbackArguments = slice.call(arguments, 2); |
| | 2107 | this.callbackContext = context; |
| | 2108 | this.callArgProp = prop; |
| | 2109 | this.callbackAsync = false; |
| | 2110 | |
| | 2111 | return this; |
| | 2112 | }, |
| | 2113 | |
| | 2114 | |
| | 2115 | "throws": throwsException, |
| | 2116 | throwsException: throwsException, |
| | 2117 | |
| | 2118 | returns: function returns(value) { |
| | 2119 | this.returnValue = value; |
| | 2120 | this.returnValueDefined = true; |
| | 2121 | |
| | 2122 | return this; |
| | 2123 | }, |
| | 2124 | |
| | 2125 | returnsArg: function returnsArg(pos) { |
| | 2126 | if (typeof pos != "number") { |
| | 2127 | throw new TypeError("argument index is not number"); |
| | 2128 | } |
| | 2129 | |
| | 2130 | this.returnArgAt = pos; |
| | 2131 | |
| | 2132 | return this; |
| | 2133 | }, |
| | 2134 | |
| | 2135 | returnsThis: function returnsThis() { |
| | 2136 | this.returnThis = true; |
| | 2137 | |
| | 2138 | return this; |
| | 2139 | } |
| | 2140 | }; |
| | 2141 | |
| | 2142 | // create asynchronous versions of callsArg* and yields* methods |
| | 2143 | for (var method in proto) { |
| | 2144 | // need to avoid creating anotherasync versions of the newly added async methods |
| | 2145 | if (proto.hasOwnProperty(method) && |
| | 2146 | method.match(/^(callsArg|yields)/) && |
| | 2147 | !method.match(/Async/)) { |
| | 2148 | proto[method + 'Async'] = (function (syncFnName) { |
| | 2149 | return function () { |
| | 2150 | var result = this[syncFnName].apply(this, arguments); |
| | 2151 | this.callbackAsync = true; |
| | 2152 | return result; |
| | 2153 | }; |
| | 2154 | })(method); |
| | 2155 | } |
| | 2156 | } |
| | 2157 | |
| | 2158 | if (commonJSModule) { |
| | 2159 | module.exports = proto; |
| | 2160 | } else { |
| | 2161 | sinon.behavior = proto; |
| | 2162 | } |
| | 2163 | }(typeof sinon == "object" && sinon || null)); |
| | 2164 | /** |
| | 2165 | * @depend ../sinon.js |
| | 2166 | * @depend spy.js |
| | 2167 | * @depend behavior.js |
| | 2168 | */ |
| | 2169 | /*jslint eqeqeq: false, onevar: false*/ |
| | 2170 | /*global module, require, sinon*/ |
| | 2171 | /** |
| | 2172 | * Stub functions |
| | 2173 | * |
| | 2174 | * @author Christian Johansen (christian@cjohansen.no) |
| | 2175 | * @license BSD |
| | 2176 | * |
| | 2177 | * Copyright (c) 2010-2013 Christian Johansen |
| | 2178 | */ |
| | 2179 | |
| | 2180 | (function (sinon) { |
| | 2181 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 2182 | |
| | 2183 | if (!sinon && commonJSModule) { |
| | 2184 | sinon = require("../sinon"); |
| | 2185 | } |
| | 2186 | |
| | 2187 | if (!sinon) { |
| | 2188 | return; |
| | 2189 | } |
| | 2190 | |
| | 2191 | function stub(object, property, func) { |
| | 2192 | if (!!func && typeof func != "function") { |
| | 2193 | throw new TypeError("Custom stub should be function"); |
| | 2194 | } |
| | 2195 | |
| | 2196 | var wrapper; |
| | 2197 | |
| | 2198 | if (func) { |
| | 2199 | wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func; |
| | 2200 | } else { |
| | 2201 | wrapper = stub.create(); |
| | 2202 | } |
| | 2203 | |
| | 2204 | if (!object && typeof property === "undefined") { |
| | 2205 | return sinon.stub.create(); |
| | 2206 | } |
| | 2207 | |
| | 2208 | if (typeof property === "undefined" && typeof object == "object") { |
| | 2209 | for (var prop in object) { |
| | 2210 | if (typeof object[prop] === "function") { |
| | 2211 | stub(object, prop); |
| | 2212 | } |
| | 2213 | } |
| | 2214 | |
| | 2215 | return object; |
| | 2216 | } |
| | 2217 | |
| | 2218 | return sinon.wrapMethod(object, property, wrapper); |
| | 2219 | } |
| | 2220 | |
| | 2221 | function getDefaultBehavior(stub) { |
| | 2222 | return stub.defaultBehavior || getParentBehaviour(stub) || sinon.behavior.create(stub); |
| | 2223 | } |
| | 2224 | |
| | 2225 | function getParentBehaviour(stub) { |
| | 2226 | return (stub.parent && getCurrentBehavior(stub.parent)); |
| | 2227 | } |
| | 2228 | |
| | 2229 | function getCurrentBehavior(stub) { |
| | 2230 | var behavior = stub.behaviors[stub.callCount - 1]; |
| | 2231 | return behavior && behavior.isPresent() ? behavior : getDefaultBehavior(stub); |
| | 2232 | } |
| | 2233 | |
| | 2234 | var uuid = 0; |
| | 2235 | |
| | 2236 | sinon.extend(stub, (function () { |
| | 2237 | var proto = { |
| | 2238 | create: function create() { |
| | 2239 | var functionStub = function () { |
| | 2240 | return getCurrentBehavior(functionStub).invoke(this, arguments); |
| | 2241 | }; |
| | 2242 | |
| | 2243 | functionStub.id = "stub#" + uuid++; |
| | 2244 | var orig = functionStub; |
| | 2245 | functionStub = sinon.spy.create(functionStub); |
| | 2246 | functionStub.func = orig; |
| | 2247 | |
| | 2248 | sinon.extend(functionStub, stub); |
| | 2249 | functionStub._create = sinon.stub.create; |
| | 2250 | functionStub.displayName = "stub"; |
| | 2251 | functionStub.toString = sinon.functionToString; |
| | 2252 | |
| | 2253 | functionStub.defaultBehavior = null; |
| | 2254 | functionStub.behaviors = []; |
| | 2255 | |
| | 2256 | return functionStub; |
| | 2257 | }, |
| | 2258 | |
| | 2259 | resetBehavior: function () { |
| | 2260 | var i; |
| | 2261 | |
| | 2262 | this.defaultBehavior = null; |
| | 2263 | this.behaviors = []; |
| | 2264 | |
| | 2265 | delete this.returnValue; |
| | 2266 | delete this.returnArgAt; |
| | 2267 | this.returnThis = false; |
| | 2268 | |
| | 2269 | if (this.fakes) { |
| | 2270 | for (i = 0; i < this.fakes.length; i++) { |
| | 2271 | this.fakes[i].resetBehavior(); |
| | 2272 | } |
| | 2273 | } |
| | 2274 | }, |
| | 2275 | |
| | 2276 | onCall: function(index) { |
| | 2277 | if (!this.behaviors[index]) { |
| | 2278 | this.behaviors[index] = sinon.behavior.create(this); |
| | 2279 | } |
| | 2280 | |
| | 2281 | return this.behaviors[index]; |
| | 2282 | }, |
| | 2283 | |
| | 2284 | onFirstCall: function() { |
| | 2285 | return this.onCall(0); |
| | 2286 | }, |
| | 2287 | |
| | 2288 | onSecondCall: function() { |
| | 2289 | return this.onCall(1); |
| | 2290 | }, |
| | 2291 | |
| | 2292 | onThirdCall: function() { |
| | 2293 | return this.onCall(2); |
| | 2294 | } |
| | 2295 | }; |
| | 2296 | |
| | 2297 | for (var method in sinon.behavior) { |
| | 2298 | if (sinon.behavior.hasOwnProperty(method) && |
| | 2299 | !proto.hasOwnProperty(method) && |
| | 2300 | method != 'create' && |
| | 2301 | method != 'withArgs' && |
| | 2302 | method != 'invoke') { |
| | 2303 | proto[method] = (function(behaviorMethod) { |
| | 2304 | return function() { |
| | 2305 | this.defaultBehavior = this.defaultBehavior || sinon.behavior.create(this); |
| | 2306 | this.defaultBehavior[behaviorMethod].apply(this.defaultBehavior, arguments); |
| | 2307 | return this; |
| | 2308 | }; |
| | 2309 | }(method)); |
| | 2310 | } |
| | 2311 | } |
| | 2312 | |
| | 2313 | return proto; |
| | 2314 | }())); |
| | 2315 | |
| | 2316 | if (commonJSModule) { |
| | 2317 | module.exports = stub; |
| | 2318 | } else { |
| | 2319 | sinon.stub = stub; |
| | 2320 | } |
| | 2321 | }(typeof sinon == "object" && sinon || null)); |
| | 2322 | |
| | 2323 | /** |
| | 2324 | * @depend ../sinon.js |
| | 2325 | * @depend stub.js |
| | 2326 | */ |
| | 2327 | /*jslint eqeqeq: false, onevar: false, nomen: false*/ |
| | 2328 | /*global module, require, sinon*/ |
| | 2329 | /** |
| | 2330 | * Mock functions. |
| | 2331 | * |
| | 2332 | * @author Christian Johansen (christian@cjohansen.no) |
| | 2333 | * @license BSD |
| | 2334 | * |
| | 2335 | * Copyright (c) 2010-2013 Christian Johansen |
| | 2336 | */ |
| | 2337 | |
| | 2338 | (function (sinon) { |
| | 2339 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 2340 | var push = [].push; |
| | 2341 | var match; |
| | 2342 | |
| | 2343 | if (!sinon && commonJSModule) { |
| | 2344 | sinon = require("../sinon"); |
| | 2345 | } |
| | 2346 | |
| | 2347 | if (!sinon) { |
| | 2348 | return; |
| | 2349 | } |
| | 2350 | |
| | 2351 | match = sinon.match; |
| | 2352 | |
| | 2353 | if (!match && commonJSModule) { |
| | 2354 | match = require("./match"); |
| | 2355 | } |
| | 2356 | |
| | 2357 | function mock(object) { |
| | 2358 | if (!object) { |
| | 2359 | return sinon.expectation.create("Anonymous mock"); |
| | 2360 | } |
| | 2361 | |
| | 2362 | return mock.create(object); |
| | 2363 | } |
| | 2364 | |
| | 2365 | sinon.mock = mock; |
| | 2366 | |
| | 2367 | sinon.extend(mock, (function () { |
| | 2368 | function each(collection, callback) { |
| | 2369 | if (!collection) { |
| | 2370 | return; |
| | 2371 | } |
| | 2372 | |
| | 2373 | for (var i = 0, l = collection.length; i < l; i += 1) { |
| | 2374 | callback(collection[i]); |
| | 2375 | } |
| | 2376 | } |
| | 2377 | |
| | 2378 | return { |
| | 2379 | create: function create(object) { |
| | 2380 | if (!object) { |
| | 2381 | throw new TypeError("object is null"); |
| | 2382 | } |
| | 2383 | |
| | 2384 | var mockObject = sinon.extend({}, mock); |
| | 2385 | mockObject.object = object; |
| | 2386 | delete mockObject.create; |
| | 2387 | |
| | 2388 | return mockObject; |
| | 2389 | }, |
| | 2390 | |
| | 2391 | expects: function expects(method) { |
| | 2392 | if (!method) { |
| | 2393 | throw new TypeError("method is falsy"); |
| | 2394 | } |
| | 2395 | |
| | 2396 | if (!this.expectations) { |
| | 2397 | this.expectations = {}; |
| | 2398 | this.proxies = []; |
| | 2399 | } |
| | 2400 | |
| | 2401 | if (!this.expectations[method]) { |
| | 2402 | this.expectations[method] = []; |
| | 2403 | var mockObject = this; |
| | 2404 | |
| | 2405 | sinon.wrapMethod(this.object, method, function () { |
| | 2406 | return mockObject.invokeMethod(method, this, arguments); |
| | 2407 | }); |
| | 2408 | |
| | 2409 | push.call(this.proxies, method); |
| | 2410 | } |
| | 2411 | |
| | 2412 | var expectation = sinon.expectation.create(method); |
| | 2413 | push.call(this.expectations[method], expectation); |
| | 2414 | |
| | 2415 | return expectation; |
| | 2416 | }, |
| | 2417 | |
| | 2418 | restore: function restore() { |
| | 2419 | var object = this.object; |
| | 2420 | |
| | 2421 | each(this.proxies, function (proxy) { |
| | 2422 | if (typeof object[proxy].restore == "function") { |
| | 2423 | object[proxy].restore(); |
| | 2424 | } |
| | 2425 | }); |
| | 2426 | }, |
| | 2427 | |
| | 2428 | verify: function verify() { |
| | 2429 | var expectations = this.expectations || {}; |
| | 2430 | var messages = [], met = []; |
| | 2431 | |
| | 2432 | each(this.proxies, function (proxy) { |
| | 2433 | each(expectations[proxy], function (expectation) { |
| | 2434 | if (!expectation.met()) { |
| | 2435 | push.call(messages, expectation.toString()); |
| | 2436 | } else { |
| | 2437 | push.call(met, expectation.toString()); |
| | 2438 | } |
| | 2439 | }); |
| | 2440 | }); |
| | 2441 | |
| | 2442 | this.restore(); |
| | 2443 | |
| | 2444 | if (messages.length > 0) { |
| | 2445 | sinon.expectation.fail(messages.concat(met).join("\n")); |
| | 2446 | } else { |
| | 2447 | sinon.expectation.pass(messages.concat(met).join("\n")); |
| | 2448 | } |
| | 2449 | |
| | 2450 | return true; |
| | 2451 | }, |
| | 2452 | |
| | 2453 | invokeMethod: function invokeMethod(method, thisValue, args) { |
| | 2454 | var expectations = this.expectations && this.expectations[method]; |
| | 2455 | var length = expectations && expectations.length || 0, i; |
| | 2456 | |
| | 2457 | for (i = 0; i < length; i += 1) { |
| | 2458 | if (!expectations[i].met() && |
| | 2459 | expectations[i].allowsCall(thisValue, args)) { |
| | 2460 | return expectations[i].apply(thisValue, args); |
| | 2461 | } |
| | 2462 | } |
| | 2463 | |
| | 2464 | var messages = [], available, exhausted = 0; |
| | 2465 | |
| | 2466 | for (i = 0; i < length; i += 1) { |
| | 2467 | if (expectations[i].allowsCall(thisValue, args)) { |
| | 2468 | available = available || expectations[i]; |
| | 2469 | } else { |
| | 2470 | exhausted += 1; |
| | 2471 | } |
| | 2472 | push.call(messages, " " + expectations[i].toString()); |
| | 2473 | } |
| | 2474 | |
| | 2475 | if (exhausted === 0) { |
| | 2476 | return available.apply(thisValue, args); |
| | 2477 | } |
| | 2478 | |
| | 2479 | messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({ |
| | 2480 | proxy: method, |
| | 2481 | args: args |
| | 2482 | })); |
| | 2483 | |
| | 2484 | sinon.expectation.fail(messages.join("\n")); |
| | 2485 | } |
| | 2486 | }; |
| | 2487 | }())); |
| | 2488 | |
| | 2489 | var times = sinon.timesInWords; |
| | 2490 | |
| | 2491 | sinon.expectation = (function () { |
| | 2492 | var slice = Array.prototype.slice; |
| | 2493 | var _invoke = sinon.spy.invoke; |
| | 2494 | |
| | 2495 | function callCountInWords(callCount) { |
| | 2496 | if (callCount == 0) { |
| | 2497 | return "never called"; |
| | 2498 | } else { |
| | 2499 | return "called " + times(callCount); |
| | 2500 | } |
| | 2501 | } |
| | 2502 | |
| | 2503 | function expectedCallCountInWords(expectation) { |
| | 2504 | var min = expectation.minCalls; |
| | 2505 | var max = expectation.maxCalls; |
| | 2506 | |
| | 2507 | if (typeof min == "number" && typeof max == "number") { |
| | 2508 | var str = times(min); |
| | 2509 | |
| | 2510 | if (min != max) { |
| | 2511 | str = "at least " + str + " and at most " + times(max); |
| | 2512 | } |
| | 2513 | |
| | 2514 | return str; |
| | 2515 | } |
| | 2516 | |
| | 2517 | if (typeof min == "number") { |
| | 2518 | return "at least " + times(min); |
| | 2519 | } |
| | 2520 | |
| | 2521 | return "at most " + times(max); |
| | 2522 | } |
| | 2523 | |
| | 2524 | function receivedMinCalls(expectation) { |
| | 2525 | var hasMinLimit = typeof expectation.minCalls == "number"; |
| | 2526 | return !hasMinLimit || expectation.callCount >= expectation.minCalls; |
| | 2527 | } |
| | 2528 | |
| | 2529 | function receivedMaxCalls(expectation) { |
| | 2530 | if (typeof expectation.maxCalls != "number") { |
| | 2531 | return false; |
| | 2532 | } |
| | 2533 | |
| | 2534 | return expectation.callCount == expectation.maxCalls; |
| | 2535 | } |
| | 2536 | |
| | 2537 | function verifyMatcher(possibleMatcher, arg){ |
| | 2538 | if (match && match.isMatcher(possibleMatcher)) { |
| | 2539 | return possibleMatcher.test(arg); |
| | 2540 | } else { |
| | 2541 | return true; |
| | 2542 | } |
| | 2543 | } |
| | 2544 | |
| | 2545 | return { |
| | 2546 | minCalls: 1, |
| | 2547 | maxCalls: 1, |
| | 2548 | |
| | 2549 | create: function create(methodName) { |
| | 2550 | var expectation = sinon.extend(sinon.stub.create(), sinon.expectation); |
| | 2551 | delete expectation.create; |
| | 2552 | expectation.method = methodName; |
| | 2553 | |
| | 2554 | return expectation; |
| | 2555 | }, |
| | 2556 | |
| | 2557 | invoke: function invoke(func, thisValue, args) { |
| | 2558 | this.verifyCallAllowed(thisValue, args); |
| | 2559 | |
| | 2560 | return _invoke.apply(this, arguments); |
| | 2561 | }, |
| | 2562 | |
| | 2563 | atLeast: function atLeast(num) { |
| | 2564 | if (typeof num != "number") { |
| | 2565 | throw new TypeError("'" + num + "' is not number"); |
| | 2566 | } |
| | 2567 | |
| | 2568 | if (!this.limitsSet) { |
| | 2569 | this.maxCalls = null; |
| | 2570 | this.limitsSet = true; |
| | 2571 | } |
| | 2572 | |
| | 2573 | this.minCalls = num; |
| | 2574 | |
| | 2575 | return this; |
| | 2576 | }, |
| | 2577 | |
| | 2578 | atMost: function atMost(num) { |
| | 2579 | if (typeof num != "number") { |
| | 2580 | throw new TypeError("'" + num + "' is not number"); |
| | 2581 | } |
| | 2582 | |
| | 2583 | if (!this.limitsSet) { |
| | 2584 | this.minCalls = null; |
| | 2585 | this.limitsSet = true; |
| | 2586 | } |
| | 2587 | |
| | 2588 | this.maxCalls = num; |
| | 2589 | |
| | 2590 | return this; |
| | 2591 | }, |
| | 2592 | |
| | 2593 | never: function never() { |
| | 2594 | return this.exactly(0); |
| | 2595 | }, |
| | 2596 | |
| | 2597 | once: function once() { |
| | 2598 | return this.exactly(1); |
| | 2599 | }, |
| | 2600 | |
| | 2601 | twice: function twice() { |
| | 2602 | return this.exactly(2); |
| | 2603 | }, |
| | 2604 | |
| | 2605 | thrice: function thrice() { |
| | 2606 | return this.exactly(3); |
| | 2607 | }, |
| | 2608 | |
| | 2609 | exactly: function exactly(num) { |
| | 2610 | if (typeof num != "number") { |
| | 2611 | throw new TypeError("'" + num + "' is not a number"); |
| | 2612 | } |
| | 2613 | |
| | 2614 | this.atLeast(num); |
| | 2615 | return this.atMost(num); |
| | 2616 | }, |
| | 2617 | |
| | 2618 | met: function met() { |
| | 2619 | return !this.failed && receivedMinCalls(this); |
| | 2620 | }, |
| | 2621 | |
| | 2622 | verifyCallAllowed: function verifyCallAllowed(thisValue, args) { |
| | 2623 | if (receivedMaxCalls(this)) { |
| | 2624 | this.failed = true; |
| | 2625 | sinon.expectation.fail(this.method + " already called " + times(this.maxCalls)); |
| | 2626 | } |
| | 2627 | |
| | 2628 | if ("expectedThis" in this && this.expectedThis !== thisValue) { |
| | 2629 | sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " + |
| | 2630 | this.expectedThis); |
| | 2631 | } |
| | 2632 | |
| | 2633 | if (!("expectedArguments" in this)) { |
| | 2634 | return; |
| | 2635 | } |
| | 2636 | |
| | 2637 | if (!args) { |
| | 2638 | sinon.expectation.fail(this.method + " received no arguments, expected " + |
| | 2639 | sinon.format(this.expectedArguments)); |
| | 2640 | } |
| | 2641 | |
| | 2642 | if (args.length < this.expectedArguments.length) { |
| | 2643 | sinon.expectation.fail(this.method + " received too few arguments (" + sinon.format(args) + |
| | 2644 | "), expected " + sinon.format(this.expectedArguments)); |
| | 2645 | } |
| | 2646 | |
| | 2647 | if (this.expectsExactArgCount && |
| | 2648 | args.length != this.expectedArguments.length) { |
| | 2649 | sinon.expectation.fail(this.method + " received too many arguments (" + sinon.format(args) + |
| | 2650 | "), expected " + sinon.format(this.expectedArguments)); |
| | 2651 | } |
| | 2652 | |
| | 2653 | for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) { |
| | 2654 | |
| | 2655 | if (!verifyMatcher(this.expectedArguments[i],args[i])) { |
| | 2656 | sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) + |
| | 2657 | ", didn't match " + this.expectedArguments.toString()); |
| | 2658 | } |
| | 2659 | |
| | 2660 | if (!sinon.deepEqual(this.expectedArguments[i], args[i])) { |
| | 2661 | sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) + |
| | 2662 | ", expected " + sinon.format(this.expectedArguments)); |
| | 2663 | } |
| | 2664 | } |
| | 2665 | }, |
| | 2666 | |
| | 2667 | allowsCall: function allowsCall(thisValue, args) { |
| | 2668 | if (this.met() && receivedMaxCalls(this)) { |
| | 2669 | return false; |
| | 2670 | } |
| | 2671 | |
| | 2672 | if ("expectedThis" in this && this.expectedThis !== thisValue) { |
| | 2673 | return false; |
| | 2674 | } |
| | 2675 | |
| | 2676 | if (!("expectedArguments" in this)) { |
| | 2677 | return true; |
| | 2678 | } |
| | 2679 | |
| | 2680 | args = args || []; |
| | 2681 | |
| | 2682 | if (args.length < this.expectedArguments.length) { |
| | 2683 | return false; |
| | 2684 | } |
| | 2685 | |
| | 2686 | if (this.expectsExactArgCount && |
| | 2687 | args.length != this.expectedArguments.length) { |
| | 2688 | return false; |
| | 2689 | } |
| | 2690 | |
| | 2691 | for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) { |
| | 2692 | if (!verifyMatcher(this.expectedArguments[i],args[i])) { |
| | 2693 | return false; |
| | 2694 | } |
| | 2695 | |
| | 2696 | if (!sinon.deepEqual(this.expectedArguments[i], args[i])) { |
| | 2697 | return false; |
| | 2698 | } |
| | 2699 | } |
| | 2700 | |
| | 2701 | return true; |
| | 2702 | }, |
| | 2703 | |
| | 2704 | withArgs: function withArgs() { |
| | 2705 | this.expectedArguments = slice.call(arguments); |
| | 2706 | return this; |
| | 2707 | }, |
| | 2708 | |
| | 2709 | withExactArgs: function withExactArgs() { |
| | 2710 | this.withArgs.apply(this, arguments); |
| | 2711 | this.expectsExactArgCount = true; |
| | 2712 | return this; |
| | 2713 | }, |
| | 2714 | |
| | 2715 | on: function on(thisValue) { |
| | 2716 | this.expectedThis = thisValue; |
| | 2717 | return this; |
| | 2718 | }, |
| | 2719 | |
| | 2720 | toString: function () { |
| | 2721 | var args = (this.expectedArguments || []).slice(); |
| | 2722 | |
| | 2723 | if (!this.expectsExactArgCount) { |
| | 2724 | push.call(args, "[...]"); |
| | 2725 | } |
| | 2726 | |
| | 2727 | var callStr = sinon.spyCall.toString.call({ |
| | 2728 | proxy: this.method || "anonymous mock expectation", |
| | 2729 | args: args |
| | 2730 | }); |
| | 2731 | |
| | 2732 | var message = callStr.replace(", [...", "[, ...") + " " + |
| | 2733 | expectedCallCountInWords(this); |
| | 2734 | |
| | 2735 | if (this.met()) { |
| | 2736 | return "Expectation met: " + message; |
| | 2737 | } |
| | 2738 | |
| | 2739 | return "Expected " + message + " (" + |
| | 2740 | callCountInWords(this.callCount) + ")"; |
| | 2741 | }, |
| | 2742 | |
| | 2743 | verify: function verify() { |
| | 2744 | if (!this.met()) { |
| | 2745 | sinon.expectation.fail(this.toString()); |
| | 2746 | } else { |
| | 2747 | sinon.expectation.pass(this.toString()); |
| | 2748 | } |
| | 2749 | |
| | 2750 | return true; |
| | 2751 | }, |
| | 2752 | |
| | 2753 | pass: function(message) { |
| | 2754 | sinon.assert.pass(message); |
| | 2755 | }, |
| | 2756 | fail: function (message) { |
| | 2757 | var exception = new Error(message); |
| | 2758 | exception.name = "ExpectationError"; |
| | 2759 | |
| | 2760 | throw exception; |
| | 2761 | } |
| | 2762 | }; |
| | 2763 | }()); |
| | 2764 | |
| | 2765 | if (commonJSModule) { |
| | 2766 | module.exports = mock; |
| | 2767 | } else { |
| | 2768 | sinon.mock = mock; |
| | 2769 | } |
| | 2770 | }(typeof sinon == "object" && sinon || null)); |
| | 2771 | |
| | 2772 | /** |
| | 2773 | * @depend ../sinon.js |
| | 2774 | * @depend stub.js |
| | 2775 | * @depend mock.js |
| | 2776 | */ |
| | 2777 | /*jslint eqeqeq: false, onevar: false, forin: true*/ |
| | 2778 | /*global module, require, sinon*/ |
| | 2779 | /** |
| | 2780 | * Collections of stubs, spies and mocks. |
| | 2781 | * |
| | 2782 | * @author Christian Johansen (christian@cjohansen.no) |
| | 2783 | * @license BSD |
| | 2784 | * |
| | 2785 | * Copyright (c) 2010-2013 Christian Johansen |
| | 2786 | */ |
| | 2787 | |
| | 2788 | (function (sinon) { |
| | 2789 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 2790 | var push = [].push; |
| | 2791 | var hasOwnProperty = Object.prototype.hasOwnProperty; |
| | 2792 | |
| | 2793 | if (!sinon && commonJSModule) { |
| | 2794 | sinon = require("../sinon"); |
| | 2795 | } |
| | 2796 | |
| | 2797 | if (!sinon) { |
| | 2798 | return; |
| | 2799 | } |
| | 2800 | |
| | 2801 | function getFakes(fakeCollection) { |
| | 2802 | if (!fakeCollection.fakes) { |
| | 2803 | fakeCollection.fakes = []; |
| | 2804 | } |
| | 2805 | |
| | 2806 | return fakeCollection.fakes; |
| | 2807 | } |
| | 2808 | |
| | 2809 | function each(fakeCollection, method) { |
| | 2810 | var fakes = getFakes(fakeCollection); |
| | 2811 | |
| | 2812 | for (var i = 0, l = fakes.length; i < l; i += 1) { |
| | 2813 | if (typeof fakes[i][method] == "function") { |
| | 2814 | fakes[i][method](); |
| | 2815 | } |
| | 2816 | } |
| | 2817 | } |
| | 2818 | |
| | 2819 | function compact(fakeCollection) { |
| | 2820 | var fakes = getFakes(fakeCollection); |
| | 2821 | var i = 0; |
| | 2822 | while (i < fakes.length) { |
| | 2823 | fakes.splice(i, 1); |
| | 2824 | } |
| | 2825 | } |
| | 2826 | |
| | 2827 | var collection = { |
| | 2828 | verify: function resolve() { |
| | 2829 | each(this, "verify"); |
| | 2830 | }, |
| | 2831 | |
| | 2832 | restore: function restore() { |
| | 2833 | each(this, "restore"); |
| | 2834 | compact(this); |
| | 2835 | }, |
| | 2836 | |
| | 2837 | verifyAndRestore: function verifyAndRestore() { |
| | 2838 | var exception; |
| | 2839 | |
| | 2840 | try { |
| | 2841 | this.verify(); |
| | 2842 | } catch (e) { |
| | 2843 | exception = e; |
| | 2844 | } |
| | 2845 | |
| | 2846 | this.restore(); |
| | 2847 | |
| | 2848 | if (exception) { |
| | 2849 | throw exception; |
| | 2850 | } |
| | 2851 | }, |
| | 2852 | |
| | 2853 | add: function add(fake) { |
| | 2854 | push.call(getFakes(this), fake); |
| | 2855 | return fake; |
| | 2856 | }, |
| | 2857 | |
| | 2858 | spy: function spy() { |
| | 2859 | return this.add(sinon.spy.apply(sinon, arguments)); |
| | 2860 | }, |
| | 2861 | |
| | 2862 | stub: function stub(object, property, value) { |
| | 2863 | if (property) { |
| | 2864 | var original = object[property]; |
| | 2865 | |
| | 2866 | if (typeof original != "function") { |
| | 2867 | if (!hasOwnProperty.call(object, property)) { |
| | 2868 | throw new TypeError("Cannot stub non-existent own property " + property); |
| | 2869 | } |
| | 2870 | |
| | 2871 | object[property] = value; |
| | 2872 | |
| | 2873 | return this.add({ |
| | 2874 | restore: function () { |
| | 2875 | object[property] = original; |
| | 2876 | } |
| | 2877 | }); |
| | 2878 | } |
| | 2879 | } |
| | 2880 | if (!property && !!object && typeof object == "object") { |
| | 2881 | var stubbedObj = sinon.stub.apply(sinon, arguments); |
| | 2882 | |
| | 2883 | for (var prop in stubbedObj) { |
| | 2884 | if (typeof stubbedObj[prop] === "function") { |
| | 2885 | this.add(stubbedObj[prop]); |
| | 2886 | } |
| | 2887 | } |
| | 2888 | |
| | 2889 | return stubbedObj; |
| | 2890 | } |
| | 2891 | |
| | 2892 | return this.add(sinon.stub.apply(sinon, arguments)); |
| | 2893 | }, |
| | 2894 | |
| | 2895 | mock: function mock() { |
| | 2896 | return this.add(sinon.mock.apply(sinon, arguments)); |
| | 2897 | }, |
| | 2898 | |
| | 2899 | inject: function inject(obj) { |
| | 2900 | var col = this; |
| | 2901 | |
| | 2902 | obj.spy = function () { |
| | 2903 | return col.spy.apply(col, arguments); |
| | 2904 | }; |
| | 2905 | |
| | 2906 | obj.stub = function () { |
| | 2907 | return col.stub.apply(col, arguments); |
| | 2908 | }; |
| | 2909 | |
| | 2910 | obj.mock = function () { |
| | 2911 | return col.mock.apply(col, arguments); |
| | 2912 | }; |
| | 2913 | |
| | 2914 | return obj; |
| | 2915 | } |
| | 2916 | }; |
| | 2917 | |
| | 2918 | if (commonJSModule) { |
| | 2919 | module.exports = collection; |
| | 2920 | } else { |
| | 2921 | sinon.collection = collection; |
| | 2922 | } |
| | 2923 | }(typeof sinon == "object" && sinon || null)); |
| | 2924 | |
| | 2925 | /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/ |
| | 2926 | /*global module, require, window*/ |
| | 2927 | /** |
| | 2928 | * Fake timer API |
| | 2929 | * setTimeout |
| | 2930 | * setInterval |
| | 2931 | * clearTimeout |
| | 2932 | * clearInterval |
| | 2933 | * tick |
| | 2934 | * reset |
| | 2935 | * Date |
| | 2936 | * |
| | 2937 | * Inspired by jsUnitMockTimeOut from JsUnit |
| | 2938 | * |
| | 2939 | * @author Christian Johansen (christian@cjohansen.no) |
| | 2940 | * @license BSD |
| | 2941 | * |
| | 2942 | * Copyright (c) 2010-2013 Christian Johansen |
| | 2943 | */ |
| | 2944 | |
| | 2945 | if (typeof sinon == "undefined") { |
| | 2946 | var sinon = {}; |
| | 2947 | } |
| | 2948 | |
| | 2949 | (function (global) { |
| | 2950 | var id = 1; |
| | 2951 | |
| | 2952 | function addTimer(args, recurring) { |
| | 2953 | if (args.length === 0) { |
| | 2954 | throw new Error("Function requires at least 1 parameter"); |
| | 2955 | } |
| | 2956 | |
| | 2957 | if (typeof args[0] === "undefined") { |
| | 2958 | throw new Error("Callback must be provided to timer calls"); |
| | 2959 | } |
| | 2960 | |
| | 2961 | var toId = id++; |
| | 2962 | var delay = args[1] || 0; |
| | 2963 | |
| | 2964 | if (!this.timeouts) { |
| | 2965 | this.timeouts = {}; |
| | 2966 | } |
| | 2967 | |
| | 2968 | this.timeouts[toId] = { |
| | 2969 | id: toId, |
| | 2970 | func: args[0], |
| | 2971 | callAt: this.now + delay, |
| | 2972 | invokeArgs: Array.prototype.slice.call(args, 2) |
| | 2973 | }; |
| | 2974 | |
| | 2975 | if (recurring === true) { |
| | 2976 | this.timeouts[toId].interval = delay; |
| | 2977 | } |
| | 2978 | |
| | 2979 | return toId; |
| | 2980 | } |
| | 2981 | |
| | 2982 | function parseTime(str) { |
| | 2983 | if (!str) { |
| | 2984 | return 0; |
| | 2985 | } |
| | 2986 | |
| | 2987 | var strings = str.split(":"); |
| | 2988 | var l = strings.length, i = l; |
| | 2989 | var ms = 0, parsed; |
| | 2990 | |
| | 2991 | if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) { |
| | 2992 | throw new Error("tick only understands numbers and 'h:m:s'"); |
| | 2993 | } |
| | 2994 | |
| | 2995 | while (i--) { |
| | 2996 | parsed = parseInt(strings[i], 10); |
| | 2997 | |
| | 2998 | if (parsed >= 60) { |
| | 2999 | throw new Error("Invalid time " + str); |
| | 3000 | } |
| | 3001 | |
| | 3002 | ms += parsed * Math.pow(60, (l - i - 1)); |
| | 3003 | } |
| | 3004 | |
| | 3005 | return ms * 1000; |
| | 3006 | } |
| | 3007 | |
| | 3008 | function createObject(object) { |
| | 3009 | var newObject; |
| | 3010 | |
| | 3011 | if (Object.create) { |
| | 3012 | newObject = Object.create(object); |
| | 3013 | } else { |
| | 3014 | var F = function () {}; |
| | 3015 | F.prototype = object; |
| | 3016 | newObject = new F(); |
| | 3017 | } |
| | 3018 | |
| | 3019 | newObject.Date.clock = newObject; |
| | 3020 | return newObject; |
| | 3021 | } |
| | 3022 | |
| | 3023 | sinon.clock = { |
| | 3024 | now: 0, |
| | 3025 | |
| | 3026 | create: function create(now) { |
| | 3027 | var clock = createObject(this); |
| | 3028 | |
| | 3029 | if (typeof now == "number") { |
| | 3030 | clock.now = now; |
| | 3031 | } |
| | 3032 | |
| | 3033 | if (!!now && typeof now == "object") { |
| | 3034 | throw new TypeError("now should be milliseconds since UNIX epoch"); |
| | 3035 | } |
| | 3036 | |
| | 3037 | return clock; |
| | 3038 | }, |
| | 3039 | |
| | 3040 | setTimeout: function setTimeout(callback, timeout) { |
| | 3041 | return addTimer.call(this, arguments, false); |
| | 3042 | }, |
| | 3043 | |
| | 3044 | clearTimeout: function clearTimeout(timerId) { |
| | 3045 | if (!this.timeouts) { |
| | 3046 | this.timeouts = []; |
| | 3047 | } |
| | 3048 | |
| | 3049 | if (timerId in this.timeouts) { |
| | 3050 | delete this.timeouts[timerId]; |
| | 3051 | } |
| | 3052 | }, |
| | 3053 | |
| | 3054 | setInterval: function setInterval(callback, timeout) { |
| | 3055 | return addTimer.call(this, arguments, true); |
| | 3056 | }, |
| | 3057 | |
| | 3058 | clearInterval: function clearInterval(timerId) { |
| | 3059 | this.clearTimeout(timerId); |
| | 3060 | }, |
| | 3061 | |
| | 3062 | setImmediate: function setImmediate(callback) { |
| | 3063 | var passThruArgs = Array.prototype.slice.call(arguments, 1); |
| | 3064 | |
| | 3065 | return addTimer.call(this, [callback, 0].concat(passThruArgs), false); |
| | 3066 | }, |
| | 3067 | |
| | 3068 | clearImmediate: function clearImmediate(timerId) { |
| | 3069 | this.clearTimeout(timerId); |
| | 3070 | }, |
| | 3071 | |
| | 3072 | tick: function tick(ms) { |
| | 3073 | ms = typeof ms == "number" ? ms : parseTime(ms); |
| | 3074 | var tickFrom = this.now, tickTo = this.now + ms, previous = this.now; |
| | 3075 | var timer = this.firstTimerInRange(tickFrom, tickTo); |
| | 3076 | |
| | 3077 | var firstException; |
| | 3078 | while (timer && tickFrom <= tickTo) { |
| | 3079 | if (this.timeouts[timer.id]) { |
| | 3080 | tickFrom = this.now = timer.callAt; |
| | 3081 | try { |
| | 3082 | this.callTimer(timer); |
| | 3083 | } catch (e) { |
| | 3084 | firstException = firstException || e; |
| | 3085 | } |
| | 3086 | } |
| | 3087 | |
| | 3088 | timer = this.firstTimerInRange(previous, tickTo); |
| | 3089 | previous = tickFrom; |
| | 3090 | } |
| | 3091 | |
| | 3092 | this.now = tickTo; |
| | 3093 | |
| | 3094 | if (firstException) { |
| | 3095 | throw firstException; |
| | 3096 | } |
| | 3097 | |
| | 3098 | return this.now; |
| | 3099 | }, |
| | 3100 | |
| | 3101 | firstTimerInRange: function (from, to) { |
| | 3102 | var timer, smallest = null, originalTimer; |
| | 3103 | |
| | 3104 | for (var id in this.timeouts) { |
| | 3105 | if (this.timeouts.hasOwnProperty(id)) { |
| | 3106 | if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) { |
| | 3107 | continue; |
| | 3108 | } |
| | 3109 | |
| | 3110 | if (smallest === null || this.timeouts[id].callAt < smallest) { |
| | 3111 | originalTimer = this.timeouts[id]; |
| | 3112 | smallest = this.timeouts[id].callAt; |
| | 3113 | |
| | 3114 | timer = { |
| | 3115 | func: this.timeouts[id].func, |
| | 3116 | callAt: this.timeouts[id].callAt, |
| | 3117 | interval: this.timeouts[id].interval, |
| | 3118 | id: this.timeouts[id].id, |
| | 3119 | invokeArgs: this.timeouts[id].invokeArgs |
| | 3120 | }; |
| | 3121 | } |
| | 3122 | } |
| | 3123 | } |
| | 3124 | |
| | 3125 | return timer || null; |
| | 3126 | }, |
| | 3127 | |
| | 3128 | callTimer: function (timer) { |
| | 3129 | if (typeof timer.interval == "number") { |
| | 3130 | this.timeouts[timer.id].callAt += timer.interval; |
| | 3131 | } else { |
| | 3132 | delete this.timeouts[timer.id]; |
| | 3133 | } |
| | 3134 | |
| | 3135 | try { |
| | 3136 | if (typeof timer.func == "function") { |
| | 3137 | timer.func.apply(null, timer.invokeArgs); |
| | 3138 | } else { |
| | 3139 | eval(timer.func); |
| | 3140 | } |
| | 3141 | } catch (e) { |
| | 3142 | var exception = e; |
| | 3143 | } |
| | 3144 | |
| | 3145 | if (!this.timeouts[timer.id]) { |
| | 3146 | if (exception) { |
| | 3147 | throw exception; |
| | 3148 | } |
| | 3149 | return; |
| | 3150 | } |
| | 3151 | |
| | 3152 | if (exception) { |
| | 3153 | throw exception; |
| | 3154 | } |
| | 3155 | }, |
| | 3156 | |
| | 3157 | reset: function reset() { |
| | 3158 | this.timeouts = {}; |
| | 3159 | }, |
| | 3160 | |
| | 3161 | Date: (function () { |
| | 3162 | var NativeDate = Date; |
| | 3163 | |
| | 3164 | function ClockDate(year, month, date, hour, minute, second, ms) { |
| | 3165 | // Defensive and verbose to avoid potential harm in passing |
| | 3166 | // explicit undefined when user does not pass argument |
| | 3167 | switch (arguments.length) { |
| | 3168 | case 0: |
| | 3169 | return new NativeDate(ClockDate.clock.now); |
| | 3170 | case 1: |
| | 3171 | return new NativeDate(year); |
| | 3172 | case 2: |
| | 3173 | return new NativeDate(year, month); |
| | 3174 | case 3: |
| | 3175 | return new NativeDate(year, month, date); |
| | 3176 | case 4: |
| | 3177 | return new NativeDate(year, month, date, hour); |
| | 3178 | case 5: |
| | 3179 | return new NativeDate(year, month, date, hour, minute); |
| | 3180 | case 6: |
| | 3181 | return new NativeDate(year, month, date, hour, minute, second); |
| | 3182 | default: |
| | 3183 | return new NativeDate(year, month, date, hour, minute, second, ms); |
| | 3184 | } |
| | 3185 | } |
| | 3186 | |
| | 3187 | return mirrorDateProperties(ClockDate, NativeDate); |
| | 3188 | }()) |
| | 3189 | }; |
| | 3190 | |
| | 3191 | function mirrorDateProperties(target, source) { |
| | 3192 | if (source.now) { |
| | 3193 | target.now = function now() { |
| | 3194 | return target.clock.now; |
| | 3195 | }; |
| | 3196 | } else { |
| | 3197 | delete target.now; |
| | 3198 | } |
| | 3199 | |
| | 3200 | if (source.toSource) { |
| | 3201 | target.toSource = function toSource() { |
| | 3202 | return source.toSource(); |
| | 3203 | }; |
| | 3204 | } else { |
| | 3205 | delete target.toSource; |
| | 3206 | } |
| | 3207 | |
| | 3208 | target.toString = function toString() { |
| | 3209 | return source.toString(); |
| | 3210 | }; |
| | 3211 | |
| | 3212 | target.prototype = source.prototype; |
| | 3213 | target.parse = source.parse; |
| | 3214 | target.UTC = source.UTC; |
| | 3215 | target.prototype.toUTCString = source.prototype.toUTCString; |
| | 3216 | |
| | 3217 | for (var prop in source) { |
| | 3218 | if (source.hasOwnProperty(prop)) { |
| | 3219 | target[prop] = source[prop]; |
| | 3220 | } |
| | 3221 | } |
| | 3222 | |
| | 3223 | return target; |
| | 3224 | } |
| | 3225 | |
| | 3226 | var methods = ["Date", "setTimeout", "setInterval", |
| | 3227 | "clearTimeout", "clearInterval"]; |
| | 3228 | |
| | 3229 | if (typeof global.setImmediate !== "undefined") { |
| | 3230 | methods.push("setImmediate"); |
| | 3231 | } |
| | 3232 | |
| | 3233 | if (typeof global.clearImmediate !== "undefined") { |
| | 3234 | methods.push("clearImmediate"); |
| | 3235 | } |
| | 3236 | |
| | 3237 | function restore() { |
| | 3238 | var method; |
| | 3239 | |
| | 3240 | for (var i = 0, l = this.methods.length; i < l; i++) { |
| | 3241 | method = this.methods[i]; |
| | 3242 | |
| | 3243 | if (global[method].hadOwnProperty) { |
| | 3244 | global[method] = this["_" + method]; |
| | 3245 | } else { |
| | 3246 | try { |
| | 3247 | delete global[method]; |
| | 3248 | } catch (e) {} |
| | 3249 | } |
| | 3250 | } |
| | 3251 | |
| | 3252 | // Prevent multiple executions which will completely remove these props |
| | 3253 | this.methods = []; |
| | 3254 | } |
| | 3255 | |
| | 3256 | function stubGlobal(method, clock) { |
| | 3257 | clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(global, method); |
| | 3258 | clock["_" + method] = global[method]; |
| | 3259 | |
| | 3260 | if (method == "Date") { |
| | 3261 | var date = mirrorDateProperties(clock[method], global[method]); |
| | 3262 | global[method] = date; |
| | 3263 | } else { |
| | 3264 | global[method] = function () { |
| | 3265 | return clock[method].apply(clock, arguments); |
| | 3266 | }; |
| | 3267 | |
| | 3268 | for (var prop in clock[method]) { |
| | 3269 | if (clock[method].hasOwnProperty(prop)) { |
| | 3270 | global[method][prop] = clock[method][prop]; |
| | 3271 | } |
| | 3272 | } |
| | 3273 | } |
| | 3274 | |
| | 3275 | global[method].clock = clock; |
| | 3276 | } |
| | 3277 | |
| | 3278 | sinon.useFakeTimers = function useFakeTimers(now) { |
| | 3279 | var clock = sinon.clock.create(now); |
| | 3280 | clock.restore = restore; |
| | 3281 | clock.methods = Array.prototype.slice.call(arguments, |
| | 3282 | typeof now == "number" ? 1 : 0); |
| | 3283 | |
| | 3284 | if (clock.methods.length === 0) { |
| | 3285 | clock.methods = methods; |
| | 3286 | } |
| | 3287 | |
| | 3288 | for (var i = 0, l = clock.methods.length; i < l; i++) { |
| | 3289 | stubGlobal(clock.methods[i], clock); |
| | 3290 | } |
| | 3291 | |
| | 3292 | return clock; |
| | 3293 | }; |
| | 3294 | }(typeof global != "undefined" && typeof global !== "function" ? global : this)); |
| | 3295 | |
| | 3296 | sinon.timers = { |
| | 3297 | setTimeout: setTimeout, |
| | 3298 | clearTimeout: clearTimeout, |
| | 3299 | setImmediate: (typeof setImmediate !== "undefined" ? setImmediate : undefined), |
| | 3300 | clearImmediate: (typeof clearImmediate !== "undefined" ? clearImmediate: undefined), |
| | 3301 | setInterval: setInterval, |
| | 3302 | clearInterval: clearInterval, |
| | 3303 | Date: Date |
| | 3304 | }; |
| | 3305 | |
| | 3306 | if (typeof module !== 'undefined' && module.exports) { |
| | 3307 | module.exports = sinon; |
| | 3308 | } |
| | 3309 | |
| | 3310 | /*jslint eqeqeq: false, onevar: false*/ |
| | 3311 | /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/ |
| | 3312 | /** |
| | 3313 | * Minimal Event interface implementation |
| | 3314 | * |
| | 3315 | * Original implementation by Sven Fuchs: https://gist.github.com/995028 |
| | 3316 | * Modifications and tests by Christian Johansen. |
| | 3317 | * |
| | 3318 | * @author Sven Fuchs (svenfuchs@artweb-design.de) |
| | 3319 | * @author Christian Johansen (christian@cjohansen.no) |
| | 3320 | * @license BSD |
| | 3321 | * |
| | 3322 | * Copyright (c) 2011 Sven Fuchs, Christian Johansen |
| | 3323 | */ |
| | 3324 | |
| | 3325 | if (typeof sinon == "undefined") { |
| | 3326 | this.sinon = {}; |
| | 3327 | } |
| | 3328 | |
| | 3329 | (function () { |
| | 3330 | var push = [].push; |
| | 3331 | |
| | 3332 | sinon.Event = function Event(type, bubbles, cancelable, target) { |
| | 3333 | this.initEvent(type, bubbles, cancelable, target); |
| | 3334 | }; |
| | 3335 | |
| | 3336 | sinon.Event.prototype = { |
| | 3337 | initEvent: function(type, bubbles, cancelable, target) { |
| | 3338 | this.type = type; |
| | 3339 | this.bubbles = bubbles; |
| | 3340 | this.cancelable = cancelable; |
| | 3341 | this.target = target; |
| | 3342 | }, |
| | 3343 | |
| | 3344 | stopPropagation: function () {}, |
| | 3345 | |
| | 3346 | preventDefault: function () { |
| | 3347 | this.defaultPrevented = true; |
| | 3348 | } |
| | 3349 | }; |
| | 3350 | |
| | 3351 | sinon.EventTarget = { |
| | 3352 | addEventListener: function addEventListener(event, listener) { |
| | 3353 | this.eventListeners = this.eventListeners || {}; |
| | 3354 | this.eventListeners[event] = this.eventListeners[event] || []; |
| | 3355 | push.call(this.eventListeners[event], listener); |
| | 3356 | }, |
| | 3357 | |
| | 3358 | removeEventListener: function removeEventListener(event, listener) { |
| | 3359 | var listeners = this.eventListeners && this.eventListeners[event] || []; |
| | 3360 | |
| | 3361 | for (var i = 0, l = listeners.length; i < l; ++i) { |
| | 3362 | if (listeners[i] == listener) { |
| | 3363 | return listeners.splice(i, 1); |
| | 3364 | } |
| | 3365 | } |
| | 3366 | }, |
| | 3367 | |
| | 3368 | dispatchEvent: function dispatchEvent(event) { |
| | 3369 | var type = event.type; |
| | 3370 | var listeners = this.eventListeners && this.eventListeners[type] || []; |
| | 3371 | |
| | 3372 | for (var i = 0; i < listeners.length; i++) { |
| | 3373 | if (typeof listeners[i] == "function") { |
| | 3374 | listeners[i].call(this, event); |
| | 3375 | } else { |
| | 3376 | listeners[i].handleEvent(event); |
| | 3377 | } |
| | 3378 | } |
| | 3379 | |
| | 3380 | return !!event.defaultPrevented; |
| | 3381 | } |
| | 3382 | }; |
| | 3383 | }()); |
| | 3384 | |
| | 3385 | /** |
| | 3386 | * @depend ../../sinon.js |
| | 3387 | * @depend event.js |
| | 3388 | */ |
| | 3389 | /*jslint eqeqeq: false, onevar: false*/ |
| | 3390 | /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/ |
| | 3391 | /** |
| | 3392 | * Fake XMLHttpRequest object |
| | 3393 | * |
| | 3394 | * @author Christian Johansen (christian@cjohansen.no) |
| | 3395 | * @license BSD |
| | 3396 | * |
| | 3397 | * Copyright (c) 2010-2013 Christian Johansen |
| | 3398 | */ |
| | 3399 | |
| | 3400 | // wrapper for global |
| | 3401 | (function(global) { |
| | 3402 | if (typeof sinon === "undefined") { |
| | 3403 | global.sinon = {}; |
| | 3404 | } |
| | 3405 | |
| | 3406 | var supportsProgress = typeof ProgressEvent !== "undefined"; |
| | 3407 | var supportsCustomEvent = typeof CustomEvent !== "undefined"; |
| | 3408 | sinon.xhr = { XMLHttpRequest: global.XMLHttpRequest }; |
| | 3409 | var xhr = sinon.xhr; |
| | 3410 | xhr.GlobalXMLHttpRequest = global.XMLHttpRequest; |
| | 3411 | xhr.GlobalActiveXObject = global.ActiveXObject; |
| | 3412 | xhr.supportsActiveX = typeof xhr.GlobalActiveXObject != "undefined"; |
| | 3413 | xhr.supportsXHR = typeof xhr.GlobalXMLHttpRequest != "undefined"; |
| | 3414 | xhr.workingXHR = xhr.supportsXHR ? xhr.GlobalXMLHttpRequest : xhr.supportsActiveX |
| | 3415 | ? function() { return new xhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0") } : false; |
| | 3416 | xhr.supportsCORS = 'withCredentials' in (new sinon.xhr.GlobalXMLHttpRequest()); |
| | 3417 | |
| | 3418 | /*jsl:ignore*/ |
| | 3419 | var unsafeHeaders = { |
| | 3420 | "Accept-Charset": true, |
| | 3421 | "Accept-Encoding": true, |
| | 3422 | "Connection": true, |
| | 3423 | "Content-Length": true, |
| | 3424 | "Cookie": true, |
| | 3425 | "Cookie2": true, |
| | 3426 | "Content-Transfer-Encoding": true, |
| | 3427 | "Date": true, |
| | 3428 | "Expect": true, |
| | 3429 | "Host": true, |
| | 3430 | "Keep-Alive": true, |
| | 3431 | "Referer": true, |
| | 3432 | "TE": true, |
| | 3433 | "Trailer": true, |
| | 3434 | "Transfer-Encoding": true, |
| | 3435 | "Upgrade": true, |
| | 3436 | "User-Agent": true, |
| | 3437 | "Via": true |
| | 3438 | }; |
| | 3439 | /*jsl:end*/ |
| | 3440 | |
| | 3441 | function FakeXMLHttpRequest() { |
| | 3442 | this.readyState = FakeXMLHttpRequest.UNSENT; |
| | 3443 | this.requestHeaders = {}; |
| | 3444 | this.requestBody = null; |
| | 3445 | this.status = 0; |
| | 3446 | this.statusText = ""; |
| | 3447 | this.upload = new UploadProgress(); |
| | 3448 | if (sinon.xhr.supportsCORS) { |
| | 3449 | this.withCredentials = false; |
| | 3450 | } |
| | 3451 | |
| | 3452 | |
| | 3453 | var xhr = this; |
| | 3454 | var events = ["loadstart", "load", "abort", "loadend"]; |
| | 3455 | |
| | 3456 | function addEventListener(eventName) { |
| | 3457 | xhr.addEventListener(eventName, function (event) { |
| | 3458 | var listener = xhr["on" + eventName]; |
| | 3459 | |
| | 3460 | if (listener && typeof listener == "function") { |
| | 3461 | listener(event); |
| | 3462 | } |
| | 3463 | }); |
| | 3464 | } |
| | 3465 | |
| | 3466 | for (var i = events.length - 1; i >= 0; i--) { |
| | 3467 | addEventListener(events[i]); |
| | 3468 | } |
| | 3469 | |
| | 3470 | if (typeof FakeXMLHttpRequest.onCreate == "function") { |
| | 3471 | FakeXMLHttpRequest.onCreate(this); |
| | 3472 | } |
| | 3473 | } |
| | 3474 | |
| | 3475 | // An upload object is created for each |
| | 3476 | // FakeXMLHttpRequest and allows upload |
| | 3477 | // events to be simulated using uploadProgress |
| | 3478 | // and uploadError. |
| | 3479 | function UploadProgress() { |
| | 3480 | this.eventListeners = { |
| | 3481 | "progress": [], |
| | 3482 | "load": [], |
| | 3483 | "abort": [], |
| | 3484 | "error": [] |
| | 3485 | } |
| | 3486 | } |
| | 3487 | |
| | 3488 | UploadProgress.prototype.addEventListener = function(event, listener) { |
| | 3489 | this.eventListeners[event].push(listener); |
| | 3490 | }; |
| | 3491 | |
| | 3492 | UploadProgress.prototype.removeEventListener = function(event, listener) { |
| | 3493 | var listeners = this.eventListeners[event] || []; |
| | 3494 | |
| | 3495 | for (var i = 0, l = listeners.length; i < l; ++i) { |
| | 3496 | if (listeners[i] == listener) { |
| | 3497 | return listeners.splice(i, 1); |
| | 3498 | } |
| | 3499 | } |
| | 3500 | }; |
| | 3501 | |
| | 3502 | UploadProgress.prototype.dispatchEvent = function(event) { |
| | 3503 | var listeners = this.eventListeners[event.type] || []; |
| | 3504 | |
| | 3505 | for (var i = 0, listener; (listener = listeners[i]) != null; i++) { |
| | 3506 | listener(event); |
| | 3507 | } |
| | 3508 | }; |
| | 3509 | |
| | 3510 | function verifyState(xhr) { |
| | 3511 | if (xhr.readyState !== FakeXMLHttpRequest.OPENED) { |
| | 3512 | throw new Error("INVALID_STATE_ERR"); |
| | 3513 | } |
| | 3514 | |
| | 3515 | if (xhr.sendFlag) { |
| | 3516 | throw new Error("INVALID_STATE_ERR"); |
| | 3517 | } |
| | 3518 | } |
| | 3519 | |
| | 3520 | // filtering to enable a white-list version of Sinon FakeXhr, |
| | 3521 | // where whitelisted requests are passed through to real XHR |
| | 3522 | function each(collection, callback) { |
| | 3523 | if (!collection) return; |
| | 3524 | for (var i = 0, l = collection.length; i < l; i += 1) { |
| | 3525 | callback(collection[i]); |
| | 3526 | } |
| | 3527 | } |
| | 3528 | function some(collection, callback) { |
| | 3529 | for (var index = 0; index < collection.length; index++) { |
| | 3530 | if(callback(collection[index]) === true) return true; |
| | 3531 | } |
| | 3532 | return false; |
| | 3533 | } |
| | 3534 | // largest arity in XHR is 5 - XHR#open |
| | 3535 | var apply = function(obj,method,args) { |
| | 3536 | switch(args.length) { |
| | 3537 | case 0: return obj[method](); |
| | 3538 | case 1: return obj[method](args[0]); |
| | 3539 | case 2: return obj[method](args[0],args[1]); |
| | 3540 | case 3: return obj[method](args[0],args[1],args[2]); |
| | 3541 | case 4: return obj[method](args[0],args[1],args[2],args[3]); |
| | 3542 | case 5: return obj[method](args[0],args[1],args[2],args[3],args[4]); |
| | 3543 | } |
| | 3544 | }; |
| | 3545 | |
| | 3546 | FakeXMLHttpRequest.filters = []; |
| | 3547 | FakeXMLHttpRequest.addFilter = function(fn) { |
| | 3548 | this.filters.push(fn) |
| | 3549 | }; |
| | 3550 | var IE6Re = /MSIE 6/; |
| | 3551 | FakeXMLHttpRequest.defake = function(fakeXhr,xhrArgs) { |
| | 3552 | var xhr = new sinon.xhr.workingXHR(); |
| | 3553 | each(["open","setRequestHeader","send","abort","getResponseHeader", |
| | 3554 | "getAllResponseHeaders","addEventListener","overrideMimeType","removeEventListener"], |
| | 3555 | function(method) { |
| | 3556 | fakeXhr[method] = function() { |
| | 3557 | return apply(xhr,method,arguments); |
| | 3558 | }; |
| | 3559 | }); |
| | 3560 | |
| | 3561 | var copyAttrs = function(args) { |
| | 3562 | each(args, function(attr) { |
| | 3563 | try { |
| | 3564 | fakeXhr[attr] = xhr[attr] |
| | 3565 | } catch(e) { |
| | 3566 | if(!IE6Re.test(navigator.userAgent)) throw e; |
| | 3567 | } |
| | 3568 | }); |
| | 3569 | }; |
| | 3570 | |
| | 3571 | var stateChange = function() { |
| | 3572 | fakeXhr.readyState = xhr.readyState; |
| | 3573 | if(xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) { |
| | 3574 | copyAttrs(["status","statusText"]); |
| | 3575 | } |
| | 3576 | if(xhr.readyState >= FakeXMLHttpRequest.LOADING) { |
| | 3577 | copyAttrs(["responseText"]); |
| | 3578 | } |
| | 3579 | if(xhr.readyState === FakeXMLHttpRequest.DONE) { |
| | 3580 | copyAttrs(["responseXML"]); |
| | 3581 | } |
| | 3582 | if(fakeXhr.onreadystatechange) fakeXhr.onreadystatechange.call(fakeXhr, { target: fakeXhr }); |
| | 3583 | }; |
| | 3584 | if(xhr.addEventListener) { |
| | 3585 | for(var event in fakeXhr.eventListeners) { |
| | 3586 | if(fakeXhr.eventListeners.hasOwnProperty(event)) { |
| | 3587 | each(fakeXhr.eventListeners[event],function(handler) { |
| | 3588 | xhr.addEventListener(event, handler); |
| | 3589 | }); |
| | 3590 | } |
| | 3591 | } |
| | 3592 | xhr.addEventListener("readystatechange",stateChange); |
| | 3593 | } else { |
| | 3594 | xhr.onreadystatechange = stateChange; |
| | 3595 | } |
| | 3596 | apply(xhr,"open",xhrArgs); |
| | 3597 | }; |
| | 3598 | FakeXMLHttpRequest.useFilters = false; |
| | 3599 | |
| | 3600 | function verifyRequestSent(xhr) { |
| | 3601 | if (xhr.readyState == FakeXMLHttpRequest.DONE) { |
| | 3602 | throw new Error("Request done"); |
| | 3603 | } |
| | 3604 | } |
| | 3605 | |
| | 3606 | function verifyHeadersReceived(xhr) { |
| | 3607 | if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) { |
| | 3608 | throw new Error("No headers received"); |
| | 3609 | } |
| | 3610 | } |
| | 3611 | |
| | 3612 | function verifyResponseBodyType(body) { |
| | 3613 | if (typeof body != "string") { |
| | 3614 | var error = new Error("Attempted to respond to fake XMLHttpRequest with " + |
| | 3615 | body + ", which is not a string."); |
| | 3616 | error.name = "InvalidBodyException"; |
| | 3617 | throw error; |
| | 3618 | } |
| | 3619 | } |
| | 3620 | |
| | 3621 | sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, { |
| | 3622 | async: true, |
| | 3623 | |
| | 3624 | open: function open(method, url, async, username, password) { |
| | 3625 | this.method = method; |
| | 3626 | this.url = url; |
| | 3627 | this.async = typeof async == "boolean" ? async : true; |
| | 3628 | this.username = username; |
| | 3629 | this.password = password; |
| | 3630 | this.responseText = null; |
| | 3631 | this.responseXML = null; |
| | 3632 | this.requestHeaders = {}; |
| | 3633 | this.sendFlag = false; |
| | 3634 | if(sinon.FakeXMLHttpRequest.useFilters === true) { |
| | 3635 | var xhrArgs = arguments; |
| | 3636 | var defake = some(FakeXMLHttpRequest.filters,function(filter) { |
| | 3637 | return filter.apply(this,xhrArgs) |
| | 3638 | }); |
| | 3639 | if (defake) { |
| | 3640 | return sinon.FakeXMLHttpRequest.defake(this,arguments); |
| | 3641 | } |
| | 3642 | } |
| | 3643 | this.readyStateChange(FakeXMLHttpRequest.OPENED); |
| | 3644 | }, |
| | 3645 | |
| | 3646 | readyStateChange: function readyStateChange(state) { |
| | 3647 | this.readyState = state; |
| | 3648 | |
| | 3649 | if (typeof this.onreadystatechange == "function") { |
| | 3650 | try { |
| | 3651 | this.onreadystatechange(); |
| | 3652 | } catch (e) { |
| | 3653 | sinon.logError("Fake XHR onreadystatechange handler", e); |
| | 3654 | } |
| | 3655 | } |
| | 3656 | |
| | 3657 | this.dispatchEvent(new sinon.Event("readystatechange")); |
| | 3658 | |
| | 3659 | switch (this.readyState) { |
| | 3660 | case FakeXMLHttpRequest.DONE: |
| | 3661 | this.dispatchEvent(new sinon.Event("load", false, false, this)); |
| | 3662 | this.dispatchEvent(new sinon.Event("loadend", false, false, this)); |
| | 3663 | this.upload.dispatchEvent(new sinon.Event("load", false, false, this)); |
| | 3664 | if (supportsProgress) { |
| | 3665 | this.upload.dispatchEvent(new ProgressEvent("progress", {loaded: 100, total: 100})); |
| | 3666 | } |
| | 3667 | break; |
| | 3668 | } |
| | 3669 | }, |
| | 3670 | |
| | 3671 | setRequestHeader: function setRequestHeader(header, value) { |
| | 3672 | verifyState(this); |
| | 3673 | |
| | 3674 | if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) { |
| | 3675 | throw new Error("Refused to set unsafe header \"" + header + "\""); |
| | 3676 | } |
| | 3677 | |
| | 3678 | if (this.requestHeaders[header]) { |
| | 3679 | this.requestHeaders[header] += "," + value; |
| | 3680 | } else { |
| | 3681 | this.requestHeaders[header] = value; |
| | 3682 | } |
| | 3683 | }, |
| | 3684 | |
| | 3685 | // Helps testing |
| | 3686 | setResponseHeaders: function setResponseHeaders(headers) { |
| | 3687 | this.responseHeaders = {}; |
| | 3688 | |
| | 3689 | for (var header in headers) { |
| | 3690 | if (headers.hasOwnProperty(header)) { |
| | 3691 | this.responseHeaders[header] = headers[header]; |
| | 3692 | } |
| | 3693 | } |
| | 3694 | |
| | 3695 | if (this.async) { |
| | 3696 | this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED); |
| | 3697 | } else { |
| | 3698 | this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED; |
| | 3699 | } |
| | 3700 | }, |
| | 3701 | |
| | 3702 | // Currently treats ALL data as a DOMString (i.e. no Document) |
| | 3703 | send: function send(data) { |
| | 3704 | verifyState(this); |
| | 3705 | |
| | 3706 | if (!/^(get|head)$/i.test(this.method)) { |
| | 3707 | if (this.requestHeaders["Content-Type"]) { |
| | 3708 | var value = this.requestHeaders["Content-Type"].split(";"); |
| | 3709 | this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8"; |
| | 3710 | } else { |
| | 3711 | this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8"; |
| | 3712 | } |
| | 3713 | |
| | 3714 | this.requestBody = data; |
| | 3715 | } |
| | 3716 | |
| | 3717 | this.errorFlag = false; |
| | 3718 | this.sendFlag = this.async; |
| | 3719 | this.readyStateChange(FakeXMLHttpRequest.OPENED); |
| | 3720 | |
| | 3721 | if (typeof this.onSend == "function") { |
| | 3722 | this.onSend(this); |
| | 3723 | } |
| | 3724 | |
| | 3725 | this.dispatchEvent(new sinon.Event("loadstart", false, false, this)); |
| | 3726 | }, |
| | 3727 | |
| | 3728 | abort: function abort() { |
| | 3729 | this.aborted = true; |
| | 3730 | this.responseText = null; |
| | 3731 | this.errorFlag = true; |
| | 3732 | this.requestHeaders = {}; |
| | 3733 | |
| | 3734 | if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) { |
| | 3735 | this.readyStateChange(sinon.FakeXMLHttpRequest.DONE); |
| | 3736 | this.sendFlag = false; |
| | 3737 | } |
| | 3738 | |
| | 3739 | this.readyState = sinon.FakeXMLHttpRequest.UNSENT; |
| | 3740 | |
| | 3741 | this.dispatchEvent(new sinon.Event("abort", false, false, this)); |
| | 3742 | |
| | 3743 | this.upload.dispatchEvent(new sinon.Event("abort", false, false, this)); |
| | 3744 | |
| | 3745 | if (typeof this.onerror === "function") { |
| | 3746 | this.onerror(); |
| | 3747 | } |
| | 3748 | }, |
| | 3749 | |
| | 3750 | getResponseHeader: function getResponseHeader(header) { |
| | 3751 | if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) { |
| | 3752 | return null; |
| | 3753 | } |
| | 3754 | |
| | 3755 | if (/^Set-Cookie2?$/i.test(header)) { |
| | 3756 | return null; |
| | 3757 | } |
| | 3758 | |
| | 3759 | header = header.toLowerCase(); |
| | 3760 | |
| | 3761 | for (var h in this.responseHeaders) { |
| | 3762 | if (h.toLowerCase() == header) { |
| | 3763 | return this.responseHeaders[h]; |
| | 3764 | } |
| | 3765 | } |
| | 3766 | |
| | 3767 | return null; |
| | 3768 | }, |
| | 3769 | |
| | 3770 | getAllResponseHeaders: function getAllResponseHeaders() { |
| | 3771 | if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) { |
| | 3772 | return ""; |
| | 3773 | } |
| | 3774 | |
| | 3775 | var headers = ""; |
| | 3776 | |
| | 3777 | for (var header in this.responseHeaders) { |
| | 3778 | if (this.responseHeaders.hasOwnProperty(header) && |
| | 3779 | !/^Set-Cookie2?$/i.test(header)) { |
| | 3780 | headers += header + ": " + this.responseHeaders[header] + "\r\n"; |
| | 3781 | } |
| | 3782 | } |
| | 3783 | |
| | 3784 | return headers; |
| | 3785 | }, |
| | 3786 | |
| | 3787 | setResponseBody: function setResponseBody(body) { |
| | 3788 | verifyRequestSent(this); |
| | 3789 | verifyHeadersReceived(this); |
| | 3790 | verifyResponseBodyType(body); |
| | 3791 | |
| | 3792 | var chunkSize = this.chunkSize || 10; |
| | 3793 | var index = 0; |
| | 3794 | this.responseText = ""; |
| | 3795 | |
| | 3796 | do { |
| | 3797 | if (this.async) { |
| | 3798 | this.readyStateChange(FakeXMLHttpRequest.LOADING); |
| | 3799 | } |
| | 3800 | |
| | 3801 | this.responseText += body.substring(index, index + chunkSize); |
| | 3802 | index += chunkSize; |
| | 3803 | } while (index < body.length); |
| | 3804 | |
| | 3805 | var type = this.getResponseHeader("Content-Type"); |
| | 3806 | |
| | 3807 | if (this.responseText && |
| | 3808 | (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) { |
| | 3809 | try { |
| | 3810 | this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText); |
| | 3811 | } catch (e) { |
| | 3812 | // Unable to parse XML - no biggie |
| | 3813 | } |
| | 3814 | } |
| | 3815 | |
| | 3816 | if (this.async) { |
| | 3817 | this.readyStateChange(FakeXMLHttpRequest.DONE); |
| | 3818 | } else { |
| | 3819 | this.readyState = FakeXMLHttpRequest.DONE; |
| | 3820 | } |
| | 3821 | }, |
| | 3822 | |
| | 3823 | respond: function respond(status, headers, body) { |
| | 3824 | this.status = typeof status == "number" ? status : 200; |
| | 3825 | this.statusText = FakeXMLHttpRequest.statusCodes[this.status]; |
| | 3826 | this.setResponseHeaders(headers || {}); |
| | 3827 | this.setResponseBody(body || ""); |
| | 3828 | }, |
| | 3829 | |
| | 3830 | uploadProgress: function uploadProgress(progressEventRaw) { |
| | 3831 | if (supportsProgress) { |
| | 3832 | this.upload.dispatchEvent(new ProgressEvent("progress", progressEventRaw)); |
| | 3833 | } |
| | 3834 | }, |
| | 3835 | |
| | 3836 | uploadError: function uploadError(error) { |
| | 3837 | if (supportsCustomEvent) { |
| | 3838 | this.upload.dispatchEvent(new CustomEvent("error", {"detail": error})); |
| | 3839 | } |
| | 3840 | } |
| | 3841 | }); |
| | 3842 | |
| | 3843 | sinon.extend(FakeXMLHttpRequest, { |
| | 3844 | UNSENT: 0, |
| | 3845 | OPENED: 1, |
| | 3846 | HEADERS_RECEIVED: 2, |
| | 3847 | LOADING: 3, |
| | 3848 | DONE: 4 |
| | 3849 | }); |
| | 3850 | |
| | 3851 | // Borrowed from JSpec |
| | 3852 | FakeXMLHttpRequest.parseXML = function parseXML(text) { |
| | 3853 | var xmlDoc; |
| | 3854 | |
| | 3855 | if (typeof DOMParser != "undefined") { |
| | 3856 | var parser = new DOMParser(); |
| | 3857 | xmlDoc = parser.parseFromString(text, "text/xml"); |
| | 3858 | } else { |
| | 3859 | xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); |
| | 3860 | xmlDoc.async = "false"; |
| | 3861 | xmlDoc.loadXML(text); |
| | 3862 | } |
| | 3863 | |
| | 3864 | return xmlDoc; |
| | 3865 | }; |
| | 3866 | |
| | 3867 | FakeXMLHttpRequest.statusCodes = { |
| | 3868 | 100: "Continue", |
| | 3869 | 101: "Switching Protocols", |
| | 3870 | 200: "OK", |
| | 3871 | 201: "Created", |
| | 3872 | 202: "Accepted", |
| | 3873 | 203: "Non-Authoritative Information", |
| | 3874 | 204: "No Content", |
| | 3875 | 205: "Reset Content", |
| | 3876 | 206: "Partial Content", |
| | 3877 | 300: "Multiple Choice", |
| | 3878 | 301: "Moved Permanently", |
| | 3879 | 302: "Found", |
| | 3880 | 303: "See Other", |
| | 3881 | 304: "Not Modified", |
| | 3882 | 305: "Use Proxy", |
| | 3883 | 307: "Temporary Redirect", |
| | 3884 | 400: "Bad Request", |
| | 3885 | 401: "Unauthorized", |
| | 3886 | 402: "Payment Required", |
| | 3887 | 403: "Forbidden", |
| | 3888 | 404: "Not Found", |
| | 3889 | 405: "Method Not Allowed", |
| | 3890 | 406: "Not Acceptable", |
| | 3891 | 407: "Proxy Authentication Required", |
| | 3892 | 408: "Request Timeout", |
| | 3893 | 409: "Conflict", |
| | 3894 | 410: "Gone", |
| | 3895 | 411: "Length Required", |
| | 3896 | 412: "Precondition Failed", |
| | 3897 | 413: "Request Entity Too Large", |
| | 3898 | 414: "Request-URI Too Long", |
| | 3899 | 415: "Unsupported Media Type", |
| | 3900 | 416: "Requested Range Not Satisfiable", |
| | 3901 | 417: "Expectation Failed", |
| | 3902 | 422: "Unprocessable Entity", |
| | 3903 | 500: "Internal Server Error", |
| | 3904 | 501: "Not Implemented", |
| | 3905 | 502: "Bad Gateway", |
| | 3906 | 503: "Service Unavailable", |
| | 3907 | 504: "Gateway Timeout", |
| | 3908 | 505: "HTTP Version Not Supported" |
| | 3909 | }; |
| | 3910 | |
| | 3911 | sinon.useFakeXMLHttpRequest = function () { |
| | 3912 | sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) { |
| | 3913 | if (xhr.supportsXHR) { |
| | 3914 | global.XMLHttpRequest = xhr.GlobalXMLHttpRequest; |
| | 3915 | } |
| | 3916 | |
| | 3917 | if (xhr.supportsActiveX) { |
| | 3918 | global.ActiveXObject = xhr.GlobalActiveXObject; |
| | 3919 | } |
| | 3920 | |
| | 3921 | delete sinon.FakeXMLHttpRequest.restore; |
| | 3922 | |
| | 3923 | if (keepOnCreate !== true) { |
| | 3924 | delete sinon.FakeXMLHttpRequest.onCreate; |
| | 3925 | } |
| | 3926 | }; |
| | 3927 | if (xhr.supportsXHR) { |
| | 3928 | global.XMLHttpRequest = sinon.FakeXMLHttpRequest; |
| | 3929 | } |
| | 3930 | |
| | 3931 | if (xhr.supportsActiveX) { |
| | 3932 | global.ActiveXObject = function ActiveXObject(objId) { |
| | 3933 | if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) { |
| | 3934 | |
| | 3935 | return new sinon.FakeXMLHttpRequest(); |
| | 3936 | } |
| | 3937 | |
| | 3938 | return new xhr.GlobalActiveXObject(objId); |
| | 3939 | }; |
| | 3940 | } |
| | 3941 | |
| | 3942 | return sinon.FakeXMLHttpRequest; |
| | 3943 | }; |
| | 3944 | |
| | 3945 | sinon.FakeXMLHttpRequest = FakeXMLHttpRequest; |
| | 3946 | |
| | 3947 | })(typeof global === "object" ? global : this); |
| | 3948 | |
| | 3949 | if (typeof module !== 'undefined' && module.exports) { |
| | 3950 | module.exports = sinon; |
| | 3951 | } |
| | 3952 | |
| | 3953 | /** |
| | 3954 | * @depend fake_xml_http_request.js |
| | 3955 | */ |
| | 3956 | /*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/ |
| | 3957 | /*global module, require, window*/ |
| | 3958 | /** |
| | 3959 | * The Sinon "server" mimics a web server that receives requests from |
| | 3960 | * sinon.FakeXMLHttpRequest and provides an API to respond to those requests, |
| | 3961 | * both synchronously and asynchronously. To respond synchronuously, canned |
| | 3962 | * answers have to be provided upfront. |
| | 3963 | * |
| | 3964 | * @author Christian Johansen (christian@cjohansen.no) |
| | 3965 | * @license BSD |
| | 3966 | * |
| | 3967 | * Copyright (c) 2010-2013 Christian Johansen |
| | 3968 | */ |
| | 3969 | |
| | 3970 | if (typeof sinon == "undefined") { |
| | 3971 | var sinon = {}; |
| | 3972 | } |
| | 3973 | |
| | 3974 | sinon.fakeServer = (function () { |
| | 3975 | var push = [].push; |
| | 3976 | function F() {} |
| | 3977 | |
| | 3978 | function create(proto) { |
| | 3979 | F.prototype = proto; |
| | 3980 | return new F(); |
| | 3981 | } |
| | 3982 | |
| | 3983 | function responseArray(handler) { |
| | 3984 | var response = handler; |
| | 3985 | |
| | 3986 | if (Object.prototype.toString.call(handler) != "[object Array]") { |
| | 3987 | response = [200, {}, handler]; |
| | 3988 | } |
| | 3989 | |
| | 3990 | if (typeof response[2] != "string") { |
| | 3991 | throw new TypeError("Fake server response body should be string, but was " + |
| | 3992 | typeof response[2]); |
| | 3993 | } |
| | 3994 | |
| | 3995 | return response; |
| | 3996 | } |
| | 3997 | |
| | 3998 | var wloc = typeof window !== "undefined" ? window.location : {}; |
| | 3999 | var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host); |
| | 4000 | |
| | 4001 | function matchOne(response, reqMethod, reqUrl) { |
| | 4002 | var rmeth = response.method; |
| | 4003 | var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase(); |
| | 4004 | var url = response.url; |
| | 4005 | var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl)); |
| | 4006 | |
| | 4007 | return matchMethod && matchUrl; |
| | 4008 | } |
| | 4009 | |
| | 4010 | function match(response, request) { |
| | 4011 | var requestUrl = request.url; |
| | 4012 | |
| | 4013 | if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) { |
| | 4014 | requestUrl = requestUrl.replace(rCurrLoc, ""); |
| | 4015 | } |
| | 4016 | |
| | 4017 | if (matchOne(response, this.getHTTPMethod(request), requestUrl)) { |
| | 4018 | if (typeof response.response == "function") { |
| | 4019 | var ru = response.url; |
| | 4020 | var args = [request].concat(ru && typeof ru.exec == "function" ? ru.exec(requestUrl).slice(1) : []); |
| | 4021 | return response.response.apply(response, args); |
| | 4022 | } |
| | 4023 | |
| | 4024 | return true; |
| | 4025 | } |
| | 4026 | |
| | 4027 | return false; |
| | 4028 | } |
| | 4029 | |
| | 4030 | function log(response, request) { |
| | 4031 | var str; |
| | 4032 | |
| | 4033 | str = "Request:\n" + sinon.format(request) + "\n\n"; |
| | 4034 | str += "Response:\n" + sinon.format(response) + "\n\n"; |
| | 4035 | |
| | 4036 | sinon.log(str); |
| | 4037 | } |
| | 4038 | |
| | 4039 | return { |
| | 4040 | create: function () { |
| | 4041 | var server = create(this); |
| | 4042 | this.xhr = sinon.useFakeXMLHttpRequest(); |
| | 4043 | server.requests = []; |
| | 4044 | |
| | 4045 | this.xhr.onCreate = function (xhrObj) { |
| | 4046 | server.addRequest(xhrObj); |
| | 4047 | }; |
| | 4048 | |
| | 4049 | return server; |
| | 4050 | }, |
| | 4051 | |
| | 4052 | addRequest: function addRequest(xhrObj) { |
| | 4053 | var server = this; |
| | 4054 | push.call(this.requests, xhrObj); |
| | 4055 | |
| | 4056 | xhrObj.onSend = function () { |
| | 4057 | server.handleRequest(this); |
| | 4058 | |
| | 4059 | if (server.autoRespond && !server.responding) { |
| | 4060 | setTimeout(function () { |
| | 4061 | server.responding = false; |
| | 4062 | server.respond(); |
| | 4063 | }, server.autoRespondAfter || 10); |
| | 4064 | |
| | 4065 | server.responding = true; |
| | 4066 | } |
| | 4067 | }; |
| | 4068 | }, |
| | 4069 | |
| | 4070 | getHTTPMethod: function getHTTPMethod(request) { |
| | 4071 | if (this.fakeHTTPMethods && /post/i.test(request.method)) { |
| | 4072 | var matches = (request.requestBody || "").match(/_method=([^\b;]+)/); |
| | 4073 | return !!matches ? matches[1] : request.method; |
| | 4074 | } |
| | 4075 | |
| | 4076 | return request.method; |
| | 4077 | }, |
| | 4078 | |
| | 4079 | handleRequest: function handleRequest(xhr) { |
| | 4080 | if (xhr.async) { |
| | 4081 | if (!this.queue) { |
| | 4082 | this.queue = []; |
| | 4083 | } |
| | 4084 | |
| | 4085 | push.call(this.queue, xhr); |
| | 4086 | } else { |
| | 4087 | this.processRequest(xhr); |
| | 4088 | } |
| | 4089 | }, |
| | 4090 | |
| | 4091 | respondWith: function respondWith(method, url, body) { |
| | 4092 | if (arguments.length == 1 && typeof method != "function") { |
| | 4093 | this.response = responseArray(method); |
| | 4094 | return; |
| | 4095 | } |
| | 4096 | |
| | 4097 | if (!this.responses) { this.responses = []; } |
| | 4098 | |
| | 4099 | if (arguments.length == 1) { |
| | 4100 | body = method; |
| | 4101 | url = method = null; |
| | 4102 | } |
| | 4103 | |
| | 4104 | if (arguments.length == 2) { |
| | 4105 | body = url; |
| | 4106 | url = method; |
| | 4107 | method = null; |
| | 4108 | } |
| | 4109 | |
| | 4110 | push.call(this.responses, { |
| | 4111 | method: method, |
| | 4112 | url: url, |
| | 4113 | response: typeof body == "function" ? body : responseArray(body) |
| | 4114 | }); |
| | 4115 | }, |
| | 4116 | |
| | 4117 | respond: function respond() { |
| | 4118 | if (arguments.length > 0) this.respondWith.apply(this, arguments); |
| | 4119 | var queue = this.queue || []; |
| | 4120 | var requests = queue.splice(0); |
| | 4121 | var request; |
| | 4122 | |
| | 4123 | while(request = requests.shift()) { |
| | 4124 | this.processRequest(request); |
| | 4125 | } |
| | 4126 | }, |
| | 4127 | |
| | 4128 | processRequest: function processRequest(request) { |
| | 4129 | try { |
| | 4130 | if (request.aborted) { |
| | 4131 | return; |
| | 4132 | } |
| | 4133 | |
| | 4134 | var response = this.response || [404, {}, ""]; |
| | 4135 | |
| | 4136 | if (this.responses) { |
| | 4137 | for (var l = this.responses.length, i = l - 1; i >= 0; i--) { |
| | 4138 | if (match.call(this, this.responses[i], request)) { |
| | 4139 | response = this.responses[i].response; |
| | 4140 | break; |
| | 4141 | } |
| | 4142 | } |
| | 4143 | } |
| | 4144 | |
| | 4145 | if (request.readyState != 4) { |
| | 4146 | log(response, request); |
| | 4147 | |
| | 4148 | request.respond(response[0], response[1], response[2]); |
| | 4149 | } |
| | 4150 | } catch (e) { |
| | 4151 | sinon.logError("Fake server request processing", e); |
| | 4152 | } |
| | 4153 | }, |
| | 4154 | |
| | 4155 | restore: function restore() { |
| | 4156 | return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments); |
| | 4157 | } |
| | 4158 | }; |
| | 4159 | }()); |
| | 4160 | |
| | 4161 | if (typeof module !== 'undefined' && module.exports) { |
| | 4162 | module.exports = sinon; |
| | 4163 | } |
| | 4164 | |
| | 4165 | /** |
| | 4166 | * @depend fake_server.js |
| | 4167 | * @depend fake_timers.js |
| | 4168 | */ |
| | 4169 | /*jslint browser: true, eqeqeq: false, onevar: false*/ |
| | 4170 | /*global sinon*/ |
| | 4171 | /** |
| | 4172 | * Add-on for sinon.fakeServer that automatically handles a fake timer along with |
| | 4173 | * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery |
| | 4174 | * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead, |
| | 4175 | * it polls the object for completion with setInterval. Dispite the direct |
| | 4176 | * motivation, there is nothing jQuery-specific in this file, so it can be used |
| | 4177 | * in any environment where the ajax implementation depends on setInterval or |
| | 4178 | * setTimeout. |
| | 4179 | * |
| | 4180 | * @author Christian Johansen (christian@cjohansen.no) |
| | 4181 | * @license BSD |
| | 4182 | * |
| | 4183 | * Copyright (c) 2010-2013 Christian Johansen |
| | 4184 | */ |
| | 4185 | |
| | 4186 | (function () { |
| | 4187 | function Server() {} |
| | 4188 | Server.prototype = sinon.fakeServer; |
| | 4189 | |
| | 4190 | sinon.fakeServerWithClock = new Server(); |
| | 4191 | |
| | 4192 | sinon.fakeServerWithClock.addRequest = function addRequest(xhr) { |
| | 4193 | if (xhr.async) { |
| | 4194 | if (typeof setTimeout.clock == "object") { |
| | 4195 | this.clock = setTimeout.clock; |
| | 4196 | } else { |
| | 4197 | this.clock = sinon.useFakeTimers(); |
| | 4198 | this.resetClock = true; |
| | 4199 | } |
| | 4200 | |
| | 4201 | if (!this.longestTimeout) { |
| | 4202 | var clockSetTimeout = this.clock.setTimeout; |
| | 4203 | var clockSetInterval = this.clock.setInterval; |
| | 4204 | var server = this; |
| | 4205 | |
| | 4206 | this.clock.setTimeout = function (fn, timeout) { |
| | 4207 | server.longestTimeout = Math.max(timeout, server.longestTimeout || 0); |
| | 4208 | |
| | 4209 | return clockSetTimeout.apply(this, arguments); |
| | 4210 | }; |
| | 4211 | |
| | 4212 | this.clock.setInterval = function (fn, timeout) { |
| | 4213 | server.longestTimeout = Math.max(timeout, server.longestTimeout || 0); |
| | 4214 | |
| | 4215 | return clockSetInterval.apply(this, arguments); |
| | 4216 | }; |
| | 4217 | } |
| | 4218 | } |
| | 4219 | |
| | 4220 | return sinon.fakeServer.addRequest.call(this, xhr); |
| | 4221 | }; |
| | 4222 | |
| | 4223 | sinon.fakeServerWithClock.respond = function respond() { |
| | 4224 | var returnVal = sinon.fakeServer.respond.apply(this, arguments); |
| | 4225 | |
| | 4226 | if (this.clock) { |
| | 4227 | this.clock.tick(this.longestTimeout || 0); |
| | 4228 | this.longestTimeout = 0; |
| | 4229 | |
| | 4230 | if (this.resetClock) { |
| | 4231 | this.clock.restore(); |
| | 4232 | this.resetClock = false; |
| | 4233 | } |
| | 4234 | } |
| | 4235 | |
| | 4236 | return returnVal; |
| | 4237 | }; |
| | 4238 | |
| | 4239 | sinon.fakeServerWithClock.restore = function restore() { |
| | 4240 | if (this.clock) { |
| | 4241 | this.clock.restore(); |
| | 4242 | } |
| | 4243 | |
| | 4244 | return sinon.fakeServer.restore.apply(this, arguments); |
| | 4245 | }; |
| | 4246 | }()); |
| | 4247 | |
| | 4248 | /** |
| | 4249 | * @depend ../sinon.js |
| | 4250 | * @depend collection.js |
| | 4251 | * @depend util/fake_timers.js |
| | 4252 | * @depend util/fake_server_with_clock.js |
| | 4253 | */ |
| | 4254 | /*jslint eqeqeq: false, onevar: false, plusplus: false*/ |
| | 4255 | /*global require, module*/ |
| | 4256 | /** |
| | 4257 | * Manages fake collections as well as fake utilities such as Sinon's |
| | 4258 | * timers and fake XHR implementation in one convenient object. |
| | 4259 | * |
| | 4260 | * @author Christian Johansen (christian@cjohansen.no) |
| | 4261 | * @license BSD |
| | 4262 | * |
| | 4263 | * Copyright (c) 2010-2013 Christian Johansen |
| | 4264 | */ |
| | 4265 | |
| | 4266 | if (typeof module !== 'undefined' && module.exports) { |
| | 4267 | var sinon = require("../sinon"); |
| | 4268 | sinon.extend(sinon, require("./util/fake_timers")); |
| | 4269 | } |
| | 4270 | |
| | 4271 | (function () { |
| | 4272 | var push = [].push; |
| | 4273 | |
| | 4274 | function exposeValue(sandbox, config, key, value) { |
| | 4275 | if (!value) { |
| | 4276 | return; |
| | 4277 | } |
| | 4278 | |
| | 4279 | if (config.injectInto && !(key in config.injectInto) ) { |
| | 4280 | config.injectInto[key] = value; |
| | 4281 | } else { |
| | 4282 | push.call(sandbox.args, value); |
| | 4283 | } |
| | 4284 | } |
| | 4285 | |
| | 4286 | function prepareSandboxFromConfig(config) { |
| | 4287 | var sandbox = sinon.create(sinon.sandbox); |
| | 4288 | |
| | 4289 | if (config.useFakeServer) { |
| | 4290 | if (typeof config.useFakeServer == "object") { |
| | 4291 | sandbox.serverPrototype = config.useFakeServer; |
| | 4292 | } |
| | 4293 | |
| | 4294 | sandbox.useFakeServer(); |
| | 4295 | } |
| | 4296 | |
| | 4297 | if (config.useFakeTimers) { |
| | 4298 | if (typeof config.useFakeTimers == "object") { |
| | 4299 | sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers); |
| | 4300 | } else { |
| | 4301 | sandbox.useFakeTimers(); |
| | 4302 | } |
| | 4303 | } |
| | 4304 | |
| | 4305 | return sandbox; |
| | 4306 | } |
| | 4307 | |
| | 4308 | sinon.sandbox = sinon.extend(sinon.create(sinon.collection), { |
| | 4309 | useFakeTimers: function useFakeTimers() { |
| | 4310 | this.clock = sinon.useFakeTimers.apply(sinon, arguments); |
| | 4311 | |
| | 4312 | return this.add(this.clock); |
| | 4313 | }, |
| | 4314 | |
| | 4315 | serverPrototype: sinon.fakeServer, |
| | 4316 | |
| | 4317 | useFakeServer: function useFakeServer() { |
| | 4318 | var proto = this.serverPrototype || sinon.fakeServer; |
| | 4319 | |
| | 4320 | if (!proto || !proto.create) { |
| | 4321 | return null; |
| | 4322 | } |
| | 4323 | |
| | 4324 | this.server = proto.create(); |
| | 4325 | return this.add(this.server); |
| | 4326 | }, |
| | 4327 | |
| | 4328 | inject: function (obj) { |
| | 4329 | sinon.collection.inject.call(this, obj); |
| | 4330 | |
| | 4331 | if (this.clock) { |
| | 4332 | obj.clock = this.clock; |
| | 4333 | } |
| | 4334 | |
| | 4335 | if (this.server) { |
| | 4336 | obj.server = this.server; |
| | 4337 | obj.requests = this.server.requests; |
| | 4338 | } |
| | 4339 | |
| | 4340 | return obj; |
| | 4341 | }, |
| | 4342 | |
| | 4343 | create: function (config) { |
| | 4344 | if (!config) { |
| | 4345 | return sinon.create(sinon.sandbox); |
| | 4346 | } |
| | 4347 | |
| | 4348 | var sandbox = prepareSandboxFromConfig(config); |
| | 4349 | sandbox.args = sandbox.args || []; |
| | 4350 | var prop, value, exposed = sandbox.inject({}); |
| | 4351 | |
| | 4352 | if (config.properties) { |
| | 4353 | for (var i = 0, l = config.properties.length; i < l; i++) { |
| | 4354 | prop = config.properties[i]; |
| | 4355 | value = exposed[prop] || prop == "sandbox" && sandbox; |
| | 4356 | exposeValue(sandbox, config, prop, value); |
| | 4357 | } |
| | 4358 | } else { |
| | 4359 | exposeValue(sandbox, config, "sandbox", value); |
| | 4360 | } |
| | 4361 | |
| | 4362 | return sandbox; |
| | 4363 | } |
| | 4364 | }); |
| | 4365 | |
| | 4366 | sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer; |
| | 4367 | |
| | 4368 | if (typeof module !== 'undefined' && module.exports) { |
| | 4369 | module.exports = sinon.sandbox; |
| | 4370 | } |
| | 4371 | }()); |
| | 4372 | |
| | 4373 | /** |
| | 4374 | * @depend ../sinon.js |
| | 4375 | * @depend stub.js |
| | 4376 | * @depend mock.js |
| | 4377 | * @depend sandbox.js |
| | 4378 | */ |
| | 4379 | /*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/ |
| | 4380 | /*global module, require, sinon*/ |
| | 4381 | /** |
| | 4382 | * Test function, sandboxes fakes |
| | 4383 | * |
| | 4384 | * @author Christian Johansen (christian@cjohansen.no) |
| | 4385 | * @license BSD |
| | 4386 | * |
| | 4387 | * Copyright (c) 2010-2013 Christian Johansen |
| | 4388 | */ |
| | 4389 | |
| | 4390 | (function (sinon) { |
| | 4391 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 4392 | |
| | 4393 | if (!sinon && commonJSModule) { |
| | 4394 | sinon = require("../sinon"); |
| | 4395 | } |
| | 4396 | |
| | 4397 | if (!sinon) { |
| | 4398 | return; |
| | 4399 | } |
| | 4400 | |
| | 4401 | function test(callback) { |
| | 4402 | var type = typeof callback; |
| | 4403 | |
| | 4404 | if (type != "function") { |
| | 4405 | throw new TypeError("sinon.test needs to wrap a test function, got " + type); |
| | 4406 | } |
| | 4407 | |
| | 4408 | return function () { |
| | 4409 | var config = sinon.getConfig(sinon.config); |
| | 4410 | config.injectInto = config.injectIntoThis && this || config.injectInto; |
| | 4411 | var sandbox = sinon.sandbox.create(config); |
| | 4412 | var exception, result; |
| | 4413 | var args = Array.prototype.slice.call(arguments).concat(sandbox.args); |
| | 4414 | |
| | 4415 | try { |
| | 4416 | result = callback.apply(this, args); |
| | 4417 | } catch (e) { |
| | 4418 | exception = e; |
| | 4419 | } |
| | 4420 | |
| | 4421 | if (typeof exception !== "undefined") { |
| | 4422 | sandbox.restore(); |
| | 4423 | throw exception; |
| | 4424 | } |
| | 4425 | else { |
| | 4426 | sandbox.verifyAndRestore(); |
| | 4427 | } |
| | 4428 | |
| | 4429 | return result; |
| | 4430 | }; |
| | 4431 | } |
| | 4432 | |
| | 4433 | test.config = { |
| | 4434 | injectIntoThis: true, |
| | 4435 | injectInto: null, |
| | 4436 | properties: ["spy", "stub", "mock", "clock", "server", "requests"], |
| | 4437 | useFakeTimers: true, |
| | 4438 | useFakeServer: true |
| | 4439 | }; |
| | 4440 | |
| | 4441 | if (commonJSModule) { |
| | 4442 | module.exports = test; |
| | 4443 | } else { |
| | 4444 | sinon.test = test; |
| | 4445 | } |
| | 4446 | }(typeof sinon == "object" && sinon || null)); |
| | 4447 | |
| | 4448 | /** |
| | 4449 | * @depend ../sinon.js |
| | 4450 | * @depend test.js |
| | 4451 | */ |
| | 4452 | /*jslint eqeqeq: false, onevar: false, eqeqeq: false*/ |
| | 4453 | /*global module, require, sinon*/ |
| | 4454 | /** |
| | 4455 | * Test case, sandboxes all test functions |
| | 4456 | * |
| | 4457 | * @author Christian Johansen (christian@cjohansen.no) |
| | 4458 | * @license BSD |
| | 4459 | * |
| | 4460 | * Copyright (c) 2010-2013 Christian Johansen |
| | 4461 | */ |
| | 4462 | |
| | 4463 | (function (sinon) { |
| | 4464 | var commonJSModule = typeof module !== 'undefined' && module.exports; |
| | 4465 | |
| | 4466 | if (!sinon && commonJSModule) { |
| | 4467 | sinon = require("../sinon"); |
| | 4468 | } |
| | 4469 | |
| | 4470 | if (!sinon || !Object.prototype.hasOwnProperty) { |
| | 4471 | return; |
| | 4472 | } |
| | 4473 | |
| | 4474 | function createTest(property, setUp, tearDown) { |
| | 4475 | return function () { |
| | 4476 | if (setUp) { |
| | 4477 | setUp.apply(this, arguments); |
| | 4478 | } |
| | 4479 | |
| | 4480 | var exception, result; |
| | 4481 | |
| | 4482 | try { |
| | 4483 | result = property.apply(this, arguments); |
| | 4484 | } catch (e) { |
| | 4485 | exception = e; |
| | 4486 | } |
| | 4487 | |
| | 4488 | if (tearDown) { |
| | 4489 | tearDown.apply(this, arguments); |
| | 4490 | } |
| | 4491 | |
| | 4492 | if (exception) { |
| | 4493 | throw exception; |
| | 4494 | } |
| | 4495 | |
| | 4496 | return result; |
| | 4497 | }; |
| | 4498 | } |
| | 4499 | |
| | 4500 | function testCase(tests, prefix) { |
| | 4501 | /*jsl:ignore*/ |
| | 4502 | if (!tests || typeof tests != "object") { |
| | 4503 | throw new TypeError("sinon.testCase needs an object with test functions"); |
| | 4504 | } |
| | 4505 | /*jsl:end*/ |
| | 4506 | |
| | 4507 | prefix = prefix || "test"; |
| | 4508 | var rPrefix = new RegExp("^" + prefix); |
| | 4509 | var methods = {}, testName, property, method; |
| | 4510 | var setUp = tests.setUp; |
| | 4511 | var tearDown = tests.tearDown; |
| | 4512 | |
| | 4513 | for (testName in tests) { |
| | 4514 | if (tests.hasOwnProperty(testName)) { |
| | 4515 | property = tests[testName]; |
| | 4516 | |
| | 4517 | if (/^(setUp|tearDown)$/.test(testName)) { |
| | 4518 | continue; |
| | 4519 | } |
| | 4520 | |
| | 4521 | if (typeof property == "function" && rPrefix.test(testName)) { |
| | 4522 | method = property; |
| | 4523 | |
| | 4524 | if (setUp || tearDown) { |
| | 4525 | method = createTest(property, setUp, tearDown); |
| | 4526 | } |
| | 4527 | |
| | 4528 | methods[testName] = sinon.test(method); |
| | 4529 | } else { |
| | 4530 | methods[testName] = tests[testName]; |
| | 4531 | } |
| | 4532 | } |
| | 4533 | } |
| | 4534 | |
| | 4535 | return methods; |
| | 4536 | } |
| | 4537 | |
| | 4538 | if (commonJSModule) { |
| | 4539 | module.exports = testCase; |
| | 4540 | } else { |
| | 4541 | sinon.testCase = testCase; |
| | 4542 | } |
| | 4543 | }(typeof sinon == "object" && sinon || null)); |
| | 4544 | |
| | 4545 | /** |
| | 4546 | * @depend ../sinon.js |
| | 4547 | * @depend stub.js |
| | 4548 | */ |
| | 4549 | /*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/ |
| | 4550 | /*global module, require, sinon*/ |
| | 4551 | /** |
| | 4552 | * Assertions matching the test spy retrieval interface. |
| | 4553 | * |
| | 4554 | * @author Christian Johansen (christian@cjohansen.no) |
| | 4555 | * @license BSD |
| | 4556 | * |
| | 4557 | * Copyright (c) 2010-2013 Christian Johansen |
| | 4558 | */ |
| | 4559 | |
| | 4560 | (function (sinon, global) { |
| | 4561 | var commonJSModule = typeof module !== "undefined" && module.exports; |
| | 4562 | var slice = Array.prototype.slice; |
| | 4563 | var assert; |
| | 4564 | |
| | 4565 | if (!sinon && commonJSModule) { |
| | 4566 | sinon = require("../sinon"); |
| | 4567 | } |
| | 4568 | |
| | 4569 | if (!sinon) { |
| | 4570 | return; |
| | 4571 | } |
| | 4572 | |
| | 4573 | function verifyIsStub() { |
| | 4574 | var method; |
| | 4575 | |
| | 4576 | for (var i = 0, l = arguments.length; i < l; ++i) { |
| | 4577 | method = arguments[i]; |
| | 4578 | |
| | 4579 | if (!method) { |
| | 4580 | assert.fail("fake is not a spy"); |
| | 4581 | } |
| | 4582 | |
| | 4583 | if (typeof method != "function") { |
| | 4584 | assert.fail(method + " is not a function"); |
| | 4585 | } |
| | 4586 | |
| | 4587 | if (typeof method.getCall != "function") { |
| | 4588 | assert.fail(method + " is not stubbed"); |
| | 4589 | } |
| | 4590 | } |
| | 4591 | } |
| | 4592 | |
| | 4593 | function failAssertion(object, msg) { |
| | 4594 | object = object || global; |
| | 4595 | var failMethod = object.fail || assert.fail; |
| | 4596 | failMethod.call(object, msg); |
| | 4597 | } |
| | 4598 | |
| | 4599 | function mirrorPropAsAssertion(name, method, message) { |
| | 4600 | if (arguments.length == 2) { |
| | 4601 | message = method; |
| | 4602 | method = name; |
| | 4603 | } |
| | 4604 | |
| | 4605 | assert[name] = function (fake) { |
| | 4606 | verifyIsStub(fake); |
| | 4607 | |
| | 4608 | var args = slice.call(arguments, 1); |
| | 4609 | var failed = false; |
| | 4610 | |
| | 4611 | if (typeof method == "function") { |
| | 4612 | failed = !method(fake); |
| | 4613 | } else { |
| | 4614 | failed = typeof fake[method] == "function" ? |
| | 4615 | !fake[method].apply(fake, args) : !fake[method]; |
| | 4616 | } |
| | 4617 | |
| | 4618 | if (failed) { |
| | 4619 | failAssertion(this, fake.printf.apply(fake, [message].concat(args))); |
| | 4620 | } else { |
| | 4621 | assert.pass(name); |
| | 4622 | } |
| | 4623 | }; |
| | 4624 | } |
| | 4625 | |
| | 4626 | function exposedName(prefix, prop) { |
| | 4627 | return !prefix || /^fail/.test(prop) ? prop : |
| | 4628 | prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1); |
| | 4629 | } |
| | 4630 | |
| | 4631 | assert = { |
| | 4632 | failException: "AssertError", |
| | 4633 | |
| | 4634 | fail: function fail(message) { |
| | 4635 | var error = new Error(message); |
| | 4636 | error.name = this.failException || assert.failException; |
| | 4637 | |
| | 4638 | throw error; |
| | 4639 | }, |
| | 4640 | |
| | 4641 | pass: function pass(assertion) {}, |
| | 4642 | |
| | 4643 | callOrder: function assertCallOrder() { |
| | 4644 | verifyIsStub.apply(null, arguments); |
| | 4645 | var expected = "", actual = ""; |
| | 4646 | |
| | 4647 | if (!sinon.calledInOrder(arguments)) { |
| | 4648 | try { |
| | 4649 | expected = [].join.call(arguments, ", "); |
| | 4650 | var calls = slice.call(arguments); |
| | 4651 | var i = calls.length; |
| | 4652 | while (i) { |
| | 4653 | if (!calls[--i].called) { |
| | 4654 | calls.splice(i, 1); |
| | 4655 | } |
| | 4656 | } |
| | 4657 | actual = sinon.orderByFirstCall(calls).join(", "); |
| | 4658 | } catch (e) { |
| | 4659 | // If this fails, we'll just fall back to the blank string |
| | 4660 | } |
| | 4661 | |
| | 4662 | failAssertion(this, "expected " + expected + " to be " + |
| | 4663 | "called in order but were called as " + actual); |
| | 4664 | } else { |
| | 4665 | assert.pass("callOrder"); |
| | 4666 | } |
| | 4667 | }, |
| | 4668 | |
| | 4669 | callCount: function assertCallCount(method, count) { |
| | 4670 | verifyIsStub(method); |
| | 4671 | |
| | 4672 | if (method.callCount != count) { |
| | 4673 | var msg = "expected %n to be called " + sinon.timesInWords(count) + |
| | 4674 | " but was called %c%C"; |
| | 4675 | failAssertion(this, method.printf(msg)); |
| | 4676 | } else { |
| | 4677 | assert.pass("callCount"); |
| | 4678 | } |
| | 4679 | }, |
| | 4680 | |
| | 4681 | expose: function expose(target, options) { |
| | 4682 | if (!target) { |
| | 4683 | throw new TypeError("target is null or undefined"); |
| | 4684 | } |
| | 4685 | |
| | 4686 | var o = options || {}; |
| | 4687 | var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix; |
| | 4688 | var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail; |
| | 4689 | |
| | 4690 | for (var method in this) { |
| | 4691 | if (method != "export" && (includeFail || !/^(fail)/.test(method))) { |
| | 4692 | target[exposedName(prefix, method)] = this[method]; |
| | 4693 | } |
| | 4694 | } |
| | 4695 | |
| | 4696 | return target; |
| | 4697 | } |
| | 4698 | }; |
| | 4699 | |
| | 4700 | mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called"); |
| | 4701 | mirrorPropAsAssertion("notCalled", function (spy) { return !spy.called; }, |
| | 4702 | "expected %n to not have been called but was called %c%C"); |
| | 4703 | mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C"); |
| | 4704 | mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C"); |
| | 4705 | mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C"); |
| | 4706 | mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t"); |
| | 4707 | mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t"); |
| | 4708 | mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new"); |
| | 4709 | mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new"); |
| | 4710 | mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C"); |
| | 4711 | mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C"); |
| | 4712 | mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C"); |
| | 4713 | mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C"); |
| | 4714 | mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C"); |
| | 4715 | mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C"); |
| | 4716 | mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C"); |
| | 4717 | mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C"); |
| | 4718 | mirrorPropAsAssertion("threw", "%n did not throw exception%C"); |
| | 4719 | mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C"); |
| | 4720 | |
| | 4721 | if (commonJSModule) { |
| | 4722 | module.exports = assert; |
| | 4723 | } else { |
| | 4724 | sinon.assert = assert; |
| | 4725 | } |
| | 4726 | }(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : (typeof self != "undefined") ? self : global)); |
| | 4727 | |
| | 4728 | return sinon;}.call(typeof window != 'undefined' && window || {})); |