Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/labs/testing/dictionarymatcher.js
2868 views
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 dictionary matcher methods like
17
* hasEntry, hasEntries, hasKey, hasValue, etc.
18
*/
19
20
21
22
goog.provide('goog.labs.testing.HasEntriesMatcher');
23
goog.provide('goog.labs.testing.HasEntryMatcher');
24
goog.provide('goog.labs.testing.HasKeyMatcher');
25
goog.provide('goog.labs.testing.HasValueMatcher');
26
27
28
goog.require('goog.asserts');
29
goog.require('goog.labs.testing.Matcher');
30
goog.require('goog.object');
31
32
33
34
/**
35
* The HasEntries matcher.
36
*
37
* @param {!Object} entries The entries to check in the object.
38
*
39
* @constructor
40
* @struct
41
* @implements {goog.labs.testing.Matcher}
42
* @final
43
*/
44
goog.labs.testing.HasEntriesMatcher = function(entries) {
45
/**
46
* @type {Object}
47
* @private
48
*/
49
this.entries_ = entries;
50
};
51
52
53
/**
54
* Determines if an object has particular entries.
55
*
56
* @override
57
*/
58
goog.labs.testing.HasEntriesMatcher.prototype.matches = function(actualObject) {
59
goog.asserts.assertObject(actualObject, 'Expected an Object');
60
var object = /** @type {!Object} */ (actualObject);
61
return goog.object.every(this.entries_, function(value, key) {
62
return goog.object.containsKey(object, key) && object[key] === value;
63
});
64
};
65
66
67
/**
68
* @override
69
*/
70
goog.labs.testing.HasEntriesMatcher.prototype.describe = function(
71
actualObject) {
72
goog.asserts.assertObject(actualObject, 'Expected an Object');
73
var object = /** @type {!Object} */ (actualObject);
74
var errorString = 'Input object did not contain the following entries:\n';
75
goog.object.forEach(this.entries_, function(value, key) {
76
if (!goog.object.containsKey(object, key) || object[key] !== value) {
77
errorString += key + ': ' + value + '\n';
78
}
79
});
80
return errorString;
81
};
82
83
84
85
/**
86
* The HasEntry matcher.
87
*
88
* @param {string} key The key for the entry.
89
* @param {*} value The value for the key.
90
*
91
* @constructor
92
* @struct
93
* @implements {goog.labs.testing.Matcher}
94
* @final
95
*/
96
goog.labs.testing.HasEntryMatcher = function(key, value) {
97
/**
98
* @type {string}
99
* @private
100
*/
101
this.key_ = key;
102
/**
103
* @type {*}
104
* @private
105
*/
106
this.value_ = value;
107
};
108
109
110
/**
111
* Determines if an object has a particular entry.
112
*
113
* @override
114
*/
115
goog.labs.testing.HasEntryMatcher.prototype.matches = function(actualObject) {
116
goog.asserts.assertObject(actualObject);
117
return goog.object.containsKey(actualObject, this.key_) &&
118
actualObject[this.key_] === this.value_;
119
};
120
121
122
/**
123
* @override
124
*/
125
goog.labs.testing.HasEntryMatcher.prototype.describe = function(actualObject) {
126
goog.asserts.assertObject(actualObject);
127
var errorMsg;
128
if (goog.object.containsKey(actualObject, this.key_)) {
129
errorMsg = 'Input object did not contain key: ' + this.key_;
130
} else {
131
errorMsg = 'Value for key did not match value: ' + this.value_;
132
}
133
return errorMsg;
134
};
135
136
137
138
/**
139
* The HasKey matcher.
140
*
141
* @param {string} key The key to check in the object.
142
*
143
* @constructor
144
* @struct
145
* @implements {goog.labs.testing.Matcher}
146
* @final
147
*/
148
goog.labs.testing.HasKeyMatcher = function(key) {
149
/**
150
* @type {string}
151
* @private
152
*/
153
this.key_ = key;
154
};
155
156
157
/**
158
* Determines if an object has a key.
159
*
160
* @override
161
*/
162
goog.labs.testing.HasKeyMatcher.prototype.matches = function(actualObject) {
163
goog.asserts.assertObject(actualObject);
164
return goog.object.containsKey(actualObject, this.key_);
165
};
166
167
168
/**
169
* @override
170
*/
171
goog.labs.testing.HasKeyMatcher.prototype.describe = function(actualObject) {
172
goog.asserts.assertObject(actualObject);
173
return 'Input object did not contain the key: ' + this.key_;
174
};
175
176
177
178
/**
179
* The HasValue matcher.
180
*
181
* @param {*} value The value to check in the object.
182
*
183
* @constructor
184
* @struct
185
* @implements {goog.labs.testing.Matcher}
186
* @final
187
*/
188
goog.labs.testing.HasValueMatcher = function(value) {
189
/**
190
* @type {*}
191
* @private
192
*/
193
this.value_ = value;
194
};
195
196
197
/**
198
* Determines if an object contains a value
199
*
200
* @override
201
*/
202
goog.labs.testing.HasValueMatcher.prototype.matches = function(actualObject) {
203
goog.asserts.assertObject(actualObject, 'Expected an Object');
204
var object = /** @type {!Object} */ (actualObject);
205
return goog.object.containsValue(object, this.value_);
206
};
207
208
209
/**
210
* @override
211
*/
212
goog.labs.testing.HasValueMatcher.prototype.describe = function(actualObject) {
213
return 'Input object did not contain the value: ' + this.value_;
214
};
215
216
217
/**
218
* Gives a matcher that asserts an object contains all the given key-value pairs
219
* in the input object.
220
*
221
* @param {!Object} entries The entries to check for presence in the object.
222
*
223
* @return {!goog.labs.testing.HasEntriesMatcher} A HasEntriesMatcher.
224
*/
225
function hasEntries(entries) {
226
return new goog.labs.testing.HasEntriesMatcher(entries);
227
}
228
229
230
/**
231
* Gives a matcher that asserts an object contains the given key-value pair.
232
*
233
* @param {string} key The key to check for presence in the object.
234
* @param {*} value The value to check for presence in the object.
235
*
236
* @return {!goog.labs.testing.HasEntryMatcher} A HasEntryMatcher.
237
*/
238
function hasEntry(key, value) {
239
return new goog.labs.testing.HasEntryMatcher(key, value);
240
}
241
242
243
/**
244
* Gives a matcher that asserts an object contains the given key.
245
*
246
* @param {string} key The key to check for presence in the object.
247
*
248
* @return {!goog.labs.testing.HasKeyMatcher} A HasKeyMatcher.
249
*/
250
function hasKey(key) {
251
return new goog.labs.testing.HasKeyMatcher(key);
252
}
253
254
255
/**
256
* Gives a matcher that asserts an object contains the given value.
257
*
258
* @param {*} value The value to check for presence in the object.
259
*
260
* @return {!goog.labs.testing.HasValueMatcher} A HasValueMatcher.
261
*/
262
function hasValue(value) {
263
return new goog.labs.testing.HasValueMatcher(value);
264
}
265
266