Debian Codegraph @

closure-library/closure/goog/labs/testing/stringmatcher.js

raw
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 string matchers like containsString,
17 * startsWith, endsWith, etc.
18 */
19
20 goog.provide('goog.labs.testing.AnyStringMatcher');
21 goog.provide('goog.labs.testing.ContainsStringMatcher');
22 goog.provide('goog.labs.testing.EndsWithMatcher');
23 goog.provide('goog.labs.testing.EqualToIgnoringWhitespaceMatcher');
24 goog.provide('goog.labs.testing.EqualsMatcher');
25 goog.provide('goog.labs.testing.RegexMatcher');
26 goog.provide('goog.labs.testing.StartsWithMatcher');
27 goog.provide('goog.labs.testing.StringContainsInOrderMatcher');
28
29 goog.require('goog.asserts');
30 goog.require('goog.labs.testing.Matcher');
31 goog.require('goog.string');
32
33
34
35 /**
36 * Matches any string value.
37 *
38 * @constructor @struct @implements {goog.labs.testing.Matcher} @final
39 */
40 goog.labs.testing.AnyStringMatcher = function() {};
41
42
43 /** @override */
44 goog.labs.testing.AnyStringMatcher.prototype.matches = function(actualValue) {
45 return goog.isString(actualValue);
46 };
47
48
49 /** @override */
50 goog.labs.testing.AnyStringMatcher.prototype.describe = function(actualValue) {
51 return '<' + actualValue + '> is not a string';
52 };
53
54
55
56 /**
57 * The ContainsString matcher.
58 *
59 * @param {string} value The expected string.
60 *
61 * @constructor
62 * @struct
63 * @implements {goog.labs.testing.Matcher}
64 * @final
65 */
66 goog.labs.testing.ContainsStringMatcher = function(value) {
67 /**
68 * @type {string}
69 * @private
70 */
71 this.value_ = value;
72 };
73
74
75 /**
76 * Determines if input string contains the expected string.
77 *
78 * @override
79 */
80 goog.labs.testing.ContainsStringMatcher.prototype.matches = function(
81 actualValue) {
82 goog.asserts.assertString(actualValue);
83 return goog.string.contains(actualValue, this.value_);
84 };
85
86
87 /**
88 * @override
89 */
90 goog.labs.testing.ContainsStringMatcher.prototype.describe = function(
91 actualValue) {
92 return actualValue + ' does not contain ' + this.value_;
93 };
94
95
96
97 /**
98 * The EndsWith matcher.
99 *
100 * @param {string} value The expected string.
101 *
102 * @constructor
103 * @struct
104 * @implements {goog.labs.testing.Matcher}
105 * @final
106 */
107 goog.labs.testing.EndsWithMatcher = function(value) {
108 /**
109 * @type {string}
110 * @private
111 */
112 this.value_ = value;
113 };
114
115
116 /**
117 * Determines if input string ends with the expected string.
118 *
119 * @override
120 */
121 goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {
122 goog.asserts.assertString(actualValue);
123 return goog.string.endsWith(actualValue, this.value_);
124 };
125
126
127 /**
128 * @override
129 */
130 goog.labs.testing.EndsWithMatcher.prototype.describe = function(actualValue) {
131 return actualValue + ' does not end with ' + this.value_;
132 };
133
134
135
136 /**
137 * The EqualToIgnoringWhitespace matcher.
138 *
139 * @param {string} value The expected string.
140 *
141 * @constructor
142 * @struct
143 * @implements {goog.labs.testing.Matcher}
144 * @final
145 */
146 goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {
147 /**
148 * @type {string}
149 * @private
150 */
151 this.value_ = value;
152 };
153
154
155 /**
156 * Determines if input string contains the expected string.
157 *
158 * @override
159 */
160 goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches = function(
161 actualValue) {
162 goog.asserts.assertString(actualValue);
163 var string1 = goog.string.collapseWhitespace(actualValue);
164
165 return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;
166 };
167
168
169 /**
170 * @override
171 */
172 goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =
173 function(actualValue) {
174 return actualValue + ' is not equal(ignoring whitespace) to ' + this.value_;
175 };
176
177
178
179 /**
180 * The Equals matcher.
181 *
182 * @param {string} value The expected string.
183 *
184 * @constructor
185 * @struct
186 * @implements {goog.labs.testing.Matcher}
187 * @final
188 */
189 goog.labs.testing.EqualsMatcher = function(value) {
190 /**
191 * @type {string}
192 * @private
193 */
194 this.value_ = value;
195 };
196
197
198 /**
199 * Determines if input string is equal to the expected string.
200 *
201 * @override
202 */
203 goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {
204 goog.asserts.assertString(actualValue);
205 return this.value_ === actualValue;
206 };
207
208
209 /**
210 * @override
211 */
212 goog.labs.testing.EqualsMatcher.prototype.describe = function(actualValue) {
213 return actualValue + ' is not equal to ' + this.value_;
214 };
215
216
217
218 /**
219 * The MatchesRegex matcher.
220 *
221 * @param {!RegExp} regex The expected regex.
222 *
223 * @constructor
224 * @struct
225 * @implements {goog.labs.testing.Matcher}
226 * @final
227 */
228 goog.labs.testing.RegexMatcher = function(regex) {
229 /**
230 * @type {!RegExp}
231 * @private
232 */
233 this.regex_ = regex;
234 };
235
236
237 /**
238 * Determines if input string is equal to the expected string.
239 *
240 * @override
241 */
242 goog.labs.testing.RegexMatcher.prototype.matches = function(actualValue) {
243 goog.asserts.assertString(actualValue);
244 return this.regex_.test(actualValue);
245 };
246
247
248 /**
249 * @override
250 */
251 goog.labs.testing.RegexMatcher.prototype.describe = function(actualValue) {
252 return actualValue + ' does not match ' + this.regex_;
253 };
254
255
256
257 /**
258 * The StartsWith matcher.
259 *
260 * @param {string} value The expected string.
261 *
262 * @constructor
263 * @struct
264 * @implements {goog.labs.testing.Matcher}
265 * @final
266 */
267 goog.labs.testing.StartsWithMatcher = function(value) {
268 /**
269 * @type {string}
270 * @private
271 */
272 this.value_ = value;
273 };
274
275
276 /**
277 * Determines if input string starts with the expected string.
278 *
279 * @override
280 */
281 goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {
282 goog.asserts.assertString(actualValue);
283 return goog.string.startsWith(actualValue, this.value_);
284 };
285
286
287 /**
288 * @override
289 */
290 goog.labs.testing.StartsWithMatcher.prototype.describe = function(actualValue) {
291 return actualValue + ' does not start with ' + this.value_;
292 };
293
294
295
296 /**
297 * The StringContainsInOrdermatcher.
298 *
299 * @param {Array<string>} values The expected string values.
300 *
301 * @constructor
302 * @struct
303 * @implements {goog.labs.testing.Matcher}
304 * @final
305 */
306 goog.labs.testing.StringContainsInOrderMatcher = function(values) {
307 /**
308 * @type {Array<string>}
309 * @private
310 */
311 this.values_ = values;
312 };
313
314
315 /**
316 * Determines if input string contains, in order, the expected array of strings.
317 *
318 * @override
319 */
320 goog.labs.testing.StringContainsInOrderMatcher.prototype.matches = function(
321 actualValue) {
322 goog.asserts.assertString(actualValue);
323 var currentIndex, previousIndex = 0;
324 for (var i = 0; i < this.values_.length; i++) {
325 currentIndex = goog.string.contains(actualValue, this.values_[i]);
326 if (currentIndex < 0 || currentIndex < previousIndex) {
327 return false;
328 }
329 previousIndex = currentIndex;
330 }
331 return true;
332 };
333
334
335 /**
336 * @override
337 */
338 goog.labs.testing.StringContainsInOrderMatcher.prototype.describe = function(
339 actualValue) {
340 return actualValue + ' does not contain the expected values in order.';
341 };
342
343
344 /** @return {!goog.labs.testing.AnyStringMatcher} */
345 function anyString() {
346 return new goog.labs.testing.AnyStringMatcher();
347 }
348
349
350 /**
351 * Matches a string containing the given string.
352 *
353 * @param {string} value The expected value.
354 *
355 * @return {!goog.labs.testing.ContainsStringMatcher} A
356 * ContainsStringMatcher.
357 */
358 function containsString(value) {
359 return new goog.labs.testing.ContainsStringMatcher(value);
360 }
361
362
363 /**
364 * Matches a string that ends with the given string.
365 *
366 * @param {string} value The expected value.
367 *
368 * @return {!goog.labs.testing.EndsWithMatcher} A
369 * EndsWithMatcher.
370 */
371 function endsWith(value) {
372 return new goog.labs.testing.EndsWithMatcher(value);
373 }
374
375
376 /**
377 * Matches a string that equals (ignoring whitespace) the given string.
378 *
379 * @param {string} value The expected value.
380 *
381 * @return {!goog.labs.testing.EqualToIgnoringWhitespaceMatcher} A
382 * EqualToIgnoringWhitespaceMatcher.
383 */
384 function equalToIgnoringWhitespace(value) {
385 return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);
386 }
387
388
389 /**
390 * Matches a string that equals the given string.
391 *
392 * @param {string} value The expected value.
393 *
394 * @return {!goog.labs.testing.EqualsMatcher} A EqualsMatcher.
395 */
396 function equals(value) {
397 return new goog.labs.testing.EqualsMatcher(value);
398 }
399
400
401 /**
402 * Matches a string against a regular expression.
403 *
404 * @param {!RegExp} regex The expected regex.
405 *
406 * @return {!goog.labs.testing.RegexMatcher} A RegexMatcher.
407 */
408 function matchesRegex(regex) {
409 return new goog.labs.testing.RegexMatcher(regex);
410 }
411
412
413 /**
414 * Matches a string that starts with the given string.
415 *
416 * @param {string} value The expected value.
417 *
418 * @return {!goog.labs.testing.StartsWithMatcher} A
419 * StartsWithMatcher.
420 */
421 function startsWith(value) {
422 return new goog.labs.testing.StartsWithMatcher(value);
423 }
424
425
426 /**
427 * Matches a string that contains the given strings in order.
428 *
429 * @param {Array<string>} values The expected value.
430 *
431 * @return {!goog.labs.testing.StringContainsInOrderMatcher} A
432 * StringContainsInOrderMatcher.
433 */
434 function stringContainsInOrder(values) {
435 return new goog.labs.testing.StringContainsInOrderMatcher(values);
436 }