Strings.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const StringsMock = artifacts.require('StringsMock');
  4. contract('Strings', function (accounts) {
  5. beforeEach(async function () {
  6. this.strings = await StringsMock.new();
  7. });
  8. describe('from uint256 - decimal format', function () {
  9. it('converts 0', async function () {
  10. expect(await this.strings.fromUint256(0)).to.equal('0');
  11. });
  12. it('converts a positive number', async function () {
  13. expect(await this.strings.fromUint256(4132)).to.equal('4132');
  14. });
  15. it('converts MAX_UINT256', async function () {
  16. expect(await this.strings.fromUint256(constants.MAX_UINT256)).to.equal(constants.MAX_UINT256.toString());
  17. });
  18. });
  19. describe('from uint256 - hex format', function () {
  20. it('converts 0', async function () {
  21. expect(await this.strings.fromUint256Hex(0)).to.equal('0x00');
  22. });
  23. it('converts a positive number', async function () {
  24. expect(await this.strings.fromUint256Hex(0x4132)).to.equal('0x4132');
  25. });
  26. it('converts MAX_UINT256', async function () {
  27. expect(await this.strings.fromUint256Hex(constants.MAX_UINT256))
  28. .to.equal(web3.utils.toHex(constants.MAX_UINT256));
  29. });
  30. });
  31. describe('from uint256 - fixed hex format', function () {
  32. it('converts a positive number (long)', async function () {
  33. expect(await this.strings.fromUint256HexFixed(0x4132, 32))
  34. .to.equal('0x0000000000000000000000000000000000000000000000000000000000004132');
  35. });
  36. it('converts a positive number (short)', async function () {
  37. await expectRevert(
  38. this.strings.fromUint256HexFixed(0x4132, 1),
  39. 'Strings: hex length insufficient',
  40. );
  41. });
  42. it('converts MAX_UINT256', async function () {
  43. expect(await this.strings.fromUint256HexFixed(constants.MAX_UINT256, 32))
  44. .to.equal(web3.utils.toHex(constants.MAX_UINT256));
  45. });
  46. });
  47. describe('from address - fixed hex format', function () {
  48. it('converts a random address', async function () {
  49. const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f';
  50. expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr);
  51. });
  52. it('converts an address with leading zeros', async function () {
  53. const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000';
  54. expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr);
  55. });
  56. });
  57. });