Add toStringSigned to Strings.sol (#4330)
Co-authored-by: Francisco <fg@frang.io> Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
committed by
GitHub
parent
fc19a7947c
commit
1f4e33fb72
5
.changeset/tasty-tomatoes-turn.md
Normal file
5
.changeset/tasty-tomatoes-turn.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'openzeppelin-solidity': major
|
||||||
|
---
|
||||||
|
|
||||||
|
`Strings`: Rename `toString(int256)` to `toStringSigned(int256)`.
|
||||||
@ -46,7 +46,7 @@ library Strings {
|
|||||||
/**
|
/**
|
||||||
* @dev Converts a `int256` to its ASCII `string` decimal representation.
|
* @dev Converts a `int256` to its ASCII `string` decimal representation.
|
||||||
*/
|
*/
|
||||||
function toString(int256 value) internal pure returns (string memory) {
|
function toStringSigned(int256 value) internal pure returns (string memory) {
|
||||||
return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
|
return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -48,22 +48,22 @@ contract('Strings', function () {
|
|||||||
describe('int256', function () {
|
describe('int256', function () {
|
||||||
it('converts MAX_INT256', async function () {
|
it('converts MAX_INT256', async function () {
|
||||||
const value = constants.MAX_INT256;
|
const value = constants.MAX_INT256;
|
||||||
expect(await this.strings.methods['$toString(int256)'](value)).to.equal(value.toString(10));
|
expect(await this.strings.methods['$toStringSigned(int256)'](value)).to.equal(value.toString(10));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('converts MIN_INT256', async function () {
|
it('converts MIN_INT256', async function () {
|
||||||
const value = constants.MIN_INT256;
|
const value = constants.MIN_INT256;
|
||||||
expect(await this.strings.methods['$toString(int256)'](value)).to.equal(value.toString(10));
|
expect(await this.strings.methods['$toStringSigned(int256)'](value)).to.equal(value.toString(10));
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const value of values) {
|
for (const value of values) {
|
||||||
it(`convert ${value}`, async function () {
|
it(`convert ${value}`, async function () {
|
||||||
expect(await this.strings.methods['$toString(int256)'](value)).to.equal(value);
|
expect(await this.strings.methods['$toStringSigned(int256)'](value)).to.equal(value);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`convert negative ${value}`, async function () {
|
it(`convert negative ${value}`, async function () {
|
||||||
const negated = new BN(value).neg();
|
const negated = new BN(value).neg();
|
||||||
expect(await this.strings.methods['$toString(int256)'](negated)).to.equal(negated.toString(10));
|
expect(await this.strings.methods['$toStringSigned(int256)'](negated)).to.equal(negated.toString(10));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user