Base64.test.js 1.1 KB

1234567891011121314151617181920212223242526272829
  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('$Base64');
  6. return { mock };
  7. }
  8. describe('Strings', function () {
  9. beforeEach(async function () {
  10. Object.assign(this, await loadFixture(fixture));
  11. });
  12. describe('from bytes - base64', function () {
  13. for (const { title, input, expected } of [
  14. { title: 'converts to base64 encoded string with double padding', input: 'test', expected: 'dGVzdA==' },
  15. { title: 'converts to base64 encoded string with single padding', input: 'test1', expected: 'dGVzdDE=' },
  16. { title: 'converts to base64 encoded string without padding', input: 'test12', expected: 'dGVzdDEy' },
  17. { title: 'empty bytes', input: '0x', expected: '' },
  18. ])
  19. it(title, async function () {
  20. const raw = ethers.isBytesLike(input) ? input : ethers.toUtf8Bytes(input);
  21. expect(await this.mock.$encode(raw)).to.equal(ethers.encodeBase64(raw));
  22. expect(await this.mock.$encode(raw)).to.equal(expected);
  23. });
  24. });
  25. });