unittests/sinon.js
author hamidouk
Mon, 19 Dec 2011 15:25:22 +0100
branchpopcorn-port
changeset 481 a46cfeee6d77
parent 58 f92e6c4baae9
permissions -rw-r--r--
using jquery ui draggable changes the state of an element from absolute to relative positioning, which breaks the way our seek button expands itself, so we need to force absolute positioning, quite uglily, using jquery.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     1
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     2
 * Sinon.JS 1.2.0, 2011/09/27
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     3
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     4
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     5
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     6
 * (The BSD License)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     7
 * 
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     8
 * Copyright (c) 2010-2011, Christian Johansen, christian@cjohansen.no
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
     9
 * All rights reserved.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    10
 * 
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    11
 * Redistribution and use in source and binary forms, with or without modification,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    12
 * are permitted provided that the following conditions are met:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    13
 * 
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    14
 *     * Redistributions of source code must retain the above copyright notice,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    15
 *       this list of conditions and the following disclaimer.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    16
 *     * Redistributions in binary form must reproduce the above copyright notice,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    17
 *       this list of conditions and the following disclaimer in the documentation
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    18
 *       and/or other materials provided with the distribution.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    19
 *     * Neither the name of Christian Johansen nor the names of his contributors
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    20
 *       may be used to endorse or promote products derived from this software
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    21
 *       without specific prior written permission.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    22
 * 
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    25
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    26
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    29
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    30
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    31
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    32
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    33
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    34
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    35
"use strict";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    36
/*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    37
/*global module, require, __dirname, document*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    38
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    39
 * Sinon core utilities. For internal use only.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    40
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    41
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    42
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    43
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    44
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    45
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    46
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    47
var sinon = (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    48
    var div = typeof document != "undefined" && document.createElement("div");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    49
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    50
    function isNode(obj) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    51
        var success = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    52
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    53
        try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    54
            obj.appendChild(div);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    55
            success = div.parentNode == obj;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    56
        } catch (e) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    57
            return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    58
        } finally {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    59
            try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    60
                obj.removeChild(div);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    61
            } catch (e) {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    62
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    63
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    64
        return success;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    65
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    66
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    67
    function isElement(obj) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    68
        return div && obj && obj.nodeType === 1 && isNode(obj);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    69
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    70
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    71
    return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    72
        wrapMethod: function wrapMethod(object, property, method) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    73
            if (!object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    74
                throw new TypeError("Should wrap property of object");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    75
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    76
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    77
            if (typeof method != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    78
                throw new TypeError("Method wrapper should be function");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    79
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    80
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    81
            var wrappedMethod = object[property];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    82
            var type = typeof wrappedMethod;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    83
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    84
            if (type != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    85
                throw new TypeError("Attempted to wrap " + type + " property " + property +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    86
                                    " as function");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    87
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    88
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    89
            if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    90
                throw new TypeError("Attempted to wrap " + property + " which is already wrapped");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    91
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    92
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    93
            if (wrappedMethod.calledBefore) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    94
                var verb = !!wrappedMethod.returns ? "stubbed" : "spied on";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    95
                throw new TypeError("Attempted to wrap " + property + " which is already " + verb);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    96
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    97
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    98
            var owned = object.hasOwnProperty(property);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
    99
            object[property] = method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   100
            method.displayName = property;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   101
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   102
            method.restore = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   103
                if(owned) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   104
                    object[property] = wrappedMethod;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   105
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   106
                    delete object[property];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   107
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   108
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   109
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   110
            method.restore.sinon = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   111
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   112
            return method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   113
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   114
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   115
        extend: function extend(target) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   116
            for (var i = 1, l = arguments.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   117
                for (var prop in arguments[i]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   118
                    if (arguments[i].hasOwnProperty(prop)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   119
                        target[prop] = arguments[i][prop];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   120
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   121
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   122
                    // DONT ENUM bug, only care about toString
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   123
                    if (arguments[i].hasOwnProperty("toString") &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   124
                        arguments[i].toString != target.toString) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   125
                        target.toString = arguments[i].toString;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   126
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   127
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   128
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   129
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   130
            return target;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   131
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   132
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   133
        create: function create(proto) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   134
            var F = function () {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   135
            F.prototype = proto;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   136
            return new F();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   137
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   138
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   139
        deepEqual: function deepEqual(a, b) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   140
            if (typeof a != "object" || typeof b != "object") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   141
                return a === b;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   142
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   143
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   144
            if (isElement(a) || isElement(b)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   145
                return a === b;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   146
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   147
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   148
            if (a === b) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   149
                return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   150
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   151
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   152
            if (Object.prototype.toString.call(a) == "[object Array]") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   153
                if (a.length !== b.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   154
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   155
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   156
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   157
                for (var i = 0, l = a.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   158
                    if (!deepEqual(a[i], b[i])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   159
                        return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   160
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   161
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   162
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   163
                return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   164
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   165
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   166
            var prop, aLength = 0, bLength = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   167
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   168
            for (prop in a) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   169
                aLength += 1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   170
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   171
                if (!deepEqual(a[prop], b[prop])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   172
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   173
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   174
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   175
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   176
            for (prop in b) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   177
                bLength += 1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   178
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   179
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   180
            if (aLength != bLength) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   181
                return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   182
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   183
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   184
            return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   185
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   186
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   187
        functionName: function functionName(func) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   188
            var name = func.displayName || func.name;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   189
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   190
            // Use function decomposition as a last resort to get function
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   191
            // name. Does not rely on function decomposition to work - if it
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   192
            // doesn't debugging will be slightly less informative
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   193
            // (i.e. toString will say 'spy' rather than 'myFunc').
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   194
            if (!name) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   195
                var matches = func.toString().match(/function ([^\s\(]+)/);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   196
                name = matches && matches[1];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   197
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   198
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   199
            return name;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   200
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   201
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   202
        functionToString: function toString() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   203
            if (this.getCall && this.callCount) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   204
                var thisValue, prop, i = this.callCount;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   205
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   206
                while (i--) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   207
                    thisValue = this.getCall(i).thisValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   208
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   209
                    for (prop in thisValue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   210
                        if (thisValue[prop] === this) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   211
                            return prop;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   212
                        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   213
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   214
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   215
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   216
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   217
            return this.displayName || "sinon fake";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   218
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   219
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   220
        getConfig: function (custom) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   221
            var config = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   222
            custom = custom || {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   223
            var defaults = sinon.defaultConfig;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   224
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   225
            for (var prop in defaults) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   226
                if (defaults.hasOwnProperty(prop)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   227
                    config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   228
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   229
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   230
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   231
            return config;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   232
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   233
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   234
        format: function (val) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   235
            return "" + val;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   236
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   237
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   238
        defaultConfig: {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   239
            injectIntoThis: true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   240
            injectInto: null,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   241
            properties: ["spy", "stub", "mock", "clock", "server", "requests"],
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   242
            useFakeTimers: true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   243
            useFakeServer: true
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   244
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   245
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   246
        timesInWords: function timesInWords(count) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   247
            return count == 1 && "once" ||
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   248
                count == 2 && "twice" ||
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   249
                count == 3 && "thrice" ||
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   250
                (count || 0) + " times";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   251
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   252
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   253
        calledInOrder: function (spies) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   254
            for (var i = 1, l = spies.length; i < l; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   255
                if (!spies[i - 1].calledBefore(spies[i])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   256
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   257
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   258
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   259
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   260
            return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   261
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   262
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   263
        orderByFirstCall: function (spies) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   264
            return spies.sort(function (a, b) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   265
                // uuid, won't ever be equal
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   266
                return a.getCall(0).callId < b.getCall(0).callId ? -1 : 1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   267
            });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   268
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   269
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   270
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   271
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   272
if (typeof module == "object" && typeof require == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   273
    module.exports = sinon;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   274
    module.exports.spy = require("./sinon/spy");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   275
    module.exports.stub = require("./sinon/stub");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   276
    module.exports.mock = require("./sinon/mock");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   277
    module.exports.collection = require("./sinon/collection");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   278
    module.exports.assert = require("./sinon/assert");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   279
    module.exports.sandbox = require("./sinon/sandbox");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   280
    module.exports.test = require("./sinon/test");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   281
    module.exports.testCase = require("./sinon/test_case");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   282
    module.exports.assert = require("./sinon/assert");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   283
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   284
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   285
/* @depend ../sinon.js */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   286
/*jslint eqeqeq: false, onevar: false, plusplus: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   287
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   288
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   289
 * Spy functions
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   290
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   291
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   292
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   293
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   294
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   295
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   296
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   297
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   298
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   299
    var spyCall;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   300
    var callId = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   301
    var push = [].push;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   302
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   303
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   304
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   305
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   306
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   307
    if (!sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   308
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   309
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   310
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   311
    function spy(object, property) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   312
        if (!property && typeof object == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   313
            return spy.create(object);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   314
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   315
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   316
        if (!object || !property) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   317
            return spy.create(function () {});
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   318
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   319
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   320
        var method = object[property];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   321
        return sinon.wrapMethod(object, property, spy.create(method));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   322
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   323
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   324
    sinon.extend(spy, (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   325
        var slice = Array.prototype.slice;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   326
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   327
        function delegateToCalls(api, method, matchAny, actual, notCalled) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   328
            api[method] = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   329
                if (!this.called) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   330
                    return !!notCalled;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   331
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   332
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   333
                var currentCall;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   334
                var matches = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   335
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   336
                for (var i = 0, l = this.callCount; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   337
                    currentCall = this.getCall(i);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   338
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   339
                    if (currentCall[actual || method].apply(currentCall, arguments)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   340
                        matches += 1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   341
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   342
                        if (matchAny) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   343
                            return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   344
                        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   345
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   346
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   347
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   348
                return matches === this.callCount;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   349
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   350
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   351
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   352
        function matchingFake(fakes, args, strict) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   353
            if (!fakes) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   354
                return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   355
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   356
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   357
            var alen = args.length;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   358
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   359
            for (var i = 0, l = fakes.length; i < l; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   360
                if (fakes[i].matches(args, strict)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   361
                    return fakes[i];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   362
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   363
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   364
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   365
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   366
        var uuid = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   367
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   368
        // Public API
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   369
        var spyApi = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   370
            reset: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   371
                this.called = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   372
                this.calledOnce = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   373
                this.calledTwice = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   374
                this.calledThrice = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   375
                this.callCount = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   376
                this.args = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   377
                this.returnValues = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   378
                this.thisValues = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   379
                this.exceptions = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   380
                this.callIds = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   381
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   382
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   383
            create: function create(func) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   384
                var name;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   385
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   386
                if (typeof func != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   387
                    func = function () {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   388
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   389
                    name = sinon.functionName(func);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   390
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   391
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   392
                function proxy() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   393
                    return proxy.invoke(func, this, slice.call(arguments));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   394
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   395
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   396
                sinon.extend(proxy, spy);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   397
                delete proxy.create;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   398
                sinon.extend(proxy, func);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   399
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   400
                proxy.reset();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   401
                proxy.prototype = func.prototype;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   402
                proxy.displayName = name || "spy";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   403
                proxy.toString = sinon.functionToString;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   404
                proxy._create = sinon.spy.create;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   405
                proxy.id = "spy#" + uuid++;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   406
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   407
                return proxy;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   408
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   409
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   410
            invoke: function invoke(func, thisValue, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   411
                var matching = matchingFake(this.fakes, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   412
                var exception, returnValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   413
                this.called = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   414
                this.callCount += 1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   415
                this.calledOnce = this.callCount == 1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   416
                this.calledTwice = this.callCount == 2;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   417
                this.calledThrice = this.callCount == 3;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   418
                push.call(this.thisValues, thisValue);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   419
                push.call(this.args, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   420
                push.call(this.callIds, callId++);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   421
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   422
                try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   423
                    if (matching) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   424
                        returnValue = matching.invoke(func, thisValue, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   425
                    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   426
                        returnValue = (this.func || func).apply(thisValue, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   427
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   428
                } catch (e) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   429
                    push.call(this.returnValues, undefined);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   430
                    exception = e;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   431
                    throw e;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   432
                } finally {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   433
                    push.call(this.exceptions, exception);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   434
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   435
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   436
                push.call(this.returnValues, returnValue);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   437
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   438
                return returnValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   439
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   440
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   441
            getCall: function getCall(i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   442
                if (i < 0 || i >= this.callCount) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   443
                    return null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   444
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   445
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   446
                return spyCall.create(this, this.thisValues[i], this.args[i],
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   447
                                      this.returnValues[i], this.exceptions[i],
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   448
                                      this.callIds[i]);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   449
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   450
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   451
            calledBefore: function calledBefore(spyFn) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   452
                if (!this.called) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   453
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   454
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   455
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   456
                if (!spyFn.called) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   457
                    return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   458
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   459
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   460
                return this.callIds[0] < spyFn.callIds[0];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   461
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   462
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   463
            calledAfter: function calledAfter(spyFn) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   464
                if (!this.called || !spyFn.called) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   465
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   466
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   467
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   468
                return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   469
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   470
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   471
            withArgs: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   472
                var args = slice.call(arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   473
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   474
                if (this.fakes) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   475
                    var match = matchingFake(this.fakes, args, true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   476
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   477
                    if (match) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   478
                        return match;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   479
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   480
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   481
                    this.fakes = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   482
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   483
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   484
                var original = this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   485
                var fake = this._create();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   486
                fake.matchingAguments = args;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   487
                push.call(this.fakes, fake);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   488
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   489
                fake.withArgs = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   490
                    return original.withArgs.apply(original, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   491
                };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   492
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   493
                return fake;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   494
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   495
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   496
            matches: function (args, strict) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   497
                var margs = this.matchingAguments;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   498
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   499
                if (margs.length <= args.length &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   500
                    sinon.deepEqual(margs, args.slice(0, margs.length))) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   501
                    return !strict || margs.length == args.length;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   502
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   503
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   504
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   505
            printf: function (format) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   506
                var spy = this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   507
                var args = [].slice.call(arguments, 1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   508
                var formatter;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   509
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   510
                return (format || "").replace(/%(.)/g, function (match, specifyer) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   511
                    formatter = spyApi.formatters[specifyer];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   512
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   513
                    if (typeof formatter == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   514
                        return formatter.call(null, spy, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   515
                    } else if (!isNaN(parseInt(specifyer), 10)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   516
                        return sinon.format(args[specifyer - 1]);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   517
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   518
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   519
                    return "%" + specifyer;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   520
                });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   521
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   522
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   523
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   524
        delegateToCalls(spyApi, "calledOn", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   525
        delegateToCalls(spyApi, "alwaysCalledOn", false, "calledOn");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   526
        delegateToCalls(spyApi, "calledWith", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   527
        delegateToCalls(spyApi, "alwaysCalledWith", false, "calledWith");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   528
        delegateToCalls(spyApi, "calledWithExactly", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   529
        delegateToCalls(spyApi, "alwaysCalledWithExactly", false, "calledWithExactly");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   530
        delegateToCalls(spyApi, "neverCalledWith", false, "notCalledWith", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   531
        delegateToCalls(spyApi, "threw", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   532
        delegateToCalls(spyApi, "alwaysThrew", false, "threw");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   533
        delegateToCalls(spyApi, "returned", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   534
        delegateToCalls(spyApi, "alwaysReturned", false, "returned");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   535
        delegateToCalls(spyApi, "calledWithNew", true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   536
        delegateToCalls(spyApi, "alwaysCalledWithNew", false, "calledWithNew");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   537
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   538
        spyApi.formatters = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   539
            "c": function (spy) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   540
                return sinon.timesInWords(spy.callCount);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   541
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   542
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   543
            "n": function (spy) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   544
                return spy.toString();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   545
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   546
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   547
            "C": function (spy) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   548
                var calls = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   549
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   550
                for (var i = 0, l = spy.callCount; i < l; ++i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   551
                    push.call(calls, "    " + spy.getCall(i).toString());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   552
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   553
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   554
                return calls.length > 0 ? "\n" + calls.join("\n") : "";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   555
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   556
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   557
            "t": function (spy) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   558
                var objects = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   559
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   560
                for (var i = 0, l = spy.callCount; i < l; ++i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   561
                    push.call(objects, sinon.format(spy.thisValues[i]));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   562
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   563
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   564
                return objects.join(", ");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   565
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   566
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   567
            "*": function (spy, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   568
                return args.join(", ");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   569
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   570
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   571
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   572
        return spyApi;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   573
    }()));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   574
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   575
    spyCall = (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   576
        return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   577
            create: function create(spy, thisValue, args, returnValue, exception, id) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   578
                var proxyCall = sinon.create(spyCall);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   579
                delete proxyCall.create;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   580
                proxyCall.proxy = spy;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   581
                proxyCall.thisValue = thisValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   582
                proxyCall.args = args;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   583
                proxyCall.returnValue = returnValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   584
                proxyCall.exception = exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   585
                proxyCall.callId = typeof id == "number" && id || callId++;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   586
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   587
                return proxyCall;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   588
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   589
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   590
            calledOn: function calledOn(thisValue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   591
                return this.thisValue === thisValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   592
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   593
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   594
            calledWith: function calledWith() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   595
                for (var i = 0, l = arguments.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   596
                    if (!sinon.deepEqual(arguments[i], this.args[i])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   597
                        return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   598
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   599
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   600
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   601
                return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   602
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   603
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   604
            calledWithExactly: function calledWithExactly() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   605
                return arguments.length == this.args.length &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   606
                    this.calledWith.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   607
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   608
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   609
            notCalledWith: function notCalledWith() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   610
                for (var i = 0, l = arguments.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   611
                    if (!sinon.deepEqual(arguments[i], this.args[i])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   612
                        return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   613
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   614
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   615
                return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   616
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   617
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   618
            returned: function returned(value) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   619
                return this.returnValue === value;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   620
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   621
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   622
            threw: function threw(error) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   623
                if (typeof error == "undefined" || !this.exception) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   624
                    return !!this.exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   625
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   626
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   627
                if (typeof error == "string") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   628
                    return this.exception.name == error;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   629
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   630
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   631
                return this.exception === error;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   632
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   633
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   634
            calledWithNew: function calledWithNew(thisValue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   635
                return this.thisValue instanceof this.proxy;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   636
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   637
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   638
            calledBefore: function (other) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   639
                return this.callId < other.callId;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   640
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   641
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   642
            calledAfter: function (other) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   643
                return this.callId > other.callId;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   644
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   645
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   646
            toString: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   647
                var callStr = this.proxy.toString() + "(";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   648
                var args = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   649
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   650
                for (var i = 0, l = this.args.length; i < l; ++i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   651
                    push.call(args, sinon.format(this.args[i]));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   652
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   653
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   654
                callStr = callStr + args.join(", ") + ")";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   655
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   656
                if (typeof this.returnValue != "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   657
                    callStr += " => " + sinon.format(this.returnValue);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   658
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   659
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   660
                if (this.exception) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   661
                    callStr += " !" + this.exception.name;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   662
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   663
                    if (this.exception.message) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   664
                        callStr += "(" + this.exception.message + ")";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   665
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   666
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   667
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   668
                return callStr;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   669
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   670
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   671
    }());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   672
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   673
    spy.spyCall = spyCall;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   674
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   675
    // This steps outside the module sandbox and will be removed
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   676
    sinon.spyCall = spyCall;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   677
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   678
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   679
        module.exports = spy;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   680
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   681
        sinon.spy = spy;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   682
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   683
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   684
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   685
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   686
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   687
 * @depend spy.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   688
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   689
/*jslint eqeqeq: false, onevar: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   690
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   691
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   692
 * Stub functions
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   693
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   694
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   695
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   696
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   697
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   698
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   699
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   700
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   701
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   702
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   703
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   704
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   705
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   706
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   707
    if (!sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   708
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   709
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   710
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   711
    function stub(object, property, func) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   712
        if (!!func && typeof func != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   713
            throw new TypeError("Custom stub should be function");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   714
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   715
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   716
        var wrapper;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   717
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   718
        if (func) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   719
            wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   720
        } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   721
            wrapper = stub.create();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   722
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   723
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   724
        if (!object && !property) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   725
            return sinon.stub.create();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   726
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   727
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   728
        if (!property && !!object && typeof object == "object") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   729
            for (var prop in object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   730
                if (object.hasOwnProperty(prop) && typeof object[prop] == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   731
                    stub(object, prop);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   732
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   733
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   734
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   735
            return object;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   736
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   737
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   738
        return sinon.wrapMethod(object, property, wrapper);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   739
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   740
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   741
    function getCallback(stub, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   742
        if (stub.callArgAt < 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   743
            for (var i = 0, l = args.length; i < l; ++i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   744
                if (!stub.callArgProp && typeof args[i] == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   745
                    return args[i];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   746
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   747
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   748
                if (stub.callArgProp && args[i] &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   749
                    typeof args[i][stub.callArgProp] == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   750
                    return args[i][stub.callArgProp];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   751
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   752
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   753
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   754
            return null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   755
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   756
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   757
        return args[stub.callArgAt];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   758
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   759
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   760
    var join = Array.prototype.join;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   761
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   762
    function getCallbackError(stub, func, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   763
        if (stub.callArgAt < 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   764
            var msg;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   765
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   766
            if (stub.callArgProp) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   767
                msg = sinon.functionName(stub) +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   768
                    " expected to yield to '" + stub.callArgProp +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   769
                    "', but no object with such a property was passed."
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   770
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   771
                msg = sinon.functionName(stub) +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   772
                            " expected to yield, but no callback was passed."
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   773
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   774
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   775
            if (args.length > 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   776
                msg += " Received [" + join.call(args, ", ") + "]";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   777
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   778
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   779
            return msg;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   780
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   781
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   782
        return "argument at index " + stub.callArgAt + " is not a function: " + func;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   783
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   784
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   785
    function callCallback(stub, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   786
        if (typeof stub.callArgAt == "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   787
            var func = getCallback(stub, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   788
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   789
            if (typeof func != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   790
                throw new TypeError(getCallbackError(stub, func, args));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   791
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   792
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   793
            func.apply(null, stub.callbackArguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   794
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   795
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   796
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   797
    var uuid = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   798
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   799
    sinon.extend(stub, (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   800
        var slice = Array.prototype.slice;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   801
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   802
        function throwsException(error, message) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   803
            if (typeof error == "string") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   804
                this.exception = new Error(message || "");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   805
                this.exception.name = error;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   806
            } else if (!error) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   807
                this.exception = new Error("Error");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   808
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   809
                this.exception = error;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   810
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   811
            
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   812
            return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   813
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   814
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   815
        return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   816
            create: function create() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   817
                var functionStub = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   818
                    if (functionStub.exception) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   819
                        throw functionStub.exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   820
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   821
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   822
                    callCallback(functionStub, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   823
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   824
                    return functionStub.returnValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   825
                };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   826
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   827
                functionStub.id = "stub#" + uuid++;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   828
                var orig = functionStub;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   829
                functionStub = sinon.spy.create(functionStub);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   830
                functionStub.func = orig;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   831
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   832
                sinon.extend(functionStub, stub);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   833
                functionStub._create = sinon.stub.create;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   834
                functionStub.displayName = "stub";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   835
                functionStub.toString = sinon.functionToString;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   836
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   837
                return functionStub;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   838
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   839
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   840
            returns: function returns(value) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   841
                this.returnValue = value;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   842
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   843
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   844
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   845
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   846
            "throws": throwsException,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   847
            throwsException: throwsException,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   848
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   849
            callsArg: function callsArg(pos) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   850
                if (typeof pos != "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   851
                    throw new TypeError("argument index is not number");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   852
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   853
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   854
                this.callArgAt = pos;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   855
                this.callbackArguments = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   856
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   857
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   858
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   859
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   860
            callsArgWith: function callsArgWith(pos) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   861
                if (typeof pos != "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   862
                    throw new TypeError("argument index is not number");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   863
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   864
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   865
                this.callArgAt = pos;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   866
                this.callbackArguments = slice.call(arguments, 1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   867
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   868
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   869
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   870
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   871
            yields: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   872
                this.callArgAt = -1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   873
                this.callbackArguments = slice.call(arguments, 0);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   874
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   875
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   876
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   877
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   878
            yieldsTo: function (prop) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   879
                this.callArgAt = -1;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   880
                this.callArgProp = prop;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   881
                this.callbackArguments = slice.call(arguments, 1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   882
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   883
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   884
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   885
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   886
    }()));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   887
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   888
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   889
        module.exports = stub;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   890
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   891
        sinon.stub = stub;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   892
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   893
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   894
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   895
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   896
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   897
 * @depend stub.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   898
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   899
/*jslint eqeqeq: false, onevar: false, nomen: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   900
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   901
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   902
 * Mock functions.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   903
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   904
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   905
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   906
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   907
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   908
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   909
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   910
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   911
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   912
    var push = [].push;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   913
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   914
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   915
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   916
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   917
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   918
    if (!sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   919
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   920
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   921
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   922
    function mock(object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   923
        if (!object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   924
            return sinon.expectation.create("Anonymous mock");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   925
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   926
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   927
        return mock.create(object);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   928
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   929
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   930
    sinon.mock = mock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   931
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   932
    sinon.extend(mock, (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   933
        function each(collection, callback) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   934
            if (!collection) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   935
                return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   936
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   937
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   938
            for (var i = 0, l = collection.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   939
                callback(collection[i]);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   940
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   941
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   942
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   943
        return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   944
            create: function create(object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   945
                if (!object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   946
                    throw new TypeError("object is null");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   947
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   948
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   949
                var mockObject = sinon.extend({}, mock);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   950
                mockObject.object = object;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   951
                delete mockObject.create;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   952
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   953
                return mockObject;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   954
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   955
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   956
            expects: function expects(method) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   957
                if (!method) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   958
                    throw new TypeError("method is falsy");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   959
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   960
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   961
                if (!this.expectations) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   962
                    this.expectations = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   963
                    this.proxies = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   964
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   965
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   966
                if (!this.expectations[method]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   967
                    this.expectations[method] = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   968
                    var mockObject = this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   969
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   970
                    sinon.wrapMethod(this.object, method, function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   971
                        return mockObject.invokeMethod(method, this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   972
                    });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   973
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   974
                    push.call(this.proxies, method);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   975
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   976
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   977
                var expectation = sinon.expectation.create(method);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   978
                push.call(this.expectations[method], expectation);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   979
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   980
                return expectation;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   981
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   982
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   983
            restore: function restore() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   984
                var object = this.object;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   985
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   986
                each(this.proxies, function (proxy) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   987
                    if (typeof object[proxy].restore == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   988
                        object[proxy].restore();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   989
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   990
                });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   991
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   992
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   993
            verify: function verify() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   994
                var expectations = this.expectations || {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   995
                var messages = [], met = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   996
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   997
                each(this.proxies, function (proxy) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   998
                    each(expectations[proxy], function (expectation) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
   999
                        if (!expectation.met()) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1000
                            push.call(messages, expectation.toString());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1001
                        } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1002
                            push.call(met, expectation.toString());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1003
                        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1004
                    });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1005
                });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1006
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1007
                this.restore();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1008
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1009
                if (messages.length > 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1010
                    sinon.expectation.fail(messages.concat(met).join("\n"));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1011
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1012
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1013
                return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1014
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1015
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1016
            invokeMethod: function invokeMethod(method, thisValue, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1017
                var expectations = this.expectations && this.expectations[method];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1018
                var length = expectations && expectations.length || 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1019
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1020
                for (var i = 0; i < length; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1021
                    if (!expectations[i].met() &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1022
                        expectations[i].allowsCall(thisValue, args)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1023
                        return expectations[i].apply(thisValue, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1024
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1025
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1026
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1027
                var messages = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1028
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1029
                for (i = 0; i < length; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1030
                    push.call(messages, "    " + expectations[i].toString());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1031
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1032
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1033
                messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1034
                    proxy: method,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1035
                    args: args
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1036
                }));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1037
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1038
                sinon.expectation.fail(messages.join("\n"));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1039
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1040
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1041
    }()));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1042
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1043
    var times = sinon.timesInWords;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1044
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1045
    sinon.expectation = (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1046
        var slice = Array.prototype.slice;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1047
        var _invoke = sinon.spy.invoke;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1048
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1049
        function callCountInWords(callCount) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1050
            if (callCount == 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1051
                return "never called";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1052
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1053
                return "called " + times(callCount);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1054
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1055
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1056
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1057
        function expectedCallCountInWords(expectation) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1058
            var min = expectation.minCalls;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1059
            var max = expectation.maxCalls;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1060
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1061
            if (typeof min == "number" && typeof max == "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1062
                var str = times(min);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1063
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1064
                if (min != max) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1065
                    str = "at least " + str + " and at most " + times(max);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1066
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1067
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1068
                return str;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1069
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1070
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1071
            if (typeof min == "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1072
                return "at least " + times(min);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1073
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1074
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1075
            return "at most " + times(max);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1076
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1077
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1078
        function receivedMinCalls(expectation) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1079
            var hasMinLimit = typeof expectation.minCalls == "number";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1080
            return !hasMinLimit || expectation.callCount >= expectation.minCalls;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1081
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1082
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1083
        function receivedMaxCalls(expectation) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1084
            if (typeof expectation.maxCalls != "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1085
                return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1086
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1087
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1088
            return expectation.callCount == expectation.maxCalls;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1089
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1090
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1091
        return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1092
            minCalls: 1,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1093
            maxCalls: 1,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1094
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1095
            create: function create(methodName) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1096
                var expectation = sinon.extend(sinon.stub.create(), sinon.expectation);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1097
                delete expectation.create;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1098
                expectation.method = methodName;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1099
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1100
                return expectation;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1101
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1102
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1103
            invoke: function invoke(func, thisValue, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1104
                this.verifyCallAllowed(thisValue, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1105
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1106
                return _invoke.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1107
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1108
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1109
            atLeast: function atLeast(num) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1110
                if (typeof num != "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1111
                    throw new TypeError("'" + num + "' is not number");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1112
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1113
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1114
                if (!this.limitsSet) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1115
                    this.maxCalls = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1116
                    this.limitsSet = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1117
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1118
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1119
                this.minCalls = num;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1120
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1121
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1122
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1123
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1124
            atMost: function atMost(num) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1125
                if (typeof num != "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1126
                    throw new TypeError("'" + num + "' is not number");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1127
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1128
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1129
                if (!this.limitsSet) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1130
                    this.minCalls = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1131
                    this.limitsSet = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1132
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1133
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1134
                this.maxCalls = num;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1135
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1136
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1137
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1138
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1139
            never: function never() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1140
                return this.exactly(0);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1141
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1142
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1143
            once: function once() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1144
                return this.exactly(1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1145
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1146
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1147
            twice: function twice() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1148
                return this.exactly(2);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1149
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1150
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1151
            thrice: function thrice() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1152
                return this.exactly(3);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1153
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1154
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1155
            exactly: function exactly(num) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1156
                if (typeof num != "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1157
                    throw new TypeError("'" + num + "' is not a number");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1158
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1159
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1160
                this.atLeast(num);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1161
                return this.atMost(num);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1162
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1163
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1164
            met: function met() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1165
                return !this.failed && receivedMinCalls(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1166
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1167
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1168
            verifyCallAllowed: function verifyCallAllowed(thisValue, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1169
                if (receivedMaxCalls(this)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1170
                    this.failed = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1171
                    sinon.expectation.fail(this.method + " already called " + times(this.maxCalls));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1172
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1173
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1174
                if ("expectedThis" in this && this.expectedThis !== thisValue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1175
                    sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1176
                        this.expectedThis);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1177
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1178
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1179
                if (!("expectedArguments" in this)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1180
                    return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1181
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1182
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1183
                if (!args || args.length === 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1184
                    sinon.expectation.fail(this.method + " received no arguments, expected " +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1185
                        this.expectedArguments.join());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1186
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1187
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1188
                if (args.length < this.expectedArguments.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1189
                    sinon.expectation.fail(this.method + " received too few arguments (" + args.join() +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1190
                        "), expected " + this.expectedArguments.join());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1191
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1192
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1193
                if (this.expectsExactArgCount &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1194
                    args.length != this.expectedArguments.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1195
                    sinon.expectation.fail(this.method + " received too many arguments (" + args.join() +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1196
                        "), expected " + this.expectedArguments.join());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1197
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1198
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1199
                for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1200
                    if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1201
                        sinon.expectation.fail(this.method + " received wrong arguments (" + args.join() +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1202
                            "), expected " + this.expectedArguments.join());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1203
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1204
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1205
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1206
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1207
            allowsCall: function allowsCall(thisValue, args) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1208
                if (this.met()) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1209
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1210
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1211
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1212
                if ("expectedThis" in this && this.expectedThis !== thisValue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1213
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1214
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1215
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1216
                if (!("expectedArguments" in this)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1217
                    return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1218
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1219
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1220
                args = args || [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1221
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1222
                if (args.length < this.expectedArguments.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1223
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1224
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1225
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1226
                if (this.expectsExactArgCount &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1227
                    args.length != this.expectedArguments.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1228
                    return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1229
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1230
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1231
                for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1232
                    if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1233
                        return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1234
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1235
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1236
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1237
                return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1238
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1239
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1240
            withArgs: function withArgs() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1241
                this.expectedArguments = slice.call(arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1242
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1243
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1244
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1245
            withExactArgs: function withExactArgs() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1246
                this.withArgs.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1247
                this.expectsExactArgCount = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1248
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1249
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1250
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1251
            on: function on(thisValue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1252
                this.expectedThis = thisValue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1253
                return this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1254
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1255
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1256
            toString: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1257
                var args = (this.expectedArguments || []).slice();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1258
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1259
                if (!this.expectsExactArgCount) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1260
                    push.call(args, "[...]");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1261
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1262
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1263
                var callStr = sinon.spyCall.toString.call({
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1264
                    proxy: this.method, args: args
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1265
                });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1266
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1267
                var message = callStr.replace(", [...", "[, ...") + " " +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1268
                    expectedCallCountInWords(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1269
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1270
                if (this.met()) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1271
                    return "Expectation met: " + message;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1272
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1273
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1274
                return "Expected " + message + " (" +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1275
                    callCountInWords(this.callCount) + ")";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1276
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1277
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1278
            verify: function verify() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1279
                if (!this.met()) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1280
                    sinon.expectation.fail(this.toString());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1281
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1282
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1283
                return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1284
            },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1285
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1286
            fail: function (message) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1287
                var exception = new Error(message);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1288
                exception.name = "ExpectationError";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1289
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1290
                throw exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1291
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1292
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1293
    }());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1294
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1295
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1296
        module.exports = mock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1297
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1298
        sinon.mock = mock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1299
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1300
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1301
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1302
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1303
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1304
 * @depend stub.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1305
 * @depend mock.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1306
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1307
/*jslint eqeqeq: false, onevar: false, forin: true*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1308
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1309
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1310
 * Collections of stubs, spies and mocks.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1311
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1312
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1313
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1314
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1315
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1316
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1317
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1318
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1319
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1320
    var push = [].push;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1321
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1322
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1323
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1324
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1325
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1326
    if (!sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1327
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1328
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1329
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1330
    function getFakes(fakeCollection) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1331
        if (!fakeCollection.fakes) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1332
            fakeCollection.fakes = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1333
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1334
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1335
        return fakeCollection.fakes;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1336
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1337
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1338
    function each(fakeCollection, method) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1339
        var fakes = getFakes(fakeCollection);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1340
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1341
        for (var i = 0, l = fakes.length; i < l; i += 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1342
            if (typeof fakes[i][method] == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1343
                fakes[i][method]();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1344
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1345
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1346
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1347
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1348
    function compact(fakeCollection) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1349
        var fakes = getFakes(fakeCollection);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1350
        var i = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1351
        while (i < fakes.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1352
          fakes.splice(i, 1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1353
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1354
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1355
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1356
    var collection = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1357
        verify: function resolve() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1358
            each(this, "verify");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1359
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1360
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1361
        restore: function restore() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1362
            each(this, "restore");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1363
            compact(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1364
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1365
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1366
        verifyAndRestore: function verifyAndRestore() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1367
            var exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1368
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1369
            try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1370
                this.verify();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1371
            } catch (e) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1372
                exception = e;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1373
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1374
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1375
            this.restore();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1376
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1377
            if (exception) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1378
                throw exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1379
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1380
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1381
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1382
        add: function add(fake) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1383
            push.call(getFakes(this), fake);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1384
            return fake;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1385
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1386
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1387
        spy: function spy() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1388
            return this.add(sinon.spy.apply(sinon, arguments));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1389
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1390
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1391
        stub: function stub(object, property, value) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1392
            if (property) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1393
                var original = object[property];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1394
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1395
                if (typeof original != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1396
                    if (!object.hasOwnProperty(property)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1397
                        throw new TypeError("Cannot stub non-existent own property " + property);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1398
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1399
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1400
                    object[property] = value;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1401
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1402
                    return this.add({
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1403
                        restore: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1404
                            object[property] = original;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1405
                        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1406
                    });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1407
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1408
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1409
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1410
            return this.add(sinon.stub.apply(sinon, arguments));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1411
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1412
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1413
        mock: function mock() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1414
            return this.add(sinon.mock.apply(sinon, arguments));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1415
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1416
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1417
        inject: function inject(obj) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1418
            var col = this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1419
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1420
            obj.spy = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1421
                return col.spy.apply(col, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1422
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1423
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1424
            obj.stub = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1425
                return col.stub.apply(col, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1426
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1427
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1428
            obj.mock = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1429
                return col.mock.apply(col, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1430
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1431
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1432
            return obj;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1433
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1434
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1435
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1436
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1437
        module.exports = collection;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1438
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1439
        sinon.collection = collection;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1440
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1441
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1442
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1443
/*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1444
/*global module, require, window*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1445
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1446
 * Fake timer API
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1447
 * setTimeout
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1448
 * setInterval
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1449
 * clearTimeout
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1450
 * clearInterval
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1451
 * tick
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1452
 * reset
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1453
 * Date
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1454
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1455
 * Inspired by jsUnitMockTimeOut from JsUnit
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1456
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1457
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1458
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1459
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1460
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1461
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1462
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1463
if (typeof sinon == "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1464
    var sinon = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1465
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1466
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1467
sinon.clock = (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1468
    var id = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1469
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1470
    function addTimer(args, recurring) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1471
        if (args.length === 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1472
            throw new Error("Function requires at least 1 parameter");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1473
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1474
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1475
        var toId = id++;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1476
        var delay = args[1] || 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1477
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1478
        if (!this.timeouts) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1479
            this.timeouts = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1480
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1481
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1482
        this.timeouts[toId] = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1483
            id: toId,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1484
            func: args[0],
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1485
            callAt: this.now + delay
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1486
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1487
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1488
        if (recurring === true) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1489
            this.timeouts[toId].interval = delay;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1490
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1491
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1492
        return toId;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1493
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1494
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1495
    function parseTime(str) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1496
        if (!str) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1497
            return 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1498
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1499
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1500
        var strings = str.split(":");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1501
        var l = strings.length, i = l;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1502
        var ms = 0, parsed;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1503
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1504
        if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1505
            throw new Error("tick only understands numbers and 'h:m:s'");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1506
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1507
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1508
        while (i--) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1509
            parsed = parseInt(strings[i], 10);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1510
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1511
            if (parsed >= 60) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1512
                throw new Error("Invalid time " + str);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1513
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1514
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1515
            ms += parsed * Math.pow(60, (l - i - 1));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1516
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1517
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1518
        return ms * 1000;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1519
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1520
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1521
    function createObject(object) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1522
        var newObject;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1523
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1524
        if (Object.create) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1525
            newObject = Object.create(object);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1526
        } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1527
            var F = function () {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1528
            F.prototype = object;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1529
            newObject = new F();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1530
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1531
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1532
        newObject.Date.clock = newObject;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1533
        return newObject;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1534
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1535
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1536
    return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1537
        now: 0,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1538
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1539
        create: function create(now) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1540
            var clock = createObject(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1541
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1542
            if (typeof now == "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1543
                this.now = now;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1544
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1545
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1546
            return clock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1547
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1548
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1549
        setTimeout: function setTimeout(callback, timeout) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1550
            return addTimer.call(this, arguments, false);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1551
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1552
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1553
        clearTimeout: function clearTimeout(timerId) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1554
            if (!this.timeouts) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1555
                this.timeouts = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1556
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1557
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1558
            delete this.timeouts[timerId];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1559
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1560
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1561
        setInterval: function setInterval(callback, timeout) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1562
            return addTimer.call(this, arguments, true);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1563
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1564
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1565
        clearInterval: function clearInterval(timerId) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1566
            this.clearTimeout(timerId);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1567
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1568
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1569
        tick: function tick(ms) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1570
            ms = typeof ms == "number" ? ms : parseTime(ms);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1571
            var tickFrom = this.now, tickTo = this.now + ms, previous = this.now;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1572
            var timer = this.firstTimerInRange(tickFrom, tickTo);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1573
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1574
            while (timer && tickFrom <= tickTo) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1575
                if (this.timeouts[timer.id]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1576
                    tickFrom = this.now = timer.callAt;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1577
                    this.callTimer(timer);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1578
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1579
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1580
                timer = this.firstTimerInRange(previous, tickTo);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1581
                previous = tickFrom;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1582
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1583
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1584
            this.now = tickTo;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1585
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1586
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1587
        firstTimerInRange: function (from, to) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1588
            var timer, smallest, originalTimer;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1589
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1590
            for (var id in this.timeouts) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1591
                if (this.timeouts.hasOwnProperty(id)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1592
                    if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1593
                        continue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1594
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1595
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1596
                    if (!smallest || this.timeouts[id].callAt < smallest) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1597
                        originalTimer = this.timeouts[id];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1598
                        smallest = this.timeouts[id].callAt;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1599
                        
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1600
                        timer = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1601
                            func: this.timeouts[id].func,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1602
                            callAt: this.timeouts[id].callAt,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1603
                            interval: this.timeouts[id].interval,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1604
                            id: this.timeouts[id].id
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1605
                        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1606
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1607
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1608
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1609
            
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1610
            return timer || null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1611
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1612
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1613
        callTimer: function (timer) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1614
            try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1615
                if (typeof timer.func == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1616
                    timer.func.call(null);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1617
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1618
                    eval(timer.func);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1619
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1620
            } catch (e) {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1621
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1622
            if (!this.timeouts[timer.id]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1623
                return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1624
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1625
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1626
            if (typeof timer.interval == "number") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1627
                this.timeouts[timer.id].callAt += timer.interval;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1628
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1629
                delete this.timeouts[timer.id];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1630
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1631
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1632
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1633
        reset: function reset() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1634
            this.timeouts = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1635
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1636
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1637
        Date: (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1638
            var NativeDate = Date;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1639
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1640
            function ClockDate(year, month, date, hour, minute, second, ms) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1641
                // Defensive and verbose to avoid potential harm in passing
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1642
                // explicit undefined when user does not pass argument
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1643
                switch (arguments.length) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1644
                case 0:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1645
                    return new NativeDate(ClockDate.clock.now);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1646
                case 1:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1647
                    return new NativeDate(year);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1648
                case 2:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1649
                    return new NativeDate(year, month);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1650
                case 3:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1651
                    return new NativeDate(year, month, date);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1652
                case 4:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1653
                    return new NativeDate(year, month, date, hour);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1654
                case 5:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1655
                    return new NativeDate(year, month, date, hour, minute);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1656
                case 6:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1657
                    return new NativeDate(year, month, date, hour, minute, second);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1658
                default:
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1659
                    return new NativeDate(year, month, date, hour, minute, second, ms);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1660
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1661
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1662
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1663
            if (NativeDate.now) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1664
                ClockDate.now = function now() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1665
                    return ClockDate.clock.now;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1666
                };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1667
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1668
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1669
            if (NativeDate.toSource) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1670
                ClockDate.toSource = function toSource() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1671
                    return NativeDate.toSource();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1672
                };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1673
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1674
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1675
            ClockDate.toString = function toString() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1676
                return NativeDate.toString();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1677
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1678
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1679
            ClockDate.prototype = NativeDate.prototype;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1680
            ClockDate.parse = NativeDate.parse;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1681
            ClockDate.UTC = NativeDate.UTC;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1682
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1683
            return ClockDate;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1684
        }())
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1685
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1686
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1687
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1688
sinon.timers = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1689
    setTimeout: setTimeout,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1690
    clearTimeout: clearTimeout,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1691
    setInterval: setInterval,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1692
    clearInterval: clearInterval,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1693
    Date: Date
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1694
};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1695
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1696
sinon.useFakeTimers = (function (global) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1697
    var methods = ["Date", "setTimeout", "setInterval", "clearTimeout", "clearInterval"];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1698
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1699
    function restore() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1700
        var method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1701
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1702
        for (var i = 0, l = this.methods.length; i < l; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1703
            method = this.methods[i];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1704
            global[method] = this["_" + method];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1705
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1706
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1707
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1708
    function stubGlobal(method, clock) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1709
        clock["_" + method] = global[method];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1710
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1711
        global[method] = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1712
            return clock[method].apply(clock, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1713
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1714
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1715
        for (var prop in clock[method]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1716
            if (clock[method].hasOwnProperty(prop)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1717
                global[method][prop] = clock[method][prop];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1718
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1719
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1720
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1721
        global[method].clock = clock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1722
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1723
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1724
    return function useFakeTimers(now) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1725
        var clock = sinon.clock.create(now);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1726
        clock.restore = restore;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1727
        clock.methods = Array.prototype.slice.call(arguments,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1728
                                                   typeof now == "number" ? 1 : 0);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1729
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1730
        if (clock.methods.length === 0) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1731
            clock.methods = methods;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1732
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1733
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1734
        for (var i = 0, l = clock.methods.length; i < l; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1735
            stubGlobal(clock.methods[i], clock);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1736
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1737
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1738
        return clock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1739
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1740
}(typeof global != "undefined" ? global : this));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1741
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1742
if (typeof module == "object" && typeof require == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1743
    module.exports = sinon;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1744
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1745
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1746
/*jslint eqeqeq: false, onevar: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1747
/*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1748
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1749
 * Minimal Event interface implementation
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1750
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1751
 * Original implementation by Sven Fuchs: https://gist.github.com/995028
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1752
 * Modifications and tests by Christian Johansen.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1753
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1754
 * @author Sven Fuchs (svenfuchs@artweb-design.de)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1755
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1756
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1757
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1758
 * Copyright (c) 2011 Sven Fuchs, Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1759
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1760
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1761
if (typeof sinon == "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1762
    this.sinon = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1763
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1764
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1765
(function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1766
    var push = [].push;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1767
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1768
    sinon.Event = function Event(type, bubbles, cancelable) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1769
        this.initEvent(type, bubbles, cancelable);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1770
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1771
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1772
    sinon.Event.prototype = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1773
        initEvent: function(type, bubbles, cancelable) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1774
            this.type = type;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1775
            this.bubbles = bubbles;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1776
            this.cancelable = cancelable;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1777
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1778
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1779
        stopPropagation: function () {},
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1780
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1781
        preventDefault: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1782
            this.defaultPrevented = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1783
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1784
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1785
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1786
    sinon.EventTarget = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1787
        addEventListener: function addEventListener(event, listener, useCapture) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1788
            this.eventListeners = this.eventListeners || {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1789
            this.eventListeners[event] = this.eventListeners[event] || [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1790
            push.call(this.eventListeners[event], listener);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1791
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1792
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1793
        removeEventListener: function removeEventListener(event, listener, useCapture) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1794
            var listeners = this.eventListeners && this.eventListeners[event] || [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1795
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1796
            for (var i = 0, l = listeners.length; i < l; ++i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1797
                if (listeners[i] == listener) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1798
                    return listeners.splice(i, 1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1799
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1800
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1801
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1802
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1803
        dispatchEvent: function dispatchEvent(event) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1804
            var type = event.type;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1805
            var listeners = this.eventListeners && this.eventListeners[type] || [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1806
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1807
            for (var i = 0; i < listeners.length; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1808
                if (typeof listeners[i] == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1809
                    listeners[i].call(this, event);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1810
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1811
                    listeners[i].handleEvent(event);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1812
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1813
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1814
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1815
            return !!event.defaultPrevented;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1816
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1817
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1818
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1819
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1820
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1821
 * @depend event.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1822
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1823
/*jslint eqeqeq: false, onevar: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1824
/*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1825
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1826
 * Fake XMLHttpRequest object
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1827
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1828
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1829
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1830
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1831
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1832
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1833
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1834
if (typeof sinon == "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1835
    this.sinon = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1836
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1837
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1838
sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1839
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1840
sinon.FakeXMLHttpRequest = (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1841
    /*jsl:ignore*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1842
    var unsafeHeaders = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1843
        "Accept-Charset": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1844
        "Accept-Encoding": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1845
        "Connection": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1846
        "Content-Length": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1847
        "Cookie": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1848
        "Cookie2": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1849
        "Content-Transfer-Encoding": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1850
        "Date": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1851
        "Expect": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1852
        "Host": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1853
        "Keep-Alive": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1854
        "Referer": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1855
        "TE": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1856
        "Trailer": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1857
        "Transfer-Encoding": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1858
        "Upgrade": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1859
        "User-Agent": true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1860
        "Via": true
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1861
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1862
    /*jsl:end*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1863
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1864
    function FakeXMLHttpRequest() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1865
        this.readyState = FakeXMLHttpRequest.UNSENT;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1866
        this.requestHeaders = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1867
        this.requestBody = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1868
        this.status = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1869
        this.statusText = "";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1870
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1871
        if (typeof FakeXMLHttpRequest.onCreate == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1872
            FakeXMLHttpRequest.onCreate(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1873
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1874
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1875
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1876
    function verifyState(xhr) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1877
        if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1878
            throw new Error("INVALID_STATE_ERR");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1879
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1880
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1881
        if (xhr.sendFlag) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1882
            throw new Error("INVALID_STATE_ERR");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1883
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1884
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1885
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1886
    sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1887
        async: true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1888
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1889
        open: function open(method, url, async, username, password) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1890
            this.method = method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1891
            this.url = url;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1892
            this.async = typeof async == "boolean" ? async : true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1893
            this.username = username;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1894
            this.password = password;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1895
            this.responseText = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1896
            this.responseXML = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1897
            this.requestHeaders = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1898
            this.sendFlag = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1899
            this.readyStateChange(FakeXMLHttpRequest.OPENED);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1900
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1901
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1902
        readyStateChange: function readyStateChange(state) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1903
            this.readyState = state;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1904
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1905
            if (typeof this.onreadystatechange == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1906
                this.onreadystatechange();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1907
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1908
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1909
            this.dispatchEvent(new sinon.Event("readystatechange"));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1910
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1911
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1912
        setRequestHeader: function setRequestHeader(header, value) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1913
            verifyState(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1914
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1915
            if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1916
                throw new Error("Refused to set unsafe header \"" + header + "\"");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1917
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1918
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1919
            if (this.requestHeaders[header]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1920
                this.requestHeaders[header] += "," + value; 
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1921
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1922
                this.requestHeaders[header] = value;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1923
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1924
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1925
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1926
        // Helps testing
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1927
        setResponseHeaders: function setResponseHeaders(headers) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1928
            this.responseHeaders = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1929
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1930
            for (var header in headers) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1931
                if (headers.hasOwnProperty(header)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1932
                    this.responseHeaders[header] = headers[header];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1933
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1934
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1935
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1936
            if (this.async) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1937
                this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1938
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1939
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1940
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1941
        // Currently treats ALL data as a DOMString (i.e. no Document)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1942
        send: function send(data) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1943
            verifyState(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1944
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1945
            if (!/^(get|head)$/i.test(this.method)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1946
                if (this.requestHeaders["Content-Type"]) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1947
                    var value = this.requestHeaders["Content-Type"].split(";");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1948
                    this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1949
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1950
                    this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1951
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1952
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1953
                this.requestBody = data;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1954
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1955
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1956
            this.errorFlag = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1957
            this.sendFlag = this.async;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1958
            this.readyStateChange(FakeXMLHttpRequest.OPENED);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1959
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1960
            if (typeof this.onSend == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1961
                this.onSend(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1962
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1963
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1964
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1965
        abort: function abort() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1966
            this.aborted = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1967
            this.responseText = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1968
            this.errorFlag = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1969
            this.requestHeaders = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1970
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1971
            if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1972
                this.readyStateChange(sinon.FakeXMLHttpRequest.DONE);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1973
                this.sendFlag = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1974
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1975
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1976
            this.readyState = sinon.FakeXMLHttpRequest.UNSENT;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1977
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1978
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1979
        getResponseHeader: function getResponseHeader(header) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1980
            if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1981
                return null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1982
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1983
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1984
            if (/^Set-Cookie2?$/i.test(header)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1985
                return null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1986
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1987
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1988
            header = header.toLowerCase();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1989
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1990
            for (var h in this.responseHeaders) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1991
                if (h.toLowerCase() == header) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1992
                    return this.responseHeaders[h];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1993
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1994
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1995
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1996
            return null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1997
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1998
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  1999
        getAllResponseHeaders: function getAllResponseHeaders() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2000
            if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2001
                return "";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2002
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2003
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2004
            var headers = "";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2005
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2006
            for (var header in this.responseHeaders) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2007
                if (this.responseHeaders.hasOwnProperty(header) &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2008
                    !/^Set-Cookie2?$/i.test(header)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2009
                    headers += header + ": " + this.responseHeaders[header] + "\r\n";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2010
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2011
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2012
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2013
            return headers;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2014
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2015
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2016
        setResponseBody: function setResponseBody(body) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2017
            if (this.readyState == FakeXMLHttpRequest.DONE) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2018
                throw new Error("Request done");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2019
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2020
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2021
            if (this.async && this.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2022
                throw new Error("No headers received");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2023
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2024
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2025
            var chunkSize = this.chunkSize || 10;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2026
            var index = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2027
            this.responseText = "";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2028
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2029
            do {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2030
                if (this.async) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2031
                    this.readyStateChange(FakeXMLHttpRequest.LOADING);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2032
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2033
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2034
                this.responseText += body.substring(index, index + chunkSize);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2035
                index += chunkSize;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2036
            } while (index < body.length);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2037
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2038
            var type = this.getResponseHeader("Content-Type");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2039
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2040
            if (this.responseText &&
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2041
                (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2042
                try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2043
                    this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2044
                } catch (e) {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2045
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2046
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2047
            if (this.async) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2048
                this.readyStateChange(FakeXMLHttpRequest.DONE);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2049
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2050
                this.readyState = FakeXMLHttpRequest.DONE;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2051
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2052
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2053
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2054
        respond: function respond(status, headers, body) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2055
            this.setResponseHeaders(headers || {});
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2056
            this.status = typeof status == "number" ? status : 200;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2057
            this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2058
            this.setResponseBody(body || "");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2059
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2060
    });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2061
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2062
    sinon.extend(FakeXMLHttpRequest, {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2063
        UNSENT: 0,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2064
        OPENED: 1,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2065
        HEADERS_RECEIVED: 2,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2066
        LOADING: 3,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2067
        DONE: 4
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2068
    });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2069
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2070
    // Borrowed from JSpec
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2071
    FakeXMLHttpRequest.parseXML = function parseXML(text) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2072
        var xmlDoc;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2073
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2074
        if (typeof DOMParser != "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2075
            var parser = new DOMParser();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2076
            xmlDoc = parser.parseFromString(text, "text/xml");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2077
        } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2078
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2079
            xmlDoc.async = "false";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2080
            xmlDoc.loadXML(text);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2081
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2082
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2083
        return xmlDoc;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2084
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2085
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2086
    FakeXMLHttpRequest.statusCodes = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2087
        100: "Continue",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2088
        101: "Switching Protocols",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2089
        200: "OK",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2090
        201: "Created",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2091
        202: "Accepted",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2092
        203: "Non-Authoritative Information",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2093
        204: "No Content",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2094
        205: "Reset Content",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2095
        206: "Partial Content",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2096
        300: "Multiple Choice",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2097
        301: "Moved Permanently",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2098
        302: "Found",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2099
        303: "See Other",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2100
        304: "Not Modified",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2101
        305: "Use Proxy",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2102
        307: "Temporary Redirect",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2103
        400: "Bad Request",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2104
        401: "Unauthorized",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2105
        402: "Payment Required",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2106
        403: "Forbidden",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2107
        404: "Not Found",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2108
        405: "Method Not Allowed",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2109
        406: "Not Acceptable",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2110
        407: "Proxy Authentication Required",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2111
        408: "Request Timeout",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2112
        409: "Conflict",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2113
        410: "Gone",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2114
        411: "Length Required",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2115
        412: "Precondition Failed",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2116
        413: "Request Entity Too Large",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2117
        414: "Request-URI Too Long",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2118
        415: "Unsupported Media Type",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2119
        416: "Requested Range Not Satisfiable",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2120
        417: "Expectation Failed",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2121
        422: "Unprocessable Entity",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2122
        500: "Internal Server Error",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2123
        501: "Not Implemented",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2124
        502: "Bad Gateway",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2125
        503: "Service Unavailable",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2126
        504: "Gateway Timeout",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2127
        505: "HTTP Version Not Supported"
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2128
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2129
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2130
    return FakeXMLHttpRequest;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2131
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2132
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2133
(function (global) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2134
    var GlobalXMLHttpRequest = global.XMLHttpRequest;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2135
    var GlobalActiveXObject = global.ActiveXObject;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2136
    var supportsActiveX = typeof ActiveXObject != "undefined";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2137
    var supportsXHR = typeof XMLHttpRequest != "undefined";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2138
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2139
    sinon.useFakeXMLHttpRequest = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2140
        sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2141
            if (supportsXHR) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2142
                global.XMLHttpRequest = GlobalXMLHttpRequest;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2143
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2144
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2145
            if (supportsActiveX) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2146
                global.ActiveXObject = GlobalActiveXObject;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2147
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2148
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2149
            delete sinon.FakeXMLHttpRequest.restore;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2150
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2151
            if (keepOnCreate !== true) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2152
                delete sinon.FakeXMLHttpRequest.onCreate;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2153
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2154
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2155
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2156
        if (supportsXHR) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2157
            global.XMLHttpRequest = sinon.FakeXMLHttpRequest;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2158
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2159
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2160
        if (supportsActiveX) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2161
            global.ActiveXObject = function ActiveXObject(objId) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2162
                if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2163
                    return new sinon.FakeXMLHttpRequest();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2164
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2165
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2166
                return new GlobalActiveXObject(objId);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2167
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2168
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2169
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2170
        return sinon.FakeXMLHttpRequest;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2171
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2172
}(this));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2173
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2174
if (typeof module == "object" && typeof require == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2175
    module.exports = sinon;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2176
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2177
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2178
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2179
 * @depend fake_xml_http_request.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2180
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2181
/*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2182
/*global module, require, window*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2183
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2184
 * The Sinon "server" mimics a web server that receives requests from
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2185
 * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2186
 * both synchronously and asynchronously. To respond synchronuously, canned
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2187
 * answers have to be provided upfront.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2188
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2189
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2190
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2191
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2192
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2193
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2194
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2195
if (typeof sinon == "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2196
    var sinon = {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2197
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2198
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2199
sinon.fakeServer = (function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2200
    var push = [].push;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2201
    function F() {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2202
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2203
    function create(proto) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2204
        F.prototype = proto;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2205
        return new F();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2206
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2207
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2208
    function responseArray(handler) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2209
        var response = handler;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2210
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2211
        if (Object.prototype.toString.call(handler) != "[object Array]") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2212
            response = [200, {}, handler];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2213
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2214
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2215
        if (typeof response[2] != "string") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2216
            throw new TypeError("Fake server response body should be string, but was " +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2217
                                typeof response[2]);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2218
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2219
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2220
        return response;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2221
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2222
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2223
    var wloc = window.location;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2224
    var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2225
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2226
    function matchOne(response, reqMethod, reqUrl) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2227
        var rmeth = response.method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2228
        var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2229
        var url = response.url;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2230
        var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2231
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2232
        return matchMethod && matchUrl;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2233
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2234
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2235
    function match(response, request) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2236
        var requestMethod = this.getHTTPMethod(request);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2237
        var requestUrl = request.url;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2238
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2239
        if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2240
            requestUrl = requestUrl.replace(rCurrLoc, "");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2241
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2242
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2243
        if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2244
            if (typeof response.response == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2245
                var args = [request].concat(requestUrl.match(response.url).slice(1));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2246
                return response.response.apply(response, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2247
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2248
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2249
            return true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2250
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2251
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2252
        return false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2253
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2254
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2255
    return {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2256
        create: function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2257
            var server = create(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2258
            this.xhr = sinon.useFakeXMLHttpRequest();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2259
            server.requests = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2260
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2261
            this.xhr.onCreate = function (xhrObj) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2262
                server.addRequest(xhrObj);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2263
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2264
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2265
            return server;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2266
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2267
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2268
        addRequest: function addRequest(xhrObj) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2269
            var server = this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2270
            push.call(this.requests, xhrObj);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2271
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2272
            xhrObj.onSend = function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2273
                server.handleRequest(this);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2274
            };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2275
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2276
            if (this.autoRespond && !this.responding) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2277
                setTimeout(function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2278
                    server.responding = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2279
                    server.respond();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2280
                }, this.autoRespondAfter || 10);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2281
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2282
                this.responding = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2283
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2284
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2285
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2286
        getHTTPMethod: function getHTTPMethod(request) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2287
            if (this.fakeHTTPMethods && /post/i.test(request.method)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2288
                var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2289
                return !!matches ? matches[1] : request.method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2290
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2291
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2292
            return request.method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2293
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2294
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2295
        handleRequest: function handleRequest(xhr) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2296
            if (xhr.async) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2297
                if (!this.queue) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2298
                    this.queue = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2299
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2300
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2301
                push.call(this.queue, xhr);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2302
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2303
                this.processRequest(xhr);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2304
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2305
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2306
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2307
        respondWith: function respondWith(method, url, body) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2308
            if (arguments.length == 1) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2309
                this.response = responseArray(method);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2310
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2311
                if (!this.responses) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2312
                    this.responses = [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2313
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2314
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2315
                if (arguments.length == 2) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2316
                    body = url;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2317
                    url = method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2318
                    method = null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2319
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2320
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2321
                push.call(this.responses, {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2322
                    method: method,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2323
                    url: url,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2324
                    response: typeof body == "function" ? body : responseArray(body)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2325
                });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2326
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2327
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2328
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2329
        respond: function respond() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2330
            var queue = this.queue || [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2331
            var request;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2332
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2333
            while(request = queue.shift()) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2334
                this.processRequest(request);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2335
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2336
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2337
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2338
        processRequest: function processRequest(request) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2339
            try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2340
                if (request.aborted) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2341
                    return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2342
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2343
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2344
                var response = this.response || [404, {}, ""];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2345
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2346
                if (this.responses) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2347
                    for (var i = 0, l = this.responses.length; i < l; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2348
                        if (match.call(this, this.responses[i], request)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2349
                            response = this.responses[i].response;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2350
                            break;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2351
                        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2352
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2353
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2354
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2355
                if (request.readyState != 4) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2356
                    request.respond(response[0], response[1], response[2]);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2357
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2358
            } catch (e) {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2359
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2360
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2361
        restore: function restore() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2362
            return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2363
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2364
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2365
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2366
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2367
if (typeof module == "object" && typeof require == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2368
    module.exports = sinon;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2369
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2370
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2371
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2372
 * @depend fake_server.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2373
 * @depend fake_timers.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2374
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2375
/*jslint browser: true, eqeqeq: false, onevar: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2376
/*global sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2377
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2378
 * Add-on for sinon.fakeServer that automatically handles a fake timer along with
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2379
 * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2380
 * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2381
 * it polls the object for completion with setInterval. Dispite the direct
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2382
 * motivation, there is nothing jQuery-specific in this file, so it can be used
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2383
 * in any environment where the ajax implementation depends on setInterval or
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2384
 * setTimeout.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2385
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2386
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2387
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2388
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2389
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2390
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2391
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2392
(function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2393
    function Server() {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2394
    Server.prototype = sinon.fakeServer;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2395
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2396
    sinon.fakeServerWithClock = new Server();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2397
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2398
    sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2399
        if (xhr.async) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2400
            if (typeof setTimeout.clock == "object") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2401
                this.clock = setTimeout.clock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2402
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2403
                this.clock = sinon.useFakeTimers();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2404
                this.resetClock = true;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2405
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2406
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2407
            if (!this.longestTimeout) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2408
                var clockSetTimeout = this.clock.setTimeout;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2409
                var clockSetInterval = this.clock.setInterval;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2410
                var server = this;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2411
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2412
                this.clock.setTimeout = function (fn, timeout) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2413
                    server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2414
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2415
                    return clockSetTimeout.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2416
                };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2417
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2418
                this.clock.setInterval = function (fn, timeout) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2419
                    server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2420
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2421
                    return clockSetInterval.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2422
                };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2423
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2424
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2425
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2426
        return sinon.fakeServer.addRequest.call(this, xhr);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2427
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2428
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2429
    sinon.fakeServerWithClock.respond = function respond() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2430
        var returnVal = sinon.fakeServer.respond.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2431
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2432
        if (this.clock) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2433
            this.clock.tick(this.longestTimeout || 0);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2434
            this.longestTimeout = 0;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2435
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2436
            if (this.resetClock) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2437
                this.clock.restore();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2438
                this.resetClock = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2439
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2440
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2441
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2442
        return returnVal;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2443
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2444
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2445
    sinon.fakeServerWithClock.restore = function restore() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2446
        if (this.clock) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2447
            this.clock.restore();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2448
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2449
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2450
        return sinon.fakeServer.restore.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2451
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2452
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2453
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2454
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2455
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2456
 * @depend collection.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2457
 * @depend util/fake_timers.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2458
 * @depend util/fake_server_with_clock.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2459
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2460
/*jslint eqeqeq: false, onevar: false, plusplus: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2461
/*global require, module*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2462
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2463
 * Manages fake collections as well as fake utilities such as Sinon's
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2464
 * timers and fake XHR implementation in one convenient object.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2465
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2466
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2467
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2468
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2469
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2470
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2471
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2472
if (typeof module == "object" && typeof require == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2473
    var sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2474
    sinon.extend(sinon, require("./util/fake_timers"));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2475
}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2476
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2477
(function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2478
    var push = [].push;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2479
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2480
    function exposeValue(sandbox, config, key, value) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2481
        if (!value) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2482
            return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2483
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2484
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2485
        if (config.injectInto) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2486
            config.injectInto[key] = value;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2487
        } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2488
            push.call(sandbox.args, value);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2489
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2490
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2491
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2492
    function prepareSandboxFromConfig(config) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2493
        var sandbox = sinon.create(sinon.sandbox);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2494
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2495
        if (config.useFakeServer) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2496
            if (typeof config.useFakeServer == "object") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2497
                sandbox.serverPrototype = config.useFakeServer;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2498
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2499
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2500
            sandbox.useFakeServer();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2501
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2502
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2503
        if (config.useFakeTimers) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2504
            if (typeof config.useFakeTimers == "object") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2505
                sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2506
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2507
                sandbox.useFakeTimers();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2508
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2509
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2510
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2511
        return sandbox;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2512
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2513
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2514
    sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2515
        useFakeTimers: function useFakeTimers() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2516
            this.clock = sinon.useFakeTimers.apply(sinon, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2517
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2518
            return this.add(this.clock);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2519
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2520
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2521
        serverPrototype: sinon.fakeServer,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2522
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2523
        useFakeServer: function useFakeServer() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2524
            var proto = this.serverPrototype || sinon.fakeServer;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2525
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2526
            if (!proto || !proto.create) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2527
                return null;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2528
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2529
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2530
            this.server = proto.create();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2531
            return this.add(this.server);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2532
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2533
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2534
        inject: function (obj) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2535
            sinon.collection.inject.call(this, obj);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2536
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2537
            if (this.clock) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2538
                obj.clock = this.clock;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2539
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2540
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2541
            if (this.server) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2542
                obj.server = this.server;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2543
                obj.requests = this.server.requests;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2544
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2545
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2546
            return obj;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2547
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2548
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2549
        create: function (config) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2550
            if (!config) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2551
                return sinon.create(sinon.sandbox);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2552
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2553
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2554
            var sandbox = prepareSandboxFromConfig(config);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2555
            sandbox.args = sandbox.args || [];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2556
            var prop, value, exposed = sandbox.inject({});
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2557
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2558
            if (config.properties) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2559
                for (var i = 0, l = config.properties.length; i < l; i++) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2560
                    prop = config.properties[i];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2561
                    value = exposed[prop] || prop == "sandbox" && sandbox;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2562
                    exposeValue(sandbox, config, prop, value);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2563
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2564
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2565
                exposeValue(sandbox, config, "sandbox", value);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2566
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2567
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2568
            return sandbox;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2569
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2570
    });
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2571
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2572
    sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2573
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2574
    if (typeof module != "undefined") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2575
        module.exports = sinon.sandbox;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2576
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2577
}());
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2578
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2579
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2580
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2581
 * @depend stub.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2582
 * @depend mock.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2583
 * @depend sandbox.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2584
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2585
/*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2586
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2587
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2588
 * Test function, sandboxes fakes
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2589
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2590
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2591
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2592
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2593
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2594
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2595
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2596
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2597
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2598
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2599
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2600
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2601
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2602
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2603
    if (!sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2604
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2605
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2606
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2607
    function test(callback) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2608
        var type = typeof callback;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2609
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2610
        if (type != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2611
            throw new TypeError("sinon.test needs to wrap a test function, got " + type);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2612
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2613
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2614
        return function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2615
            var config = sinon.getConfig(sinon.config);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2616
            config.injectInto = config.injectIntoThis && this || config.injectInto;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2617
            var sandbox = sinon.sandbox.create(config);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2618
            var exception, result;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2619
            var args = Array.prototype.slice.call(arguments).concat(sandbox.args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2620
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2621
            try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2622
                result = callback.apply(this, args);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2623
            } catch (e) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2624
                exception = e;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2625
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2626
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2627
            sandbox.verifyAndRestore();
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2628
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2629
            if (exception) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2630
                throw exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2631
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2632
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2633
            return result;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2634
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2635
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2636
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2637
    test.config = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2638
        injectIntoThis: true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2639
        injectInto: null,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2640
        properties: ["spy", "stub", "mock", "clock", "server", "requests"],
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2641
        useFakeTimers: true,
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2642
        useFakeServer: true
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2643
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2644
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2645
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2646
        module.exports = test;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2647
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2648
        sinon.test = test;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2649
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2650
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2651
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2652
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2653
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2654
 * @depend test.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2655
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2656
/*jslint eqeqeq: false, onevar: false, eqeqeq: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2657
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2658
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2659
 * Test case, sandboxes all test functions
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2660
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2661
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2662
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2663
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2664
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2665
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2666
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2667
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2668
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2669
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2670
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2671
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2672
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2673
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2674
    if (!sinon || !Object.prototype.hasOwnProperty) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2675
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2676
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2677
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2678
    function createTest(property, setUp, tearDown) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2679
        return function () {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2680
            if (setUp) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2681
                setUp.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2682
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2683
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2684
            var exception, result;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2685
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2686
            try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2687
                result = property.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2688
            } catch (e) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2689
                exception = e;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2690
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2691
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2692
            if (tearDown) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2693
                tearDown.apply(this, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2694
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2695
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2696
            if (exception) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2697
                throw exception;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2698
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2699
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2700
            return result;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2701
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2702
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2703
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2704
    function testCase(tests, prefix) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2705
        /*jsl:ignore*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2706
        if (!tests || typeof tests != "object") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2707
            throw new TypeError("sinon.testCase needs an object with test functions");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2708
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2709
        /*jsl:end*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2710
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2711
        prefix = prefix || "test";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2712
        var rPrefix = new RegExp("^" + prefix);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2713
        var methods = {}, testName, property, method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2714
        var setUp = tests.setUp;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2715
        var tearDown = tests.tearDown;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2716
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2717
        for (testName in tests) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2718
            if (tests.hasOwnProperty(testName)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2719
                property = tests[testName];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2720
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2721
                if (/^(setUp|tearDown)$/.test(testName)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2722
                    continue;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2723
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2724
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2725
                if (typeof property == "function" && rPrefix.test(testName)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2726
                    method = property;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2727
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2728
                    if (setUp || tearDown) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2729
                        method = createTest(property, setUp, tearDown);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2730
                    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2731
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2732
                    methods[testName] = sinon.test(method);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2733
                } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2734
                    methods[testName] = tests[testName];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2735
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2736
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2737
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2738
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2739
        return methods;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2740
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2741
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2742
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2743
        module.exports = testCase;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2744
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2745
        sinon.testCase = testCase;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2746
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2747
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2748
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2749
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2750
 * @depend ../sinon.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2751
 * @depend stub.js
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2752
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2753
/*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2754
/*global module, require, sinon*/
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2755
/**
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2756
 * Assertions matching the test spy retrieval interface.
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2757
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2758
 * @author Christian Johansen (christian@cjohansen.no)
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2759
 * @license BSD
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2760
 *
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2761
 * Copyright (c) 2010-2011 Christian Johansen
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2762
 */
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2763
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2764
(function (sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2765
    var commonJSModule = typeof module == "object" && typeof require == "function";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2766
    var slice = Array.prototype.slice;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2767
    var assert;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2768
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2769
    if (!sinon && commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2770
        sinon = require("../sinon");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2771
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2772
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2773
    if (!sinon) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2774
        return;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2775
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2776
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2777
    function verifyIsStub() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2778
        var method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2779
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2780
        for (var i = 0, l = arguments.length; i < l; ++i) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2781
            method = arguments[i];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2782
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2783
            if (!method) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2784
                assert.fail("fake is not a spy");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2785
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2786
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2787
            if (typeof method != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2788
                assert.fail(method + " is not a function");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2789
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2790
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2791
            if (typeof method.getCall != "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2792
                assert.fail(method + " is not stubbed");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2793
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2794
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2795
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2796
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2797
    function failAssertion(object, msg) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2798
        var failMethod = object.fail || assert.fail;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2799
        failMethod.call(object, msg);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2800
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2801
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2802
    function mirrorPropAsAssertion(name, method, message) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2803
        if (arguments.length == 2) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2804
            message = method;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2805
            method = name;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2806
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2807
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2808
        assert[name] = function (fake) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2809
            verifyIsStub(fake);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2810
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2811
            var args = slice.call(arguments, 1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2812
            var failed = false;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2813
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2814
            if (typeof method == "function") {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2815
                failed = !method(fake);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2816
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2817
                failed = typeof fake[method] == "function" ?
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2818
                    !fake[method].apply(fake, args) : !fake[method];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2819
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2820
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2821
            if (failed) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2822
                failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2823
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2824
                assert.pass(name);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2825
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2826
        };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2827
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2828
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2829
    function exposedName(prefix, prop) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2830
        return !prefix || /^fail/.test(prop) ? prop :
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2831
            prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2832
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2833
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2834
    assert = {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2835
        failException: "AssertError",
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2836
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2837
        fail: function fail(message) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2838
            var error = new Error(message);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2839
            error.name = this.failException || assert.failException;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2840
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2841
            throw error;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2842
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2843
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2844
        pass: function pass(assertion) {},
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2845
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2846
        callOrder: function assertCallOrder() {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2847
            verifyIsStub.apply(null, arguments);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2848
            var expected = "", actual = "";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2849
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2850
            if (!sinon.calledInOrder(arguments)) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2851
                try {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2852
                    expected = [].join.call(arguments, ", ");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2853
                    actual = sinon.orderByFirstCall(slice.call(arguments)).join(", ");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2854
                } catch (e) {}
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2855
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2856
                failAssertion(this, "expected " + expected + " to be " +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2857
                              "called in order but were called as " + actual);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2858
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2859
                assert.pass("callOrder");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2860
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2861
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2862
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2863
        callCount: function assertCallCount(method, count) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2864
            verifyIsStub(method);
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2865
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2866
            if (method.callCount != count) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2867
                var msg = "expected %n to be called " + sinon.timesInWords(count) +
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2868
                    " but was called %c%C";
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2869
                failAssertion(this, method.printf(msg));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2870
            } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2871
                assert.pass("callCount");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2872
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2873
        },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2874
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2875
        expose: function expose(target, options) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2876
            if (!target) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2877
                throw new TypeError("target is null or undefined");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2878
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2879
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2880
            var o = options || {};
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2881
            var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2882
            var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2883
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2884
            for (var method in this) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2885
                if (method != "export" && (includeFail || !/^(fail)/.test(method))) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2886
                    target[exposedName(prefix, method)] = this[method];
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2887
                }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2888
            }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2889
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2890
            return target;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2891
        }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2892
    };
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2893
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2894
    mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2895
    mirrorPropAsAssertion("notCalled", function (spy) { return !spy.called; },
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2896
                          "expected %n to not have been called but was called %c%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2897
    mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2898
    mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2899
    mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2900
    mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2901
    mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2902
    mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2903
    mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2904
    mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2905
    mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2906
    mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2907
    mirrorPropAsAssertion("threw", "%n did not throw exception%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2908
    mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2909
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2910
    if (commonJSModule) {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2911
        module.exports = assert;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2912
    } else {
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2913
        sinon.assert = assert;
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2914
    }
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2915
}(typeof sinon == "object" && sinon || null));
f92e6c4baae9 Renamed LAYOUT.txt and added sinon.js for mockups.
hamidouk
parents:
diff changeset
  2916