Skip to content

Commit 5fcb380

Browse files
committed
Add tests to buildGlobalParameterDefinition
1 parent 15ec3c7 commit 5fcb380

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,95 @@
1-
import { describe, it } from "node:test";
21
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { HardhatError } from "@nomicfoundation/hardhat-errors";
35

46
import { ParameterType } from "../../src/config.js";
57
import { buildGlobalParameterDefinition } from "../../src/internal/global-parameters.js";
8+
import { RESERVED_PARAMETER_NAMES } from "../../src/internal/parameters.js";
69

710
describe("Global Parameters", () => {
11+
describe.todo("buildGlobalParameterMap", () => {
12+
// TODO: Implement tests.
13+
});
14+
815
describe("buildGlobalParameterDefinition", () => {
916
it("should build a global parameter definition", () => {
1017
const options = {
1118
name: "foo",
1219
description: "Foo description",
13-
parameterType: ParameterType.STRING,
14-
defaultValue: "bar",
20+
parameterType: ParameterType.BOOLEAN,
21+
defaultValue: true,
1522
};
1623
const globalParameter = buildGlobalParameterDefinition(options);
1724

1825
assert.deepEqual(globalParameter, options);
1926
});
27+
28+
it("should build a global parameter definition with a default type of STRING", () => {
29+
const options = {
30+
name: "foo",
31+
description: "Foo description",
32+
defaultValue: "bar",
33+
};
34+
const globalParameter = buildGlobalParameterDefinition(options);
35+
36+
assert.deepEqual(globalParameter, {
37+
...options,
38+
parameterType: ParameterType.STRING,
39+
});
40+
});
41+
42+
it("should throw if the parameter name is not valid", () => {
43+
assert.throws(
44+
() =>
45+
buildGlobalParameterDefinition({
46+
name: "foo bar",
47+
description: "Foo description",
48+
defaultValue: "bar",
49+
}),
50+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
51+
name: "foo bar",
52+
}),
53+
);
54+
});
55+
56+
it("should throw if the parameter name is reserved", () => {
57+
RESERVED_PARAMETER_NAMES.forEach((name) => {
58+
assert.throws(
59+
() =>
60+
buildGlobalParameterDefinition({
61+
name,
62+
description: "Foo description",
63+
defaultValue: "bar",
64+
}),
65+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
66+
name,
67+
}),
68+
);
69+
});
70+
});
71+
72+
it("should throw if the default value does not match the type", () => {
73+
assert.throws(
74+
() =>
75+
buildGlobalParameterDefinition({
76+
name: "foo",
77+
description: "Foo description",
78+
parameterType: ParameterType.BOOLEAN,
79+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
80+
Intentionally testing an invalid type */
81+
defaultValue: "bar" as any,
82+
}),
83+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
84+
value: "bar",
85+
name: "defaultValue",
86+
type: ParameterType.BOOLEAN,
87+
}),
88+
);
89+
});
90+
});
91+
92+
describe.todo("resolveGlobalArguments", () => {
93+
// TODO: Implement tests.
2094
});
2195
});

0 commit comments

Comments
 (0)