logicmatcher.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * @fileoverview Provides the built-in logic matchers: anyOf, allOf, and isNot.
  16. *
  17. */
  18. goog.provide('goog.labs.testing.AllOfMatcher');
  19. goog.provide('goog.labs.testing.AnyOfMatcher');
  20. goog.provide('goog.labs.testing.IsNotMatcher');
  21. goog.require('goog.array');
  22. goog.require('goog.labs.testing.Matcher');
  23. /**
  24. * The AllOf matcher.
  25. *
  26. * @param {!Array<!goog.labs.testing.Matcher>} matchers Input matchers.
  27. *
  28. * @constructor
  29. * @struct
  30. * @implements {goog.labs.testing.Matcher}
  31. * @final
  32. */
  33. goog.labs.testing.AllOfMatcher = function(matchers) {
  34. /**
  35. * @type {!Array<!goog.labs.testing.Matcher>}
  36. * @private
  37. */
  38. this.matchers_ = matchers;
  39. };
  40. /**
  41. * Determines if all of the matchers match the input value.
  42. *
  43. * @override
  44. */
  45. goog.labs.testing.AllOfMatcher.prototype.matches = function(actualValue) {
  46. return goog.array.every(this.matchers_, function(matcher) {
  47. return matcher.matches(actualValue);
  48. });
  49. };
  50. /**
  51. * Describes why the matcher failed. The returned string is a concatenation of
  52. * all the failed matchers' error strings.
  53. *
  54. * @override
  55. */
  56. goog.labs.testing.AllOfMatcher.prototype.describe = function(actualValue) {
  57. // TODO(user) : Optimize this to remove duplication with matches ?
  58. var errorString = '';
  59. goog.array.forEach(this.matchers_, function(matcher) {
  60. if (!matcher.matches(actualValue)) {
  61. errorString += matcher.describe(actualValue) + '\n';
  62. }
  63. });
  64. return errorString;
  65. };
  66. /**
  67. * The AnyOf matcher.
  68. *
  69. * @param {!Array<!goog.labs.testing.Matcher>} matchers Input matchers.
  70. *
  71. * @constructor
  72. * @struct
  73. * @implements {goog.labs.testing.Matcher}
  74. * @final
  75. */
  76. goog.labs.testing.AnyOfMatcher = function(matchers) {
  77. /**
  78. * @type {!Array<!goog.labs.testing.Matcher>}
  79. * @private
  80. */
  81. this.matchers_ = matchers;
  82. };
  83. /**
  84. * Determines if any of the matchers matches the input value.
  85. *
  86. * @override
  87. */
  88. goog.labs.testing.AnyOfMatcher.prototype.matches = function(actualValue) {
  89. return goog.array.some(this.matchers_, function(matcher) {
  90. return matcher.matches(actualValue);
  91. });
  92. };
  93. /**
  94. * Describes why the matcher failed.
  95. *
  96. * @override
  97. */
  98. goog.labs.testing.AnyOfMatcher.prototype.describe = function(actualValue) {
  99. // TODO(user) : Optimize this to remove duplication with matches ?
  100. var errorString = '';
  101. goog.array.forEach(this.matchers_, function(matcher) {
  102. if (!matcher.matches(actualValue)) {
  103. errorString += matcher.describe(actualValue) + '\n';
  104. }
  105. });
  106. return errorString;
  107. };
  108. /**
  109. * The IsNot matcher.
  110. *
  111. * @param {!goog.labs.testing.Matcher} matcher The matcher to negate.
  112. *
  113. * @constructor
  114. * @struct
  115. * @implements {goog.labs.testing.Matcher}
  116. * @final
  117. */
  118. goog.labs.testing.IsNotMatcher = function(matcher) {
  119. /**
  120. * @type {!goog.labs.testing.Matcher}
  121. * @private
  122. */
  123. this.matcher_ = matcher;
  124. };
  125. /**
  126. * Determines if the input value doesn't satisfy a matcher.
  127. *
  128. * @override
  129. */
  130. goog.labs.testing.IsNotMatcher.prototype.matches = function(actualValue) {
  131. return !this.matcher_.matches(actualValue);
  132. };
  133. /**
  134. * Describes why the matcher failed.
  135. *
  136. * @override
  137. */
  138. goog.labs.testing.IsNotMatcher.prototype.describe = function(actualValue) {
  139. return 'The following is false: ' + this.matcher_.describe(actualValue);
  140. };
  141. /**
  142. * Creates a matcher that will succeed only if all of the given matchers
  143. * succeed.
  144. *
  145. * @param {...goog.labs.testing.Matcher} var_args The matchers to test
  146. * against.
  147. *
  148. * @return {!goog.labs.testing.AllOfMatcher} The AllOf matcher.
  149. */
  150. function allOf(var_args) {
  151. var matchers = goog.array.toArray(arguments);
  152. return new goog.labs.testing.AllOfMatcher(matchers);
  153. }
  154. /**
  155. * Accepts a set of matchers and returns a matcher which matches
  156. * values which satisfy the constraints of any of the given matchers.
  157. *
  158. * @param {...goog.labs.testing.Matcher} var_args The matchers to test
  159. * against.
  160. *
  161. * @return {!goog.labs.testing.AnyOfMatcher} The AnyOf matcher.
  162. */
  163. function anyOf(var_args) {
  164. var matchers = goog.array.toArray(arguments);
  165. return new goog.labs.testing.AnyOfMatcher(matchers);
  166. }
  167. /**
  168. * Returns a matcher that negates the input matcher. The returned
  169. * matcher matches the values not matched by the input matcher and vice-versa.
  170. *
  171. * @param {!goog.labs.testing.Matcher} matcher The matcher to test against.
  172. *
  173. * @return {!goog.labs.testing.IsNotMatcher} The IsNot matcher.
  174. */
  175. function isNot(matcher) {
  176. return new goog.labs.testing.IsNotMatcher(matcher);
  177. }