Merge pull request #232 from CosmWasm/test-and-harden-coin-helper

Harden and test coin/coins helpers
This commit is contained in:
Simon Warta 2020-06-23 12:57:01 +02:00 committed by GitHub
commit b9cce63514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -0,0 +1,35 @@
import { coin, coins } from "./coins";
describe("coins", () => {
describe("coin", () => {
it("works for basic values", () => {
expect(coin(123, "utoken")).toEqual({ amount: "123", denom: "utoken" });
expect(coin(123.0, "utoken")).toEqual({ amount: "123", denom: "utoken" });
expect(coin(Number.MAX_SAFE_INTEGER, "utoken")).toEqual({
amount: "9007199254740991",
denom: "utoken",
});
expect(coin(+0, "utoken")).toEqual({ amount: "0", denom: "utoken" });
expect(coin(-0, "utoken")).toEqual({ amount: "0", denom: "utoken" });
});
it("throws for non-safe-integer values", () => {
expect(() => coin(1.23, "utoken")).toThrow();
expect(() => coin(NaN, "utoken")).toThrow();
expect(() => coin(Number.POSITIVE_INFINITY, "utoken")).toThrow();
expect(() => coin(Number.MAX_SAFE_INTEGER + 1, "utoken")).toThrow();
});
it("throws for negative values", () => {
expect(() => coin(-1, "utoken")).toThrow();
expect(() => coin(Number.MIN_SAFE_INTEGER, "utoken")).toThrow();
expect(() => coin(Number.NEGATIVE_INFINITY, "utoken")).toThrow();
});
});
describe("coins", () => {
it("returns one element array of coin", () => {
expect(coins(123, "utoken")).toEqual([{ amount: "123", denom: "utoken" }]);
});
});
});

View File

@ -1,3 +1,5 @@
import { Uint53 } from "@cosmjs/math";
export interface Coin {
readonly denom: string;
readonly amount: string;
@ -5,7 +7,7 @@ export interface Coin {
/** Creates a coin */
export function coin(amount: number, denom: string): Coin {
return { amount: amount.toString(), denom: denom };
return { amount: new Uint53(amount).toString(), denom: denom };
}
/** Creates a list of coins with one element */