Path: blob/trunk/third_party/closure/goog/labs/testing/stringmatcher.js
2868 views
// Copyright 2012 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Provides the built-in string matchers like containsString,16* startsWith, endsWith, etc.17*/1819goog.provide('goog.labs.testing.AnyStringMatcher');20goog.provide('goog.labs.testing.ContainsStringMatcher');21goog.provide('goog.labs.testing.EndsWithMatcher');22goog.provide('goog.labs.testing.EqualToIgnoringWhitespaceMatcher');23goog.provide('goog.labs.testing.EqualsMatcher');24goog.provide('goog.labs.testing.RegexMatcher');25goog.provide('goog.labs.testing.StartsWithMatcher');26goog.provide('goog.labs.testing.StringContainsInOrderMatcher');2728goog.require('goog.asserts');29goog.require('goog.labs.testing.Matcher');30goog.require('goog.string');31323334/**35* Matches any string value.36*37* @constructor @struct @implements {goog.labs.testing.Matcher} @final38*/39goog.labs.testing.AnyStringMatcher = function() {};404142/** @override */43goog.labs.testing.AnyStringMatcher.prototype.matches = function(actualValue) {44return goog.isString(actualValue);45};464748/** @override */49goog.labs.testing.AnyStringMatcher.prototype.describe = function(actualValue) {50return '<' + actualValue + '> is not a string';51};52535455/**56* The ContainsString matcher.57*58* @param {string} value The expected string.59*60* @constructor61* @struct62* @implements {goog.labs.testing.Matcher}63* @final64*/65goog.labs.testing.ContainsStringMatcher = function(value) {66/**67* @type {string}68* @private69*/70this.value_ = value;71};727374/**75* Determines if input string contains the expected string.76*77* @override78*/79goog.labs.testing.ContainsStringMatcher.prototype.matches = function(80actualValue) {81goog.asserts.assertString(actualValue);82return goog.string.contains(actualValue, this.value_);83};848586/**87* @override88*/89goog.labs.testing.ContainsStringMatcher.prototype.describe = function(90actualValue) {91return actualValue + ' does not contain ' + this.value_;92};93949596/**97* The EndsWith matcher.98*99* @param {string} value The expected string.100*101* @constructor102* @struct103* @implements {goog.labs.testing.Matcher}104* @final105*/106goog.labs.testing.EndsWithMatcher = function(value) {107/**108* @type {string}109* @private110*/111this.value_ = value;112};113114115/**116* Determines if input string ends with the expected string.117*118* @override119*/120goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {121goog.asserts.assertString(actualValue);122return goog.string.endsWith(actualValue, this.value_);123};124125126/**127* @override128*/129goog.labs.testing.EndsWithMatcher.prototype.describe = function(actualValue) {130return actualValue + ' does not end with ' + this.value_;131};132133134135/**136* The EqualToIgnoringWhitespace matcher.137*138* @param {string} value The expected string.139*140* @constructor141* @struct142* @implements {goog.labs.testing.Matcher}143* @final144*/145goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {146/**147* @type {string}148* @private149*/150this.value_ = value;151};152153154/**155* Determines if input string contains the expected string.156*157* @override158*/159goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches = function(160actualValue) {161goog.asserts.assertString(actualValue);162var string1 = goog.string.collapseWhitespace(actualValue);163164return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;165};166167168/**169* @override170*/171goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =172function(actualValue) {173return actualValue + ' is not equal(ignoring whitespace) to ' + this.value_;174};175176177178/**179* The Equals matcher.180*181* @param {string} value The expected string.182*183* @constructor184* @struct185* @implements {goog.labs.testing.Matcher}186* @final187*/188goog.labs.testing.EqualsMatcher = function(value) {189/**190* @type {string}191* @private192*/193this.value_ = value;194};195196197/**198* Determines if input string is equal to the expected string.199*200* @override201*/202goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {203goog.asserts.assertString(actualValue);204return this.value_ === actualValue;205};206207208/**209* @override210*/211goog.labs.testing.EqualsMatcher.prototype.describe = function(actualValue) {212return actualValue + ' is not equal to ' + this.value_;213};214215216217/**218* The MatchesRegex matcher.219*220* @param {!RegExp} regex The expected regex.221*222* @constructor223* @struct224* @implements {goog.labs.testing.Matcher}225* @final226*/227goog.labs.testing.RegexMatcher = function(regex) {228/**229* @type {!RegExp}230* @private231*/232this.regex_ = regex;233};234235236/**237* Determines if input string is equal to the expected string.238*239* @override240*/241goog.labs.testing.RegexMatcher.prototype.matches = function(actualValue) {242goog.asserts.assertString(actualValue);243return this.regex_.test(actualValue);244};245246247/**248* @override249*/250goog.labs.testing.RegexMatcher.prototype.describe = function(actualValue) {251return actualValue + ' does not match ' + this.regex_;252};253254255256/**257* The StartsWith matcher.258*259* @param {string} value The expected string.260*261* @constructor262* @struct263* @implements {goog.labs.testing.Matcher}264* @final265*/266goog.labs.testing.StartsWithMatcher = function(value) {267/**268* @type {string}269* @private270*/271this.value_ = value;272};273274275/**276* Determines if input string starts with the expected string.277*278* @override279*/280goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {281goog.asserts.assertString(actualValue);282return goog.string.startsWith(actualValue, this.value_);283};284285286/**287* @override288*/289goog.labs.testing.StartsWithMatcher.prototype.describe = function(actualValue) {290return actualValue + ' does not start with ' + this.value_;291};292293294295/**296* The StringContainsInOrdermatcher.297*298* @param {Array<string>} values The expected string values.299*300* @constructor301* @struct302* @implements {goog.labs.testing.Matcher}303* @final304*/305goog.labs.testing.StringContainsInOrderMatcher = function(values) {306/**307* @type {Array<string>}308* @private309*/310this.values_ = values;311};312313314/**315* Determines if input string contains, in order, the expected array of strings.316*317* @override318*/319goog.labs.testing.StringContainsInOrderMatcher.prototype.matches = function(320actualValue) {321goog.asserts.assertString(actualValue);322var currentIndex, previousIndex = 0;323for (var i = 0; i < this.values_.length; i++) {324currentIndex = goog.string.contains(actualValue, this.values_[i]);325if (currentIndex < 0 || currentIndex < previousIndex) {326return false;327}328previousIndex = currentIndex;329}330return true;331};332333334/**335* @override336*/337goog.labs.testing.StringContainsInOrderMatcher.prototype.describe = function(338actualValue) {339return actualValue + ' does not contain the expected values in order.';340};341342343/** @return {!goog.labs.testing.AnyStringMatcher} */344function anyString() {345return new goog.labs.testing.AnyStringMatcher();346}347348349/**350* Matches a string containing the given string.351*352* @param {string} value The expected value.353*354* @return {!goog.labs.testing.ContainsStringMatcher} A355* ContainsStringMatcher.356*/357function containsString(value) {358return new goog.labs.testing.ContainsStringMatcher(value);359}360361362/**363* Matches a string that ends with the given string.364*365* @param {string} value The expected value.366*367* @return {!goog.labs.testing.EndsWithMatcher} A368* EndsWithMatcher.369*/370function endsWith(value) {371return new goog.labs.testing.EndsWithMatcher(value);372}373374375/**376* Matches a string that equals (ignoring whitespace) the given string.377*378* @param {string} value The expected value.379*380* @return {!goog.labs.testing.EqualToIgnoringWhitespaceMatcher} A381* EqualToIgnoringWhitespaceMatcher.382*/383function equalToIgnoringWhitespace(value) {384return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);385}386387388/**389* Matches a string that equals the given string.390*391* @param {string} value The expected value.392*393* @return {!goog.labs.testing.EqualsMatcher} A EqualsMatcher.394*/395function equals(value) {396return new goog.labs.testing.EqualsMatcher(value);397}398399400/**401* Matches a string against a regular expression.402*403* @param {!RegExp} regex The expected regex.404*405* @return {!goog.labs.testing.RegexMatcher} A RegexMatcher.406*/407function matchesRegex(regex) {408return new goog.labs.testing.RegexMatcher(regex);409}410411412/**413* Matches a string that starts with the given string.414*415* @param {string} value The expected value.416*417* @return {!goog.labs.testing.StartsWithMatcher} A418* StartsWithMatcher.419*/420function startsWith(value) {421return new goog.labs.testing.StartsWithMatcher(value);422}423424425/**426* Matches a string that contains the given strings in order.427*428* @param {Array<string>} values The expected value.429*430* @return {!goog.labs.testing.StringContainsInOrderMatcher} A431* StringContainsInOrderMatcher.432*/433function stringContainsInOrder(values) {434return new goog.labs.testing.StringContainsInOrderMatcher(values);435}436437438