assertthat_test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. goog.provide('goog.labs.testing.assertThatTest');
  15. goog.setTestOnly('goog.labs.testing.assertThatTest');
  16. goog.require('goog.labs.testing.MatcherError');
  17. goog.require('goog.labs.testing.assertThat');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.recordFunction');
  20. var successMatchesFn, failureMatchesFn, describeFn, successTestMatcher;
  21. var failureTestMatcher;
  22. function setUp() {
  23. successMatchesFn =
  24. new goog.testing.recordFunction(function() { return true; });
  25. failureMatchesFn =
  26. new goog.testing.recordFunction(function() { return false; });
  27. describeFn = new goog.testing.recordFunction();
  28. successTestMatcher = function() {
  29. return {matches: successMatchesFn, describe: describeFn};
  30. };
  31. failureTestMatcher = function() {
  32. return {matches: failureMatchesFn, describe: describeFn};
  33. };
  34. }
  35. function testAssertthatAlwaysCallsMatches() {
  36. var value = 7;
  37. goog.labs.testing.assertThat(
  38. value, successTestMatcher(), 'matches is called on success');
  39. assertEquals(1, successMatchesFn.getCallCount());
  40. var matchesCall = successMatchesFn.popLastCall();
  41. assertEquals(value, matchesCall.getArgument(0));
  42. var e = assertThrows(
  43. goog.bind(
  44. goog.labs.testing.assertThat, null, value, failureTestMatcher(),
  45. 'matches is called on failure'));
  46. assertTrue(e instanceof goog.labs.testing.MatcherError);
  47. assertEquals(1, failureMatchesFn.getCallCount());
  48. }
  49. function testAssertthatCallsDescribeOnFailure() {
  50. var value = 7;
  51. var e = assertThrows(
  52. goog.bind(
  53. goog.labs.testing.assertThat, null, value, failureTestMatcher(),
  54. 'describe is called on failure'));
  55. assertTrue(e instanceof goog.labs.testing.MatcherError);
  56. assertEquals(1, failureMatchesFn.getCallCount());
  57. assertEquals(1, describeFn.getCallCount());
  58. var matchesCall = describeFn.popLastCall();
  59. assertEquals(value, matchesCall.getArgument(0));
  60. }