Strings.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const StringsMock = artifacts.require('StringsMock');
  4. contract('Strings', function (accounts) {
  5. before(async function () {
  6. this.strings = await StringsMock.new();
  7. });
  8. describe('toString', function () {
  9. for (const [ key, value ] of Object.entries([
  10. '0',
  11. '7',
  12. '10',
  13. '99',
  14. '100',
  15. '101',
  16. '123',
  17. '4132',
  18. '12345',
  19. '1234567',
  20. '1234567890',
  21. '123456789012345',
  22. '12345678901234567890',
  23. '123456789012345678901234567890',
  24. '1234567890123456789012345678901234567890',
  25. '12345678901234567890123456789012345678901234567890',
  26. '123456789012345678901234567890123456789012345678901234567890',
  27. '1234567890123456789012345678901234567890123456789012345678901234567890',
  28. ].reduce((acc, value) => Object.assign(acc, { [value]: new BN(value) }), {
  29. MAX_UINT256: constants.MAX_UINT256.toString(),
  30. }))) {
  31. it(`converts ${key}`, async function () {
  32. expect(await this.strings.methods['toString(uint256)'](value)).to.equal(value.toString(10));
  33. });
  34. }
  35. });
  36. describe('toHexString', function () {
  37. it('converts 0', async function () {
  38. expect(await this.strings.methods['toHexString(uint256)'](0)).to.equal('0x00');
  39. });
  40. it('converts a positive number', async function () {
  41. expect(await this.strings.methods['toHexString(uint256)'](0x4132)).to.equal('0x4132');
  42. });
  43. it('converts MAX_UINT256', async function () {
  44. expect(await this.strings.methods['toHexString(uint256)'](constants.MAX_UINT256))
  45. .to.equal(web3.utils.toHex(constants.MAX_UINT256));
  46. });
  47. });
  48. describe('toHexString fixed', function () {
  49. it('converts a positive number (long)', async function () {
  50. expect(await this.strings.methods['toHexString(uint256,uint256)'](0x4132, 32))
  51. .to.equal('0x0000000000000000000000000000000000000000000000000000000000004132');
  52. });
  53. it('converts a positive number (short)', async function () {
  54. await expectRevert(
  55. this.strings.methods['toHexString(uint256,uint256)'](0x4132, 1),
  56. 'Strings: hex length insufficient',
  57. );
  58. });
  59. it('converts MAX_UINT256', async function () {
  60. expect(await this.strings.methods['toHexString(uint256,uint256)'](constants.MAX_UINT256, 32))
  61. .to.equal(web3.utils.toHex(constants.MAX_UINT256));
  62. });
  63. });
  64. describe('toHexString address', function () {
  65. it('converts a random address', async function () {
  66. const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f';
  67. expect(await this.strings.methods['toHexString(address)'](addr)).to.equal(addr);
  68. });
  69. it('converts an address with leading zeros', async function () {
  70. const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000';
  71. expect(await this.strings.methods['toHexString(address)'](addr)).to.equal(addr);
  72. });
  73. });
  74. describe('equal', function () {
  75. it('compares two empty strings', async function () {
  76. expect(await this.strings.methods['equal(string,string)']('', '')).to.equal(true);
  77. });
  78. it('compares two equal strings', async function () {
  79. expect(await this.strings.methods['equal(string,string)']('a', 'a')).to.equal(true);
  80. });
  81. it('compares two different strings', async function () {
  82. expect(await this.strings.methods['equal(string,string)']('a', 'b')).to.equal(false);
  83. });
  84. it('compares two different strings of different lengths', async function () {
  85. expect(await this.strings.methods['equal(string,string)']('a', 'aa')).to.equal(false);
  86. expect(await this.strings.methods['equal(string,string)']('aa', 'a')).to.equal(false);
  87. });
  88. it('compares two different strings of different (big) lengths', async function () {
  89. const str1 = 'a'.repeat(201);
  90. const str2 = 'a'.repeat(200) + 'b';
  91. expect(await this.strings.methods['equal(string,string)'](str1, str2)).to.equal(false);
  92. });
  93. });
  94. });