Add unified address `toString` (#358)

Test Plan:
Unit tests added. Run `yarn travis`.

wchargin-branch: address-tostring
This commit is contained in:
William Chargin 2018-06-07 09:20:55 -07:00 committed by GitHub
parent 32aba15b01
commit 0bf1ef8af2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View File

@ -191,8 +191,8 @@ export function makeAddressModule(options: Options): AddressModule<string> {
}
function toString(address: Address): string {
const _ = address;
throw new Error("toString");
const parts = toParts(address);
return `${name}${stringify(parts)}`;
}
function append(address: Address, ...parts: string[]): Address {

View File

@ -216,5 +216,50 @@ describe("core/address", () => {
check("on an input with repeated components", ["jar", "jar"]);
});
});
describe("toString", () => {
const {FooAddress, BarAddress} = makeModules();
const partsToString = (parts) =>
FooAddress.toString(FooAddress.fromParts(parts));
// We use this next test as a proxy for fully correct validation,
// in conjunction with tests on `assertValid`.
it("validates address kind", () => {
const bar = BarAddress.fromParts(["hello"]);
expect(() => {
FooAddress.toString(bar);
}).toThrow("expected FooAddress, got BarAddress:");
});
it("includes the address kind", () => {
expect(partsToString(["hello", "world"])).toContain("FooAddress");
});
it("includes the address parts", () => {
expect(partsToString(["hello", "world"])).toContain("hello");
expect(partsToString(["hello", "world"])).toContain("world");
});
it("does not include any NUL characters", () => {
expect(partsToString(["hello", "world"])).not.toContain("\0");
});
it("works on the empty address", () => {
expect(partsToString([])).toEqual(expect.anything());
});
it("differentiates empty components", () => {
// Each of the following should have a different `toString` form.
const inputs = [
partsToString([]),
partsToString([""]),
partsToString(["", ""]),
partsToString(["hello", "world"]),
partsToString(["", "hello", "world"]),
partsToString(["", "hello", "", "world"]),
partsToString(["", "hello", "", "", "world"]),
partsToString(["", "hello", "", "", "world", ""]),
partsToString(["", "hello", "", "", "world", "", ""]),
partsToString(["", "hello", "", "", "world", "", "", ""]),
];
expect(Array.from(new Set(inputs)).sort()).toHaveLength(inputs.length);
});
});
});
});