Skip to content

Commit 73ead10

Browse files
committed
export and test parseParameterValue
1 parent 590b1b1 commit 73ead10

File tree

4 files changed

+74
-110
lines changed

4 files changed

+74
-110
lines changed

v-next/core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ export async function createBaseHardhatRuntimeEnvironment(
3131
);
3232
}
3333

34+
export { parseArgumentValue } from "./internal/arguments.js";
3435
export { resolvePluginList } from "./internal/plugins/resolve-plugin-list.js";
3536
export { buildGlobalOptionDefinitions } from "./internal/global-options.js";

v-next/core/src/internal/arguments.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ const argumentTypeValidators: Record<
5757

5858
/**
5959
* Parses an argument value from a string to the corresponding type.
60+
*
61+
* @param value - The string value to parse.
62+
* @param type - The type of the argument.
63+
* @param name - The name of the argument.
6064
*/
61-
// TODO: this code is duplicated in v-next/hardhat/src/internal/cli/main.ts
62-
// we should move it to a shared place and add tests
6365
export function parseArgumentValue(
6466
value: string,
6567
type: ArgumentType,

v-next/core/test/internal/arguments.ts

+68
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ArgumentType } from "../../src/config.js";
55
import {
66
isArgumentNameValid,
77
isArgumentValueValid,
8+
parseArgumentValue,
89
} from "../../src/internal/arguments.js";
910

1011
describe("Arguments", () => {
@@ -86,4 +87,71 @@ describe("Arguments", () => {
8687
);
8788
});
8889
});
90+
91+
describe("parseArgumentValue", () => {
92+
it("should parse string arguments", () => {
93+
assert.equal(
94+
parseArgumentValue("foo", ArgumentType.STRING, "name"),
95+
"foo",
96+
);
97+
});
98+
99+
it("should parse file arguments", () => {
100+
assert.equal(
101+
parseArgumentValue("foo.txt", ArgumentType.FILE, "name"),
102+
"foo.txt",
103+
);
104+
});
105+
106+
it("should parse int arguments", () => {
107+
assert.equal(parseArgumentValue("123", ArgumentType.INT, "name"), 123);
108+
});
109+
110+
it("should parse float arguments", () => {
111+
assert.equal(
112+
parseArgumentValue("123.45", ArgumentType.FLOAT, "name"),
113+
123.45,
114+
);
115+
});
116+
117+
it("should parse bigint arguments", () => {
118+
assert.equal(
119+
parseArgumentValue("123", ArgumentType.BIGINT, "name"),
120+
BigInt(123),
121+
);
122+
});
123+
124+
it("should parse boolean arguments", () => {
125+
assert.equal(
126+
parseArgumentValue("true", ArgumentType.BOOLEAN, "name"),
127+
true,
128+
);
129+
});
130+
131+
describe("should throw an error for invalid values", () => {
132+
it("for int arguments", () => {
133+
assert.throws(() => {
134+
parseArgumentValue("foo", ArgumentType.INT, "name");
135+
}, /Invalid value foo for argument name of type INT/);
136+
});
137+
138+
it("for float arguments", () => {
139+
assert.throws(() => {
140+
parseArgumentValue("foo", ArgumentType.FLOAT, "name");
141+
}, /Invalid value foo for argument name of type FLOAT/);
142+
});
143+
144+
it("for bigint arguments", () => {
145+
assert.throws(() => {
146+
parseArgumentValue("foo", ArgumentType.BIGINT, "name");
147+
}, /Invalid value foo for argument name of type BIGINT/);
148+
});
149+
150+
it("for boolean arguments", () => {
151+
assert.throws(() => {
152+
parseArgumentValue("foo", ArgumentType.BOOLEAN, "name");
153+
}, /Invalid value foo for argument name of type BOOLEAN/);
154+
});
155+
});
156+
});
89157
});

v-next/hardhat/src/internal/cli/main.ts

+1-108
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ArgumentValue } from "@ignored/hardhat-vnext-core/types/arguments";
21
import type {
32
GlobalOptions,
43
GlobalOptionDefinition,
@@ -16,6 +15,7 @@ import "tsx"; // NOTE: This is important, it allows us to load .ts files form th
1615

1716
import {
1817
buildGlobalOptionDefinitions,
18+
parseArgumentValue,
1919
resolvePluginList,
2020
} from "@ignored/hardhat-vnext-core";
2121
import { ArgumentType } from "@ignored/hardhat-vnext-core/types/arguments";
@@ -521,110 +521,3 @@ function validateRequiredArguments(
521521
{ argument: missingRequiredArgument.name },
522522
);
523523
}
524-
525-
function parseArgumentValue(
526-
strValue: string,
527-
type: ArgumentType,
528-
argName: string,
529-
): ArgumentValue {
530-
switch (type) {
531-
case ArgumentType.STRING:
532-
return validateAndParseString(argName, strValue);
533-
case ArgumentType.FILE:
534-
return validateAndParseFile(argName, strValue);
535-
case ArgumentType.INT:
536-
return validateAndParseInt(argName, strValue);
537-
case ArgumentType.FLOAT:
538-
return validateAndParseFloat(argName, strValue);
539-
case ArgumentType.BIGINT:
540-
return validateAndParseBigInt(argName, strValue);
541-
case ArgumentType.BOOLEAN:
542-
return validateAndParseBoolean(argName, strValue);
543-
}
544-
}
545-
546-
function validateAndParseInt(argName: string, strValue: string): number {
547-
const decimalPattern = /^\d+(?:[eE]\d+)?$/;
548-
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
549-
550-
if (
551-
strValue.match(decimalPattern) === null &&
552-
strValue.match(hexPattern) === null
553-
) {
554-
throw new HardhatError(
555-
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
556-
{
557-
value: strValue,
558-
name: argName,
559-
type: "int",
560-
},
561-
);
562-
}
563-
564-
return Number(strValue);
565-
}
566-
567-
function validateAndParseString(_argName: string, strValue: string): string {
568-
return strValue;
569-
}
570-
571-
function validateAndParseFile(_argName: string, strValue: string): string {
572-
return strValue;
573-
}
574-
575-
function validateAndParseFloat(argName: string, strValue: string): number {
576-
const decimalPattern = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]\d+)?$/;
577-
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
578-
579-
if (
580-
strValue.match(decimalPattern) === null &&
581-
strValue.match(hexPattern) === null
582-
) {
583-
throw new HardhatError(
584-
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
585-
{
586-
value: strValue,
587-
name: argName,
588-
type: "float",
589-
},
590-
);
591-
}
592-
593-
return Number(strValue);
594-
}
595-
596-
function validateAndParseBigInt(argName: string, strValue: string): bigint {
597-
const decimalPattern = /^\d+(?:n)?$/;
598-
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
599-
600-
if (
601-
strValue.match(decimalPattern) === null &&
602-
strValue.match(hexPattern) === null
603-
) {
604-
throw new HardhatError(
605-
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
606-
{
607-
value: strValue,
608-
name: argName,
609-
type: "bigint",
610-
},
611-
);
612-
}
613-
614-
return BigInt(strValue.replace("n", ""));
615-
}
616-
617-
function validateAndParseBoolean(argName: string, strValue: string): boolean {
618-
if (strValue.toLowerCase() === "true") {
619-
return true;
620-
}
621-
if (strValue.toLowerCase() === "false") {
622-
return false;
623-
}
624-
625-
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
626-
value: strValue,
627-
name: argName,
628-
type: "boolean",
629-
});
630-
}

0 commit comments

Comments
 (0)