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