npmtest-sinon (v0.0.1)

Code coverage report for node-npmtest-sinon/sinon/lib/sinon/stub.js

Statements: 30.49% (25 / 82)      Branches: 19.23% (10 / 52)      Functions: 11.76% (2 / 17)      Lines: 30.49% (25 / 82)      Ignored: none     

All files » node-npmtest-sinon/sinon/lib/sinon/ » stub.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165    1 1 1 1 1 1 1 1 1 1   1                                                                               1               1       1           1           1   1                                                                                                                                   1 10         3       1 29 29       1 1    
"use strict";
 
var behavior = require("./behavior");
var behaviors = require("./default-behaviors");
var spy = require("./spy");
var extend = require("./util/core/extend");
var functionToString = require("./util/core/function-to-string");
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var wrapMethod = require("./util/core/wrap-method");
var stubEntireObject = require("./stub-entire-object");
var stubDescriptor = require("./stub-descriptor");
var throwOnFalsyObject = require("./throw-on-falsy-object");
 
function stub(object, property, descriptor) {
    throwOnFalsyObject.apply(null, arguments);
 
    var actualDescriptor = getPropertyDescriptor(object, property);
    var isStubbingEntireObject = typeof property === "undefined" && typeof object === "object";
    var isCreatingNewStub = !object && typeof property === "undefined";
    var isStubbingDescriptor = object && property && Boolean(descriptor);
    var isStubbingNonFuncProperty = typeof object === "object"
                                    && typeof property !== "undefined"
                                    && (typeof actualDescriptor === "undefined"
                                    || typeof actualDescriptor.value !== "function")
                                    && typeof descriptor === "undefined";
    var isStubbingExistingMethod = !isStubbingDescriptor
                                    && typeof object === "object"
                                    && typeof actualDescriptor !== "undefined"
                                    && typeof actualDescriptor.value === "function";
    var arity = isStubbingExistingMethod ? object[property].length : 0;
 
    if (isStubbingEntireObject) {
        return stubEntireObject(stub, object);
    }
 
    if (isStubbingDescriptor) {
        return stubDescriptor.apply(null, arguments);
    }
 
    if (isCreatingNewStub) {
        return stub.create();
    }
 
    var s = stub.create(arity);
    s.rootObj = object;
    s.propName = property;
    s.restore = function restore() {
        Object.defineProperty(object, property, actualDescriptor);
    };
 
    return isStubbingNonFuncProperty ? s : wrapMethod(object, property, s);
}
 
stub.createStubInstance = function (constructor) {
    if (typeof constructor !== "function") {
        throw new TypeError("The constructor should be a function.");
    }
    return stub(Object.create(constructor.prototype));
};
 
/*eslint-disable no-use-before-define*/
function getParentBehaviour(stubInstance) {
    return (stubInstance.parent && getCurrentBehavior(stubInstance.parent));
}
 
function getDefaultBehavior(stubInstance) {
    return stubInstance.defaultBehavior ||
            getParentBehaviour(stubInstance) ||
            behavior.create(stubInstance);
}
 
function getCurrentBehavior(stubInstance) {
    var currentBehavior = stubInstance.behaviors[stubInstance.callCount - 1];
    return currentBehavior && currentBehavior.isPresent() ? currentBehavior : getDefaultBehavior(stubInstance);
}
/*eslint-enable no-use-before-define*/
 
var uuid = 0;
 
var proto = {
    create: function create(stubLength) {
        var functionStub = function () {
            return getCurrentBehavior(functionStub).invoke(this, arguments);
        };
 
        functionStub.id = "stub#" + uuid++;
        var orig = functionStub;
        functionStub = spy.create(functionStub, stubLength);
        functionStub.func = orig;
 
        extend(functionStub, stub);
        functionStub.instantiateFake = stub.create;
        functionStub.displayName = "stub";
        functionStub.toString = functionToString;
 
        functionStub.defaultBehavior = null;
        functionStub.behaviors = [];
 
        return functionStub;
    },
 
    resetBehavior: function () {
        var fakes = this.fakes || [];
 
        this.defaultBehavior = null;
        this.behaviors = [];
 
        delete this.returnValue;
        delete this.returnArgAt;
        delete this.fakeFn;
        this.returnThis = false;
 
        fakes.forEach(function (fake) {
            fake.resetBehavior();
        });
    },
 
    resetHistory: spy.reset,
 
    reset: function () {
        this.resetHistory();
        this.resetBehavior();
    },
 
    onCall: function onCall(index) {
        if (!this.behaviors[index]) {
            this.behaviors[index] = behavior.create(this);
        }
 
        return this.behaviors[index];
    },
 
    onFirstCall: function onFirstCall() {
        return this.onCall(0);
    },
 
    onSecondCall: function onSecondCall() {
        return this.onCall(1);
    },
 
    onThirdCall: function onThirdCall() {
        return this.onCall(2);
    }
};
 
Object.keys(behavior).forEach(function (method) {
    if (behavior.hasOwnProperty(method) &&
        !proto.hasOwnProperty(method) &&
        method !== "create" &&
        method !== "withArgs" &&
        method !== "invoke") {
        proto[method] = behavior.createBehavior(method);
    }
});
 
Object.keys(behaviors).forEach(function (method) {
    Eif (behaviors.hasOwnProperty(method) && !proto.hasOwnProperty(method)) {
        behavior.addBehavior(stub, method, behaviors[method]);
    }
});
 
extend(stub, proto);
module.exports = stub;