aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/labs/testing/objectmatcher.js
blob: 974d024da95a648d323dc89947d4e6eed57cd26e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Provides the built-in object matchers like equalsObject,
 *     hasProperty, instanceOf, etc.
 */



goog.provide('goog.labs.testing.HasPropertyMatcher');
goog.provide('goog.labs.testing.InstanceOfMatcher');
goog.provide('goog.labs.testing.IsNullMatcher');
goog.provide('goog.labs.testing.IsNullOrUndefinedMatcher');
goog.provide('goog.labs.testing.IsUndefinedMatcher');
goog.provide('goog.labs.testing.ObjectEqualsMatcher');


goog.require('goog.labs.testing.Matcher');
goog.require('goog.string');



/**
 * The Equals matcher.
 *
 * @param {!Object} expectedObject The expected object.
 *
 * @constructor
 * @implements {goog.labs.testing.Matcher}
 */
goog.labs.testing.ObjectEqualsMatcher = function(expectedObject) {
  /**
   * @type {!Object}
   * @private
   */
  this.object_ = expectedObject;
};


/**
 * Determines if two objects are the same.
 *
 * @override
 */
goog.labs.testing.ObjectEqualsMatcher.prototype.matches =
    function(actualObject) {
  return actualObject === this.object_;
};


/**
 * @override
 */
goog.labs.testing.ObjectEqualsMatcher.prototype.describe =
    function(actualObject) {
  return 'Input object is not the same as the expected object.';
};



/**
 * The HasProperty matcher.
 *
 * @param {string} property Name of the property to test.
 *
 * @constructor
 * @implements {goog.labs.testing.Matcher}
 */
goog.labs.testing.HasPropertyMatcher = function(property) {
  /**
   * @type {string}
   * @private
   */
  this.property_ = property;
};


/**
 * Determines if an object has a property.
 *
 * @override
 */
goog.labs.testing.HasPropertyMatcher.prototype.matches =
    function(actualObject) {
  return this.property_ in actualObject;
};


/**
 * @override
 */
goog.labs.testing.HasPropertyMatcher.prototype.describe =
    function(actualObject) {
  return 'Object does not have property: ' + this.property_;
};



/**
 * The InstanceOf matcher.
 *
 * @param {!Object} object The expected class object.
 *
 * @constructor
 * @implements {goog.labs.testing.Matcher}
 */
goog.labs.testing.InstanceOfMatcher = function(object) {
  /**
   * @type {!Object}
   * @private
   */
  this.object_ = object;
};


/**
 * Determines if an object is an instance of another object.
 *
 * @override
 */
goog.labs.testing.InstanceOfMatcher.prototype.matches =
    function(actualObject) {
  return actualObject instanceof this.object_;
};


/**
 * @override
 */
goog.labs.testing.InstanceOfMatcher.prototype.describe =
    function(actualObject) {
  return 'Input object is not an instance of the expected object';
};



/**
 * The IsNullOrUndefined matcher.
 *
 * @constructor
 * @implements {goog.labs.testing.Matcher}
 */
goog.labs.testing.IsNullOrUndefinedMatcher = function() {};


/**
 * Determines if input value is null or undefined.
 *
 * @override
 */
goog.labs.testing.IsNullOrUndefinedMatcher.prototype.matches =
    function(actualValue) {
  return !goog.isDefAndNotNull(actualValue);
};


/**
 * @override
 */
goog.labs.testing.IsNullOrUndefinedMatcher.prototype.describe =
    function(actualValue) {
  return actualValue + ' is not null or undefined.';
};



/**
 * The IsNull matcher.
 *
 * @constructor
 * @implements {goog.labs.testing.Matcher}
 */
goog.labs.testing.IsNullMatcher = function() {};


/**
 * Determines if input value is null.
 *
 * @override
 */
goog.labs.testing.IsNullMatcher.prototype.matches =
    function(actualValue) {
  return goog.isNull(actualValue);
};


/**
 * @override
 */
goog.labs.testing.IsNullMatcher.prototype.describe =
    function(actualValue) {
  return actualValue + ' is not null.';
};



/**
 * The IsUndefined matcher.
 *
 * @constructor
 * @implements {goog.labs.testing.Matcher}
 */
goog.labs.testing.IsUndefinedMatcher = function() {};


/**
 * Determines if input value is undefined.
 *
 * @override
 */
goog.labs.testing.IsUndefinedMatcher.prototype.matches =
    function(actualValue) {
  return !goog.isDef(actualValue);
};


/**
 * @override
 */
goog.labs.testing.IsUndefinedMatcher.prototype.describe =
    function(actualValue) {
  return actualValue + ' is not undefined.';
};


/**
 * Returns a matcher that matches objects that are equal to the input object.
 * Equality in this case means the two objects are references to the same
 * object.
 *
 * @param {!Object} object The expected object.
 *
 * @return {!goog.labs.testing.ObjectEqualsMatcher} A
 *     ObjectEqualsMatcher.
 */
function equalsObject(object) {
  return new goog.labs.testing.ObjectEqualsMatcher(object);
}


/**
 * Returns a matcher that matches objects that contain the input property.
 *
 * @param {string} property The property name to check.
 *
 * @return {!goog.labs.testing.HasPropertyMatcher} A HasPropertyMatcher.
 */
function hasProperty(property) {
  return new goog.labs.testing.HasPropertyMatcher(property);
}


/**
 * Returns a matcher that matches instances of the input class.
 *
 * @param {!Object} object The class object.
 *
 * @return {!goog.labs.testing.InstanceOfMatcher} A
 *     InstanceOfMatcher.
 */
function instanceOfClass(object) {
  return new goog.labs.testing.InstanceOfMatcher(object);
}


/**
 * Returns a matcher that matches all null values.
 *
 * @return {!goog.labs.testing.IsNullMatcher} A IsNullMatcher.
 */
function isNull() {
  return new goog.labs.testing.IsNullMatcher();
}


/**
 * Returns a matcher that matches all null and undefined values.
 *
 * @return {!goog.labs.testing.IsNullOrUndefinedMatcher} A
 *     IsNullOrUndefinedMatcher.
 */
function isNullOrUndefined() {
  return new goog.labs.testing.IsNullOrUndefinedMatcher();
}


/**
 * Returns a matcher that matches undefined values.
 *
 * @return {!goog.labs.testing.IsUndefinedMatcher} A IsUndefinedMatcher.
 */
function isUndefined() {
  return new goog.labs.testing.IsUndefinedMatcher();
}