123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- // Copyright 2012 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Provides the built-in dictionary matcher methods like
- * hasEntry, hasEntries, hasKey, hasValue, etc.
- */
- goog.provide('goog.labs.testing.HasEntriesMatcher');
- goog.provide('goog.labs.testing.HasEntryMatcher');
- goog.provide('goog.labs.testing.HasKeyMatcher');
- goog.provide('goog.labs.testing.HasValueMatcher');
- goog.require('goog.asserts');
- goog.require('goog.labs.testing.Matcher');
- goog.require('goog.object');
- /**
- * The HasEntries matcher.
- *
- * @param {!Object} entries The entries to check in the object.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.HasEntriesMatcher = function(entries) {
- /**
- * @type {Object}
- * @private
- */
- this.entries_ = entries;
- };
- /**
- * Determines if an object has particular entries.
- *
- * @override
- */
- goog.labs.testing.HasEntriesMatcher.prototype.matches = function(actualObject) {
- goog.asserts.assertObject(actualObject, 'Expected an Object');
- var object = /** @type {!Object} */ (actualObject);
- return goog.object.every(this.entries_, function(value, key) {
- return goog.object.containsKey(object, key) && object[key] === value;
- });
- };
- /**
- * @override
- */
- goog.labs.testing.HasEntriesMatcher.prototype.describe = function(
- actualObject) {
- goog.asserts.assertObject(actualObject, 'Expected an Object');
- var object = /** @type {!Object} */ (actualObject);
- var errorString = 'Input object did not contain the following entries:\n';
- goog.object.forEach(this.entries_, function(value, key) {
- if (!goog.object.containsKey(object, key) || object[key] !== value) {
- errorString += key + ': ' + value + '\n';
- }
- });
- return errorString;
- };
- /**
- * The HasEntry matcher.
- *
- * @param {string} key The key for the entry.
- * @param {*} value The value for the key.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.HasEntryMatcher = function(key, value) {
- /**
- * @type {string}
- * @private
- */
- this.key_ = key;
- /**
- * @type {*}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if an object has a particular entry.
- *
- * @override
- */
- goog.labs.testing.HasEntryMatcher.prototype.matches = function(actualObject) {
- goog.asserts.assertObject(actualObject);
- return goog.object.containsKey(actualObject, this.key_) &&
- actualObject[this.key_] === this.value_;
- };
- /**
- * @override
- */
- goog.labs.testing.HasEntryMatcher.prototype.describe = function(actualObject) {
- goog.asserts.assertObject(actualObject);
- var errorMsg;
- if (goog.object.containsKey(actualObject, this.key_)) {
- errorMsg = 'Input object did not contain key: ' + this.key_;
- } else {
- errorMsg = 'Value for key did not match value: ' + this.value_;
- }
- return errorMsg;
- };
- /**
- * The HasKey matcher.
- *
- * @param {string} key The key to check in the object.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.HasKeyMatcher = function(key) {
- /**
- * @type {string}
- * @private
- */
- this.key_ = key;
- };
- /**
- * Determines if an object has a key.
- *
- * @override
- */
- goog.labs.testing.HasKeyMatcher.prototype.matches = function(actualObject) {
- goog.asserts.assertObject(actualObject);
- return goog.object.containsKey(actualObject, this.key_);
- };
- /**
- * @override
- */
- goog.labs.testing.HasKeyMatcher.prototype.describe = function(actualObject) {
- goog.asserts.assertObject(actualObject);
- return 'Input object did not contain the key: ' + this.key_;
- };
- /**
- * The HasValue matcher.
- *
- * @param {*} value The value to check in the object.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.HasValueMatcher = function(value) {
- /**
- * @type {*}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if an object contains a value
- *
- * @override
- */
- goog.labs.testing.HasValueMatcher.prototype.matches = function(actualObject) {
- goog.asserts.assertObject(actualObject, 'Expected an Object');
- var object = /** @type {!Object} */ (actualObject);
- return goog.object.containsValue(object, this.value_);
- };
- /**
- * @override
- */
- goog.labs.testing.HasValueMatcher.prototype.describe = function(actualObject) {
- return 'Input object did not contain the value: ' + this.value_;
- };
- /**
- * Gives a matcher that asserts an object contains all the given key-value pairs
- * in the input object.
- *
- * @param {!Object} entries The entries to check for presence in the object.
- *
- * @return {!goog.labs.testing.HasEntriesMatcher} A HasEntriesMatcher.
- */
- function hasEntries(entries) {
- return new goog.labs.testing.HasEntriesMatcher(entries);
- }
- /**
- * Gives a matcher that asserts an object contains the given key-value pair.
- *
- * @param {string} key The key to check for presence in the object.
- * @param {*} value The value to check for presence in the object.
- *
- * @return {!goog.labs.testing.HasEntryMatcher} A HasEntryMatcher.
- */
- function hasEntry(key, value) {
- return new goog.labs.testing.HasEntryMatcher(key, value);
- }
- /**
- * Gives a matcher that asserts an object contains the given key.
- *
- * @param {string} key The key to check for presence in the object.
- *
- * @return {!goog.labs.testing.HasKeyMatcher} A HasKeyMatcher.
- */
- function hasKey(key) {
- return new goog.labs.testing.HasKeyMatcher(key);
- }
- /**
- * Gives a matcher that asserts an object contains the given value.
- *
- * @param {*} value The value to check for presence in the object.
- *
- * @return {!goog.labs.testing.HasValueMatcher} A HasValueMatcher.
- */
- function hasValue(value) {
- return new goog.labs.testing.HasValueMatcher(value);
- }
|