dictionarymatcher.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // 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 and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides the built-in dictionary matcher methods like
  16. * hasEntry, hasEntries, hasKey, hasValue, etc.
  17. */
  18. goog.provide('goog.labs.testing.HasEntriesMatcher');
  19. goog.provide('goog.labs.testing.HasEntryMatcher');
  20. goog.provide('goog.labs.testing.HasKeyMatcher');
  21. goog.provide('goog.labs.testing.HasValueMatcher');
  22. goog.require('goog.asserts');
  23. goog.require('goog.labs.testing.Matcher');
  24. goog.require('goog.object');
  25. /**
  26. * The HasEntries matcher.
  27. *
  28. * @param {!Object} entries The entries to check in the object.
  29. *
  30. * @constructor
  31. * @struct
  32. * @implements {goog.labs.testing.Matcher}
  33. * @final
  34. */
  35. goog.labs.testing.HasEntriesMatcher = function(entries) {
  36. /**
  37. * @type {Object}
  38. * @private
  39. */
  40. this.entries_ = entries;
  41. };
  42. /**
  43. * Determines if an object has particular entries.
  44. *
  45. * @override
  46. */
  47. goog.labs.testing.HasEntriesMatcher.prototype.matches = function(actualObject) {
  48. goog.asserts.assertObject(actualObject, 'Expected an Object');
  49. var object = /** @type {!Object} */ (actualObject);
  50. return goog.object.every(this.entries_, function(value, key) {
  51. return goog.object.containsKey(object, key) && object[key] === value;
  52. });
  53. };
  54. /**
  55. * @override
  56. */
  57. goog.labs.testing.HasEntriesMatcher.prototype.describe = function(
  58. actualObject) {
  59. goog.asserts.assertObject(actualObject, 'Expected an Object');
  60. var object = /** @type {!Object} */ (actualObject);
  61. var errorString = 'Input object did not contain the following entries:\n';
  62. goog.object.forEach(this.entries_, function(value, key) {
  63. if (!goog.object.containsKey(object, key) || object[key] !== value) {
  64. errorString += key + ': ' + value + '\n';
  65. }
  66. });
  67. return errorString;
  68. };
  69. /**
  70. * The HasEntry matcher.
  71. *
  72. * @param {string} key The key for the entry.
  73. * @param {*} value The value for the key.
  74. *
  75. * @constructor
  76. * @struct
  77. * @implements {goog.labs.testing.Matcher}
  78. * @final
  79. */
  80. goog.labs.testing.HasEntryMatcher = function(key, value) {
  81. /**
  82. * @type {string}
  83. * @private
  84. */
  85. this.key_ = key;
  86. /**
  87. * @type {*}
  88. * @private
  89. */
  90. this.value_ = value;
  91. };
  92. /**
  93. * Determines if an object has a particular entry.
  94. *
  95. * @override
  96. */
  97. goog.labs.testing.HasEntryMatcher.prototype.matches = function(actualObject) {
  98. goog.asserts.assertObject(actualObject);
  99. return goog.object.containsKey(actualObject, this.key_) &&
  100. actualObject[this.key_] === this.value_;
  101. };
  102. /**
  103. * @override
  104. */
  105. goog.labs.testing.HasEntryMatcher.prototype.describe = function(actualObject) {
  106. goog.asserts.assertObject(actualObject);
  107. var errorMsg;
  108. if (goog.object.containsKey(actualObject, this.key_)) {
  109. errorMsg = 'Input object did not contain key: ' + this.key_;
  110. } else {
  111. errorMsg = 'Value for key did not match value: ' + this.value_;
  112. }
  113. return errorMsg;
  114. };
  115. /**
  116. * The HasKey matcher.
  117. *
  118. * @param {string} key The key to check in the object.
  119. *
  120. * @constructor
  121. * @struct
  122. * @implements {goog.labs.testing.Matcher}
  123. * @final
  124. */
  125. goog.labs.testing.HasKeyMatcher = function(key) {
  126. /**
  127. * @type {string}
  128. * @private
  129. */
  130. this.key_ = key;
  131. };
  132. /**
  133. * Determines if an object has a key.
  134. *
  135. * @override
  136. */
  137. goog.labs.testing.HasKeyMatcher.prototype.matches = function(actualObject) {
  138. goog.asserts.assertObject(actualObject);
  139. return goog.object.containsKey(actualObject, this.key_);
  140. };
  141. /**
  142. * @override
  143. */
  144. goog.labs.testing.HasKeyMatcher.prototype.describe = function(actualObject) {
  145. goog.asserts.assertObject(actualObject);
  146. return 'Input object did not contain the key: ' + this.key_;
  147. };
  148. /**
  149. * The HasValue matcher.
  150. *
  151. * @param {*} value The value to check in the object.
  152. *
  153. * @constructor
  154. * @struct
  155. * @implements {goog.labs.testing.Matcher}
  156. * @final
  157. */
  158. goog.labs.testing.HasValueMatcher = function(value) {
  159. /**
  160. * @type {*}
  161. * @private
  162. */
  163. this.value_ = value;
  164. };
  165. /**
  166. * Determines if an object contains a value
  167. *
  168. * @override
  169. */
  170. goog.labs.testing.HasValueMatcher.prototype.matches = function(actualObject) {
  171. goog.asserts.assertObject(actualObject, 'Expected an Object');
  172. var object = /** @type {!Object} */ (actualObject);
  173. return goog.object.containsValue(object, this.value_);
  174. };
  175. /**
  176. * @override
  177. */
  178. goog.labs.testing.HasValueMatcher.prototype.describe = function(actualObject) {
  179. return 'Input object did not contain the value: ' + this.value_;
  180. };
  181. /**
  182. * Gives a matcher that asserts an object contains all the given key-value pairs
  183. * in the input object.
  184. *
  185. * @param {!Object} entries The entries to check for presence in the object.
  186. *
  187. * @return {!goog.labs.testing.HasEntriesMatcher} A HasEntriesMatcher.
  188. */
  189. function hasEntries(entries) {
  190. return new goog.labs.testing.HasEntriesMatcher(entries);
  191. }
  192. /**
  193. * Gives a matcher that asserts an object contains the given key-value pair.
  194. *
  195. * @param {string} key The key to check for presence in the object.
  196. * @param {*} value The value to check for presence in the object.
  197. *
  198. * @return {!goog.labs.testing.HasEntryMatcher} A HasEntryMatcher.
  199. */
  200. function hasEntry(key, value) {
  201. return new goog.labs.testing.HasEntryMatcher(key, value);
  202. }
  203. /**
  204. * Gives a matcher that asserts an object contains the given key.
  205. *
  206. * @param {string} key The key to check for presence in the object.
  207. *
  208. * @return {!goog.labs.testing.HasKeyMatcher} A HasKeyMatcher.
  209. */
  210. function hasKey(key) {
  211. return new goog.labs.testing.HasKeyMatcher(key);
  212. }
  213. /**
  214. * Gives a matcher that asserts an object contains the given value.
  215. *
  216. * @param {*} value The value to check for presence in the object.
  217. *
  218. * @return {!goog.labs.testing.HasValueMatcher} A HasValueMatcher.
  219. */
  220. function hasValue(value) {
  221. return new goog.labs.testing.HasValueMatcher(value);
  222. }