Skip to content

Commit

Permalink
Add tests to buildGlobalParametersMap
Browse files Browse the repository at this point in the history
  • Loading branch information
schaable committed Jun 15, 2024
1 parent 57d04c7 commit 0e1ba7a
Showing 1 changed file with 154 additions and 3 deletions.
157 changes: 154 additions & 3 deletions v-next/core/test/internal/global-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,163 @@ import { describe, it } from "node:test";
import { HardhatError } from "@nomicfoundation/hardhat-errors";

import { ParameterType } from "../../src/config.js";
import { buildGlobalParameterDefinition } from "../../src/internal/global-parameters.js";
import {
buildGlobalParametersMap,
buildGlobalParameterDefinition,
} from "../../src/internal/global-parameters.js";
import { RESERVED_PARAMETER_NAMES } from "../../src/internal/parameters.js";

describe("Global Parameters", () => {
describe.todo("buildGlobalParameterMap", () => {
// TODO: Implement tests.
describe("buildGlobalParametersMap", () => {
it("should build an empty map of global parameters if no plugins are provided", () => {
const globalParametersMap = buildGlobalParametersMap([]);

assert.deepEqual(globalParametersMap, new Map());
});

it("should build an empty map of global parameters if there are no global parameters defined by plugins", () => {
const globalParametersMap = buildGlobalParametersMap([
{
id: "plugin1",
},
]);

assert.deepEqual(globalParametersMap, new Map());
});

it("should build a map of global parameters", () => {
const globalParameterDefinition = {
name: "param1",
description: "param1 description",
parameterType: ParameterType.BOOLEAN,
defaultValue: true,
};
const globalParametersMap = buildGlobalParametersMap([
{
id: "plugin1",
globalParameters: [globalParameterDefinition],
},
]);

assert.ok(
globalParametersMap.has("param1"),
"Expected 'param1' to be defined in the global parameters map",
);
assert.deepEqual(
globalParametersMap.get("param1")?.param,
globalParameterDefinition,
);
assert.deepEqual(globalParametersMap.get("param1")?.pluginId, "plugin1");
});

it("should throw if a global parameter is already defined by another plugin", () => {
const globalParameterDefinition = {
name: "param1",
description: "param1 description",
parameterType: ParameterType.BOOLEAN,
defaultValue: true,
};
const globalParameterDefinition2 = {
name: "param1",
description: "param1 description 2",
parameterType: ParameterType.BOOLEAN,
defaultValue: false,
};

assert.throws(
() =>
buildGlobalParametersMap([
{
id: "plugin1",
globalParameters: [globalParameterDefinition],
},
{
id: "plugin2",
globalParameters: [globalParameterDefinition2],
},
]),
new HardhatError(
HardhatError.ERRORS.GENERAL.GLOBAL_PARAMETER_ALREADY_DEFINED,
{
plugin: "plugin2",
globalParameter: "param1",
definedByPlugin: "plugin1",
},
),
);
});

it("should throw if a parameter name is not valid", () => {
assert.throws(
() =>
buildGlobalParametersMap([
{
id: "plugin1",
globalParameters: [
{
name: "foo bar",
description: "Foo description",
parameterType: ParameterType.STRING,
defaultValue: "bar",
},
],
},
]),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
name: "foo bar",
}),
);
});

it("should throw if a parameter name is reserved", () => {
RESERVED_PARAMETER_NAMES.forEach((name) => {
assert.throws(
() =>
buildGlobalParametersMap([
{
id: "plugin1",
globalParameters: [
{
name,
description: "Foo description",
parameterType: ParameterType.STRING,
defaultValue: "bar",
},
],
},
]),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
name,
}),
);
});
});

it("should throw if a parameter default value does not match the type", () => {
assert.throws(
() =>
buildGlobalParametersMap([
{
id: "plugin1",
globalParameters: [
{
name: "foo",
description: "Foo description",
parameterType: ParameterType.BOOLEAN,
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
Intentionally testing an invalid type */
defaultValue: "bar" as any,
},
],
},
]),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
value: "bar",
name: "defaultValue",
type: ParameterType.BOOLEAN,
}),
);
});
});

describe("buildGlobalParameterDefinition", () => {
Expand Down

0 comments on commit 0e1ba7a

Please sign in to comment.