Add a Strings.toHexString function (#2504)

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
Hadrien Croubois
2021-02-08 21:55:56 +01:00
committed by GitHub
parent f2112be4d8
commit 9c1e703990
4 changed files with 82 additions and 8 deletions

View File

@ -1,4 +1,4 @@
const { constants } = require('@openzeppelin/test-helpers');
const { constants, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
@ -9,7 +9,7 @@ contract('Strings', function (accounts) {
this.strings = await StringsMock.new();
});
describe('from uint256', function () {
describe('from uint256 - decimal format', function () {
it('converts 0', async function () {
expect(await this.strings.fromUint256(0)).to.equal('0');
});
@ -22,4 +22,38 @@ contract('Strings', function (accounts) {
expect(await this.strings.fromUint256(constants.MAX_UINT256)).to.equal(constants.MAX_UINT256.toString());
});
});
describe('from uint256 - hex format', function () {
it('converts 0', async function () {
expect(await this.strings.fromUint256Hex(0)).to.equal('0x00');
});
it('converts a positive number', async function () {
expect(await this.strings.fromUint256Hex(0x4132)).to.equal('0x4132');
});
it('converts MAX_UINT256', async function () {
expect(await this.strings.fromUint256Hex(constants.MAX_UINT256))
.to.equal(web3.utils.toHex(constants.MAX_UINT256));
});
});
describe('from uint256 - fixed hex format', function () {
it('converts a positive number (long)', async function () {
expect(await this.strings.fromUint256HexFixed(0x4132, 32))
.to.equal('0x0000000000000000000000000000000000000000000000000000000000004132');
});
it('converts a positive number (short)', async function () {
await expectRevert(
this.strings.fromUint256HexFixed(0x4132, 1),
'Strings: hex length insufficient',
);
});
it('converts MAX_UINT256', async function () {
expect(await this.strings.fromUint256HexFixed(constants.MAX_UINT256, 32))
.to.equal(web3.utils.toHex(constants.MAX_UINT256));
});
});
});