| 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 main functionality of assertThat. assertThat calls the |
| 17 | * matcher's matches method to test if a matcher matches assertThat's arguments. |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | goog.provide('goog.labs.testing.MatcherError'); |
| 22 | goog.provide('goog.labs.testing.assertThat'); |
| 23 | |
| 24 | goog.require('goog.asserts'); |
| 25 | goog.require('goog.debug.Error'); |
| 26 | goog.require('goog.labs.testing.Matcher'); |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Asserts that the actual value evaluated by the matcher is true. |
| 31 | * |
| 32 | * @param {*} actual The object to assert by the matcher. |
| 33 | * @param {!goog.labs.testing.Matcher} matcher A matcher to verify values. |
| 34 | * @param {string=} opt_reason Description of what is asserted. |
| 35 | * |
| 36 | */ |
| 37 | goog.labs.testing.assertThat = function(actual, matcher, opt_reason) { |
| 38 | if (!matcher.matches(actual)) { |
| 39 | // Prefix the error description with a reason from the assert ? |
| 40 | var prefix = opt_reason ? opt_reason + ': ' : ''; |
| 41 | var desc = prefix + matcher.describe(actual); |
| 42 | |
| 43 | // some sort of failure here |
| 44 | throw new goog.labs.testing.MatcherError(desc); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Error thrown when a Matcher fails to match the input value. |
| 52 | * @param {string=} opt_message The error message. |
| 53 | * @constructor |
| 54 | * @extends {goog.debug.Error} |
| 55 | */ |
| 56 | goog.labs.testing.MatcherError = function(opt_message) { |
| 57 | goog.base(this, opt_message); |
| 58 | }; |
| 59 | goog.inherits(goog.labs.testing.MatcherError, goog.debug.Error); |