numbermatcher_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.numberMatcherTest');
  15. goog.setTestOnly('goog.labs.testing.numberMatcherTest');
  16. /** @suppress {extraRequire} */
  17. goog.require('goog.labs.testing.LessThanMatcher');
  18. goog.require('goog.labs.testing.MatcherError');
  19. goog.require('goog.labs.testing.assertThat');
  20. goog.require('goog.testing.jsunit');
  21. function testAnyNumber() {
  22. goog.labs.testing.assertThat(0, anyNumber(3), 'typeof 0 == "number"');
  23. assertMatcherError(function() {
  24. goog.labs.testing.assertThat('chicken', anyNumber());
  25. }, 'typeof "chicken" == "number"');
  26. }
  27. function testGreaterThan() {
  28. goog.labs.testing.assertThat(4, greaterThan(3), '4 > 3');
  29. assertMatcherError(function() {
  30. goog.labs.testing.assertThat(2, greaterThan(3));
  31. }, '2 > 3');
  32. }
  33. function testGreaterThanEqualTo() {
  34. goog.labs.testing.assertThat(5, greaterThanEqualTo(4), '5 >= 4');
  35. goog.labs.testing.assertThat(5, greaterThanEqualTo(5), '5 >= 5');
  36. assertMatcherError(function() {
  37. goog.labs.testing.assertThat(3, greaterThanEqualTo(5));
  38. }, '3 >= 5');
  39. }
  40. function testLessThan() {
  41. goog.labs.testing.assertThat(6, lessThan(7), '6 < 7');
  42. assertMatcherError(function() {
  43. goog.labs.testing.assertThat(7, lessThan(5));
  44. }, '7 < 5');
  45. }
  46. function testLessThanEqualTo() {
  47. goog.labs.testing.assertThat(8, lessThanEqualTo(8), '8 <= 8');
  48. goog.labs.testing.assertThat(8, lessThanEqualTo(9), '8 <= 9');
  49. assertMatcherError(function() {
  50. goog.labs.testing.assertThat(7, lessThanEqualTo(5));
  51. }, '7 <= 5');
  52. }
  53. function testEqualTo() {
  54. goog.labs.testing.assertThat(7, equalTo(7), '7 equals 7');
  55. assertMatcherError(function() {
  56. goog.labs.testing.assertThat(7, equalTo(5));
  57. }, '7 == 5');
  58. }
  59. function testCloseTo() {
  60. goog.labs.testing.assertThat(7, closeTo(10, 4), '7 within range(4) of 10');
  61. assertMatcherError(function() {
  62. goog.labs.testing.assertThat(5, closeTo(10, 3));
  63. }, '5 within range(3) of 10');
  64. }
  65. function assertMatcherError(callable, errorString) {
  66. var e = assertThrows(errorString || 'callable throws exception', callable);
  67. assertTrue(e instanceof goog.labs.testing.MatcherError);
  68. }