Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/labs/testing/logicmatcher.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 logic matchers: anyOf, allOf, and isNot.
17
*
18
*/
19
20
21
goog.provide('goog.labs.testing.AllOfMatcher');
22
goog.provide('goog.labs.testing.AnyOfMatcher');
23
goog.provide('goog.labs.testing.IsNotMatcher');
24
25
26
goog.require('goog.array');
27
goog.require('goog.labs.testing.Matcher');
28
29
30
31
/**
32
* The AllOf matcher.
33
*
34
* @param {!Array<!goog.labs.testing.Matcher>} matchers Input matchers.
35
*
36
* @constructor
37
* @struct
38
* @implements {goog.labs.testing.Matcher}
39
* @final
40
*/
41
goog.labs.testing.AllOfMatcher = function(matchers) {
42
/**
43
* @type {!Array<!goog.labs.testing.Matcher>}
44
* @private
45
*/
46
this.matchers_ = matchers;
47
};
48
49
50
/**
51
* Determines if all of the matchers match the input value.
52
*
53
* @override
54
*/
55
goog.labs.testing.AllOfMatcher.prototype.matches = function(actualValue) {
56
return goog.array.every(this.matchers_, function(matcher) {
57
return matcher.matches(actualValue);
58
});
59
};
60
61
62
/**
63
* Describes why the matcher failed. The returned string is a concatenation of
64
* all the failed matchers' error strings.
65
*
66
* @override
67
*/
68
goog.labs.testing.AllOfMatcher.prototype.describe = function(actualValue) {
69
// TODO(user) : Optimize this to remove duplication with matches ?
70
var errorString = '';
71
goog.array.forEach(this.matchers_, function(matcher) {
72
if (!matcher.matches(actualValue)) {
73
errorString += matcher.describe(actualValue) + '\n';
74
}
75
});
76
return errorString;
77
};
78
79
80
81
/**
82
* The AnyOf matcher.
83
*
84
* @param {!Array<!goog.labs.testing.Matcher>} matchers Input matchers.
85
*
86
* @constructor
87
* @struct
88
* @implements {goog.labs.testing.Matcher}
89
* @final
90
*/
91
goog.labs.testing.AnyOfMatcher = function(matchers) {
92
/**
93
* @type {!Array<!goog.labs.testing.Matcher>}
94
* @private
95
*/
96
this.matchers_ = matchers;
97
};
98
99
100
/**
101
* Determines if any of the matchers matches the input value.
102
*
103
* @override
104
*/
105
goog.labs.testing.AnyOfMatcher.prototype.matches = function(actualValue) {
106
return goog.array.some(this.matchers_, function(matcher) {
107
return matcher.matches(actualValue);
108
});
109
};
110
111
112
/**
113
* Describes why the matcher failed.
114
*
115
* @override
116
*/
117
goog.labs.testing.AnyOfMatcher.prototype.describe = function(actualValue) {
118
// TODO(user) : Optimize this to remove duplication with matches ?
119
var errorString = '';
120
goog.array.forEach(this.matchers_, function(matcher) {
121
if (!matcher.matches(actualValue)) {
122
errorString += matcher.describe(actualValue) + '\n';
123
}
124
});
125
return errorString;
126
};
127
128
129
130
/**
131
* The IsNot matcher.
132
*
133
* @param {!goog.labs.testing.Matcher} matcher The matcher to negate.
134
*
135
* @constructor
136
* @struct
137
* @implements {goog.labs.testing.Matcher}
138
* @final
139
*/
140
goog.labs.testing.IsNotMatcher = function(matcher) {
141
/**
142
* @type {!goog.labs.testing.Matcher}
143
* @private
144
*/
145
this.matcher_ = matcher;
146
};
147
148
149
/**
150
* Determines if the input value doesn't satisfy a matcher.
151
*
152
* @override
153
*/
154
goog.labs.testing.IsNotMatcher.prototype.matches = function(actualValue) {
155
return !this.matcher_.matches(actualValue);
156
};
157
158
159
/**
160
* Describes why the matcher failed.
161
*
162
* @override
163
*/
164
goog.labs.testing.IsNotMatcher.prototype.describe = function(actualValue) {
165
return 'The following is false: ' + this.matcher_.describe(actualValue);
166
};
167
168
169
/**
170
* Creates a matcher that will succeed only if all of the given matchers
171
* succeed.
172
*
173
* @param {...goog.labs.testing.Matcher} var_args The matchers to test
174
* against.
175
*
176
* @return {!goog.labs.testing.AllOfMatcher} The AllOf matcher.
177
*/
178
function allOf(var_args) {
179
var matchers = goog.array.toArray(arguments);
180
return new goog.labs.testing.AllOfMatcher(matchers);
181
}
182
183
184
/**
185
* Accepts a set of matchers and returns a matcher which matches
186
* values which satisfy the constraints of any of the given matchers.
187
*
188
* @param {...goog.labs.testing.Matcher} var_args The matchers to test
189
* against.
190
*
191
* @return {!goog.labs.testing.AnyOfMatcher} The AnyOf matcher.
192
*/
193
function anyOf(var_args) {
194
var matchers = goog.array.toArray(arguments);
195
return new goog.labs.testing.AnyOfMatcher(matchers);
196
}
197
198
199
/**
200
* Returns a matcher that negates the input matcher. The returned
201
* matcher matches the values not matched by the input matcher and vice-versa.
202
*
203
* @param {!goog.labs.testing.Matcher} matcher The matcher to test against.
204
*
205
* @return {!goog.labs.testing.IsNotMatcher} The IsNot matcher.
206
*/
207
function isNot(matcher) {
208
return new goog.labs.testing.IsNotMatcher(matcher);
209
}
210
211