Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pLabarta committed Jul 14, 2023
1 parent 5afdd88 commit ac125cd
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions scripts/cc-cli/src/test/parsing.test.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import {
parseAddress,
parseAmount,
parseAddressInternal,
parseAmountInternal,
parseBoolean,
parseChoice,
parseHexString,
parseInteger,
parsePercentAsPerbill,
parseChoiceInternal,
parseHexStringInternal,
parseIntegerInternal,
parsePercentAsPerbillInternal,
} from "../utils/parsing";

describe("parseAddress", () => {
test("with valid argument returns the same address", () => {
const substrateAddress = "5EACfEfYjfg5ZHpzp9uoMCR68UNGBUAu5AYjvZdM5aLYaojx";
const parsedAddress = parseAddress(substrateAddress);
const parsedAddress = parseAddressInternal(substrateAddress);
expect(parsedAddress).toBe(substrateAddress);
});

test("with invalid argument throws an error", () => {
// address is too short
const substrateAddress = "5EACfEfYjfg5ZHpzp9uoMCZdM5aLYaojx";
const parsedInvalid = () => parseAddress(substrateAddress);
const parsedInvalid = () => parseAddressInternal(substrateAddress);
expect(parsedInvalid).toThrowError(Error);
});
});

describe("parseAmount", () => {
test("with valid integer argument returns the same amount * 10^^18", () => {
const amount = "1";
const parsedAmount = parseAmount(amount);
const parsedAmount = parseAmountInternal(amount);
expect(parsedAmount.toString()).toBe("1000000000000000000");
});

test("with valid float argument returns the same amount * 10^^18", () => {
const amount = "0.4";
const parsedAmount = parseAmount(amount);
const parsedAmount = parseAmountInternal(amount);
expect(parsedAmount.toString()).toBe("400000000000000000");
});

test("with negative argument throws an error", () => {
const amount = "-1";
const parsedInvalid = () => parseAmount(amount);
const parsedInvalid = () => parseAmountInternal(amount);
expect(parsedInvalid).toThrowError(Error);
});

test("with argument containing decimal comma throws an error", () => {
const amount = "100,1";
const parsedInvalid = () => parseAmount(amount);
const parsedInvalid = () => parseAmountInternal(amount);
expect(parsedInvalid).toThrowError(Error);
});

test("with 0 as argument throws an error", () => {
const amount = "0";
const parsedInvalid = () => parseAmount(amount);
const parsedInvalid = () => parseAmountInternal(amount);
expect(parsedInvalid).toThrowError(Error);
});

test("with string argument throws an error", () => {
const amount = "abcdef";
const parsedInvalid = () => parseAmount(amount);
const parsedInvalid = () => parseAmountInternal(amount);
expect(parsedInvalid).toThrowError(Error);
});
});
Expand All @@ -65,21 +65,21 @@ describe("parseChoice", () => {
test("with valid argument returns the same choice", () => {
const choice = "Staked";
const choices = ["Staked", "Stash", "Controller"];
const parsedChoice = parseChoice(choice, choices);
const parsedChoice = parseChoiceInternal(choice, choices);
expect(parsedChoice).toBe(choice);
});

test("with valid mixed case argument returns choice in canonical format", () => {
const choice = "stAKed";
const choices = ["Staked", "Stash", "Controller"];
const parsedChoice = parseChoice(choice, choices);
const parsedChoice = parseChoiceInternal(choice, choices);
expect(parsedChoice).toBe("Staked");
});

test("with invalid argument throws an error", () => {
const choice = "Bonded";
const choices = ["Staked", "Stash", "Controller"];
const parsedInvalid = () => parseChoice(choice, choices);
const parsedInvalid = () => parseChoiceInternal(choice, choices);
expect(parsedInvalid).toThrowError(Error);
});
});
Expand All @@ -100,97 +100,97 @@ describe("parseBoolean", () => {

describe("parseInteger", () => {
test("with valid argument, 0, returns the same integer", () => {
const parsedInteger = parseInteger("0");
const parsedInteger = parseIntegerInternal("0");
expect(parsedInteger).toBe(0);
});

test("with valid argument, > 0, returns the same integer", () => {
const parsedInteger = parseInteger("1");
const parsedInteger = parseIntegerInternal("1");
expect(parsedInteger).toBe(1);
});

test("with valid argument, < 0, returns the same integer", () => {
const parsedInteger = parseInteger("-1");
const parsedInteger = parseIntegerInternal("-1");
expect(parsedInteger).toBe(-1);
});

test("with float argument throws an error", () => {
const parsedInvalid = () => parseInteger("0.1");
const parsedInvalid = () => parseIntegerInternal("0.1");
expect(parsedInvalid).toThrowError(Error);
});

test("with string argument throws an error", () => {
const integer = "abcdef";
const parsedInvalid = () => parseInteger(integer);
const parsedInvalid = () => parseIntegerInternal(integer);
expect(parsedInvalid).toThrowError(Error);
});
});

describe("parseHexString", () => {
test("with valid argument, lower case, returns the same hex string", () => {
const hexString = "0x1234567890abcdef";
const parsedHexString = parseHexString(hexString);
const parsedHexString = parseHexStringInternal(hexString);
expect(parsedHexString).toBe(hexString);
});

test("with valid argument, mixed case, returns the same hex string", () => {
const hexString = "0x1234567890AbCdeF";
const parsedHexString = parseHexString(hexString);
const parsedHexString = parseHexStringInternal(hexString);
expect(parsedHexString).toBe(hexString);
});

test("with invalid argument, missing 0x prefix, throws an error", () => {
const hexString = "1234567890abcdef";
const parsedInvalid = () => parseHexString(hexString);
const parsedInvalid = () => parseHexStringInternal(hexString);
expect(parsedInvalid).toThrowError(Error);
});

test("with invalid argument, contains invalid hex digits, throws an error", () => {
const parsedInvalid = () => parseHexString("0x123x==xZZZ");
const parsedInvalid = () => parseHexStringInternal("0x123x==xZZZ");
expect(parsedInvalid).toThrowError(Error);
});
});

describe("parsePercentAsPerbill", () => {
test("with valid argument, lower boundary, returns correct perbill", () => {
const parsedPerbill = parsePercentAsPerbill("0");
const parsedPerbill = parsePercentAsPerbillInternal("0");
expect(parsedPerbill).toBe(0);
});

test("with valid argument, upper boundary, returns correct perbill", () => {
const parsedPerbill = parsePercentAsPerbill("100");
const parsedPerbill = parsePercentAsPerbillInternal("100");
expect(parsedPerbill).toBe(1_000_000_000);
});

test("with valid argument, float value inside boundaries, returns correct perbill", () => {
const parsedPerbill = parsePercentAsPerbill("50.5");
const parsedPerbill = parsePercentAsPerbillInternal("50.5");
expect(parsedPerbill).toBe(505_000_000);
});

test("with invalid argument, float < lower boundary, throws an error", () => {
const parsedInvalid = () => parsePercentAsPerbill("-0.1");
const parsedInvalid = () => parsePercentAsPerbillInternal("-0.1");
expect(parsedInvalid).toThrowError(Error);
});

test("with invalid argument, int < lower boundary, throws an error", () => {
const parsedInvalid = () => parsePercentAsPerbill("-1");
const parsedInvalid = () => parsePercentAsPerbillInternal("-1");
expect(parsedInvalid).toThrowError(Error);
});

test("with invalid argument, float > upper boundary, throws an error", () => {
const perbill = "100.1";
const parsedInvalid = () => parsePercentAsPerbill(perbill);
const parsedInvalid = () => parsePercentAsPerbillInternal(perbill);
expect(parsedInvalid).toThrowError(Error);
});

test("with invalid argument, int > upper boundary, throws an error", () => {
const parsedInvalid = () => parsePercentAsPerbill("101");
const parsedInvalid = () => parsePercentAsPerbillInternal("101");
expect(parsedInvalid).toThrowError(Error);
});

test("with invalid argument, a string, throws an error", () => {
const perbill = "abcdef";
const parsedInvalid = () => parsePercentAsPerbill(perbill);
const parsedInvalid = () => parsePercentAsPerbillInternal(perbill);
expect(parsedInvalid).toThrowError(Error);
});
});

0 comments on commit ac125cd

Please sign in to comment.