Skip to content

Commit 026f282

Browse files
committed
export and test parseParameterValue
1 parent 7b214d8 commit 026f282

File tree

4 files changed

+93
-114
lines changed

4 files changed

+93
-114
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-112
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import type {
2-
ArgumentValue,
3-
OptionDefinition,
4-
PositionalArgumentDefinition,
5-
} from "@ignored/hardhat-vnext-core/types/arguments";
61
import type {
72
GlobalOptions,
83
GlobalOptionDefinitions,
@@ -17,6 +12,7 @@ import "tsx"; // NOTE: This is important, it allows us to load .ts files form th
1712

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

0 commit comments

Comments
 (0)