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