Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Tidy up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsmale90 committed Mar 29, 2022
1 parent cd833af commit 4a2472a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 53 deletions.
30 changes: 16 additions & 14 deletions src/chains/ethereum/address/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});
62 changes: 23 additions & 39 deletions src/packages/utils/tests/json-rpc-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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

Expand Down

0 comments on commit 4a2472a

Please sign in to comment.