matcher.js 2.2 KB

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