objectmatcher.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 object matchers like equalsObject,
  16. * hasProperty, instanceOf, etc.
  17. */
  18. goog.provide('goog.labs.testing.AnyObjectMatcher');
  19. goog.provide('goog.labs.testing.HasPropertyMatcher');
  20. goog.provide('goog.labs.testing.InstanceOfMatcher');
  21. goog.provide('goog.labs.testing.IsNullMatcher');
  22. goog.provide('goog.labs.testing.IsNullOrUndefinedMatcher');
  23. goog.provide('goog.labs.testing.IsUndefinedMatcher');
  24. goog.provide('goog.labs.testing.ObjectEqualsMatcher');
  25. goog.require('goog.labs.testing.Matcher');
  26. /**
  27. * Matches any object value.
  28. *
  29. * @constructor @struct @implements {goog.labs.testing.Matcher} @final
  30. */
  31. goog.labs.testing.AnyObjectMatcher = function() {};
  32. /** @override */
  33. goog.labs.testing.AnyObjectMatcher.prototype.matches = function(actualValue) {
  34. return goog.isObject(actualValue);
  35. };
  36. /** @override */
  37. goog.labs.testing.AnyObjectMatcher.prototype.describe = function(actualValue) {
  38. return '<' + actualValue + '> is not an object';
  39. };
  40. /**
  41. * The Equals matcher.
  42. *
  43. * @param {!Object} expectedObject The expected object.
  44. *
  45. * @constructor
  46. * @struct
  47. * @implements {goog.labs.testing.Matcher}
  48. * @final
  49. */
  50. goog.labs.testing.ObjectEqualsMatcher = function(expectedObject) {
  51. /**
  52. * @type {!Object}
  53. * @private
  54. */
  55. this.object_ = expectedObject;
  56. };
  57. /**
  58. * Determines if two objects are the same.
  59. *
  60. * @override
  61. */
  62. goog.labs.testing.ObjectEqualsMatcher.prototype.matches = function(
  63. actualObject) {
  64. return actualObject === this.object_;
  65. };
  66. /**
  67. * @override
  68. */
  69. goog.labs.testing.ObjectEqualsMatcher.prototype.describe = function(
  70. actualObject) {
  71. return 'Input object is not the same as the expected object.';
  72. };
  73. /**
  74. * The HasProperty matcher.
  75. *
  76. * @param {string} property Name of the property to test.
  77. *
  78. * @constructor
  79. * @struct
  80. * @implements {goog.labs.testing.Matcher}
  81. * @final
  82. */
  83. goog.labs.testing.HasPropertyMatcher = function(property) {
  84. /**
  85. * @type {string}
  86. * @private
  87. */
  88. this.property_ = property;
  89. };
  90. /**
  91. * Determines if an object has a property.
  92. *
  93. * @override
  94. */
  95. goog.labs.testing.HasPropertyMatcher.prototype.matches = function(
  96. actualObject) {
  97. return this.property_ in actualObject;
  98. };
  99. /**
  100. * @override
  101. */
  102. goog.labs.testing.HasPropertyMatcher.prototype.describe = function(
  103. actualObject) {
  104. return 'Object does not have property: ' + this.property_;
  105. };
  106. /**
  107. * The InstanceOf matcher.
  108. *
  109. * @param {!Object} object The expected class object.
  110. *
  111. * @constructor
  112. * @struct
  113. * @implements {goog.labs.testing.Matcher}
  114. * @final
  115. */
  116. goog.labs.testing.InstanceOfMatcher = function(object) {
  117. /**
  118. * @type {!Object}
  119. * @private
  120. */
  121. this.object_ = object;
  122. };
  123. /**
  124. * Determines if an object is an instance of another object.
  125. *
  126. * @override
  127. */
  128. goog.labs.testing.InstanceOfMatcher.prototype.matches = function(actualObject) {
  129. return actualObject instanceof this.object_;
  130. };
  131. /**
  132. * @override
  133. */
  134. goog.labs.testing.InstanceOfMatcher.prototype.describe = function(
  135. actualObject) {
  136. return 'Input object is not an instance of the expected object';
  137. };
  138. /**
  139. * The IsNullOrUndefined matcher.
  140. *
  141. * @constructor
  142. * @struct
  143. * @implements {goog.labs.testing.Matcher}
  144. * @final
  145. */
  146. goog.labs.testing.IsNullOrUndefinedMatcher = function() {};
  147. /**
  148. * Determines if input value is null or undefined.
  149. *
  150. * @override
  151. */
  152. goog.labs.testing.IsNullOrUndefinedMatcher.prototype.matches = function(
  153. actualValue) {
  154. return !goog.isDefAndNotNull(actualValue);
  155. };
  156. /**
  157. * @override
  158. */
  159. goog.labs.testing.IsNullOrUndefinedMatcher.prototype.describe = function(
  160. actualValue) {
  161. return actualValue + ' is not null or undefined.';
  162. };
  163. /**
  164. * The IsNull matcher.
  165. *
  166. * @constructor
  167. * @struct
  168. * @implements {goog.labs.testing.Matcher}
  169. * @final
  170. */
  171. goog.labs.testing.IsNullMatcher = function() {};
  172. /**
  173. * Determines if input value is null.
  174. *
  175. * @override
  176. */
  177. goog.labs.testing.IsNullMatcher.prototype.matches = function(actualValue) {
  178. return goog.isNull(actualValue);
  179. };
  180. /**
  181. * @override
  182. */
  183. goog.labs.testing.IsNullMatcher.prototype.describe = function(actualValue) {
  184. return actualValue + ' is not null.';
  185. };
  186. /**
  187. * The IsUndefined matcher.
  188. *
  189. * @constructor
  190. * @struct
  191. * @implements {goog.labs.testing.Matcher}
  192. * @final
  193. */
  194. goog.labs.testing.IsUndefinedMatcher = function() {};
  195. /**
  196. * Determines if input value is undefined.
  197. *
  198. * @override
  199. */
  200. goog.labs.testing.IsUndefinedMatcher.prototype.matches = function(actualValue) {
  201. return !goog.isDef(actualValue);
  202. };
  203. /**
  204. * @override
  205. */
  206. goog.labs.testing.IsUndefinedMatcher.prototype.describe = function(
  207. actualValue) {
  208. return actualValue + ' is not undefined.';
  209. };
  210. /** @return {!goog.labs.testing.AnyObjectMatcher} */
  211. function anyObject() {
  212. return new goog.labs.testing.AnyObjectMatcher();
  213. }
  214. /**
  215. * Returns a matcher that matches objects that are equal to the input object.
  216. * Equality in this case means the two objects are references to the same
  217. * object.
  218. *
  219. * @param {!Object} object The expected object.
  220. *
  221. * @return {!goog.labs.testing.ObjectEqualsMatcher} A
  222. * ObjectEqualsMatcher.
  223. */
  224. function equalsObject(object) {
  225. return new goog.labs.testing.ObjectEqualsMatcher(object);
  226. }
  227. /**
  228. * Returns a matcher that matches objects that contain the input property.
  229. *
  230. * @param {string} property The property name to check.
  231. *
  232. * @return {!goog.labs.testing.HasPropertyMatcher} A HasPropertyMatcher.
  233. */
  234. function hasProperty(property) {
  235. return new goog.labs.testing.HasPropertyMatcher(property);
  236. }
  237. /**
  238. * Returns a matcher that matches instances of the input class.
  239. *
  240. * @param {!Object} object The class object.
  241. *
  242. * @return {!goog.labs.testing.InstanceOfMatcher} A
  243. * InstanceOfMatcher.
  244. */
  245. function instanceOfClass(object) {
  246. return new goog.labs.testing.InstanceOfMatcher(object);
  247. }
  248. /**
  249. * Returns a matcher that matches all null values.
  250. *
  251. * @return {!goog.labs.testing.IsNullMatcher} A IsNullMatcher.
  252. */
  253. function isNull() {
  254. return new goog.labs.testing.IsNullMatcher();
  255. }
  256. /**
  257. * Returns a matcher that matches all null and undefined values.
  258. *
  259. * @return {!goog.labs.testing.IsNullOrUndefinedMatcher} A
  260. * IsNullOrUndefinedMatcher.
  261. */
  262. function isNullOrUndefined() {
  263. return new goog.labs.testing.IsNullOrUndefinedMatcher();
  264. }
  265. /**
  266. * Returns a matcher that matches undefined values.
  267. *
  268. * @return {!goog.labs.testing.IsUndefinedMatcher} A IsUndefinedMatcher.
  269. */
  270. function isUndefined() {
  271. return new goog.labs.testing.IsUndefinedMatcher();
  272. }