decoratormatcher.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 built-in decorators: is, describedAs, anything.
  16. */
  17. goog.provide('goog.labs.testing.AnythingMatcher');
  18. goog.require('goog.labs.testing.Matcher');
  19. /**
  20. * The Anything matcher. Matches all possible inputs.
  21. *
  22. * @constructor
  23. * @implements {goog.labs.testing.Matcher}
  24. * @final
  25. */
  26. goog.labs.testing.AnythingMatcher = function() {};
  27. /**
  28. * Matches anything. Useful if one doesn't care what the object under test is.
  29. *
  30. * @override
  31. */
  32. goog.labs.testing.AnythingMatcher.prototype.matches = function(actualObject) {
  33. return true;
  34. };
  35. /**
  36. * This method is never called but is needed so AnythingMatcher implements the
  37. * Matcher interface.
  38. *
  39. * @override
  40. */
  41. goog.labs.testing.AnythingMatcher.prototype.describe = function(actualObject) {
  42. throw Error('AnythingMatcher should never fail!');
  43. };
  44. /**
  45. * Returns a matcher that matches anything.
  46. *
  47. * @return {!goog.labs.testing.AnythingMatcher} A AnythingMatcher.
  48. */
  49. function anything() {
  50. return new goog.labs.testing.AnythingMatcher();
  51. }
  52. /**
  53. * Returnes any matcher that is passed to it (aids readability).
  54. *
  55. * @param {!goog.labs.testing.Matcher} matcher A matcher.
  56. * @return {!goog.labs.testing.Matcher} The wrapped matcher.
  57. */
  58. function is(matcher) {
  59. return matcher;
  60. }
  61. /**
  62. * Returns a matcher with a customized description for the given matcher.
  63. *
  64. * @param {string} description The custom description for the matcher.
  65. * @param {!goog.labs.testing.Matcher} matcher The matcher.
  66. *
  67. * @return {!goog.labs.testing.Matcher} The matcher with custom description.
  68. */
  69. function describedAs(description, matcher) {
  70. matcher.describe = function(value) { return description; };
  71. return matcher;
  72. }