BigInt.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. describe("correct behavior", () => {
  2. test("basic functionality", () => {
  3. expect(BigInt).toHaveLength(1);
  4. expect(BigInt.name).toBe("BigInt");
  5. });
  6. test("constructor with numbers", () => {
  7. expect(BigInt(0)).toBe(0n);
  8. expect(BigInt(1)).toBe(1n);
  9. expect(BigInt(+1)).toBe(1n);
  10. expect(BigInt(-1)).toBe(-1n);
  11. expect(BigInt(123n)).toBe(123n);
  12. });
  13. test("constructor with strings", () => {
  14. expect(BigInt("")).toBe(0n);
  15. expect(BigInt("0")).toBe(0n);
  16. expect(BigInt("1")).toBe(1n);
  17. expect(BigInt("+1")).toBe(1n);
  18. expect(BigInt("-1")).toBe(-1n);
  19. expect(BigInt("-1")).toBe(-1n);
  20. expect(BigInt("42")).toBe(42n);
  21. expect(BigInt(" \n 00100 \n ")).toBe(100n);
  22. expect(BigInt("3323214327642987348732109829832143298746432437532197321")).toBe(
  23. 3323214327642987348732109829832143298746432437532197321n
  24. );
  25. });
  26. test("constructor with objects", () => {
  27. expect(BigInt([])).toBe(0n);
  28. });
  29. });
  30. describe("errors", () => {
  31. test('cannot be constructed with "new"', () => {
  32. expect(() => {
  33. new BigInt();
  34. }).toThrowWithMessage(TypeError, "BigInt is not a constructor");
  35. });
  36. test("invalid arguments", () => {
  37. expect(() => {
  38. BigInt(null);
  39. }).toThrowWithMessage(TypeError, "Cannot convert null to BigInt");
  40. expect(() => {
  41. BigInt(undefined);
  42. }).toThrowWithMessage(TypeError, "Cannot convert undefined to BigInt");
  43. expect(() => {
  44. BigInt(Symbol());
  45. }).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
  46. ["foo", "123n", "1+1", {}, function () {}].forEach(value => {
  47. expect(() => {
  48. BigInt(value);
  49. }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
  50. });
  51. });
  52. test("invalid numeric arguments", () => {
  53. [1.23, Infinity, -Infinity, NaN].forEach(value => {
  54. expect(() => {
  55. BigInt(value);
  56. }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
  57. });
  58. });
  59. });