Path: blob/trunk/third_party/closure/goog/labs/testing/dictionarymatcher.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 dictionary matcher methods like16* hasEntry, hasEntries, hasKey, hasValue, etc.17*/18192021goog.provide('goog.labs.testing.HasEntriesMatcher');22goog.provide('goog.labs.testing.HasEntryMatcher');23goog.provide('goog.labs.testing.HasKeyMatcher');24goog.provide('goog.labs.testing.HasValueMatcher');252627goog.require('goog.asserts');28goog.require('goog.labs.testing.Matcher');29goog.require('goog.object');30313233/**34* The HasEntries matcher.35*36* @param {!Object} entries The entries to check in the object.37*38* @constructor39* @struct40* @implements {goog.labs.testing.Matcher}41* @final42*/43goog.labs.testing.HasEntriesMatcher = function(entries) {44/**45* @type {Object}46* @private47*/48this.entries_ = entries;49};505152/**53* Determines if an object has particular entries.54*55* @override56*/57goog.labs.testing.HasEntriesMatcher.prototype.matches = function(actualObject) {58goog.asserts.assertObject(actualObject, 'Expected an Object');59var object = /** @type {!Object} */ (actualObject);60return goog.object.every(this.entries_, function(value, key) {61return goog.object.containsKey(object, key) && object[key] === value;62});63};646566/**67* @override68*/69goog.labs.testing.HasEntriesMatcher.prototype.describe = function(70actualObject) {71goog.asserts.assertObject(actualObject, 'Expected an Object');72var object = /** @type {!Object} */ (actualObject);73var errorString = 'Input object did not contain the following entries:\n';74goog.object.forEach(this.entries_, function(value, key) {75if (!goog.object.containsKey(object, key) || object[key] !== value) {76errorString += key + ': ' + value + '\n';77}78});79return errorString;80};81828384/**85* The HasEntry matcher.86*87* @param {string} key The key for the entry.88* @param {*} value The value for the key.89*90* @constructor91* @struct92* @implements {goog.labs.testing.Matcher}93* @final94*/95goog.labs.testing.HasEntryMatcher = function(key, value) {96/**97* @type {string}98* @private99*/100this.key_ = key;101/**102* @type {*}103* @private104*/105this.value_ = value;106};107108109/**110* Determines if an object has a particular entry.111*112* @override113*/114goog.labs.testing.HasEntryMatcher.prototype.matches = function(actualObject) {115goog.asserts.assertObject(actualObject);116return goog.object.containsKey(actualObject, this.key_) &&117actualObject[this.key_] === this.value_;118};119120121/**122* @override123*/124goog.labs.testing.HasEntryMatcher.prototype.describe = function(actualObject) {125goog.asserts.assertObject(actualObject);126var errorMsg;127if (goog.object.containsKey(actualObject, this.key_)) {128errorMsg = 'Input object did not contain key: ' + this.key_;129} else {130errorMsg = 'Value for key did not match value: ' + this.value_;131}132return errorMsg;133};134135136137/**138* The HasKey matcher.139*140* @param {string} key The key to check in the object.141*142* @constructor143* @struct144* @implements {goog.labs.testing.Matcher}145* @final146*/147goog.labs.testing.HasKeyMatcher = function(key) {148/**149* @type {string}150* @private151*/152this.key_ = key;153};154155156/**157* Determines if an object has a key.158*159* @override160*/161goog.labs.testing.HasKeyMatcher.prototype.matches = function(actualObject) {162goog.asserts.assertObject(actualObject);163return goog.object.containsKey(actualObject, this.key_);164};165166167/**168* @override169*/170goog.labs.testing.HasKeyMatcher.prototype.describe = function(actualObject) {171goog.asserts.assertObject(actualObject);172return 'Input object did not contain the key: ' + this.key_;173};174175176177/**178* The HasValue matcher.179*180* @param {*} value The value to check in the object.181*182* @constructor183* @struct184* @implements {goog.labs.testing.Matcher}185* @final186*/187goog.labs.testing.HasValueMatcher = function(value) {188/**189* @type {*}190* @private191*/192this.value_ = value;193};194195196/**197* Determines if an object contains a value198*199* @override200*/201goog.labs.testing.HasValueMatcher.prototype.matches = function(actualObject) {202goog.asserts.assertObject(actualObject, 'Expected an Object');203var object = /** @type {!Object} */ (actualObject);204return goog.object.containsValue(object, this.value_);205};206207208/**209* @override210*/211goog.labs.testing.HasValueMatcher.prototype.describe = function(actualObject) {212return 'Input object did not contain the value: ' + this.value_;213};214215216/**217* Gives a matcher that asserts an object contains all the given key-value pairs218* in the input object.219*220* @param {!Object} entries The entries to check for presence in the object.221*222* @return {!goog.labs.testing.HasEntriesMatcher} A HasEntriesMatcher.223*/224function hasEntries(entries) {225return new goog.labs.testing.HasEntriesMatcher(entries);226}227228229/**230* Gives a matcher that asserts an object contains the given key-value pair.231*232* @param {string} key The key to check for presence in the object.233* @param {*} value The value to check for presence in the object.234*235* @return {!goog.labs.testing.HasEntryMatcher} A HasEntryMatcher.236*/237function hasEntry(key, value) {238return new goog.labs.testing.HasEntryMatcher(key, value);239}240241242/**243* Gives a matcher that asserts an object contains the given key.244*245* @param {string} key The key to check for presence in the object.246*247* @return {!goog.labs.testing.HasKeyMatcher} A HasKeyMatcher.248*/249function hasKey(key) {250return new goog.labs.testing.HasKeyMatcher(key);251}252253254/**255* Gives a matcher that asserts an object contains the given value.256*257* @param {*} value The value to check for presence in the object.258*259* @return {!goog.labs.testing.HasValueMatcher} A HasValueMatcher.260*/261function hasValue(value) {262return new goog.labs.testing.HasValueMatcher(value);263}264265266