Bytes.test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const mock = await ethers.deployContract('$Bytes');
  6. return { mock };
  7. }
  8. const lorem = ethers.toUtf8Bytes(
  9. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
  10. );
  11. const present = lorem.at(1);
  12. const absent = 255;
  13. describe('Bytes', function () {
  14. before(async function () {
  15. Object.assign(this, await loadFixture(fixture));
  16. });
  17. describe('indexOf', function () {
  18. it('first', async function () {
  19. expect(await this.mock.$indexOf(lorem, ethers.toBeHex(present))).to.equal(lorem.indexOf(present));
  20. });
  21. it('from index', async function () {
  22. for (const start in Array(lorem.length + 10).fill()) {
  23. const index = lorem.indexOf(present, start);
  24. const result = index === -1 ? ethers.MaxUint256 : index;
  25. expect(await this.mock.$indexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start))).to.equal(result);
  26. }
  27. });
  28. it('absent', async function () {
  29. expect(await this.mock.$indexOf(lorem, ethers.toBeHex(absent))).to.equal(ethers.MaxUint256);
  30. });
  31. });
  32. describe('lastIndexOf', function () {
  33. it('first', async function () {
  34. expect(await this.mock.$lastIndexOf(lorem, ethers.toBeHex(present))).to.equal(lorem.lastIndexOf(present));
  35. });
  36. it('from index', async function () {
  37. for (const start in Array(lorem.length + 10).fill()) {
  38. const index = lorem.lastIndexOf(present, start);
  39. const result = index === -1 ? ethers.MaxUint256 : index;
  40. expect(await this.mock.$lastIndexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start))).to.equal(
  41. result,
  42. );
  43. }
  44. });
  45. it('absent', async function () {
  46. expect(await this.mock.$lastIndexOf(lorem, ethers.toBeHex(absent))).to.equal(ethers.MaxUint256);
  47. });
  48. });
  49. describe('slice', function () {
  50. describe('slice(bytes, uint256)', function () {
  51. for (const [descr, start] of Object.entries({
  52. 'start = 0': 0,
  53. 'start within bound': 10,
  54. 'start out of bound': 1000,
  55. })) {
  56. it(descr, async function () {
  57. const result = ethers.hexlify(lorem.slice(start));
  58. expect(await this.mock.$slice(lorem, start)).to.equal(result);
  59. });
  60. }
  61. });
  62. describe('slice(bytes, uint256, uint256)', function () {
  63. for (const [descr, [start, end]] of Object.entries({
  64. 'start = 0': [0, 42],
  65. 'start and end within bound': [17, 42],
  66. 'end out of bound': [42, 1000],
  67. 'start = end': [17, 17],
  68. 'start > end': [42, 17],
  69. })) {
  70. it(descr, async function () {
  71. const result = ethers.hexlify(lorem.slice(start, end));
  72. expect(await this.mock.$slice(lorem, start, ethers.Typed.uint256(end))).to.equal(result);
  73. });
  74. }
  75. });
  76. });
  77. });