This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add tests for json-rpc-data and address to describe expected beh…
…aviour #1594
- Loading branch information
1 parent
386771d
commit 0ec3f30
Showing
2 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
import assert from "assert"; | ||
import ethereumAddress from "../"; | ||
import {Address} from "../"; | ||
|
||
describe("@ganache/ethereum-address", () => { | ||
it("needs tests"); | ||
it("should pad an address to 20 bytes", () => { | ||
const address = new Address("0x1"); | ||
const stringifiedAddress = address.toString(); | ||
|
||
assert.equal(stringifiedAddress, "0x0000000000000000000000000000000000000001"); | ||
}); | ||
|
||
it("should pad an address to the specified length", () => { | ||
const address = new Address("0x1"); | ||
const stringifiedAddress = address.toString(1); | ||
|
||
assert.equal(stringifiedAddress, "0x01"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import assert from "assert"; | ||
import {Data} from ".."; | ||
|
||
describe("json-rpc-data", () => { | ||
describe("Data", () => { | ||
it("should stringify without specifying byteLength", () => { | ||
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"}, | ||
{input: "0x0123456789abcdef", expected: "0x0123456789abcdef"}, | ||
{input: Buffer.from([0x01]), expected: "0x01"} | ||
]; | ||
|
||
cases.forEach(c => { | ||
const d = new Data(c.input); | ||
const stringified = d.toString(); | ||
|
||
assert.equal(stringified, c.expected); | ||
}); | ||
}); | ||
|
||
it("should stringify to fixed 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"} | ||
]; | ||
|
||
cases.forEach(c => { | ||
const d = new Data(c.input); | ||
const stringified = d.toString(c.length); | ||
|
||
assert.equal(stringified, c.expected); | ||
}); | ||
}); | ||
|
||
it("should stringify and truncate to a shorter byteLength", () => { | ||
const cases = [ | ||
{input: "0x0123", length: 1, expected: "0x01"}, | ||
{input: "0x0123456789abcdef", length: 2, expected: "0x0123"}, | ||
{input: Buffer.from([0x01, 0x23]), length: 1, expected: "0x01"} | ||
]; | ||
|
||
cases.forEach(c => { | ||
const d = new Data(c.input); | ||
const stringified = d.toString(c.length); | ||
|
||
assert.equal(stringified, c.expected); | ||
}); | ||
}); | ||
|
||
it("should stringify to the byteLength specified in the constructor", () => { | ||
const cases = [ | ||
{input: "0x01", constructorLength: 1, expected: "0x01"}, | ||
{input: "0x01", constructorLength: 10, expected: "0x00000000000000000001"}, | ||
{input: "0x0123456789abcdef", constructorLength: 2, expected: "0x0123"}, | ||
{input: Buffer.from([0x01, 0x23]), constructorLength: 1, expected: "0x01"}, | ||
{input: Buffer.from([0x01]), constructorLength: 2, expected: "0x0001"} | ||
]; | ||
|
||
cases.forEach(c => { | ||
const d = new Data(c.input, c.constructorLength); | ||
const stringified = d.toString(); | ||
|
||
assert.equal(stringified, c.expected); | ||
}); | ||
}); | ||
|
||
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); | ||
|
||
assert.equal(stringified, c.expected); | ||
}); | ||
}); | ||
|
||
it("should fail with invalid byte lengths", () => { | ||
const invalidByteLengths = [ -1, 0, "1", {}, [], null, NaN ]; // undefined is a valid value | ||
|
||
const d = new Data("0x01"); | ||
|
||
invalidByteLengths.forEach(byteLength => { | ||
assert.throws(() => d.toString(<any>byteLength), { message: "byteLength must be a number greater than 0" }, `Invalid bytelength provided: <${typeof byteLength}>: ${byteLength}`); | ||
}); | ||
}); | ||
}); | ||
}); |