Debian Codegraph @

closure-library/closure/goog/labs/testing/objectmatcher.js

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