number.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Should
  3. * Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. module.exports = function(should, Assertion) {
  7. /**
  8. * Assert given object is NaN
  9. * @name NaN
  10. * @memberOf Assertion
  11. * @category assertion numbers
  12. * @example
  13. *
  14. * (10).should.not.be.NaN();
  15. * NaN.should.be.NaN();
  16. */
  17. Assertion.add('NaN', function() {
  18. this.params = { operator: 'to be NaN' };
  19. this.assert(this.obj !== this.obj);
  20. });
  21. /**
  22. * Assert given object is not finite (positive or negative)
  23. *
  24. * @name Infinity
  25. * @memberOf Assertion
  26. * @category assertion numbers
  27. * @example
  28. *
  29. * (10).should.not.be.Infinity();
  30. * NaN.should.not.be.Infinity();
  31. */
  32. Assertion.add('Infinity', function() {
  33. this.params = { operator: 'to be Infinity' };
  34. this.is.a.Number()
  35. .and.not.a.NaN()
  36. .and.assert(!isFinite(this.obj));
  37. });
  38. /**
  39. * Assert given number between `start` and `finish` or equal one of them.
  40. *
  41. * @name within
  42. * @memberOf Assertion
  43. * @category assertion numbers
  44. * @param {number} start Start number
  45. * @param {number} finish Finish number
  46. * @param {string} [description] Optional message
  47. * @example
  48. *
  49. * (10).should.be.within(0, 20);
  50. */
  51. Assertion.add('within', function(start, finish, description) {
  52. this.params = { operator: 'to be within ' + start + '..' + finish, message: description };
  53. this.assert(this.obj >= start && this.obj <= finish);
  54. });
  55. /**
  56. * Assert given number near some other `value` within `delta`
  57. *
  58. * @name approximately
  59. * @memberOf Assertion
  60. * @category assertion numbers
  61. * @param {number} value Center number
  62. * @param {number} delta Radius
  63. * @param {string} [description] Optional message
  64. * @example
  65. *
  66. * (9.99).should.be.approximately(10, 0.1);
  67. */
  68. Assertion.add('approximately', function(value, delta, description) {
  69. this.params = { operator: 'to be approximately ' + value + ' ±' + delta, message: description };
  70. this.assert(Math.abs(this.obj - value) <= delta);
  71. });
  72. /**
  73. * Assert given number above `n`.
  74. *
  75. * @name above
  76. * @alias Assertion#greaterThan
  77. * @memberOf Assertion
  78. * @category assertion numbers
  79. * @param {number} n Margin number
  80. * @param {string} [description] Optional message
  81. * @example
  82. *
  83. * (10).should.be.above(0);
  84. */
  85. Assertion.add('above', function(n, description) {
  86. this.params = { operator: 'to be above ' + n, message: description };
  87. this.assert(this.obj > n);
  88. });
  89. /**
  90. * Assert given number below `n`.
  91. *
  92. * @name below
  93. * @alias Assertion#lessThan
  94. * @memberOf Assertion
  95. * @category assertion numbers
  96. * @param {number} n Margin number
  97. * @param {string} [description] Optional message
  98. * @example
  99. *
  100. * (0).should.be.below(10);
  101. */
  102. Assertion.add('below', function(n, description) {
  103. this.params = { operator: 'to be below ' + n, message: description };
  104. this.assert(this.obj < n);
  105. });
  106. Assertion.alias('above', 'greaterThan');
  107. Assertion.alias('below', 'lessThan');
  108. /**
  109. * Assert given number above `n`.
  110. *
  111. * @name aboveOrEqual
  112. * @alias Assertion#greaterThanOrEqual
  113. * @memberOf Assertion
  114. * @category assertion numbers
  115. * @param {number} n Margin number
  116. * @param {string} [description] Optional message
  117. * @example
  118. *
  119. * (10).should.be.aboveOrEqual(0);
  120. * (10).should.be.aboveOrEqual(10);
  121. */
  122. Assertion.add('aboveOrEqual', function(n, description) {
  123. this.params = { operator: 'to be above or equal' + n, message: description };
  124. this.assert(this.obj >= n);
  125. });
  126. /**
  127. * Assert given number below `n`.
  128. *
  129. * @name belowOrEqual
  130. * @alias Assertion#lessThanOrEqual
  131. * @memberOf Assertion
  132. * @category assertion numbers
  133. * @param {number} n Margin number
  134. * @param {string} [description] Optional message
  135. * @example
  136. *
  137. * (0).should.be.belowOrEqual(10);
  138. * (0).should.be.belowOrEqual(0);
  139. */
  140. Assertion.add('belowOrEqual', function(n, description) {
  141. this.params = { operator: 'to be below or equal' + n, message: description };
  142. this.assert(this.obj <= n);
  143. });
  144. Assertion.alias('aboveOrEqual', 'greaterThanOrEqual');
  145. Assertion.alias('belowOrEqual', 'lessThanOrEqual');
  146. };