Path: blob/trunk/third_party/closure/goog/labs/testing/objectmatcher.js
2868 views
// Copyright 2012 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Provides the built-in object matchers like equalsObject,16* hasProperty, instanceOf, etc.17*/1819goog.provide('goog.labs.testing.AnyObjectMatcher');20goog.provide('goog.labs.testing.HasPropertyMatcher');21goog.provide('goog.labs.testing.InstanceOfMatcher');22goog.provide('goog.labs.testing.IsNullMatcher');23goog.provide('goog.labs.testing.IsNullOrUndefinedMatcher');24goog.provide('goog.labs.testing.IsUndefinedMatcher');25goog.provide('goog.labs.testing.ObjectEqualsMatcher');2627goog.require('goog.labs.testing.Matcher');28293031/**32* Matches any object value.33*34* @constructor @struct @implements {goog.labs.testing.Matcher} @final35*/36goog.labs.testing.AnyObjectMatcher = function() {};373839/** @override */40goog.labs.testing.AnyObjectMatcher.prototype.matches = function(actualValue) {41return goog.isObject(actualValue);42};434445/** @override */46goog.labs.testing.AnyObjectMatcher.prototype.describe = function(actualValue) {47return '<' + actualValue + '> is not an object';48};49505152/**53* The Equals matcher.54*55* @param {!Object} expectedObject The expected object.56*57* @constructor58* @struct59* @implements {goog.labs.testing.Matcher}60* @final61*/62goog.labs.testing.ObjectEqualsMatcher = function(expectedObject) {63/**64* @type {!Object}65* @private66*/67this.object_ = expectedObject;68};697071/**72* Determines if two objects are the same.73*74* @override75*/76goog.labs.testing.ObjectEqualsMatcher.prototype.matches = function(77actualObject) {78return actualObject === this.object_;79};808182/**83* @override84*/85goog.labs.testing.ObjectEqualsMatcher.prototype.describe = function(86actualObject) {87return 'Input object is not the same as the expected object.';88};89909192/**93* The HasProperty matcher.94*95* @param {string} property Name of the property to test.96*97* @constructor98* @struct99* @implements {goog.labs.testing.Matcher}100* @final101*/102goog.labs.testing.HasPropertyMatcher = function(property) {103/**104* @type {string}105* @private106*/107this.property_ = property;108};109110111/**112* Determines if an object has a property.113*114* @override115*/116goog.labs.testing.HasPropertyMatcher.prototype.matches = function(117actualObject) {118return this.property_ in actualObject;119};120121122/**123* @override124*/125goog.labs.testing.HasPropertyMatcher.prototype.describe = function(126actualObject) {127return 'Object does not have property: ' + this.property_;128};129130131132/**133* The InstanceOf matcher.134*135* @param {!Object} object The expected class object.136*137* @constructor138* @struct139* @implements {goog.labs.testing.Matcher}140* @final141*/142goog.labs.testing.InstanceOfMatcher = function(object) {143/**144* @type {!Object}145* @private146*/147this.object_ = object;148};149150151/**152* Determines if an object is an instance of another object.153*154* @override155*/156goog.labs.testing.InstanceOfMatcher.prototype.matches = function(actualObject) {157return actualObject instanceof this.object_;158};159160161/**162* @override163*/164goog.labs.testing.InstanceOfMatcher.prototype.describe = function(165actualObject) {166return 'Input object is not an instance of the expected object';167};168169170171/**172* The IsNullOrUndefined matcher.173*174* @constructor175* @struct176* @implements {goog.labs.testing.Matcher}177* @final178*/179goog.labs.testing.IsNullOrUndefinedMatcher = function() {};180181182/**183* Determines if input value is null or undefined.184*185* @override186*/187goog.labs.testing.IsNullOrUndefinedMatcher.prototype.matches = function(188actualValue) {189return !goog.isDefAndNotNull(actualValue);190};191192193/**194* @override195*/196goog.labs.testing.IsNullOrUndefinedMatcher.prototype.describe = function(197actualValue) {198return actualValue + ' is not null or undefined.';199};200201202203/**204* The IsNull matcher.205*206* @constructor207* @struct208* @implements {goog.labs.testing.Matcher}209* @final210*/211goog.labs.testing.IsNullMatcher = function() {};212213214/**215* Determines if input value is null.216*217* @override218*/219goog.labs.testing.IsNullMatcher.prototype.matches = function(actualValue) {220return goog.isNull(actualValue);221};222223224/**225* @override226*/227goog.labs.testing.IsNullMatcher.prototype.describe = function(actualValue) {228return actualValue + ' is not null.';229};230231232233/**234* The IsUndefined matcher.235*236* @constructor237* @struct238* @implements {goog.labs.testing.Matcher}239* @final240*/241goog.labs.testing.IsUndefinedMatcher = function() {};242243244/**245* Determines if input value is undefined.246*247* @override248*/249goog.labs.testing.IsUndefinedMatcher.prototype.matches = function(actualValue) {250return !goog.isDef(actualValue);251};252253254/**255* @override256*/257goog.labs.testing.IsUndefinedMatcher.prototype.describe = function(258actualValue) {259return actualValue + ' is not undefined.';260};261262263/** @return {!goog.labs.testing.AnyObjectMatcher} */264function anyObject() {265return new goog.labs.testing.AnyObjectMatcher();266}267268269/**270* Returns a matcher that matches objects that are equal to the input object.271* Equality in this case means the two objects are references to the same272* object.273*274* @param {!Object} object The expected object.275*276* @return {!goog.labs.testing.ObjectEqualsMatcher} A277* ObjectEqualsMatcher.278*/279function equalsObject(object) {280return new goog.labs.testing.ObjectEqualsMatcher(object);281}282283284/**285* Returns a matcher that matches objects that contain the input property.286*287* @param {string} property The property name to check.288*289* @return {!goog.labs.testing.HasPropertyMatcher} A HasPropertyMatcher.290*/291function hasProperty(property) {292return new goog.labs.testing.HasPropertyMatcher(property);293}294295296/**297* Returns a matcher that matches instances of the input class.298*299* @param {!Object} object The class object.300*301* @return {!goog.labs.testing.InstanceOfMatcher} A302* InstanceOfMatcher.303*/304function instanceOfClass(object) {305return new goog.labs.testing.InstanceOfMatcher(object);306}307308309/**310* Returns a matcher that matches all null values.311*312* @return {!goog.labs.testing.IsNullMatcher} A IsNullMatcher.313*/314function isNull() {315return new goog.labs.testing.IsNullMatcher();316}317318319/**320* Returns a matcher that matches all null and undefined values.321*322* @return {!goog.labs.testing.IsNullOrUndefinedMatcher} A323* IsNullOrUndefinedMatcher.324*/325function isNullOrUndefined() {326return new goog.labs.testing.IsNullOrUndefinedMatcher();327}328329330/**331* Returns a matcher that matches undefined values.332*333* @return {!goog.labs.testing.IsUndefinedMatcher} A IsUndefinedMatcher.334*/335function isUndefined() {336return new goog.labs.testing.IsUndefinedMatcher();337}338339340