From 4a2472a0f1f28a3faaac509e0c0056dc0be30a5c Mon Sep 17 00:00:00 2001 From: Jeff Smale <6363749+jeffsmale90@users.noreply.github.com> Date: Tue, 29 Mar 2022 15:09:27 +1300 Subject: [PATCH] Tidy up tests --- .../ethereum/address/tests/index.test.ts | 30 ++++----- .../utils/tests/json-rpc-data.test.ts | 62 +++++++------------ 2 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/chains/ethereum/address/tests/index.test.ts b/src/chains/ethereum/address/tests/index.test.ts index 5c9a88d31f..0251283e3a 100644 --- a/src/chains/ethereum/address/tests/index.test.ts +++ b/src/chains/ethereum/address/tests/index.test.ts @@ -2,24 +2,26 @@ import assert from "assert"; import {Address} from "../"; describe("@ganache/ethereum-address", () => { - it("should pad an address to 20 bytes", () => { - const address = new Address("0x1"); - const stringifiedAddress = address.toString(); + describe("toString()", () => { + it("should pad an address to 20 bytes", () => { + const address = new Address("0x1"); + const stringifiedAddress = address.toString(); - assert.equal(stringifiedAddress, "0x0000000000000000000000000000000000000001"); - }); + assert.equal(stringifiedAddress, "0x0000000000000000000000000000000000000001"); + }); - it("should pad an address to the specified length", () => { - const address = new Address("0x1"); - const stringifiedAddress = address.toString(1); + it("should truncate an address to the specified length", () => { + const address = new Address("0x1"); + const stringifiedAddress = address.toString(1); - assert.equal(stringifiedAddress, "0x01"); - }); + assert.equal(stringifiedAddress, "0x01"); + }); - it("should create an address from a 20 byte address string", () => { - const address = new Address("0x2104859394604359378433865360947116707876"); - const stringifiedAddress = address.toString(); + it("should stringify a 20 byte address string", () => { + const address = new Address("0x2104859394604359378433865360947116707876"); + const stringifiedAddress = address.toString(); - assert.equal(stringifiedAddress, "0x2104859394604359378433865360947116707876"); + assert.equal(stringifiedAddress, "0x2104859394604359378433865360947116707876"); + }); }); }); diff --git a/src/packages/utils/tests/json-rpc-data.test.ts b/src/packages/utils/tests/json-rpc-data.test.ts index 0c4a73915d..b9c98b885b 100644 --- a/src/packages/utils/tests/json-rpc-data.test.ts +++ b/src/packages/utils/tests/json-rpc-data.test.ts @@ -2,7 +2,8 @@ import assert from "assert"; import {Data} from ".."; describe("json-rpc-data", () => { - it("should stringify without specifying byteLength", () => { + describe("toString()", () => { + it("should stringify without arguments", () => { const cases = [ {input: "0x1", expected: "0x1"}, // I'm not sure whether this is the correct behaviour - should it return a single byte length string - ie 0x01? {input: "0x01", expected: "0x01"}, @@ -18,79 +19,62 @@ describe("json-rpc-data", () => { }); }); - it("should stringify to fixed byteLength", () => { + it("should stringify and truncate to a shorter bytelength", () => { const cases = [ - {input: "0x1", length: 1, expected: "0x01"}, - {input: "0x01", length: 2, expected: "0x0001"}, - {input: "0x01", length: 10, expected: "0x00000000000000000001"}, - {input: Buffer.from([0x01]), length: 2, expected: "0x0001"} + {input: "0x0123456789abcdef", expected: "0x0123", byteLength: 2}, + {input: Buffer.from([0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef]), expected: "0x0123", byteLength: 2} ]; cases.forEach(c => { const d = new Data(c.input); - const stringified = d.toString(c.length); + const stringified = d.toString(c.byteLength); assert.equal(stringified, c.expected); }); }); - it("should stringify and truncate to a shorter byteLength", () => { + it("should stringify and pad to a longer bytelength", () => { const cases = [ - {input: "0x0123", length: 1, expected: "0x01"}, - {input: "0x0123456789abcdef", length: 2, expected: "0x0123"}, - {input: Buffer.from([0x01, 0x23]), length: 1, expected: "0x01"} + {input: "0x0123", expected: "0x000123", byteLength: 3}, + {input: Buffer.from([0x01,0x23]), expected: "0x000123", byteLength: 3} ]; cases.forEach(c => { const d = new Data(c.input); - const stringified = d.toString(c.length); + const stringified = d.toString(c.byteLength); assert.equal(stringified, c.expected); }); }); - it("should stringify to the byteLength specified in the constructor", () => { + it("should stringify a long bytelength", () => { const cases = [ - {input: "0x01", constructorLength: 1, expected: "0x01"}, - {input: "0x01", constructorLength: 10, expected: "0x00000000000000000001"}, - {input: "0x0123456789abcdef", constructorLength: 2, expected: "0x0123"}, - {input: "0x0123456789abcdef", constructorLength: 8, expected: "0x0123456789abcdef"}, - {input: Buffer.from([0x01, 0x23]), constructorLength: 1, expected: "0x01"}, - {input: Buffer.from([0x01]), constructorLength: 2, expected: "0x0001"} + {input: "0x0123456789abcdef0123456789abcdef01234567", expected: "0x0123456789abcdef0123456789abcdef01234567", byteLength: 20}, + {input: Buffer.from([0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67]), expected: "0x0123456789abcdef0123456789abcdef01234567", byteLength: 20} ]; cases.forEach(c => { - const d = new Data(c.input, c.constructorLength); - const stringified = d.toString(); - - assert.equal(stringified, c.expected, `Expected "${stringified}" to be "${c.expected}", for input "${c.input}" and constructorLength ${c.constructorLength}`); - }); - }); - - it("should stringify to the byteLength specified in toString, over the byteLength specified in the constructor", () => { - const cases = [ - {input: "0x01", constructorLength: 2, length: 1, expected: "0x01"}, - {input: "0x01", constructorLength: 1, length: 10, expected: "0x00000000000000000001"}, - {input: "0x0123456789abcdef", constructorLength: 1, length: 2, expected: "0x0123"}, - {input: Buffer.from([0x01, 0x23]), constructorLength: 2, length: 1, expected: "0x01"}, - {input: Buffer.from([0x01]), constructorLength: 1, length: 2, expected: "0x0001"} - ]; - - cases.forEach(c => { - const d = new Data(c.input, c.constructorLength); - const stringified = d.toString(c.length); + const d = new Data(c.input); + const stringified = d.toString(c.byteLength); assert.equal(stringified, c.expected); }); }); it("should stringify an empty value", () => { - const d = new Data("0x", 0); + const d = new Data("0x"); const stringified = d.toString(0); assert.equal(stringified, "0x"); }); + it("should respect the bytelength passed to toString(), over that passed to the ctor", () => { + const d = new Data("0x0123", 2); + const stringified = d.toString(1); + + assert.equal(stringified, "0x01"); + }); + it("should fail with invalid byte lengths", () => { const invalidByteLengths = [ -1, "1", {}, [], null, NaN ]; // undefined is a valid value