assertthat.js 2.0 KB

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