Skip to content

Commit 523fe3d

Browse files
committed
export and test parseParameterValue
1 parent 590b1b1 commit 523fe3d

File tree

4 files changed

+93
-110
lines changed

4 files changed

+93
-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

+87
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
33

4+
import { HardhatError } from "@ignored/hardhat-vnext-errors";
5+
import { assertThrowsHardhatError } from "@nomicfoundation/hardhat-test-utils";
6+
47
import { ArgumentType } from "../../src/config.js";
58
import {
69
isArgumentNameValid,
710
isArgumentValueValid,
11+
parseArgumentValue,
812
} from "../../src/internal/arguments.js";
913

1014
describe("Arguments", () => {
@@ -86,4 +90,87 @@ describe("Arguments", () => {
8690
);
8791
});
8892
});
93+
94+
describe("parseArgumentValue", () => {
95+
it("should parse string arguments", () => {
96+
assert.equal(
97+
parseArgumentValue("foo", ArgumentType.STRING, "name"),
98+
"foo",
99+
);
100+
});
101+
102+
it("should parse file arguments", () => {
103+
assert.equal(
104+
parseArgumentValue("foo.txt", ArgumentType.FILE, "name"),
105+
"foo.txt",
106+
);
107+
});
108+
109+
it("should parse int arguments", () => {
110+
assert.equal(parseArgumentValue("123", ArgumentType.INT, "name"), 123);
111+
});
112+
113+
it("should parse float arguments", () => {
114+
assert.equal(
115+
parseArgumentValue("123.45", ArgumentType.FLOAT, "name"),
116+
123.45,
117+
);
118+
});
119+
120+
it("should parse bigint arguments", () => {
121+
assert.equal(
122+
parseArgumentValue("123", ArgumentType.BIGINT, "name"),
123+
BigInt(123),
124+
);
125+
});
126+
127+
it("should parse boolean arguments", () => {
128+
assert.equal(
129+
parseArgumentValue("true", ArgumentType.BOOLEAN, "name"),
130+
true,
131+
);
132+
});
133+
134+
describe("should throw an error for invalid values", () => {
135+
it("for int arguments", () => {
136+
assertThrowsHardhatError(
137+
() => {
138+
parseArgumentValue("foo", ArgumentType.INT, "name");
139+
},
140+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
141+
{ value: "foo", name: "name", type: ArgumentType.INT },
142+
);
143+
});
144+
145+
it("for float arguments", () => {
146+
assertThrowsHardhatError(
147+
() => {
148+
parseArgumentValue("foo", ArgumentType.FLOAT, "name");
149+
},
150+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
151+
{ value: "foo", name: "name", type: ArgumentType.FLOAT },
152+
);
153+
});
154+
155+
it("for bigint arguments", () => {
156+
assertThrowsHardhatError(
157+
() => {
158+
parseArgumentValue("foo", ArgumentType.BIGINT, "name");
159+
},
160+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
161+
{ value: "foo", name: "name", type: ArgumentType.BIGINT },
162+
);
163+
});
164+
165+
it("for boolean arguments", () => {
166+
assertThrowsHardhatError(
167+
() => {
168+
parseArgumentValue("foo", ArgumentType.BOOLEAN, "name");
169+
},
170+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
171+
{ value: "foo", name: "name", type: ArgumentType.BOOLEAN },
172+
);
173+
});
174+
});
175+
});
89176
});

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)