From 193a6b961d24ae95f0951fc01eaa479f5f45ac9c Mon Sep 17 00:00:00 2001 From: murrayee Date: Tue, 14 Nov 2023 20:00:04 +0800 Subject: [PATCH 1/7] chore: Add tests for utils toHex --- packages/utils/test/unit/bytes.test.ts | 42 +++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index b09625d7f13..7c2036482a9 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -1,6 +1,6 @@ import "../setup.js"; import {assert, expect} from "chai"; -import {intToBytes, bytesToInt} from "../../src/index.js"; +import {intToBytes, bytesToInt, toHex} from "../../src/index.js"; describe("intToBytes", () => { const zeroedArray = (length: number): number[] => Array.from({length}, () => 0); @@ -47,3 +47,43 @@ describe("bytesToInt", () => { }); } }); + +describe("toHex function", () => { + it("should convert a Buffer to hex string", () => { + const buffer = Buffer.from("Hello, World!", "utf-8"); + const result = toHex(buffer); + const expected = "0x48656c6c6f2c20576f726c6421"; + + expect(result).to.equal(expected); + }); + + it("should convert a Uint8Array to hex string", () => { + const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); + const result = toHex(uint8Array); + const expected = "0x48656c6c6f"; + + expect(result).to.equal(expected); + }); + + it("should convert an array to hex string", () => { + const array = Buffer.from([72, 101, 108, 108, 111]); + const result = toHex(array); + const expected = "0x48656c6c6f"; + + expect(result).to.equal(expected); + }); + + it("should handle an empty input", () => { + const result = toHex(Buffer.from([])); + const expected = "0x"; + + expect(result).to.equal(expected); + }); + + it("should handle invalid input", () => { + const result = toHex("invalidInput"); + const expected = "0x"; + + expect(result).to.equal(expected); + }); +}); From c970cc0601edd8375bb525de239185b5e9ff1468 Mon Sep 17 00:00:00 2001 From: murrayee Date: Tue, 14 Nov 2023 20:06:02 +0800 Subject: [PATCH 2/7] chore: Add tests for utils fromHex --- packages/utils/test/unit/bytes.test.ts | 35 +++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index 7c2036482a9..6d3e80a7916 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -1,6 +1,6 @@ import "../setup.js"; import {assert, expect} from "chai"; -import {intToBytes, bytesToInt, toHex} from "../../src/index.js"; +import {intToBytes, bytesToInt, toHex, fromHex} from "../../src/index.js"; describe("intToBytes", () => { const zeroedArray = (length: number): number[] => Array.from({length}, () => 0); @@ -87,3 +87,36 @@ describe("toHex function", () => { expect(result).to.equal(expected); }); }); + +describe("fromHex function", () => { + it("should convert hex string to Uint8Array", () => { + const hexString = "0x48656c6c6f2c20576f726c6421"; + const result = fromHex(hexString); + const expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]); + + expect(result).to.deep.equal(expected); + }); + + it("should handle hex string without 0x prefix", () => { + const hexString = "48656c6c6f2c20576f726c6421"; + const result = fromHex(hexString); + const expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]); + + expect(result).to.deep.equal(expected); + }); + + it("should handle empty hex string", () => { + const hexString = "0x"; + const result = fromHex(hexString); + const expected = new Uint8Array([]); + + expect(result).to.deep.equal(expected); + }); + + it("should throw an error for invalid hex string", () => { + const hexString = "invalidHex"; + + // Expect an error to be thrown + expect(() => fromHex(hexString)).to.throw(); + }); +}); From 75c5fe20f0c23e9824956c94c33427bddeffce06 Mon Sep 17 00:00:00 2001 From: murrayee Date: Tue, 14 Nov 2023 20:08:55 +0800 Subject: [PATCH 3/7] chore: Add tests for utils toHexString --- packages/utils/test/unit/bytes.test.ts | 54 ++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index 6d3e80a7916..e45eff1c5cf 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -1,6 +1,6 @@ import "../setup.js"; import {assert, expect} from "chai"; -import {intToBytes, bytesToInt, toHex, fromHex} from "../../src/index.js"; +import {intToBytes, bytesToInt, toHex, fromHex, toHexString} from "../../src/index.js"; describe("intToBytes", () => { const zeroedArray = (length: number): number[] => Array.from({length}, () => 0); @@ -115,8 +115,56 @@ describe("fromHex function", () => { it("should throw an error for invalid hex string", () => { const hexString = "invalidHex"; - - // Expect an error to be thrown expect(() => fromHex(hexString)).to.throw(); }); }); + +describe("toHexString function", () => { + it("should convert Uint8Array to hex string", () => { + const bytes = new Uint8Array([72, 101, 108, 108, 111]); + const result = toHexString(bytes); + const expected = "0x48656c6c6f"; + + expect(result).to.equal(expected); + }); + + it("should handle Uint8Array with single-digit bytes", () => { + const bytes = new Uint8Array([1, 2, 3]); + const result = toHexString(bytes); + const expected = "0x010203"; + + expect(result).to.equal(expected); + }); + + it("should handle Uint8Array with zero-length", () => { + const bytes = new Uint8Array([]); + const result = toHexString(bytes); + const expected = "0x"; + + expect(result).to.equal(expected); + }); + + it("should handle Uint8Array with all zeros", () => { + const bytes = new Uint8Array([0, 0, 0, 0]); + const result = toHexString(bytes); + const expected = "0x00000000"; + + expect(result).to.equal(expected); + }); + + it("should handle Uint8Array with mixed single and double-digit bytes", () => { + const bytes = new Uint8Array([15, 255, 16, 0, 127]); + const result = toHexString(bytes); + const expected = "0x0fff10007f"; + + expect(result).to.equal(expected); + }); + + it("should handle large Uint8Array", () => { + const bytes = new Uint8Array(1000).fill(255); + const result = toHexString(bytes); + const expected = "0xff".repeat(1000); + + expect(result).to.equal(expected); + }); +}); From 94fbf076332afa4e52051f2f913b21a4b3907c1b Mon Sep 17 00:00:00 2001 From: murrayee Date: Tue, 21 Nov 2023 10:00:00 +0800 Subject: [PATCH 4/7] chore: enhance tests for bytes utils --- packages/utils/test/unit/bytes.test.ts | 206 ++++++++----------------- 1 file changed, 68 insertions(+), 138 deletions(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index e45eff1c5cf..40b892a9a87 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -1,29 +1,29 @@ import "../setup.js"; -import {assert, expect} from "chai"; -import {intToBytes, bytesToInt, toHex, fromHex, toHexString} from "../../src/index.js"; +import { assert, expect } from "chai"; +import { intToBytes, bytesToInt, toHex, fromHex, toHexString } from "../../src/index.js"; describe("intToBytes", () => { - const zeroedArray = (length: number): number[] => Array.from({length}, () => 0); - const testCases: {input: [bigint | number, number]; output: Buffer}[] = [ - {input: [255, 1], output: Buffer.from([255])}, - {input: [1, 4], output: Buffer.from([1, 0, 0, 0])}, - {input: [BigInt(255), 1], output: Buffer.from([255])}, - {input: [65535, 2], output: Buffer.from([255, 255])}, - {input: [BigInt(65535), 2], output: Buffer.from([255, 255])}, - {input: [16777215, 3], output: Buffer.from([255, 255, 255])}, - {input: [BigInt(16777215), 3], output: Buffer.from([255, 255, 255])}, - {input: [4294967295, 4], output: Buffer.from([255, 255, 255, 255])}, - {input: [BigInt(4294967295), 4], output: Buffer.from([255, 255, 255, 255])}, - {input: [65535, 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)])}, - {input: [BigInt(65535), 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)])}, - {input: [65535, 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)])}, - {input: [BigInt(65535), 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)])}, - {input: [65535, 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)])}, - {input: [BigInt(65535), 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)])}, - {input: [65535, 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)])}, - {input: [BigInt(65535), 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)])}, + const zeroedArray = (length: number): number[] => Array.from({ length }, () => 0); + const testCases: { input: [bigint | number, number]; output: Buffer }[] = [ + { input: [255, 1], output: Buffer.from([255]) }, + { input: [1, 4], output: Buffer.from([1, 0, 0, 0]) }, + { input: [BigInt(255), 1], output: Buffer.from([255]) }, + { input: [65535, 2], output: Buffer.from([255, 255]) }, + { input: [BigInt(65535), 2], output: Buffer.from([255, 255]) }, + { input: [16777215, 3], output: Buffer.from([255, 255, 255]) }, + { input: [BigInt(16777215), 3], output: Buffer.from([255, 255, 255]) }, + { input: [4294967295, 4], output: Buffer.from([255, 255, 255, 255]) }, + { input: [BigInt(4294967295), 4], output: Buffer.from([255, 255, 255, 255]) }, + { input: [65535, 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)]) }, + { input: [BigInt(65535), 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)]) }, + { input: [65535, 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)]) }, + { input: [BigInt(65535), 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)]) }, + { input: [65535, 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)]) }, + { input: [BigInt(65535), 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)]) }, + { input: [65535, 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)]) }, + { input: [BigInt(65535), 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)]) }, ]; - for (const {input, output} of testCases) { + for (const { input, output } of testCases) { const type = typeof input; const length = input[1]; it(`should correctly serialize ${type} to bytes length ${length}`, () => { @@ -33,138 +33,68 @@ describe("intToBytes", () => { }); describe("bytesToInt", () => { - const testCases: {input: Buffer; output: number}[] = [ - {input: Buffer.from([3]), output: 3}, - {input: Buffer.from([20, 0]), output: 20}, - {input: Buffer.from([3, 20]), output: 5123}, - {input: Buffer.from([255, 255]), output: 65535}, - {input: Buffer.from([255, 255, 255]), output: 16777215}, - {input: Buffer.from([255, 255, 255, 255]), output: 4294967295}, + const testCases: { input: Buffer; output: number }[] = [ + { input: Buffer.from([3]), output: 3 }, + { input: Buffer.from([20, 0]), output: 20 }, + { input: Buffer.from([3, 20]), output: 5123 }, + { input: Buffer.from([255, 255]), output: 65535 }, + { input: Buffer.from([255, 255, 255]), output: 16777215 }, + { input: Buffer.from([255, 255, 255, 255]), output: 4294967295 }, ]; - for (const {input, output} of testCases) { + for (const { input, output } of testCases) { it(`should produce ${output}`, () => { expect(bytesToInt(input)).to.be.equal(output); }); } }); -describe("toHex function", () => { - it("should convert a Buffer to hex string", () => { - const buffer = Buffer.from("Hello, World!", "utf-8"); - const result = toHex(buffer); - const expected = "0x48656c6c6f2c20576f726c6421"; - - expect(result).to.equal(expected); - }); - - it("should convert a Uint8Array to hex string", () => { - const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); - const result = toHex(uint8Array); - const expected = "0x48656c6c6f"; - - expect(result).to.equal(expected); - }); - - it("should convert an array to hex string", () => { - const array = Buffer.from([72, 101, 108, 108, 111]); - const result = toHex(array); - const expected = "0x48656c6c6f"; - - expect(result).to.equal(expected); - }); - - it("should handle an empty input", () => { - const result = toHex(Buffer.from([])); - const expected = "0x"; - - expect(result).to.equal(expected); - }); - - it("should handle invalid input", () => { - const result = toHex("invalidInput"); - const expected = "0x"; - - expect(result).to.equal(expected); - }); +describe("toHex", () => { + const testCases: { input: Buffer | Uint8Array | string; output: string }[] = [ + { input: Buffer.from("Hello, World!", "utf-8"), output: '0x48656c6c6f2c20576f726c6421' }, + { input: new Uint8Array([72, 101, 108, 108, 111]), output: '0x48656c6c6f' }, + { input: Buffer.from([72, 101, 108, 108, 111]), output: '0x48656c6c6f' }, + { input: Buffer.from([]), output: '0x' }, + { input: 'invalidInput', output: '0x' }, + ] + for (const { input, output } of testCases) { + it(`should convert ${input} to hex string ${output}`, () => { + expect(toHex(input)).to.be.equal(output); + }); + } }); -describe("fromHex function", () => { - it("should convert hex string to Uint8Array", () => { - const hexString = "0x48656c6c6f2c20576f726c6421"; - const result = fromHex(hexString); - const expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]); - - expect(result).to.deep.equal(expected); - }); - - it("should handle hex string without 0x prefix", () => { - const hexString = "48656c6c6f2c20576f726c6421"; - const result = fromHex(hexString); - const expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]); - - expect(result).to.deep.equal(expected); - }); - - it("should handle empty hex string", () => { - const hexString = "0x"; - const result = fromHex(hexString); - const expected = new Uint8Array([]); - - expect(result).to.deep.equal(expected); - }); +describe("fromHex", () => { + const testCases: { input: string; output: Buffer | Uint8Array }[] = [ + { input: '0x48656c6c6f2c20576f726c6421', output: new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]) }, + { input: '48656c6c6f2c20576f726c6421', output: new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]) }, + { input: '0x', output: new Uint8Array([]) }, + ] + for (const { input, output } of testCases) { + it(`should convert hex string ${input} to Uint8Array ${output}`, () => { + expect(fromHex(input)).to.be.equal(output); + }); + } it("should throw an error for invalid hex string", () => { const hexString = "invalidHex"; expect(() => fromHex(hexString)).to.throw(); }); }); -describe("toHexString function", () => { - it("should convert Uint8Array to hex string", () => { - const bytes = new Uint8Array([72, 101, 108, 108, 111]); - const result = toHexString(bytes); - const expected = "0x48656c6c6f"; - - expect(result).to.equal(expected); - }); +describe("toHexString", () => { - it("should handle Uint8Array with single-digit bytes", () => { - const bytes = new Uint8Array([1, 2, 3]); - const result = toHexString(bytes); - const expected = "0x010203"; - - expect(result).to.equal(expected); - }); - - it("should handle Uint8Array with zero-length", () => { - const bytes = new Uint8Array([]); - const result = toHexString(bytes); - const expected = "0x"; - - expect(result).to.equal(expected); - }); + const testCases: { input: Uint8Array; output: string }[] = [ + { input: new Uint8Array([1, 2, 3]), output: '0x010203' }, + { input: new Uint8Array([72, 101, 108, 108, 111]), output: '0x48656c6c6f' }, + { input: new Uint8Array([]), output: '0x' }, + { input: new Uint8Array([0, 0, 0, 0]), output: '0x00000000' }, + { input: new Uint8Array([15, 255, 16, 0, 127]), output: '0x0fff10007f' }, + { input: new Uint8Array(1000).fill(255), output: "0xff".repeat(1000) }, + ] - it("should handle Uint8Array with all zeros", () => { - const bytes = new Uint8Array([0, 0, 0, 0]); - const result = toHexString(bytes); - const expected = "0x00000000"; - - expect(result).to.equal(expected); - }); - - it("should handle Uint8Array with mixed single and double-digit bytes", () => { - const bytes = new Uint8Array([15, 255, 16, 0, 127]); - const result = toHexString(bytes); - const expected = "0x0fff10007f"; - - expect(result).to.equal(expected); - }); - - it("should handle large Uint8Array", () => { - const bytes = new Uint8Array(1000).fill(255); - const result = toHexString(bytes); - const expected = "0xff".repeat(1000); - - expect(result).to.equal(expected); - }); + for (const { input, output } of testCases) { + it(`should convert Uint8Array ${input} to hex string ${output}`, () => { + expect(toHexString(input)).to.be.equal(output); + }); + } }); From 2a2b7baf3f39a7197a487e29dc5f9ede973a053f Mon Sep 17 00:00:00 2001 From: murrayee Date: Tue, 21 Nov 2023 10:05:23 +0800 Subject: [PATCH 5/7] fix: fix lint warning --- packages/utils/test/unit/bytes.test.ts | 119 +++++++++++++------------ 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index 40b892a9a87..2d175c6191f 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -1,29 +1,29 @@ import "../setup.js"; -import { assert, expect } from "chai"; -import { intToBytes, bytesToInt, toHex, fromHex, toHexString } from "../../src/index.js"; +import {assert, expect} from "chai"; +import {intToBytes, bytesToInt, toHex, fromHex, toHexString} from "../../src/index.js"; describe("intToBytes", () => { - const zeroedArray = (length: number): number[] => Array.from({ length }, () => 0); - const testCases: { input: [bigint | number, number]; output: Buffer }[] = [ - { input: [255, 1], output: Buffer.from([255]) }, - { input: [1, 4], output: Buffer.from([1, 0, 0, 0]) }, - { input: [BigInt(255), 1], output: Buffer.from([255]) }, - { input: [65535, 2], output: Buffer.from([255, 255]) }, - { input: [BigInt(65535), 2], output: Buffer.from([255, 255]) }, - { input: [16777215, 3], output: Buffer.from([255, 255, 255]) }, - { input: [BigInt(16777215), 3], output: Buffer.from([255, 255, 255]) }, - { input: [4294967295, 4], output: Buffer.from([255, 255, 255, 255]) }, - { input: [BigInt(4294967295), 4], output: Buffer.from([255, 255, 255, 255]) }, - { input: [65535, 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)]) }, - { input: [BigInt(65535), 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)]) }, - { input: [65535, 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)]) }, - { input: [BigInt(65535), 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)]) }, - { input: [65535, 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)]) }, - { input: [BigInt(65535), 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)]) }, - { input: [65535, 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)]) }, - { input: [BigInt(65535), 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)]) }, + const zeroedArray = (length: number): number[] => Array.from({length}, () => 0); + const testCases: {input: [bigint | number, number]; output: Buffer}[] = [ + {input: [255, 1], output: Buffer.from([255])}, + {input: [1, 4], output: Buffer.from([1, 0, 0, 0])}, + {input: [BigInt(255), 1], output: Buffer.from([255])}, + {input: [65535, 2], output: Buffer.from([255, 255])}, + {input: [BigInt(65535), 2], output: Buffer.from([255, 255])}, + {input: [16777215, 3], output: Buffer.from([255, 255, 255])}, + {input: [BigInt(16777215), 3], output: Buffer.from([255, 255, 255])}, + {input: [4294967295, 4], output: Buffer.from([255, 255, 255, 255])}, + {input: [BigInt(4294967295), 4], output: Buffer.from([255, 255, 255, 255])}, + {input: [65535, 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)])}, + {input: [BigInt(65535), 8], output: Buffer.from([255, 255, ...zeroedArray(8 - 2)])}, + {input: [65535, 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)])}, + {input: [BigInt(65535), 32], output: Buffer.from([255, 255, ...zeroedArray(32 - 2)])}, + {input: [65535, 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)])}, + {input: [BigInt(65535), 48], output: Buffer.from([255, 255, ...zeroedArray(48 - 2)])}, + {input: [65535, 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)])}, + {input: [BigInt(65535), 96], output: Buffer.from([255, 255, ...zeroedArray(96 - 2)])}, ]; - for (const { input, output } of testCases) { + for (const {input, output} of testCases) { const type = typeof input; const length = input[1]; it(`should correctly serialize ${type} to bytes length ${length}`, () => { @@ -33,15 +33,15 @@ describe("intToBytes", () => { }); describe("bytesToInt", () => { - const testCases: { input: Buffer; output: number }[] = [ - { input: Buffer.from([3]), output: 3 }, - { input: Buffer.from([20, 0]), output: 20 }, - { input: Buffer.from([3, 20]), output: 5123 }, - { input: Buffer.from([255, 255]), output: 65535 }, - { input: Buffer.from([255, 255, 255]), output: 16777215 }, - { input: Buffer.from([255, 255, 255, 255]), output: 4294967295 }, + const testCases: {input: Buffer; output: number}[] = [ + {input: Buffer.from([3]), output: 3}, + {input: Buffer.from([20, 0]), output: 20}, + {input: Buffer.from([3, 20]), output: 5123}, + {input: Buffer.from([255, 255]), output: 65535}, + {input: Buffer.from([255, 255, 255]), output: 16777215}, + {input: Buffer.from([255, 255, 255, 255]), output: 4294967295}, ]; - for (const { input, output } of testCases) { + for (const {input, output} of testCases) { it(`should produce ${output}`, () => { expect(bytesToInt(input)).to.be.equal(output); }); @@ -49,29 +49,35 @@ describe("bytesToInt", () => { }); describe("toHex", () => { - const testCases: { input: Buffer | Uint8Array | string; output: string }[] = [ - { input: Buffer.from("Hello, World!", "utf-8"), output: '0x48656c6c6f2c20576f726c6421' }, - { input: new Uint8Array([72, 101, 108, 108, 111]), output: '0x48656c6c6f' }, - { input: Buffer.from([72, 101, 108, 108, 111]), output: '0x48656c6c6f' }, - { input: Buffer.from([]), output: '0x' }, - { input: 'invalidInput', output: '0x' }, - ] - for (const { input, output } of testCases) { - it(`should convert ${input} to hex string ${output}`, () => { + const testCases: {input: Buffer | Uint8Array | string; output: string}[] = [ + {input: Buffer.from("Hello, World!", "utf-8"), output: "0x48656c6c6f2c20576f726c6421"}, + {input: new Uint8Array([72, 101, 108, 108, 111]), output: "0x48656c6c6f"}, + {input: Buffer.from([72, 101, 108, 108, 111]), output: "0x48656c6c6f"}, + {input: Buffer.from([]), output: "0x"}, + {input: "invalidInput", output: "0x"}, + ]; + for (const {input, output} of testCases) { + it(`should convert Uint8Array to hex string ${output}`, () => { expect(toHex(input)).to.be.equal(output); }); } }); describe("fromHex", () => { - const testCases: { input: string; output: Buffer | Uint8Array }[] = [ - { input: '0x48656c6c6f2c20576f726c6421', output: new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]) }, - { input: '48656c6c6f2c20576f726c6421', output: new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]) }, - { input: '0x', output: new Uint8Array([]) }, - ] + const testCases: {input: string; output: Buffer | Uint8Array}[] = [ + { + input: "0x48656c6c6f2c20576f726c6421", + output: new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]), + }, + { + input: "48656c6c6f2c20576f726c6421", + output: new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]), + }, + {input: "0x", output: new Uint8Array([])}, + ]; - for (const { input, output } of testCases) { - it(`should convert hex string ${input} to Uint8Array ${output}`, () => { + for (const {input, output} of testCases) { + it(`should convert hex string ${input} to Uint8Array`, () => { expect(fromHex(input)).to.be.equal(output); }); } @@ -82,18 +88,17 @@ describe("fromHex", () => { }); describe("toHexString", () => { + const testCases: {input: Uint8Array; output: string}[] = [ + {input: new Uint8Array([1, 2, 3]), output: "0x010203"}, + {input: new Uint8Array([72, 101, 108, 108, 111]), output: "0x48656c6c6f"}, + {input: new Uint8Array([]), output: "0x"}, + {input: new Uint8Array([0, 0, 0, 0]), output: "0x00000000"}, + {input: new Uint8Array([15, 255, 16, 0, 127]), output: "0x0fff10007f"}, + {input: new Uint8Array(1000).fill(255), output: "0xff".repeat(1000)}, + ]; - const testCases: { input: Uint8Array; output: string }[] = [ - { input: new Uint8Array([1, 2, 3]), output: '0x010203' }, - { input: new Uint8Array([72, 101, 108, 108, 111]), output: '0x48656c6c6f' }, - { input: new Uint8Array([]), output: '0x' }, - { input: new Uint8Array([0, 0, 0, 0]), output: '0x00000000' }, - { input: new Uint8Array([15, 255, 16, 0, 127]), output: '0x0fff10007f' }, - { input: new Uint8Array(1000).fill(255), output: "0xff".repeat(1000) }, - ] - - for (const { input, output } of testCases) { - it(`should convert Uint8Array ${input} to hex string ${output}`, () => { + for (const {input, output} of testCases) { + it(`should convert Uint8Array to hex string ${output}`, () => { expect(toHexString(input)).to.be.equal(output); }); } From ebdc5a8ec33c4ecee420666dcbe3aeb188753036 Mon Sep 17 00:00:00 2001 From: murrayee Date: Wed, 22 Nov 2023 22:33:16 +0800 Subject: [PATCH 6/7] fix: udpate to.be => to.deep --- packages/utils/test/unit/bytes.test.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index 2d175c6191f..475d9ef74b3 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -54,7 +54,6 @@ describe("toHex", () => { {input: new Uint8Array([72, 101, 108, 108, 111]), output: "0x48656c6c6f"}, {input: Buffer.from([72, 101, 108, 108, 111]), output: "0x48656c6c6f"}, {input: Buffer.from([]), output: "0x"}, - {input: "invalidInput", output: "0x"}, ]; for (const {input, output} of testCases) { it(`should convert Uint8Array to hex string ${output}`, () => { @@ -78,13 +77,9 @@ describe("fromHex", () => { for (const {input, output} of testCases) { it(`should convert hex string ${input} to Uint8Array`, () => { - expect(fromHex(input)).to.be.equal(output); + expect(fromHex(input)).to.deep.equal(output); }); } - it("should throw an error for invalid hex string", () => { - const hexString = "invalidHex"; - expect(() => fromHex(hexString)).to.throw(); - }); }); describe("toHexString", () => { @@ -94,11 +89,11 @@ describe("toHexString", () => { {input: new Uint8Array([]), output: "0x"}, {input: new Uint8Array([0, 0, 0, 0]), output: "0x00000000"}, {input: new Uint8Array([15, 255, 16, 0, 127]), output: "0x0fff10007f"}, - {input: new Uint8Array(1000).fill(255), output: "0xff".repeat(1000)}, + {input: new Uint8Array(1000).fill(255), output: "0x" + "ff".repeat(1000)}, ]; for (const {input, output} of testCases) { - it(`should convert Uint8Array to hex string ${output}`, () => { + it(`should convert Uint8Array to hex string ${output.length}`, () => { expect(toHexString(input)).to.be.equal(output); }); } From 72c2c3cc54f0fbd847d034fcd3ef431de75934b9 Mon Sep 17 00:00:00 2001 From: murrayee Date: Thu, 23 Nov 2023 08:30:34 +0800 Subject: [PATCH 7/7] chore: adjust tests output message --- packages/utils/test/unit/bytes.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index 475d9ef74b3..f47e4c7ac3e 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -76,7 +76,7 @@ describe("fromHex", () => { ]; for (const {input, output} of testCases) { - it(`should convert hex string ${input} to Uint8Array`, () => { + it(`should convert hex string ${input} to Uint8Array`, () => { expect(fromHex(input)).to.deep.equal(output); }); } @@ -89,11 +89,11 @@ describe("toHexString", () => { {input: new Uint8Array([]), output: "0x"}, {input: new Uint8Array([0, 0, 0, 0]), output: "0x00000000"}, {input: new Uint8Array([15, 255, 16, 0, 127]), output: "0x0fff10007f"}, - {input: new Uint8Array(1000).fill(255), output: "0x" + "ff".repeat(1000)}, + {input: new Uint8Array(5).fill(255), output: "0x" + "ff".repeat(5)}, ]; for (const {input, output} of testCases) { - it(`should convert Uint8Array to hex string ${output.length}`, () => { + it(`should convert Uint8Array to hex string ${output}`, () => { expect(toHexString(input)).to.be.equal(output); }); }