Skip to content

Commit 0e1ba7a

Browse files
committed
Add tests to buildGlobalParametersMap
1 parent 57d04c7 commit 0e1ba7a

File tree

1 file changed

+154
-3
lines changed

1 file changed

+154
-3
lines changed

v-next/core/test/internal/global-parameters.ts

+154-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,163 @@ import { describe, it } from "node:test";
44
import { HardhatError } from "@nomicfoundation/hardhat-errors";
55

66
import { ParameterType } from "../../src/config.js";
7-
import { buildGlobalParameterDefinition } from "../../src/internal/global-parameters.js";
7+
import {
8+
buildGlobalParametersMap,
9+
buildGlobalParameterDefinition,
10+
} from "../../src/internal/global-parameters.js";
811
import { RESERVED_PARAMETER_NAMES } from "../../src/internal/parameters.js";
912

1013
describe("Global Parameters", () => {
11-
describe.todo("buildGlobalParameterMap", () => {
12-
// TODO: Implement tests.
14+
describe("buildGlobalParametersMap", () => {
15+
it("should build an empty map of global parameters if no plugins are provided", () => {
16+
const globalParametersMap = buildGlobalParametersMap([]);
17+
18+
assert.deepEqual(globalParametersMap, new Map());
19+
});
20+
21+
it("should build an empty map of global parameters if there are no global parameters defined by plugins", () => {
22+
const globalParametersMap = buildGlobalParametersMap([
23+
{
24+
id: "plugin1",
25+
},
26+
]);
27+
28+
assert.deepEqual(globalParametersMap, new Map());
29+
});
30+
31+
it("should build a map of global parameters", () => {
32+
const globalParameterDefinition = {
33+
name: "param1",
34+
description: "param1 description",
35+
parameterType: ParameterType.BOOLEAN,
36+
defaultValue: true,
37+
};
38+
const globalParametersMap = buildGlobalParametersMap([
39+
{
40+
id: "plugin1",
41+
globalParameters: [globalParameterDefinition],
42+
},
43+
]);
44+
45+
assert.ok(
46+
globalParametersMap.has("param1"),
47+
"Expected 'param1' to be defined in the global parameters map",
48+
);
49+
assert.deepEqual(
50+
globalParametersMap.get("param1")?.param,
51+
globalParameterDefinition,
52+
);
53+
assert.deepEqual(globalParametersMap.get("param1")?.pluginId, "plugin1");
54+
});
55+
56+
it("should throw if a global parameter is already defined by another plugin", () => {
57+
const globalParameterDefinition = {
58+
name: "param1",
59+
description: "param1 description",
60+
parameterType: ParameterType.BOOLEAN,
61+
defaultValue: true,
62+
};
63+
const globalParameterDefinition2 = {
64+
name: "param1",
65+
description: "param1 description 2",
66+
parameterType: ParameterType.BOOLEAN,
67+
defaultValue: false,
68+
};
69+
70+
assert.throws(
71+
() =>
72+
buildGlobalParametersMap([
73+
{
74+
id: "plugin1",
75+
globalParameters: [globalParameterDefinition],
76+
},
77+
{
78+
id: "plugin2",
79+
globalParameters: [globalParameterDefinition2],
80+
},
81+
]),
82+
new HardhatError(
83+
HardhatError.ERRORS.GENERAL.GLOBAL_PARAMETER_ALREADY_DEFINED,
84+
{
85+
plugin: "plugin2",
86+
globalParameter: "param1",
87+
definedByPlugin: "plugin1",
88+
},
89+
),
90+
);
91+
});
92+
93+
it("should throw if a parameter name is not valid", () => {
94+
assert.throws(
95+
() =>
96+
buildGlobalParametersMap([
97+
{
98+
id: "plugin1",
99+
globalParameters: [
100+
{
101+
name: "foo bar",
102+
description: "Foo description",
103+
parameterType: ParameterType.STRING,
104+
defaultValue: "bar",
105+
},
106+
],
107+
},
108+
]),
109+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
110+
name: "foo bar",
111+
}),
112+
);
113+
});
114+
115+
it("should throw if a parameter name is reserved", () => {
116+
RESERVED_PARAMETER_NAMES.forEach((name) => {
117+
assert.throws(
118+
() =>
119+
buildGlobalParametersMap([
120+
{
121+
id: "plugin1",
122+
globalParameters: [
123+
{
124+
name,
125+
description: "Foo description",
126+
parameterType: ParameterType.STRING,
127+
defaultValue: "bar",
128+
},
129+
],
130+
},
131+
]),
132+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
133+
name,
134+
}),
135+
);
136+
});
137+
});
138+
139+
it("should throw if a parameter default value does not match the type", () => {
140+
assert.throws(
141+
() =>
142+
buildGlobalParametersMap([
143+
{
144+
id: "plugin1",
145+
globalParameters: [
146+
{
147+
name: "foo",
148+
description: "Foo description",
149+
parameterType: ParameterType.BOOLEAN,
150+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
151+
Intentionally testing an invalid type */
152+
defaultValue: "bar" as any,
153+
},
154+
],
155+
},
156+
]),
157+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
158+
value: "bar",
159+
name: "defaultValue",
160+
type: ParameterType.BOOLEAN,
161+
}),
162+
);
163+
});
13164
});
14165

15166
describe("buildGlobalParameterDefinition", () => {

0 commit comments

Comments
 (0)