| 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 base Matcher interface. User code should use the |
| 17 |
* matchers through assertThat statements and not directly. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.labs.testing.Matcher'); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* A matcher object to be used in assertThat statements. |
| 27 |
* @interface |
| 28 |
*/ |
| 29 |
goog.labs.testing.Matcher = function() {}; |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Determines whether a value matches the constraints of the match. |
| 34 |
* |
| 35 |
* @param {*} value The object to match. |
| 36 |
* @return {boolean} Whether the input value matches this matcher. |
| 37 |
*/ |
| 38 |
goog.labs.testing.Matcher.prototype.matches = function(value) {}; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Describes why the matcher failed. |
| 43 |
* |
| 44 |
* @param {*} value The value that didn't match. |
| 45 |
* @param {string=} opt_description A partial description to which the reason |
| 46 |
* will be appended. |
| 47 |
* |
| 48 |
* @return {string} Description of why the matcher failed. |
| 49 |
*/ |
| 50 |
goog.labs.testing.Matcher.prototype.describe = function( |
| 51 |
value, opt_description) {}; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Generates a Matcher from the ‘matches’ and ‘describe’ functions passed in. |
| 56 |
* |
| 57 |
* @param {!Function} matchesFunction The ‘matches’ function. |
| 58 |
* @param {Function=} opt_describeFunction The ‘describe’ function. |
| 59 |
* @return {!Function} The custom matcher. |
| 60 |
*/ |
| 61 |
goog.labs.testing.Matcher.makeMatcher = function( |
| 62 |
matchesFunction, opt_describeFunction) { |
| 63 |
|
| 64 |
/** |
| 65 |
* @constructor |
| 66 |
* @implements {goog.labs.testing.Matcher} |
| 67 |
* @final |
| 68 |
*/ |
| 69 |
var matcherConstructor = function() {}; |
| 70 |
|
| 71 |
/** @override */ |
| 72 |
matcherConstructor.prototype.matches = matchesFunction; |
| 73 |
|
| 74 |
if (opt_describeFunction) { |
| 75 |
/** @override */ |
| 76 |
matcherConstructor.prototype.describe = opt_describeFunction; |
| 77 |
} |
| 78 |
|
| 79 |
return matcherConstructor; |
| 80 |
}; |