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