Debian Codegraph @

closure-library/closure/goog/labs/testing/decoratormatcher.js

raw
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 decorators: is, describedAs, anything.
17 */
18
19
20
21 goog.provide('goog.labs.testing.AnythingMatcher');
22
23
24 goog.require('goog.labs.testing.Matcher');
25
26
27
28 /**
29 * The Anything matcher. Matches all possible inputs.
30 *
31 * @constructor
32 * @implements {goog.labs.testing.Matcher}
33 * @final
34 */
35 goog.labs.testing.AnythingMatcher = function() {};
36
37
38 /**
39 * Matches anything. Useful if one doesn't care what the object under test is.
40 *
41 * @override
42 */
43 goog.labs.testing.AnythingMatcher.prototype.matches = function(actualObject) {
44 return true;
45 };
46
47
48 /**
49 * This method is never called but is needed so AnythingMatcher implements the
50 * Matcher interface.
51 *
52 * @override
53 */
54 goog.labs.testing.AnythingMatcher.prototype.describe = function(actualObject) {
55 throw new Error('AnythingMatcher should never fail!');
56 };
57
58
59 /**
60 * Returns a matcher that matches anything.
61 *
62 * @return {!goog.labs.testing.AnythingMatcher} A AnythingMatcher.
63 */
64 function anything() {
65 return new goog.labs.testing.AnythingMatcher();
66 }
67
68
69 /**
70 * Returnes any matcher that is passed to it (aids readability).
71 *
72 * @param {!goog.labs.testing.Matcher} matcher A matcher.
73 * @return {!goog.labs.testing.Matcher} The wrapped matcher.
74 */
75 function is(matcher) {
76 return matcher;
77 }
78
79
80 /**
81 * Returns a matcher with a customized description for the given matcher.
82 *
83 * @param {string} description The custom description for the matcher.
84 * @param {!goog.labs.testing.Matcher} matcher The matcher.
85 *
86 * @return {!goog.labs.testing.Matcher} The matcher with custom description.
87 */
88 function describedAs(description, matcher) {
89 matcher.describe = function(value) { return description; };
90 return matcher;
91 }