# 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 * @implements {goog.labs.testing.Matcher}44 */45goog.labs.testing.ContainsStringMatcher = function(value) {46  /**47   * @type {string}48   * @private49   */50  this.value_ = value;51};525354/**55 * Determines if input string contains the expected string.56 *57 * @override58 */59goog.labs.testing.ContainsStringMatcher.prototype.matches =60    function(actualValue) {61  goog.asserts.assertString(actualValue);62  return goog.string.contains(actualValue, this.value_);63};646566/**67 * @override68 */69goog.labs.testing.ContainsStringMatcher.prototype.describe =70    function(actualValue) {71  return actualValue + ' does not contain ' + this.value_;72};73747576/**77 * The EndsWith matcher.78 *79 * @param {string} value The expected string.80 *81 * @constructor82 * @implements {goog.labs.testing.Matcher}83 */84goog.labs.testing.EndsWithMatcher = function(value) {85  /**86   * @type {string}87   * @private88   */89  this.value_ = value;90};919293/**94 * Determines if input string ends with the expected string.95 *96 * @override97 */98goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {99  goog.asserts.assertString(actualValue);100  return goog.string.endsWith(actualValue, this.value_);101};102103104/**105 * @override106 */107goog.labs.testing.EndsWithMatcher.prototype.describe =108    function(actualValue) {109  return actualValue + ' does not end with ' + this.value_;110};111112113114/**115 * The EqualToIgnoringWhitespace matcher.116 *117 * @param {string} value The expected string.118 *119 * @constructor120 * @implements {goog.labs.testing.Matcher}121 */122goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {123  /**124   * @type {string}125   * @private126   */127  this.value_ = value;128};129130131/**132 * Determines if input string contains the expected string.133 *134 * @override135 */136goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches =137    function(actualValue) {138  goog.asserts.assertString(actualValue);139  var string1 = goog.string.collapseWhitespace(actualValue);140141  return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;142};143144145/**146 * @override147 */148goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =149    function(actualValue) {150  return actualValue + ' is not equal(ignoring whitespace) to ' + this.value_;151};152153154155/**156 * The Equals matcher.157 *158 * @param {string} value The expected string.159 *160 * @constructor161 * @implements {goog.labs.testing.Matcher}162 */163goog.labs.testing.EqualsMatcher = function(value) {164  /**165   * @type {string}166   * @private167   */168  this.value_ = value;169};170171172/**173 * Determines if input string is equal to the expected string.174 *175 * @override176 */177goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {178  goog.asserts.assertString(actualValue);179  return this.value_ === actualValue;180};181182183/**184 * @override185 */186goog.labs.testing.EqualsMatcher.prototype.describe =187    function(actualValue) {188  return actualValue + ' is not equal to ' + this.value_;189};190191192193/**194 * The MatchesRegex matcher.195 *196 * @param {!RegExp} regex The expected regex.197 *198 * @constructor199 * @implements {goog.labs.testing.Matcher}200 */201goog.labs.testing.RegexMatcher = function(regex) {202  /**203   * @type {!RegExp}204   * @private205   */206  this.regex_ = regex;207};208209210/**211 * Determines if input string is equal to the expected string.212 *213 * @override214 */215goog.labs.testing.RegexMatcher.prototype.matches = function(216    actualValue) {217  goog.asserts.assertString(actualValue);218  return this.regex_.test(actualValue);219};220221222/**223 * @override224 */225goog.labs.testing.RegexMatcher.prototype.describe =226    function(actualValue) {227  return actualValue + ' does not match ' + this.regex_;228};229230231232/**233 * The StartsWith matcher.234 *235 * @param {string} value The expected string.236 *237 * @constructor238 * @implements {goog.labs.testing.Matcher}239 */240goog.labs.testing.StartsWithMatcher = function(value) {241  /**242   * @type {string}243   * @private244   */245  this.value_ = value;246};247248249/**250 * Determines if input string starts with the expected string.251 *252 * @override253 */254goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {255  goog.asserts.assertString(actualValue);256  return goog.string.startsWith(actualValue, this.value_);257};258259260/**261 * @override262 */263goog.labs.testing.StartsWithMatcher.prototype.describe =264    function(actualValue) {265  return actualValue + ' does not start with ' + this.value_;266};267268269270/**271 * The StringContainsInOrdermatcher.272 *273 * @param {Array.<string>} values The expected string values.274 *275 * @constructor276 * @implements {goog.labs.testing.Matcher}277 */278goog.labs.testing.StringContainsInOrderMatcher = function(values) {279  /**280   * @type {Array.<string>}281   * @private282   */283  this.values_ = values;284};285286287/**288 * Determines if input string contains, in order, the expected array of strings.289 *290 * @override291 */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};305306307/**308 * @override309 */310goog.labs.testing.StringContainsInOrderMatcher.prototype.describe =311    function(actualValue) {312  return actualValue + ' does not contain the expected values in order.';313};314315316/**317 * Matches a string containing the given string.318 *319 * @param {string} value The expected value.320 *321 * @return {!goog.labs.testing.ContainsStringMatcher} A322 *     ContainsStringMatcher.323 */324function containsString(value) {325  return new goog.labs.testing.ContainsStringMatcher(value);326}327328329/**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} A335 *     EndsWithMatcher.336 */337function endsWith(value) {338  return new goog.labs.testing.EndsWithMatcher(value);339}340341342/**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} A348 *     EqualToIgnoringWhitespaceMatcher.349 */350function equalToIgnoringWhitespace(value) {351  return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);352}353354355/**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}365366367/**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}377378379/**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} A385 *     StartsWithMatcher.386 */387function startsWith(value) {388  return new goog.labs.testing.StartsWithMatcher(value);389}390391392/**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} A398 *     StringContainsInOrderMatcher.399 */400function stringContainsInOrder(values) {401  return new goog.labs.testing.StringContainsInOrderMatcher(values);402}