# 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 * @struct42 * @implements {goog.labs.testing.Matcher}43 * @final44 */45goog.labs.testing.ObjectEqualsMatcher = function(expectedObject) {46  /**47   * @type {!Object}48   * @private49   */50  this.object_ = expectedObject;51};525354/**55 * Determines if two objects are the same.56 *57 * @override58 */59goog.labs.testing.ObjectEqualsMatcher.prototype.matches =60    function(actualObject) {61  return actualObject === this.object_;62};636465/**66 * @override67 */68goog.labs.testing.ObjectEqualsMatcher.prototype.describe =69    function(actualObject) {70  return 'Input object is not the same as the expected object.';71};72737475/**76 * The HasProperty matcher.77 *78 * @param {string} property Name of the property to test.79 *80 * @constructor81 * @struct82 * @implements {goog.labs.testing.Matcher}83 * @final84 */85goog.labs.testing.HasPropertyMatcher = function(property) {86  /**87   * @type {string}88   * @private89   */90  this.property_ = property;91};929394/**95 * Determines if an object has a property.96 *97 * @override98 */99goog.labs.testing.HasPropertyMatcher.prototype.matches =100    function(actualObject) {101  return this.property_ in actualObject;102};103104105/**106 * @override107 */108goog.labs.testing.HasPropertyMatcher.prototype.describe =109    function(actualObject) {110  return 'Object does not have property: ' + this.property_;111};112113114115/**116 * The InstanceOf matcher.117 *118 * @param {!Object} object The expected class object.119 *120 * @constructor121 * @struct122 * @implements {goog.labs.testing.Matcher}123 * @final124 */125goog.labs.testing.InstanceOfMatcher = function(object) {126  /**127   * @type {!Object}128   * @private129   */130  this.object_ = object;131};132133134/**135 * Determines if an object is an instance of another object.136 *137 * @override138 */139goog.labs.testing.InstanceOfMatcher.prototype.matches =140    function(actualObject) {141  return actualObject instanceof this.object_;142};143144145/**146 * @override147 */148goog.labs.testing.InstanceOfMatcher.prototype.describe =149    function(actualObject) {150  return 'Input object is not an instance of the expected object';151};152153154155/**156 * The IsNullOrUndefined matcher.157 *158 * @constructor159 * @struct160 * @implements {goog.labs.testing.Matcher}161 * @final162 */163goog.labs.testing.IsNullOrUndefinedMatcher = function() {};164165166/**167 * Determines if input value is null or undefined.168 *169 * @override170 */171goog.labs.testing.IsNullOrUndefinedMatcher.prototype.matches =172    function(actualValue) {173  return !goog.isDefAndNotNull(actualValue);174};175176177/**178 * @override179 */180goog.labs.testing.IsNullOrUndefinedMatcher.prototype.describe =181    function(actualValue) {182  return actualValue + ' is not null or undefined.';183};184185186187/**188 * The IsNull matcher.189 *190 * @constructor191 * @struct192 * @implements {goog.labs.testing.Matcher}193 * @final194 */195goog.labs.testing.IsNullMatcher = function() {};196197198/**199 * Determines if input value is null.200 *201 * @override202 */203goog.labs.testing.IsNullMatcher.prototype.matches =204    function(actualValue) {205  return goog.isNull(actualValue);206};207208209/**210 * @override211 */212goog.labs.testing.IsNullMatcher.prototype.describe =213    function(actualValue) {214  return actualValue + ' is not null.';215};216217218219/**220 * The IsUndefined matcher.221 *222 * @constructor223 * @struct224 * @implements {goog.labs.testing.Matcher}225 * @final226 */227goog.labs.testing.IsUndefinedMatcher = function() {};228229230/**231 * Determines if input value is undefined.232 *233 * @override234 */235goog.labs.testing.IsUndefinedMatcher.prototype.matches =236    function(actualValue) {237  return !goog.isDef(actualValue);238};239240241/**242 * @override243 */244goog.labs.testing.IsUndefinedMatcher.prototype.describe =245    function(actualValue) {246  return actualValue + ' is not undefined.';247};248249250/**251 * Returns a matcher that matches objects that are equal to the input object.252 * Equality in this case means the two objects are references to the same253 * object.254 *255 * @param {!Object} object The expected object.256 *257 * @return {!goog.labs.testing.ObjectEqualsMatcher} A258 *     ObjectEqualsMatcher.259 */260function equalsObject(object) {261  return new goog.labs.testing.ObjectEqualsMatcher(object);262}263264265/**266 * Returns a matcher that matches objects that contain the input property.267 *268 * @param {string} property The property name to check.269 *270 * @return {!goog.labs.testing.HasPropertyMatcher} A HasPropertyMatcher.271 */272function hasProperty(property) {273  return new goog.labs.testing.HasPropertyMatcher(property);274}275276277/**278 * Returns a matcher that matches instances of the input class.279 *280 * @param {!Object} object The class object.281 *282 * @return {!goog.labs.testing.InstanceOfMatcher} A283 *     InstanceOfMatcher.284 */285function instanceOfClass(object) {286  return new goog.labs.testing.InstanceOfMatcher(object);287}288289290/**291 * Returns a matcher that matches all null values.292 *293 * @return {!goog.labs.testing.IsNullMatcher} A IsNullMatcher.294 */295function isNull() {296  return new goog.labs.testing.IsNullMatcher();297}298299300/**301 * Returns a matcher that matches all null and undefined values.302 *303 * @return {!goog.labs.testing.IsNullOrUndefinedMatcher} A304 *     IsNullOrUndefinedMatcher.305 */306function isNullOrUndefined() {307  return new goog.labs.testing.IsNullOrUndefinedMatcher();308}309310311/**312 * Returns a matcher that matches undefined values.313 *314 * @return {!goog.labs.testing.IsUndefinedMatcher} A IsUndefinedMatcher.315 */316function isUndefined() {317  return new goog.labs.testing.IsUndefinedMatcher();318}