bigint.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var inspect = require('../');
  3. var test = require('tape');
  4. var hasToStringTag = require('has-tostringtag/shams')();
  5. test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
  6. t.test('primitives', function (st) {
  7. st.plan(3);
  8. st.equal(inspect(BigInt(-256)), '-256n');
  9. st.equal(inspect(BigInt(0)), '0n');
  10. st.equal(inspect(BigInt(256)), '256n');
  11. });
  12. t.test('objects', function (st) {
  13. st.plan(3);
  14. st.equal(inspect(Object(BigInt(-256))), 'Object(-256n)');
  15. st.equal(inspect(Object(BigInt(0))), 'Object(0n)');
  16. st.equal(inspect(Object(BigInt(256))), 'Object(256n)');
  17. });
  18. t.test('syntactic primitives', function (st) {
  19. st.plan(3);
  20. /* eslint-disable no-new-func */
  21. st.equal(inspect(Function('return -256n')()), '-256n');
  22. st.equal(inspect(Function('return 0n')()), '0n');
  23. st.equal(inspect(Function('return 256n')()), '256n');
  24. });
  25. t.test('toStringTag', { skip: !hasToStringTag }, function (st) {
  26. st.plan(1);
  27. var faker = {};
  28. faker[Symbol.toStringTag] = 'BigInt';
  29. st.equal(
  30. inspect(faker),
  31. '{ [Symbol(Symbol.toStringTag)]: \'BigInt\' }',
  32. 'object lying about being a BigInt inspects as an object'
  33. );
  34. });
  35. t.test('numericSeparator', function (st) {
  36. st.equal(inspect(BigInt(0), { numericSeparator: false }), '0n', '0n, numericSeparator false');
  37. st.equal(inspect(BigInt(0), { numericSeparator: true }), '0n', '0n, numericSeparator true');
  38. st.equal(inspect(BigInt(1234), { numericSeparator: false }), '1234n', '1234n, numericSeparator false');
  39. st.equal(inspect(BigInt(1234), { numericSeparator: true }), '1_234n', '1234n, numericSeparator true');
  40. st.equal(inspect(BigInt(-1234), { numericSeparator: false }), '-1234n', '1234n, numericSeparator false');
  41. st.equal(inspect(BigInt(-1234), { numericSeparator: true }), '-1_234n', '1234n, numericSeparator true');
  42. st.end();
  43. });
  44. t.end();
  45. });