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