Path: blob/trunk/third_party/closure/goog/labs/testing/logicmatcher.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 logic matchers: anyOf, allOf, and isNot.16*17*/181920goog.provide('goog.labs.testing.AllOfMatcher');21goog.provide('goog.labs.testing.AnyOfMatcher');22goog.provide('goog.labs.testing.IsNotMatcher');232425goog.require('goog.array');26goog.require('goog.labs.testing.Matcher');27282930/**31* The AllOf matcher.32*33* @param {!Array<!goog.labs.testing.Matcher>} matchers Input matchers.34*35* @constructor36* @struct37* @implements {goog.labs.testing.Matcher}38* @final39*/40goog.labs.testing.AllOfMatcher = function(matchers) {41/**42* @type {!Array<!goog.labs.testing.Matcher>}43* @private44*/45this.matchers_ = matchers;46};474849/**50* Determines if all of the matchers match the input value.51*52* @override53*/54goog.labs.testing.AllOfMatcher.prototype.matches = function(actualValue) {55return goog.array.every(this.matchers_, function(matcher) {56return matcher.matches(actualValue);57});58};596061/**62* Describes why the matcher failed. The returned string is a concatenation of63* all the failed matchers' error strings.64*65* @override66*/67goog.labs.testing.AllOfMatcher.prototype.describe = function(actualValue) {68// TODO(user) : Optimize this to remove duplication with matches ?69var errorString = '';70goog.array.forEach(this.matchers_, function(matcher) {71if (!matcher.matches(actualValue)) {72errorString += matcher.describe(actualValue) + '\n';73}74});75return errorString;76};77787980/**81* The AnyOf matcher.82*83* @param {!Array<!goog.labs.testing.Matcher>} matchers Input matchers.84*85* @constructor86* @struct87* @implements {goog.labs.testing.Matcher}88* @final89*/90goog.labs.testing.AnyOfMatcher = function(matchers) {91/**92* @type {!Array<!goog.labs.testing.Matcher>}93* @private94*/95this.matchers_ = matchers;96};979899/**100* Determines if any of the matchers matches the input value.101*102* @override103*/104goog.labs.testing.AnyOfMatcher.prototype.matches = function(actualValue) {105return goog.array.some(this.matchers_, function(matcher) {106return matcher.matches(actualValue);107});108};109110111/**112* Describes why the matcher failed.113*114* @override115*/116goog.labs.testing.AnyOfMatcher.prototype.describe = function(actualValue) {117// TODO(user) : Optimize this to remove duplication with matches ?118var errorString = '';119goog.array.forEach(this.matchers_, function(matcher) {120if (!matcher.matches(actualValue)) {121errorString += matcher.describe(actualValue) + '\n';122}123});124return errorString;125};126127128129/**130* The IsNot matcher.131*132* @param {!goog.labs.testing.Matcher} matcher The matcher to negate.133*134* @constructor135* @struct136* @implements {goog.labs.testing.Matcher}137* @final138*/139goog.labs.testing.IsNotMatcher = function(matcher) {140/**141* @type {!goog.labs.testing.Matcher}142* @private143*/144this.matcher_ = matcher;145};146147148/**149* Determines if the input value doesn't satisfy a matcher.150*151* @override152*/153goog.labs.testing.IsNotMatcher.prototype.matches = function(actualValue) {154return !this.matcher_.matches(actualValue);155};156157158/**159* Describes why the matcher failed.160*161* @override162*/163goog.labs.testing.IsNotMatcher.prototype.describe = function(actualValue) {164return 'The following is false: ' + this.matcher_.describe(actualValue);165};166167168/**169* Creates a matcher that will succeed only if all of the given matchers170* succeed.171*172* @param {...goog.labs.testing.Matcher} var_args The matchers to test173* against.174*175* @return {!goog.labs.testing.AllOfMatcher} The AllOf matcher.176*/177function allOf(var_args) {178var matchers = goog.array.toArray(arguments);179return new goog.labs.testing.AllOfMatcher(matchers);180}181182183/**184* Accepts a set of matchers and returns a matcher which matches185* values which satisfy the constraints of any of the given matchers.186*187* @param {...goog.labs.testing.Matcher} var_args The matchers to test188* against.189*190* @return {!goog.labs.testing.AnyOfMatcher} The AnyOf matcher.191*/192function anyOf(var_args) {193var matchers = goog.array.toArray(arguments);194return new goog.labs.testing.AnyOfMatcher(matchers);195}196197198/**199* Returns a matcher that negates the input matcher. The returned200* matcher matches the values not matched by the input matcher and vice-versa.201*202* @param {!goog.labs.testing.Matcher} matcher The matcher to test against.203*204* @return {!goog.labs.testing.IsNotMatcher} The IsNot matcher.205*/206function isNot(matcher) {207return new goog.labs.testing.IsNotMatcher(matcher);208}209210211