Skip to content

Commit 7413ac8

Browse files
committed
export and test parseParameterValue
1 parent 29cef28 commit 7413ac8

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

0 commit comments

Comments
 (0)