s-number.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. describe('Number', function () {
  2. 'use strict';
  3. describe('#toExponential()', function () {
  4. // the spec allows for this test to fail.
  5. it('throws a RangeError when < 0 or > 20 (or > 100 in ES2018+)', function () {
  6. expect(function () { return 1.0.toExponential(-1); }).toThrow();
  7. expect(function () { return 1.0.toExponential(-Infinity); }).toThrow();
  8. expect(function () { return 1.0.toExponential(Infinity); }).toThrow();
  9. expect(function () {
  10. return [
  11. (0.9).toExponential(21), // ES2018 increased the limit from 21 to 100
  12. (0.9).toExponential(101)
  13. ];
  14. }).toThrow();
  15. });
  16. it('works for NaN receiver', function () {
  17. expect(NaN.toExponential(NaN)).toBe('NaN');
  18. expect(NaN.toExponential('abc')).toBe('NaN');
  19. });
  20. it('works for non-finite receivers', function () {
  21. expect(Infinity.toExponential()).toBe('Infinity');
  22. expect((-Infinity).toExponential()).toBe('-Infinity');
  23. });
  24. it('should round properly', function () {
  25. expect(1.0.toExponential()).toBe('1e+0');
  26. expect(1.0.toExponential(0)).toBe('1e+0');
  27. expect(1.0.toExponential(1)).toBe('1.0e+0');
  28. expect(1.0.toExponential(2)).toBe('1.00e+0');
  29. expect(1.2345.toExponential()).toBe('1.2345e+0');
  30. expect(1.2345.toExponential(0)).toBe('1e+0');
  31. expect(1.2345.toExponential(1)).toBe('1.2e+0');
  32. expect(1.2345.toExponential(2)).toBe('1.23e+0');
  33. expect(1.2345.toExponential(3)).toMatch(/^1.23[45]e\+0$/);
  34. expect(1.2345.toExponential(4)).toBe('1.2345e+0');
  35. expect(1.2345.toExponential(5)).toBe('1.23450e+0');
  36. expect((-6.9e-11).toExponential(4)).toBe('-6.9000e-11');
  37. });
  38. });
  39. describe('#toFixed()', function () {
  40. it('should convert numbers correctly', function () {
  41. expect((0.00008).toFixed(3)).toBe('0.000');
  42. expect((0.9).toFixed(0)).toBe('1');
  43. expect((1.255).toFixed(2)).toBe('1.25');
  44. expect((1843654265.0774949).toFixed(5)).toBe('1843654265.07749');
  45. expect((1000000000000000128).toFixed(0)).toBe('1000000000000000128');
  46. });
  47. });
  48. describe('#toPrecision()', function () {
  49. // the spec allows for this test to fail.
  50. it('throws a RangeError when < 1 or > 21 (or > 100 in ES2018+)', function () {
  51. expect(function () { return (0.9).toPrecision(0); }).toThrow();
  52. expect(function () {
  53. return [
  54. (0.9).toPrecision(22), // ES2018 increased the limit from 21 to 100
  55. (0.9).toPrecision(101)
  56. ];
  57. }).toThrow();
  58. });
  59. it('works as expected', function () {
  60. expect((0.00008).toPrecision(3)).toBe('0.0000800');
  61. expect((1.255).toPrecision(2)).toBe('1.3');
  62. expect((1843654265.0774949).toPrecision(13)).toBe('1843654265.077');
  63. expect(NaN.toPrecision(1)).toBe('NaN');
  64. });
  65. it('works with an undefined precision', function () {
  66. var num = 123.456;
  67. expect(num.toPrecision()).toBe(String(num));
  68. expect(num.toPrecision(undefined)).toBe(String(num));
  69. });
  70. });
  71. describe('constants', function () {
  72. it('should have MAX_VALUE', function () {
  73. expect(Number.MAX_VALUE).toBe(1.7976931348623157e308);
  74. });
  75. it('should have MIN_VALUE', function () {
  76. expect(Number.MIN_VALUE).toBe(5e-324);
  77. });
  78. it('should have NaN', function () {
  79. expect(Number.NaN).not.toBe(Number.NaN);
  80. });
  81. it('should have POSITIVE_INFINITY', function () {
  82. expect(Number.POSITIVE_INFINITY).toBe(Infinity);
  83. });
  84. it('should have NEGATIVE_INFINITY', function () {
  85. expect(Number.NEGATIVE_INFINITY).toBe(-Infinity);
  86. });
  87. });
  88. });