# lib/goog/labs/testing/objectmatcher.js

    1// Copyright 2012 The Closure Library Authors. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS-IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415/**16 * @fileoverview Provides the built-in object matchers like equalsObject,17 *     hasProperty, instanceOf, etc.18 */19202122goog.provide('goog.labs.testing.HasPropertyMatcher');23goog.provide('goog.labs.testing.InstanceOfMatcher');24goog.provide('goog.labs.testing.IsNullMatcher');25goog.provide('goog.labs.testing.IsNullOrUndefinedMatcher');26goog.provide('goog.labs.testing.IsUndefinedMatcher');27goog.provide('goog.labs.testing.ObjectEqualsMatcher');282930goog.require('goog.labs.testing.Matcher');31goog.require('goog.string');32333435/**36 * The Equals matcher.37 *38 * @param {!Object} expectedObject The expected object.39 *40 * @constructor41 * @implements {goog.labs.testing.Matcher}42 */43goog.labs.testing.ObjectEqualsMatcher = function(expectedObject) {44  /**45   * @type {!Object}46   * @private47   */48  this.object_ = expectedObject;49};505152/**53 * Determines if two objects are the same.54 *55 * @override56 */57goog.labs.testing.ObjectEqualsMatcher.prototype.matches =58    function(actualObject) {59  return actualObject === this.object_;60};616263/**64 * @override65 */66goog.labs.testing.ObjectEqualsMatcher.prototype.describe =67    function(actualObject) {68  return 'Input object is not the same as the expected object.';69};70717273/**74 * The HasProperty matcher.75 *76 * @param {string} property Name of the property to test.77 *78 * @constructor79 * @implements {goog.labs.testing.Matcher}80 */81goog.labs.testing.HasPropertyMatcher = function(property) {82  /**83   * @type {string}84   * @private85   */86  this.property_ = property;87};888990/**91 * Determines if an object has a property.92 *93 * @override94 */95goog.labs.testing.HasPropertyMatcher.prototype.matches =96    function(actualObject) {97  return this.property_ in actualObject;98};99100101/**102 * @override103 */104goog.labs.testing.HasPropertyMatcher.prototype.describe =105    function(actualObject) {106  return 'Object does not have property: ' + this.property_;107};108109110111/**112 * The InstanceOf matcher.113 *114 * @param {!Object} object The expected class object.115 *116 * @constructor117 * @implements {goog.labs.testing.Matcher}118 */119goog.labs.testing.InstanceOfMatcher = function(object) {120  /**121   * @type {!Object}122   * @private123   */124  this.object_ = object;125};126127128/**129 * Determines if an object is an instance of another object.130 *131 * @override132 */133goog.labs.testing.InstanceOfMatcher.prototype.matches =134    function(actualObject) {135  return actualObject instanceof this.object_;136};137138139/**140 * @override141 */142goog.labs.testing.InstanceOfMatcher.prototype.describe =143    function(actualObject) {144  return 'Input object is not an instance of the expected object';145};146147148149/**150 * The IsNullOrUndefined matcher.151 *152 * @constructor153 * @implements {goog.labs.testing.Matcher}154 */155goog.labs.testing.IsNullOrUndefinedMatcher = function() {};156157158/**159 * Determines if input value is null or undefined.160 *161 * @override162 */163goog.labs.testing.IsNullOrUndefinedMatcher.prototype.matches =164    function(actualValue) {165  return !goog.isDefAndNotNull(actualValue);166};167168169/**170 * @override171 */172goog.labs.testing.IsNullOrUndefinedMatcher.prototype.describe =173    function(actualValue) {174  return actualValue + ' is not null or undefined.';175};176177178179/**180 * The IsNull matcher.181 *182 * @constructor183 * @implements {goog.labs.testing.Matcher}184 */185goog.labs.testing.IsNullMatcher = function() {};186187188/**189 * Determines if input value is null.190 *191 * @override192 */193goog.labs.testing.IsNullMatcher.prototype.matches =194    function(actualValue) {195  return goog.isNull(actualValue);196};197198199/**200 * @override201 */202goog.labs.testing.IsNullMatcher.prototype.describe =203    function(actualValue) {204  return actualValue + ' is not null.';205};206207208209/**210 * The IsUndefined matcher.211 *212 * @constructor213 * @implements {goog.labs.testing.Matcher}214 */215goog.labs.testing.IsUndefinedMatcher = function() {};216217218/**219 * Determines if input value is undefined.220 *221 * @override222 */223goog.labs.testing.IsUndefinedMatcher.prototype.matches =224    function(actualValue) {225  return !goog.isDef(actualValue);226};227228229/**230 * @override231 */232goog.labs.testing.IsUndefinedMatcher.prototype.describe =233    function(actualValue) {234  return actualValue + ' is not undefined.';235};236237238/**239 * Returns a matcher that matches objects that are equal to the input object.240 * Equality in this case means the two objects are references to the same241 * object.242 *243 * @param {!Object} object The expected object.244 *245 * @return {!goog.labs.testing.ObjectEqualsMatcher} A246 *     ObjectEqualsMatcher.247 */248function equalsObject(object) {249  return new goog.labs.testing.ObjectEqualsMatcher(object);250}251252253/**254 * Returns a matcher that matches objects that contain the input property.255 *256 * @param {string} property The property name to check.257 *258 * @return {!goog.labs.testing.HasPropertyMatcher} A HasPropertyMatcher.259 */260function hasProperty(property) {261  return new goog.labs.testing.HasPropertyMatcher(property);262}263264265/**266 * Returns a matcher that matches instances of the input class.267 *268 * @param {!Object} object The class object.269 *270 * @return {!goog.labs.testing.InstanceOfMatcher} A271 *     InstanceOfMatcher.272 */273function instanceOfClass(object) {274  return new goog.labs.testing.InstanceOfMatcher(object);275}276277278/**279 * Returns a matcher that matches all null values.280 *281 * @return {!goog.labs.testing.IsNullMatcher} A IsNullMatcher.282 */283function isNull() {284  return new goog.labs.testing.IsNullMatcher();285}286287288/**289 * Returns a matcher that matches all null and undefined values.290 *291 * @return {!goog.labs.testing.IsNullOrUndefinedMatcher} A292 *     IsNullOrUndefinedMatcher.293 */294function isNullOrUndefined() {295  return new goog.labs.testing.IsNullOrUndefinedMatcher();296}297298299/**300 * Returns a matcher that matches undefined values.301 *302 * @return {!goog.labs.testing.IsUndefinedMatcher} A IsUndefinedMatcher.303 */304function isUndefined() {305  return new goog.labs.testing.IsUndefinedMatcher();306}