UNPKG

typemoq

Version:

A simple mocking library for TypeScript

1,233 lines (1,137 loc) 179 kB
var TypeMoqIntern; (function (TypeMoqIntern) { var Consts = (function () { function Consts() { } Consts.IMATCH_ID_VALUE = "438A51D3-6864-49D7-A655-CA1153B86965"; Consts.IMATCH_ID_NAME = "___id"; Consts.IMATCH_MATCHES_NAME = "___matches"; return Consts; }()); TypeMoqIntern.Consts = Consts; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var CurrentInterceptContext = (function () { function CurrentInterceptContext() { } return CurrentInterceptContext; }()); TypeMoqIntern.CurrentInterceptContext = CurrentInterceptContext; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { (function (GlobalType) { GlobalType[GlobalType["Class"] = 0] = "Class"; GlobalType[GlobalType["Function"] = 1] = "Function"; GlobalType[GlobalType["Value"] = 2] = "Value"; })(TypeMoqIntern.GlobalType || (TypeMoqIntern.GlobalType = {})); var GlobalType = TypeMoqIntern.GlobalType; var GlobalMock = (function () { function GlobalMock(mock, _name, _type, container) { this.mock = mock; this._name = _name; this._type = _type; this.container = container; if (!this._name) this._name = mock.name; } GlobalMock.ofInstance = function (instance, globalName, container, behavior) { if (container === void 0) { container = window; } if (behavior === void 0) { behavior = TypeMoqIntern.MockBehavior.Loose; } var mock = TypeMoqIntern.Mock.ofInstance(instance, behavior); var type = _.isFunction(instance) ? GlobalType.Function : GlobalType.Value; return new GlobalMock(mock, globalName, type, container); }; GlobalMock.ofType = function (ctor, globalName, container, behavior) { if (container === void 0) { container = window; } if (behavior === void 0) { behavior = TypeMoqIntern.MockBehavior.Loose; } var instance = new ctor(); var mock = TypeMoqIntern.Mock.ofInstance(instance, behavior); return new GlobalMock(mock, globalName, GlobalType.Class, container); }; Object.defineProperty(GlobalMock.prototype, "object", { get: function () { return this.mock.object; }, enumerable: true, configurable: true }); Object.defineProperty(GlobalMock.prototype, "name", { get: function () { return this._name || this.mock.name; }, enumerable: true, configurable: true }); Object.defineProperty(GlobalMock.prototype, "behavior", { get: function () { return this.mock.behavior; }, enumerable: true, configurable: true }); Object.defineProperty(GlobalMock.prototype, "callBase", { get: function () { return this.mock.callBase; }, set: function (value) { this.mock.callBase = value; }, enumerable: true, configurable: true }); Object.defineProperty(GlobalMock.prototype, "type", { get: function () { return this._type; }, enumerable: true, configurable: true }); // setup GlobalMock.prototype.setup = function (expression) { return this.mock.setup(expression); }; // verify GlobalMock.prototype.verify = function (expression, times) { this.mock.verify(expression, times); }; GlobalMock.prototype.verifyAll = function () { this.mock.verifyAll(); }; GlobalMock.prototype.reset = function () { this.mock.reset(); }; return GlobalMock; }()); TypeMoqIntern.GlobalMock = GlobalMock; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var PropertyRetriever = (function () { function PropertyRetriever() { } PropertyRetriever.getOwnEnumerables = function (obj) { return this._getPropertyNames(obj, true, false, this._enumerable); // Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj); }; PropertyRetriever.getOwnNonenumerables = function (obj) { return this._getPropertyNames(obj, true, false, this._notEnumerable); }; PropertyRetriever.getOwnEnumerablesAndNonenumerables = function (obj) { return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable); // Or just use: return Object.getOwnPropertyNames(obj); }; PropertyRetriever.getPrototypeEnumerables = function (obj) { return this._getPropertyNames(obj, false, true, this._enumerable); }; PropertyRetriever.getPrototypeNonenumerables = function (obj) { return this._getPropertyNames(obj, false, true, this._notEnumerable); }; PropertyRetriever.getPrototypeEnumerablesAndNonenumerables = function (obj) { return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable); }; PropertyRetriever.getOwnAndPrototypeEnumerables = function (obj) { return this._getPropertyNames(obj, true, true, this._enumerable); // Or could use unfiltered for..in }; PropertyRetriever.getOwnAndPrototypeNonenumerables = function (obj) { return this._getPropertyNames(obj, true, true, this._notEnumerable); }; PropertyRetriever.getOwnAndPrototypeEnumerablesAndNonenumerables = function (obj) { return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable); }; // Private static property checker callbacks PropertyRetriever._enumerable = function (obj, prop) { return obj.propertyIsEnumerable(prop); }; PropertyRetriever._notEnumerable = function (obj, prop) { return !obj.propertyIsEnumerable(prop); }; PropertyRetriever._enumerableAndNotEnumerable = function (obj, prop) { return true; }; PropertyRetriever._getPropertyNames = function (obj, iterateSelfBool, iteratePrototypeBool, includePropCb) { var result = []; do { if (iterateSelfBool) { var props = Object.getOwnPropertyNames(obj); _.forEach(props, function (prop) { var duplicate = _.find(result, function (p) { return p.name === prop; }); if (!duplicate && includePropCb(obj, prop)) { var propDesc = Object.getOwnPropertyDescriptor(obj, prop); result.push({ name: prop, desc: propDesc }); } }); } if (!iteratePrototypeBool) { break; } iterateSelfBool = true; } while (obj = Object.getPrototypeOf(obj)); return result; }; return PropertyRetriever; }()); TypeMoqIntern.PropertyRetriever = PropertyRetriever; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Utils = (function () { function Utils() { } Utils.getUUID = function () { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; }; Utils.functionName = function (fun) { var ret; if (fun.name) { ret = fun.name; } else { var repr = fun.toString(); repr = repr.substr('function '.length); ret = repr.substr(0, repr.indexOf('(')); } return ret; }; Utils.conthunktor = function (ctor, args) { var ret = new (ctor.bind.apply(ctor, [void 0].concat(args)))(); return ret; }; return Utils; }()); TypeMoqIntern.Utils = Utils; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Error; (function (Error) { var Exception = (function () { function Exception(name, message) { this.name = name; this.message = message; } Exception.prototype.toString = function () { var errMsg = this.message ? this.name + " - " + this.message : this.name; return errMsg; }; return Exception; }()); Error.Exception = Exception; })(Error = TypeMoqIntern.Error || (TypeMoqIntern.Error = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var TypeMoqIntern; (function (TypeMoqIntern) { var Error; (function (Error) { (function (MockExceptionReason) { MockExceptionReason[MockExceptionReason["NoSetup"] = "no setup expression"] = "NoSetup"; MockExceptionReason[MockExceptionReason["MoreThanOneSetup"] = "more than one setup expression"] = "MoreThanOneSetup"; MockExceptionReason[MockExceptionReason["InvalidSetup"] = "invalid setup expression"] = "InvalidSetup"; MockExceptionReason[MockExceptionReason["InvalidMatcher"] = "invalid matching expression"] = "InvalidMatcher"; MockExceptionReason[MockExceptionReason["InvalidProxyArg"] = "invalid proxy argument"] = "InvalidProxyArg"; MockExceptionReason[MockExceptionReason["UnknownGlobalType"] = "unknown global type"] = "UnknownGlobalType"; MockExceptionReason[MockExceptionReason["VerificationFailed"] = "verification failed"] = "VerificationFailed"; })(Error.MockExceptionReason || (Error.MockExceptionReason = {})); var MockExceptionReason = Error.MockExceptionReason; var MockException = (function (_super) { __extends(MockException, _super); function MockException(reason, ctx, name, message) { if (name === void 0) { name = 'Mock Exception'; } _super.call(this, name, message); this.reason = reason; this.ctx = ctx; } MockException.prototype.toString = function () { var errMsg = _super.prototype.toString.call(this) + " - " + this.reason; return errMsg; }; return MockException; }(Error.Exception)); Error.MockException = MockException; })(Error = TypeMoqIntern.Error || (TypeMoqIntern.Error = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Match; (function (Match) { var MatchAnyObject = (function () { function MatchAnyObject(_ctor) { this._ctor = _ctor; this.___id = TypeMoqIntern.Consts.IMATCH_ID_VALUE; } MatchAnyObject.prototype.___matches = function (object) { var match = false; if (this._ctor.prototype === object.constructor.prototype) match = true; return match; }; return MatchAnyObject; }()); Match.MatchAnyObject = MatchAnyObject; var MatchAny = (function () { function MatchAny() { this.___id = TypeMoqIntern.Consts.IMATCH_ID_VALUE; } MatchAny.prototype.___matches = function (object) { var match = false; if (!_.isUndefined(object)) match = true; return match; }; return MatchAny; }()); Match.MatchAny = MatchAny; var MatchAnyString = (function () { function MatchAnyString() { this.___id = TypeMoqIntern.Consts.IMATCH_ID_VALUE; } MatchAnyString.prototype.___matches = function (object) { var match = false; if (_.isString(object)) match = true; return match; }; return MatchAnyString; }()); Match.MatchAnyString = MatchAnyString; var MatchAnyNumber = (function () { function MatchAnyNumber() { this.___id = TypeMoqIntern.Consts.IMATCH_ID_VALUE; } MatchAnyNumber.prototype.___matches = function (object) { var match = false; if (_.isNumber(object)) match = true; return match; }; return MatchAnyNumber; }()); Match.MatchAnyNumber = MatchAnyNumber; })(Match = TypeMoqIntern.Match || (TypeMoqIntern.Match = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Match; (function (Match) { var MatchPred = (function () { function MatchPred(_pred) { this._pred = _pred; this.___id = TypeMoqIntern.Consts.IMATCH_ID_VALUE; } MatchPred.prototype.___matches = function (object) { var match = false; if (this._pred(object)) match = true; return match; }; return MatchPred; }()); Match.MatchPred = MatchPred; })(Match = TypeMoqIntern.Match || (TypeMoqIntern.Match = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Match; (function (Match) { var MatchValue = (function () { function MatchValue(_value) { this._value = _value; this.___id = TypeMoqIntern.Consts.IMATCH_ID_VALUE; } MatchValue.prototype.___matches = function (object) { var match = false; if (_.isEqual(this._value, object)) match = true; return match; }; return MatchValue; }()); Match.MatchValue = MatchValue; })(Match = TypeMoqIntern.Match || (TypeMoqIntern.Match = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Proxy; (function (Proxy) { var MethodInvocation = (function () { function MethodInvocation(_property, _args) { this._property = _property; this._args = _args; } Object.defineProperty(MethodInvocation.prototype, "args", { get: function () { return this._args || { length: 0, callee: null }; }, set: function (value) { this._args = value; }, enumerable: true, configurable: true }); Object.defineProperty(MethodInvocation.prototype, "property", { get: function () { return this._property; }, enumerable: true, configurable: true }); MethodInvocation.prototype.invokeBase = function () { this.returnValue = this._property.toFunc.apply(this._property.obj, this._args); }; return MethodInvocation; }()); Proxy.MethodInvocation = MethodInvocation; var ValueGetterInvocation = (function () { function ValueGetterInvocation(_property, value) { this._property = _property; this.returnValue = value; } Object.defineProperty(ValueGetterInvocation.prototype, "args", { get: function () { var args = []; Object.defineProperty(args, "callee", { configurable: false, enumerable: true, writable: false, value: null }); return args; }, set: function (value) { }, enumerable: true, configurable: true }); Object.defineProperty(ValueGetterInvocation.prototype, "property", { get: function () { return this._property; }, enumerable: true, configurable: true }); ValueGetterInvocation.prototype.invokeBase = function () { this.returnValue = this._property.obj[this._property.name]; }; return ValueGetterInvocation; }()); Proxy.ValueGetterInvocation = ValueGetterInvocation; var ValueSetterInvocation = (function () { function ValueSetterInvocation(_property, _args) { this._property = _property; this._args = _args; } Object.defineProperty(ValueSetterInvocation.prototype, "args", { get: function () { return this._args; }, set: function (value) { this._args = value; }, enumerable: true, configurable: true }); Object.defineProperty(ValueSetterInvocation.prototype, "property", { get: function () { return this._property; }, enumerable: true, configurable: true }); ValueSetterInvocation.prototype.invokeBase = function () { this._property.obj[this._property.name] = this._args[0]; this.returnValue = this._property.obj[this._property.name]; }; return ValueSetterInvocation; }()); Proxy.ValueSetterInvocation = ValueSetterInvocation; var MethodGetterInvocation = (function () { function MethodGetterInvocation(_property, _getter) { this._property = _property; this._getter = _getter; } Object.defineProperty(MethodGetterInvocation.prototype, "args", { get: function () { var args = []; Object.defineProperty(args, "callee", { configurable: false, enumerable: true, writable: false, value: null }); return args; }, set: function (value) { }, enumerable: true, configurable: true }); Object.defineProperty(MethodGetterInvocation.prototype, "property", { get: function () { return this._property; }, enumerable: true, configurable: true }); MethodGetterInvocation.prototype.invokeBase = function () { this.returnValue = this._property.obj[this._property.name]; }; return MethodGetterInvocation; }()); Proxy.MethodGetterInvocation = MethodGetterInvocation; var MethodSetterInvocation = (function () { function MethodSetterInvocation(_property, _setter, _args) { this._property = _property; this._setter = _setter; this._args = _args; } Object.defineProperty(MethodSetterInvocation.prototype, "args", { get: function () { return this._args; }, set: function (value) { this._args = value; }, enumerable: true, configurable: true }); Object.defineProperty(MethodSetterInvocation.prototype, "property", { get: function () { return this._property; }, enumerable: true, configurable: true }); MethodSetterInvocation.prototype.invokeBase = function () { this._property.obj[this._property.name] = this._args[0]; this.returnValue = this._property.obj[this._property.name]; }; return MethodSetterInvocation; }()); Proxy.MethodSetterInvocation = MethodSetterInvocation; var MethodInfo = (function () { function MethodInfo(obj, name) { this.obj = obj; this.name = name; } Object.defineProperty(MethodInfo.prototype, "toFunc", { get: function () { var func; if (_.isFunction(this.obj)) func = this.obj; else func = this.obj[this.name]; return func; }, enumerable: true, configurable: true }); return MethodInfo; }()); Proxy.MethodInfo = MethodInfo; var PropertyInfo = (function () { function PropertyInfo(obj, name) { this.obj = obj; this.name = name; } return PropertyInfo; }()); Proxy.PropertyInfo = PropertyInfo; })(Proxy = TypeMoqIntern.Proxy || (TypeMoqIntern.Proxy = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Proxy; (function (Proxy_1) { var Proxy = (function () { function Proxy(interceptor, instance) { var _this = this; this.check(instance); var that = this; var props = TypeMoqIntern.PropertyRetriever.getOwnAndPrototypeEnumerablesAndNonenumerables(instance); _.each(props, function (prop) { if (_.isFunction(prop.desc.value)) { var propDesc = { configurable: prop.desc.configurable, enumerable: prop.desc.enumerable, writable: prop.desc.writable }; _this.defineMethodProxy(that, interceptor, instance, prop.name, propDesc); } else { var propDesc = { configurable: prop.desc.configurable, enumerable: prop.desc.enumerable }; if (prop.desc.value !== undefined) _this.defineValuePropertyProxy(that, interceptor, instance, prop.name, prop.desc.value, propDesc); else _this.defineGetSetPropertyProxy(that, interceptor, instance, prop.name, prop.desc.get, prop.desc.set, propDesc); } }); } Proxy.of = function (instance, interceptor) { Proxy.check(instance); var result; if (_.isFunction(instance)) { var funcName = TypeMoqIntern.Utils.functionName(instance); result = Proxy.methodProxyValue(interceptor, instance, funcName); } else { result = new Proxy(interceptor, instance); } return result; }; Proxy.check = function (instance) { Proxy.checkNotNull(instance); // allow only primitive objects and functions var ok = false; if (_.isFunction(instance) || (_.isObject(instance) && !Proxy.isPrimitiveObject(instance))) ok = true; if (!ok) throw new error.MockException(error.MockExceptionReason.InvalidProxyArg, instance, "InvalidProxyArgument Exception", "Argument should be a function or a non primitive object"); }; Proxy.prototype.check = function (instance) { Proxy.checkNotNull(instance); // allow only non primitive objects var ok = false; if (!_.isFunction(instance) && (_.isObject(instance) && !Proxy.isPrimitiveObject(instance))) ok = true; if (!ok) throw new error.MockException(error.MockExceptionReason.InvalidProxyArg, instance, "InvalidProxyArgument Exception", "Argument should be a non primitive object"); }; Proxy.checkNotNull = function (instance) { if (_.isNull(instance)) throw new error.MockException(error.MockExceptionReason.InvalidProxyArg, instance, "InvalidProxyArgument Exception", "Argument cannot be null"); }; Proxy.isPrimitiveObject = function (obj) { var result = false; if (_.isFunction(obj) || _.isArray(obj) || _.isDate(obj) || _.isNull(obj)) result = true; return result; }; Proxy.prototype.defineMethodProxy = function (that, interceptor, instance, propName, propDesc) { if (propDesc === void 0) { propDesc = { configurable: false, enumerable: true, writable: false }; } propDesc.value = Proxy.methodProxyValue(interceptor, instance, propName); this.defineProperty(that, propName, propDesc); }; Proxy.methodProxyValue = function (interceptor, instance, propName) { function proxy() { var method = new Proxy_1.MethodInfo(instance, propName); var invocation = new Proxy_1.MethodInvocation(method, arguments); interceptor.intercept(invocation); return invocation.returnValue; } return proxy; }; Proxy.prototype.defineValuePropertyProxy = function (that, interceptor, instance, propName, propValue, propDesc) { if (propDesc === void 0) { propDesc = { configurable: false, enumerable: true }; } function getProxy() { var method = new Proxy_1.PropertyInfo(instance, propName); var invocation = new Proxy_1.ValueGetterInvocation(method, propValue); interceptor.intercept(invocation); return invocation.returnValue; } propDesc.get = getProxy; function setProxy(v) { var method = new Proxy_1.PropertyInfo(instance, propName); var invocation = new Proxy_1.ValueSetterInvocation(method, arguments); interceptor.intercept(invocation); } propDesc.set = setProxy; this.defineProperty(that, propName, propDesc); }; Proxy.prototype.defineGetSetPropertyProxy = function (that, interceptor, instance, propName, get, set, propDesc) { if (propDesc === void 0) { propDesc = { configurable: false, enumerable: true }; } function getProxy() { var method = new Proxy_1.PropertyInfo(instance, propName); var invocation = new Proxy_1.MethodGetterInvocation(method, get); interceptor.intercept(invocation); return invocation.returnValue; } propDesc.get = getProxy; function setProxy(v) { var method = new Proxy_1.PropertyInfo(instance, propName); var invocation = new Proxy_1.MethodSetterInvocation(method, set, arguments); interceptor.intercept(invocation); } propDesc.set = setProxy; this.defineProperty(that, propName, propDesc); }; Proxy.prototype.defineProperty = function (obj, name, desc) { try { Object.defineProperty(obj, name, desc); } catch (e) { console.log(e.message); } }; return Proxy; }()); Proxy_1.Proxy = Proxy; })(Proxy = TypeMoqIntern.Proxy || (TypeMoqIntern.Proxy = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var Proxy; (function (Proxy) { var ProxyFactory = (function () { function ProxyFactory() { } ProxyFactory.prototype.createProxy = function (interceptor, instance) { var proxy = Proxy.Proxy.of(instance, interceptor); return proxy; }; return ProxyFactory; }()); Proxy.ProxyFactory = ProxyFactory; })(Proxy = TypeMoqIntern.Proxy || (TypeMoqIntern.Proxy = {})); })(TypeMoqIntern || (TypeMoqIntern = {})); var error = TypeMoqIntern.Error; var match = TypeMoqIntern.Match; var proxy = TypeMoqIntern.Proxy; var TypeMoqIntern; (function (TypeMoqIntern) { var GlobalScope = (function () { function GlobalScope(_args) { this._args = _args; } GlobalScope.using = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i - 0] = arguments[_i]; } var scope = new GlobalScope(args); return scope; }; GlobalScope.prototype.with = function (action) { var initial = {}; try { _.each(this._args, function (a) { if (!_.isUndefined(a.container.hasOwnProperty(a.name))) { var containerProps = TypeMoqIntern.PropertyRetriever.getOwnAndPrototypeEnumerablesAndNonenumerables(a.container); var prop = _.find(containerProps, function (p) { return p.name === a.name; }); initial[a.name] = prop.desc; var desc = {}; switch (a.type) { case TypeMoqIntern.GlobalType.Class: //TODO: return a new mock every time with same interceptor as the one used by mock passed in as arg to 'using' // (to support different ctor arguments) desc.value = function () { return a.mock.object; }; break; case TypeMoqIntern.GlobalType.Function: desc.value = a.mock.object; break; case TypeMoqIntern.GlobalType.Value: desc.get = function () { return a.mock.object; }; break; default: throw new error.MockException(error.MockExceptionReason.UnknownGlobalType, a, "UnknownGlobalType Exception", "unknown global type: " + a.type); } try { Object.defineProperty(a.container, a.name, desc); } catch (e) { console.log("1: " + e); } } }); action.apply(this, this._args); } finally { _.each(this._args, function (a) { if (!_.isUndefined(a.mock.instance)) { var desc = initial[a.name]; if (desc) { switch (a.type) { case TypeMoqIntern.GlobalType.Class: break; case TypeMoqIntern.GlobalType.Function: break; case TypeMoqIntern.GlobalType.Value: desc.configurable = true; break; default: } try { Object.defineProperty(a.container, a.name, desc); } catch (e) { console.log("2: " + e); } } } }); } }; return GlobalScope; }()); TypeMoqIntern.GlobalScope = GlobalScope; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { (function (InterceptionAction) { InterceptionAction[InterceptionAction["Continue"] = 0] = "Continue"; InterceptionAction[InterceptionAction["Stop"] = 1] = "Stop"; })(TypeMoqIntern.InterceptionAction || (TypeMoqIntern.InterceptionAction = {})); var InterceptionAction = TypeMoqIntern.InterceptionAction; var InterceptorContext = (function () { function InterceptorContext(behavior, mock) { this.behavior = behavior; this.mock = mock; this._actualInvocations = []; this._orderedCalls = []; } InterceptorContext.prototype.addInvocation = function (invocation) { this._actualInvocations.push(invocation); }; InterceptorContext.prototype.actualInvocations = function () { return this._actualInvocations; }; InterceptorContext.prototype.clearInvocations = function () { this._actualInvocations = []; }; InterceptorContext.prototype.addOrderedCall = function (call) { this._orderedCalls.push(call); }; InterceptorContext.prototype.removeOrderedCall = function (call) { _.filter(this._orderedCalls, function (x) { return x.id !== call.id; }); }; InterceptorContext.prototype.orderedCalls = function () { return this._orderedCalls; }; return InterceptorContext; }()); TypeMoqIntern.InterceptorContext = InterceptorContext; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var InterceptorExecute = (function () { function InterceptorExecute(behavior, mock) { this._interceptorContext = new TypeMoqIntern.InterceptorContext(behavior, mock); } Object.defineProperty(InterceptorExecute.prototype, "interceptorContext", { get: function () { return this._interceptorContext; }, enumerable: true, configurable: true }); InterceptorExecute.prototype.intercept = function (invocation) { var _this = this; var localCtx = new TypeMoqIntern.CurrentInterceptContext(); _.some(this.interceptionStrategies(), function (strategy) { if (TypeMoqIntern.InterceptionAction.Stop === strategy.handleIntercept(invocation, _this.interceptorContext, localCtx)) { return true; } }); }; InterceptorExecute.prototype.addCall = function (call) { this._interceptorContext.addOrderedCall(call); }; InterceptorExecute.prototype.verifyCall = function (call, times) { var actualCalls = this._interceptorContext.actualInvocations(); var callCount = _.filter(actualCalls, function (c) { return call.matches(c); }).length; if (!times.verify(callCount)) { this.throwVerifyCallException(call.setupCall, times); } }; InterceptorExecute.prototype.verify = function () { var _this = this; var orderedCalls = this._interceptorContext.orderedCalls(); var verifiables = _.filter(orderedCalls, function (c) { return c.isVerifiable; }); _.forEach(verifiables, function (v) { _this.verifyCall(v, v.expectedCallCount); }); }; InterceptorExecute.prototype.interceptionStrategies = function () { var strategies = [ new TypeMoqIntern.AddActualInvocation(), new TypeMoqIntern.ExtractProxyCall(), new TypeMoqIntern.ExecuteCall(), new TypeMoqIntern.InvokeBase(), new TypeMoqIntern.HandleMockRecursion() ]; return strategies; }; InterceptorExecute.prototype.throwVerifyCallException = function (call, times) { var e = new error.MockException(error.MockExceptionReason.VerificationFailed, call, "VerifyCall Exception", times.failMessage); throw e; }; return InterceptorExecute; }()); TypeMoqIntern.InterceptorExecute = InterceptorExecute; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var InterceptorSetup = (function () { function InterceptorSetup() { } Object.defineProperty(InterceptorSetup.prototype, "interceptedCall", { get: function () { return this._interceptedCall; }, enumerable: true, configurable: true }); InterceptorSetup.prototype.intercept = function (invocation) { if (this._interceptedCall) { throw new error.MockException(error.MockExceptionReason.MoreThanOneSetup, invocation, "MoreThanOneSetupExpression Exception", "Setup should contain only one expression"); } this._interceptedCall = invocation; }; return InterceptorSetup; }()); TypeMoqIntern.InterceptorSetup = InterceptorSetup; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var AddActualInvocation = (function () { function AddActualInvocation() { } AddActualInvocation.prototype.handleIntercept = function (invocation, ctx, localCtx) { ctx.addInvocation(invocation); return TypeMoqIntern.InterceptionAction.Continue; }; return AddActualInvocation; }()); TypeMoqIntern.AddActualInvocation = AddActualInvocation; var ExtractProxyCall = (function () { function ExtractProxyCall() { } ExtractProxyCall.prototype.handleIntercept = function (invocation, ctx, localCtx) { var orderedCalls = ctx.orderedCalls().slice(); var findCallPred = function (c) { return c.matches(invocation); }; var matchingCalls = _.filter(orderedCalls, function (c) { return findCallPred(c); }); if (matchingCalls.length > 1) findCallPred = function (c) { return !c.isInvoked && c.matches(invocation); }; localCtx.call = _.find(orderedCalls, function (c) { return findCallPred(c); }); if (localCtx.call != null) { localCtx.call.evaluatedSuccessfully(); } else if (ctx.behavior == TypeMoqIntern.MockBehavior.Strict) { throw new error.MockException(error.MockExceptionReason.NoSetup, invocation); } return TypeMoqIntern.InterceptionAction.Continue; }; return ExtractProxyCall; }()); TypeMoqIntern.ExtractProxyCall = ExtractProxyCall; var ExecuteCall = (function () { function ExecuteCall() { } ExecuteCall.prototype.handleIntercept = function (invocation, ctx, localCtx) { this._ctx = ctx; var currentCall = localCtx.call; if (currentCall != null) { currentCall.execute(invocation); return TypeMoqIntern.InterceptionAction.Stop; } return TypeMoqIntern.InterceptionAction.Continue; }; return ExecuteCall; }()); TypeMoqIntern.ExecuteCall = ExecuteCall; var InvokeBase = (function () { function InvokeBase() { } InvokeBase.prototype.handleIntercept = function (invocation, ctx, localCtx) { if (ctx.mock.callBase) { invocation.invokeBase(); return TypeMoqIntern.InterceptionAction.Stop; } return TypeMoqIntern.InterceptionAction.Continue; }; return InvokeBase; }()); TypeMoqIntern.InvokeBase = InvokeBase; var HandleMockRecursion = (function () { function HandleMockRecursion() { } HandleMockRecursion.prototype.handleIntercept = function (invocation, ctx, localCtx) { //TODO: return TypeMoqIntern.InterceptionAction.Continue; }; return HandleMockRecursion; }()); TypeMoqIntern.HandleMockRecursion = HandleMockRecursion; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var It = (function () { function It() { } It.isValue = function (x) { var matcher = new match.MatchValue(x); return matcher; }; It.isAnyObject = function (x) { var matcher = new match.MatchAnyObject(x); return matcher; }; It.isAny = function () { var matcher = new match.MatchAny(); return matcher; }; It.isAnyString = function () { var matcher = new match.MatchAnyString(); return matcher; }; It.isAnyNumber = function () { var matcher = new match.MatchAnyNumber(); return matcher; }; It.is = function (predicate) { var matcher = new match.MatchPred(predicate); return matcher; }; return It; }()); TypeMoqIntern.It = It; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { var MethodCall = (function () { function MethodCall(mock, _setupExpression) { this.mock = mock; this._setupExpression = _setupExpression; this._callCount = 0; this._id = this.generateId(); var interceptor = new TypeMoqIntern.InterceptorSetup(); var proxy = TypeMoqIntern.Mock.proxyFactory.createProxy(interceptor, mock.instance); _setupExpression(proxy); if (interceptor.interceptedCall) { var ic = interceptor.interceptedCall; var newArgs = this.transformToMatchers(ic.args); Object.defineProperty(newArgs, "callee", { configurable: false, enumerable: true, writable: false, value: ic.args.callee }); ic.args = newArgs; this._setupCall = ic; } else { throw new error.MockException(error.MockExceptionReason.InvalidSetup, this._setupExpression, "InvalidSetupExpression Exception", "Invalid setup expression"); } } MethodCall.prototype.generateId = function () { return "MethodCall<" + _.uniqueId() + ">"; }; MethodCall.prototype.transformToMatchers = function (args) { var newArgs = []; _.each(args, function (a) { if (!_.isObject(a)) { var newArg = new match.MatchValue(a); newArgs.push(newArg); } else { if (!_.isUndefined(a[TypeMoqIntern.Consts.IMATCH_MATCHES_NAME]) && !_.isUndefined(a[TypeMoqIntern.Consts.IMATCH_ID_NAME]) && a[TypeMoqIntern.Consts.IMATCH_ID_NAME] === TypeMoqIntern.Consts.IMATCH_ID_VALUE) { newArgs.push(a); } else { throw new error.MockException(error.MockExceptionReason.InvalidMatcher, a, "InvalidMatcher Exception", "Invalid match object"); } } }); return newArgs; }; Object.defineProperty(MethodCall.prototype, "id", { // IProxyCall get: function () { return this._id; }, enumerable: true, configurable: true }); Object.defineProperty(MethodCall.prototype, "setupExpression", { get: function () { return this._setupExpression; }, enumerable: true, configurable: true }); Object.defineProperty(MethodCall.prototype, "setupCall", { get: function () { return this._setupCall; }, enumerable: true, configurable: true }); Object.defineProperty(MethodCall.prototype, "isVerifiable", { get: function () { return this._isVerifiable; }, enumerable: true, configurable: true }); Object.defineProperty(MethodCall.prototype, "expectedCallCount", { get: function () { return this._expectedCallCount; }, enumerable: true, configurable: true }); Object.defineProperty(MethodCall.prototype, "isInvoked", { get: function () { return this._isInvoked; }, enumerable: true, configurable: true }); Object.defineProperty(MethodCall.prototype, "callCount", { get: function () { return this._callCount; }, enumerable: true, configurable: true }); MethodCall.prototype.evaluatedSuccessfully = function () { this._evaluatedSuccessfully = true; }; MethodCall.prototype.matches = function (call) { var match = false; if (this._setupCall.property && call && call.property && this._setupCall.property.name === call.property.name) { if (this._setupCall.args.length === call.args.length) { match = true; _.each(this.setupCall.args, function (x, index) { var setupArg = x; var callArg = call.args[index]; if (match && !setupArg.___matches(callArg)) match = false; }); } } return match; }; MethodCall.prototype.execute = function (call) { this._isInvoked = true; if (this._setupCallback != null) { this._setupCallback.apply(this, call.args); } if (this._thrownException != null) { throw this._thrownException; } this._callCount++; }; // IVerifies MethodCall.prototype.verifiable = function (times) { if (times === void 0) { times = TypeMoqIntern.Times.atLeastOnce(); } this._isVerifiable = true; this._expectedCallCount = times; }; return MethodCall; }()); TypeMoqIntern.MethodCall = MethodCall; })(TypeMoqIntern || (TypeMoqIntern = {})); var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var TypeMoqIntern; (function (TypeMoqIntern) { var MethodCallReturn = (function (_super) { __extends(MethodCallReturn, _super); function MethodCallReturn(mock, setupExpression) { _super.call(this, mock, setupExpression); } // overrides MethodCallReturn.prototype.execute = function (call) { _super.prototype.execute.call(this, call); if (this._callBase) call.invokeBase(); else if (this.hasReturnValue) call.returnValue = this._returnValueFunc.apply(this, call.args); }; // ISetup MethodCallReturn.prototype.callback = function (action) { this._setupCallback = action; return this; }; MethodCallReturn.prototype.throws = function (exception) { this._thrownException = exception; return this; }; MethodCallReturn.prototype.returns = function (valueFunc) { this._returnValueFunc = valueFunc; this.hasReturnValue = true; return this; }; MethodCallReturn.prototype.callBase = function () { this._callBase = true; return this; }; return MethodCallReturn; }(TypeMoqIntern.MethodCall)); TypeMoqIntern.MethodCallReturn = MethodCallReturn; })(TypeMoqIntern || (TypeMoqIntern = {})); var TypeMoqIntern; (function (TypeMoqIntern) { (function (MockBehavior) { MockBehavior[MockBehavior["Loose"] = 0] = "Loose"; MockBehavior[MockBehavior["Strict"] = 1] = "Strict"; })(TypeMoqIntern.MockBehavior || (TypeMoqIntern.MockBehavior = {})); var MockBehavior = TypeMoqIntern.MockBehavior; var Mock = (function () { function Mock(instance, _behavior) { if (_behavior === void 0) { _behavior = MockBehavior.Loose; } this.instance = instance; this._behavior = _behavior; this._id = this.generateId(); this._name = this.getNameOf(instance); this.init(); } Mock.prototype.init = function () { this._interceptor = new TypeMoqIntern.InterceptorExecute(this._behavior, this); this._proxy = Mock.proxyFactory.createProxy(this._interceptor, this.instance); }; Mock.ofInstance = function (instance, behavior) {