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