# lib/goog/labs/testing/objectmatcher.js

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