Increase GasPrice.fromString test coverage and check characters early

This commit is contained in:
Simon Warta 2021-05-13 23:05:14 +02:00
parent 9f242d3ce5
commit f17e8b77ad
4 changed files with 66 additions and 14 deletions

View File

@ -12,12 +12,38 @@ describe("GasPrice", () => {
});
});
it("can be constructed from a config string", () => {
const inputs = ["3.14", "3", "0.14"];
inputs.forEach((input) => {
const gasPrice = GasPrice.fromString(`${input}utest`);
expect(gasPrice.amount.toString()).toEqual(input);
expect(gasPrice.denom).toEqual("utest");
describe("fromString", () => {
it("works", () => {
const inputs = ["3.14", "3", "0.14"];
inputs.forEach((input) => {
const gasPrice = GasPrice.fromString(`${input}utest`);
expect(gasPrice.amount.toString()).toEqual(input);
expect(gasPrice.denom).toEqual("utest");
});
});
it("errors for invalid gas price", () => {
// Checks basic format <amount><denom>
expect(() => GasPrice.fromString("")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("utkn")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("@utkn")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("234")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("-234tkn")).toThrowError(/Invalid gas price string/i);
// Checks details of <denom>
expect(() => GasPrice.fromString("234t")).toThrowError(
/denomination must be between 3 and 127 characters/i,
);
expect(() => GasPrice.fromString("234tt")).toThrowError(
/denomination must be between 3 and 127 characters/i,
);
expect(() =>
GasPrice.fromString(
"234tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt",
),
).toThrowError(/denomination must be between 3 and 127 characters/i);
// Checks details of <amount>
expect(() => GasPrice.fromString("3.utkn")).toThrowError(/Fractional part missing/i);
expect(() => GasPrice.fromString("..utkn")).toThrowError(/More than one separator found/i);
});
});
});

View File

@ -13,7 +13,7 @@ export class GasPrice {
}
public static fromString(gasPrice: string): GasPrice {
const matchResult = gasPrice.match(/^(?<amount>.+?)(?<denom>[a-z]+)$/);
const matchResult = gasPrice.match(/^(?<amount>[0-9.]+?)(?<denom>[a-z]+)$/);
if (!matchResult) {
throw new Error("Invalid gas price string");
}

View File

@ -12,12 +12,38 @@ describe("GasPrice", () => {
});
});
it("can be constructed from a config string", () => {
const inputs = ["3.14", "3", "0.14"];
inputs.forEach((input) => {
const gasPrice = GasPrice.fromString(`${input}utest`);
expect(gasPrice.amount.toString()).toEqual(input);
expect(gasPrice.denom).toEqual("utest");
describe("fromString", () => {
it("works", () => {
const inputs = ["3.14", "3", "0.14"];
inputs.forEach((input) => {
const gasPrice = GasPrice.fromString(`${input}utest`);
expect(gasPrice.amount.toString()).toEqual(input);
expect(gasPrice.denom).toEqual("utest");
});
});
it("errors for invalid gas price", () => {
// Checks basic format <amount><denom>
expect(() => GasPrice.fromString("")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("utkn")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("@utkn")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("234")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("-234tkn")).toThrowError(/Invalid gas price string/i);
// Checks details of <denom>
expect(() => GasPrice.fromString("234t")).toThrowError(
/denomination must be between 3 and 127 characters/i,
);
expect(() => GasPrice.fromString("234tt")).toThrowError(
/denomination must be between 3 and 127 characters/i,
);
expect(() =>
GasPrice.fromString(
"234tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt",
),
).toThrowError(/denomination must be between 3 and 127 characters/i);
// Checks details of <amount>
expect(() => GasPrice.fromString("3.utkn")).toThrowError(/Fractional part missing/i);
expect(() => GasPrice.fromString("..utkn")).toThrowError(/More than one separator found/i);
});
});
});

View File

@ -20,7 +20,7 @@ export class GasPrice {
}
public static fromString(gasPrice: string): GasPrice {
const matchResult = gasPrice.match(/^(?<amount>.+?)(?<denom>[a-z]+)$/);
const matchResult = gasPrice.match(/^(?<amount>[0-9.]+?)(?<denom>[a-z]+)$/);
if (!matchResult) {
throw new Error("Invalid gas price string");
}