# 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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415/**16 * @fileoverview Provides the built-in string matchers like containsString,17 *     startsWith, endsWith, etc.18 */192021goog.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');293031goog.require('goog.asserts');32goog.require('goog.labs.testing.Matcher');33goog.require('goog.string');34353637/**38 * The ContainsString matcher.39 *40 * @param {string} value The expected string.41 *42 * @constructor43 * @struct44 * @implements {goog.labs.testing.Matcher}45 * @final46 */47goog.labs.testing.ContainsStringMatcher = function(value) {48  /**49   * @type {string}50   * @private51   */52  this.value_ = value;53};545556/**57 * Determines if input string contains the expected string.58 *59 * @override60 */61goog.labs.testing.ContainsStringMatcher.prototype.matches =62    function(actualValue) {63  goog.asserts.assertString(actualValue);64  return goog.string.contains(actualValue, this.value_);65};666768/**69 * @override70 */71goog.labs.testing.ContainsStringMatcher.prototype.describe =72    function(actualValue) {73  return actualValue + ' does not contain ' + this.value_;74};75767778/**79 * The EndsWith matcher.80 *81 * @param {string} value The expected string.82 *83 * @constructor84 * @struct85 * @implements {goog.labs.testing.Matcher}86 * @final87 */88goog.labs.testing.EndsWithMatcher = function(value) {89  /**90   * @type {string}91   * @private92   */93  this.value_ = value;94};959697/**98 * Determines if input string ends with the expected string.99 *100 * @override101 */102goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {103  goog.asserts.assertString(actualValue);104  return goog.string.endsWith(actualValue, this.value_);105};106107108/**109 * @override110 */111goog.labs.testing.EndsWithMatcher.prototype.describe =112    function(actualValue) {113  return actualValue + ' does not end with ' + this.value_;114};115116117118/**119 * The EqualToIgnoringWhitespace matcher.120 *121 * @param {string} value The expected string.122 *123 * @constructor124 * @struct125 * @implements {goog.labs.testing.Matcher}126 * @final127 */128goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {129  /**130   * @type {string}131   * @private132   */133  this.value_ = value;134};135136137/**138 * Determines if input string contains the expected string.139 *140 * @override141 */142goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches =143    function(actualValue) {144  goog.asserts.assertString(actualValue);145  var string1 = goog.string.collapseWhitespace(actualValue);146147  return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;148};149150151/**152 * @override153 */154goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =155    function(actualValue) {156  return actualValue + ' is not equal(ignoring whitespace) to ' + this.value_;157};158159160161/**162 * The Equals matcher.163 *164 * @param {string} value The expected string.165 *166 * @constructor167 * @struct168 * @implements {goog.labs.testing.Matcher}169 * @final170 */171goog.labs.testing.EqualsMatcher = function(value) {172  /**173   * @type {string}174   * @private175   */176  this.value_ = value;177};178179180/**181 * Determines if input string is equal to the expected string.182 *183 * @override184 */185goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {186  goog.asserts.assertString(actualValue);187  return this.value_ === actualValue;188};189190191/**192 * @override193 */194goog.labs.testing.EqualsMatcher.prototype.describe =195    function(actualValue) {196  return actualValue + ' is not equal to ' + this.value_;197};198199200201/**202 * The MatchesRegex matcher.203 *204 * @param {!RegExp} regex The expected regex.205 *206 * @constructor207 * @struct208 * @implements {goog.labs.testing.Matcher}209 * @final210 */211goog.labs.testing.RegexMatcher = function(regex) {212  /**213   * @type {!RegExp}214   * @private215   */216  this.regex_ = regex;217};218219220/**221 * Determines if input string is equal to the expected string.222 *223 * @override224 */225goog.labs.testing.RegexMatcher.prototype.matches = function(226    actualValue) {227  goog.asserts.assertString(actualValue);228  return this.regex_.test(actualValue);229};230231232/**233 * @override234 */235goog.labs.testing.RegexMatcher.prototype.describe =236    function(actualValue) {237  return actualValue + ' does not match ' + this.regex_;238};239240241242/**243 * The StartsWith matcher.244 *245 * @param {string} value The expected string.246 *247 * @constructor248 * @struct249 * @implements {goog.labs.testing.Matcher}250 * @final251 */252goog.labs.testing.StartsWithMatcher = function(value) {253  /**254   * @type {string}255   * @private256   */257  this.value_ = value;258};259260261/**262 * Determines if input string starts with the expected string.263 *264 * @override265 */266goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {267  goog.asserts.assertString(actualValue);268  return goog.string.startsWith(actualValue, this.value_);269};270271272/**273 * @override274 */275goog.labs.testing.StartsWithMatcher.prototype.describe =276    function(actualValue) {277  return actualValue + ' does not start with ' + this.value_;278};279280281282/**283 * The StringContainsInOrdermatcher.284 *285 * @param {Array.<string>} values The expected string values.286 *287 * @constructor288 * @struct289 * @implements {goog.labs.testing.Matcher}290 * @final291 */292goog.labs.testing.StringContainsInOrderMatcher = function(values) {293  /**294   * @type {Array.<string>}295   * @private296   */297  this.values_ = values;298};299300301/**302 * Determines if input string contains, in order, the expected array of strings.303 *304 * @override305 */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};319320321/**322 * @override323 */324goog.labs.testing.StringContainsInOrderMatcher.prototype.describe =325    function(actualValue) {326  return actualValue + ' does not contain the expected values in order.';327};328329330/**331 * Matches a string containing the given string.332 *333 * @param {string} value The expected value.334 *335 * @return {!goog.labs.testing.ContainsStringMatcher} A336 *     ContainsStringMatcher.337 */338function containsString(value) {339  return new goog.labs.testing.ContainsStringMatcher(value);340}341342343/**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} A349 *     EndsWithMatcher.350 */351function endsWith(value) {352  return new goog.labs.testing.EndsWithMatcher(value);353}354355356/**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} A362 *     EqualToIgnoringWhitespaceMatcher.363 */364function equalToIgnoringWhitespace(value) {365  return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);366}367368369/**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}379380381/**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}391392393/**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} A399 *     StartsWithMatcher.400 */401function startsWith(value) {402  return new goog.labs.testing.StartsWithMatcher(value);403}404405406/**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} A412 *     StringContainsInOrderMatcher.413 */414function stringContainsInOrder(values) {415  return new goog.labs.testing.StringContainsInOrderMatcher(values);416}