123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- goog.setTestOnly('goog.testing.Mock');
- goog.provide('goog.testing.Mock');
- goog.provide('goog.testing.MockExpectation');
- goog.require('goog.array');
- goog.require('goog.object');
- goog.require('goog.testing.JsUnitException');
- goog.require('goog.testing.MockInterface');
- goog.require('goog.testing.mockmatchers');
- goog.testing.MockExpectation = function(name) {
-
- this.name = name;
-
- this.errorMessages = [];
- };
- goog.testing.MockExpectation.prototype.minCalls = 1;
- goog.testing.MockExpectation.prototype.maxCalls = 1;
- goog.testing.MockExpectation.prototype.returnValue;
- goog.testing.MockExpectation.prototype.exceptionToThrow;
- goog.testing.MockExpectation.prototype.argumentList;
- goog.testing.MockExpectation.prototype.actualCalls = 0;
- goog.testing.MockExpectation.prototype.verificationCalls = 0;
- goog.testing.MockExpectation.prototype.toDo;
- goog.testing.MockExpectation.prototype.addErrorMessage = function(message) {
- this.errorMessages.push(message);
- };
- goog.testing.MockExpectation.prototype.getErrorMessage = function() {
- return this.errorMessages.join('\n');
- };
- goog.testing.MockExpectation.prototype.getErrorMessageCount = function() {
- return this.errorMessages.length;
- };
- goog.testing.Mock = function(
- objectToMock, opt_mockStaticMethods, opt_createProxy) {
- if (!goog.isObject(objectToMock) && !goog.isFunction(objectToMock)) {
- throw new Error('objectToMock must be an object or constructor.');
- }
- if (opt_createProxy && !opt_mockStaticMethods &&
- goog.isFunction(objectToMock)) {
-
- var tempCtor = function() {};
- goog.inherits(tempCtor, objectToMock);
- this.$proxy = new tempCtor();
- } else if (
- opt_createProxy && opt_mockStaticMethods &&
- goog.isFunction(objectToMock)) {
- throw Error('Cannot create a proxy when opt_mockStaticMethods is true');
- } else if (opt_createProxy && !goog.isFunction(objectToMock)) {
- throw Error('Must have a constructor to create a proxy');
- }
- if (goog.isFunction(objectToMock) && !opt_mockStaticMethods) {
- this.$initializeFunctions_(objectToMock.prototype);
- } else {
- this.$initializeFunctions_(objectToMock);
- }
- this.$argumentListVerifiers_ = {};
- };
- goog.testing.Mock.LOOSE = 1;
- goog.testing.Mock.STRICT = 0;
- goog.testing.Mock.OBJECT_PROTOTYPE_FIELDS_ = [
- 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
- 'toLocaleString', 'toString', 'valueOf'
- ];
- goog.testing.Mock.FUNCTION_PROTOTYPE_FIELDS_ = ['apply', 'bind', 'call'];
- goog.testing.Mock.prototype.$proxy = null;
- goog.testing.Mock.prototype.$argumentListVerifiers_;
- goog.testing.Mock.prototype.$recording_ = true;
- goog.testing.Mock.prototype.$pendingExpectation;
- goog.testing.Mock.prototype.$threwException_ = null;
- goog.testing.Mock.prototype.$initializeFunctions_ = function(objectToMock) {
-
- var enumerableProperties = goog.object.getAllPropertyNames(
- objectToMock, false ,
- false );
- if (goog.isFunction(objectToMock)) {
- for (var i = 0; i < goog.testing.Mock.FUNCTION_PROTOTYPE_FIELDS_.length;
- i++) {
- var prop = goog.testing.Mock.FUNCTION_PROTOTYPE_FIELDS_[i];
-
-
- if (objectToMock[prop] !== Function.prototype[prop]) {
- enumerableProperties.push(prop);
- }
- }
- }
-
-
-
-
- for (var i = 0; i < goog.testing.Mock.OBJECT_PROTOTYPE_FIELDS_.length; i++) {
- var prop = goog.testing.Mock.OBJECT_PROTOTYPE_FIELDS_[i];
-
-
- if (objectToMock[prop] !== Object.prototype[prop]) {
- enumerableProperties.push(prop);
- }
- }
-
- for (var i = 0; i < enumerableProperties.length; i++) {
- var prop = enumerableProperties[i];
- if (typeof objectToMock[prop] == 'function') {
- this[prop] = goog.bind(this.$mockMethod, this, prop);
- if (this.$proxy) {
- this.$proxy[prop] = goog.bind(this.$mockMethod, this, prop);
- }
- }
- }
- };
- goog.testing.Mock.prototype.$registerArgumentListVerifier = function(
- methodName, fn) {
- this.$argumentListVerifiers_[methodName] = fn;
- return this;
- };
- goog.testing.Mock.prototype.$mockMethod = function(name) {
- try {
-
-
- var args = goog.array.slice(arguments, 1);
- if (this.$recording_) {
- this.$pendingExpectation = new goog.testing.MockExpectation(name);
- this.$pendingExpectation.argumentList = args;
- this.$recordExpectation();
- return this;
- } else {
- return this.$recordCall(name, args);
- }
- } catch (ex) {
- this.$recordAndThrow(ex);
- }
- };
- goog.testing.Mock.prototype.$recordExpectation = function() {};
- goog.testing.Mock.prototype.$recordCall = function(name, args) {
- return undefined;
- };
- goog.testing.Mock.prototype.$maybeThrow = function(expectation) {
- if (typeof expectation.exceptionToThrow != 'undefined') {
- throw expectation.exceptionToThrow;
- }
- };
- goog.testing.Mock.prototype.$do = function(expectation, args) {
- if (typeof expectation.toDo == 'undefined') {
- this.$maybeThrow(expectation);
- return expectation.returnValue;
- } else {
- return expectation.toDo.apply(this, args);
- }
- };
- goog.testing.Mock.prototype.$returns = function(val) {
- this.$pendingExpectation.returnValue = val;
- return this;
- };
- goog.testing.Mock.prototype.$throws = function(val) {
- this.$pendingExpectation.exceptionToThrow = val;
- return this;
- };
- goog.testing.Mock.prototype.$does = function(func) {
- this.$pendingExpectation.toDo = func;
- return this;
- };
- goog.testing.Mock.prototype.$atMostOnce = function() {
- this.$pendingExpectation.minCalls = 0;
- this.$pendingExpectation.maxCalls = 1;
- return this;
- };
- goog.testing.Mock.prototype.$atLeastOnce = function() {
- this.$pendingExpectation.maxCalls = Infinity;
- return this;
- };
- goog.testing.Mock.prototype.$once = function() {
- this.$pendingExpectation.minCalls = 1;
- this.$pendingExpectation.maxCalls = 1;
- return this;
- };
- goog.testing.Mock.prototype.$never = function() {
- this.$pendingExpectation.minCalls = 0;
- this.$pendingExpectation.maxCalls = 0;
- return this;
- };
- goog.testing.Mock.prototype.$anyTimes = function() {
- this.$pendingExpectation.minCalls = 0;
- this.$pendingExpectation.maxCalls = Infinity;
- return this;
- };
- goog.testing.Mock.prototype.$times = function(times) {
- this.$pendingExpectation.minCalls = times;
- this.$pendingExpectation.maxCalls = times;
- return this;
- };
- goog.testing.Mock.prototype.$replay = function() {
- this.$recording_ = false;
- };
- goog.testing.Mock.prototype.$reset = function() {
- this.$recording_ = true;
- this.$threwException_ = null;
- delete this.$pendingExpectation;
- };
- goog.testing.Mock.prototype.$throwException = function(comment, opt_message) {
- this.$recordAndThrow(new goog.testing.JsUnitException(comment, opt_message));
- };
- goog.testing.Mock.prototype.$recordAndThrow = function(ex) {
-
- if (ex['isJsUnitException']) {
- var testRunner = goog.global['G_testRunner'];
- if (testRunner) {
- var logTestFailureFunction = testRunner['logTestFailure'];
- if (logTestFailureFunction) {
- logTestFailureFunction.call(testRunner, ex);
- }
- }
- if (!this.$threwException_) {
-
- this.$threwException_ = ex;
- }
- }
- throw ex;
- };
- goog.testing.Mock.prototype.$verify = function() {
- if (this.$threwException_) {
- throw this.$threwException_;
- }
- };
- goog.testing.Mock.prototype.$verifyCall = function(expectation, name, args) {
- if (expectation.name != name) {
- return false;
- }
- var verifierFn =
- this.$argumentListVerifiers_.hasOwnProperty(expectation.name) ?
- this.$argumentListVerifiers_[expectation.name] :
- goog.testing.mockmatchers.flexibleArrayMatcher;
- return verifierFn(expectation.argumentList, args, expectation);
- };
- goog.testing.Mock.prototype.$argumentsAsString = function(args) {
- var retVal = [];
- for (var i = 0; i < args.length; i++) {
- try {
- retVal.push(goog.typeOf(args[i]));
- } catch (e) {
- retVal.push('[unknown]');
- }
- }
- return '(' + retVal.join(', ') + ')';
- };
- goog.testing.Mock.prototype.$throwCallException = function(
- name, args, opt_expectation) {
- var errorStringBuffer = [];
- var actualArgsString = this.$argumentsAsString(args);
- var expectedArgsString = opt_expectation ?
- this.$argumentsAsString(opt_expectation.argumentList) :
- '';
- if (opt_expectation && opt_expectation.name == name) {
- errorStringBuffer.push(
- 'Bad arguments to ', name, '().\n', 'Actual: ', actualArgsString, '\n',
- 'Expected: ', expectedArgsString, '\n',
- opt_expectation.getErrorMessage());
- } else {
- errorStringBuffer.push('Unexpected call to ', name, actualArgsString, '.');
- if (opt_expectation) {
- errorStringBuffer.push(
- '\nNext expected call was to ', opt_expectation.name,
- expectedArgsString);
- }
- }
- this.$throwException(errorStringBuffer.join(''));
- };
|