Skip to content

Commit a3acebd

Browse files
committed
Add tests for issue #2024
1 parent e19e4d8 commit a3acebd

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
() => null;

src/test/issueTests.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
2+
deepStrictEqual,
23
deepStrictEqual as equal,
34
notDeepStrictEqual as notEqual,
45
ok,
56
} from "assert";
6-
import type { Application } from "../lib/application";
7+
import { Application } from "../lib/application";
8+
import { ApplicationEvents } from "../lib/application-events";
79
import {
810
DeclarationReflection,
911
ProjectReflection,
@@ -18,6 +20,7 @@ import {
1820
IntrinsicType,
1921
} from "../lib/models";
2022
import type { InlineTagDisplayPart } from "../lib/models/comments/comment";
23+
import { ParameterType, TypeDocOptionMap } from "../lib/utils";
2124
import { getConverter2App } from "./programs";
2225
import type { TestLogger } from "./TestLogger";
2326

@@ -725,6 +728,40 @@ export const issueTests: {
725728
);
726729
},
727730

731+
gh2024() {
732+
const name = "objectOptions" as keyof TypeDocOptionMap;
733+
const defaultValue = {
734+
neverChange: "ok",
735+
foo: "foo",
736+
};
737+
const app = new Application();
738+
app.options.addDeclaration({
739+
help: "Test option parsing with default values from plugins",
740+
name,
741+
type: ParameterType.Object,
742+
defaultValue,
743+
});
744+
deepStrictEqual(app.options.getValue(name), defaultValue);
745+
app.options.setValue(name, { foo: "bar" });
746+
const newVal = app.options.getValue(name) as typeof defaultValue;
747+
equal(newVal.foo, "bar");
748+
equal(newVal.neverChange, "ok");
749+
app.options.setValue("out", "testOutString");
750+
app.on(ApplicationEvents.BOOTSTRAP_END, () => {
751+
equal(app.options.getValue("out"), "");
752+
app.options.setValue("out", "testOutString");
753+
app.options.setValue(name, { foo: "foo" });
754+
});
755+
app.bootstrap();
756+
app.options.setValue(name, {});
757+
app.convert();
758+
deepStrictEqual(app.options.getValue(name), defaultValue);
759+
equal(
760+
app.options.getValue("out").trim().endsWith("testOutString"),
761+
true
762+
);
763+
},
764+
728765
gh2031(project, logger) {
729766
const sig = query(project, "MyClass.aMethod").signatures![0];
730767
const summaryLink = sig.comment?.summary[0];

src/test/utils/options/declaration.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getDefaultValue,
88
MapDeclarationOption,
99
MixedDeclarationOption,
10+
ObjectDeclarationOption,
1011
NumberDeclarationOption,
1112
ParameterType,
1213
StringDeclarationOption,
@@ -321,6 +322,29 @@ describe("Options - conversions", () => {
321322
new Error("test must not be a number")
322323
);
323324
});
325+
it("Passes through object", () => {
326+
const data = {};
327+
equal(convert(data, optionWithType(ParameterType.Object), ""), data);
328+
});
329+
330+
it("Validates object options", () => {
331+
const declaration: ObjectDeclarationOption = {
332+
name: "test",
333+
help: "",
334+
type: ParameterType.Object,
335+
defaultValue: "default",
336+
validate: (value: unknown) => {
337+
if (typeof value !== "object" || Array.isArray(value)) {
338+
throw new Error("test must be an object");
339+
}
340+
},
341+
};
342+
equal(convert({}, declaration, ""), {});
343+
throws(
344+
() => convert(1, declaration, ""),
345+
new Error("test must be an object")
346+
);
347+
});
324348
});
325349

326350
describe("Options - default values", () => {

0 commit comments

Comments
 (0)