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