lib/goog/labs/testing/assertthat.js

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