Skip to content

Commit 612a94b

Browse files
committed
test: add valueSyntax coverage
1 parent 8287323 commit 612a94b

File tree

6 files changed

+64
-50
lines changed

6 files changed

+64
-50
lines changed

src/formatters/completeFormatter.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
generateFieldCacheKey,
2525
} from "../utils/FieldValueCollector";
2626
import { FieldValueProcessor } from "../utils/FieldValueProcessor";
27-
import { Formatter } from "./formatter";
27+
import { Formatter, type PromptContext } from "./formatter";
2828
import { MacroAbortError } from "../errors/MacroAbortError";
2929
import { isCancellationError } from "../utils/errorUtils";
3030

@@ -192,15 +192,7 @@ export class CompleteFormatter extends Formatter {
192192

193193
protected async promptForVariable(
194194
header?: string,
195-
context?: {
196-
type?: string;
197-
dateFormat?: string;
198-
defaultValue?: string;
199-
label?: string;
200-
description?: string;
201-
placeholder?: string;
202-
variableKey?: string;
203-
},
195+
context?: PromptContext,
204196
): Promise<string> {
205197
try {
206198
// Use VDateInputPrompt for VDATE variables
@@ -217,7 +209,7 @@ export class CompleteFormatter extends Formatter {
217209
// Use default prompt for other variables
218210
return await new InputPrompt().factory().Prompt(
219211
this.app,
220-
(header as string) ?? context?.label ?? "Enter value",
212+
header ?? context?.label ?? "Enter value",
221213
context?.placeholder ??
222214
(context?.defaultValue ? context.defaultValue : undefined),
223215
context?.defaultValue,

src/formatters/fileNameDisplayFormatter.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Formatter } from "./formatter";
1+
import { Formatter, type PromptContext } from "./formatter";
22
import type { App } from "obsidian";
33
import { DATE_VARIABLE_REGEX, GLOBAL_VAR_REGEX } from "../constants";
44
import type { IDateParser } from "../parsers/IDateParser";
@@ -109,15 +109,7 @@ export class FileNameDisplayFormatter extends Formatter {
109109

110110
protected async promptForVariable(
111111
variableName: string,
112-
context?: {
113-
type?: string;
114-
dateFormat?: string;
115-
defaultValue?: string;
116-
label?: string;
117-
description?: string;
118-
placeholder?: string;
119-
variableKey?: string;
120-
}
112+
context?: PromptContext
121113
): Promise<string> {
122114
return getVariablePromptExample(variableName);
123115
}

src/formatters/formatDisplayFormatter.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Formatter } from "./formatter";
1+
import { Formatter, type PromptContext } from "./formatter";
22
import type { App } from "obsidian";
33
import type QuickAdd from "../main";
44
import { SingleTemplateEngine } from "../engine/SingleTemplateEngine";
@@ -112,15 +112,7 @@ export class FormatDisplayFormatter extends Formatter {
112112

113113
protected promptForVariable(
114114
variableName: string,
115-
context?: {
116-
type?: string;
117-
dateFormat?: string;
118-
defaultValue?: string;
119-
label?: string;
120-
description?: string;
121-
placeholder?: string;
122-
variableKey?: string;
123-
}
115+
context?: PromptContext
124116
): Promise<string> {
125117
return Promise.resolve(getVariablePromptExample(variableName));
126118
}

src/formatters/formatter.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ import { parseMacroToken } from "../utils/macroSyntax";
3131

3232
export type LinkToCurrentFileBehavior = "required" | "optional";
3333

34+
export interface PromptContext {
35+
type?: string;
36+
dateFormat?: string;
37+
defaultValue?: string;
38+
label?: string;
39+
description?: string;
40+
placeholder?: string;
41+
variableKey?: string;
42+
}
43+
3444
export abstract class Formatter {
3545
protected value: string;
3646
protected variables: Map<string, unknown> = new Map<string, unknown>();
@@ -563,15 +573,7 @@ export abstract class Formatter {
563573

564574
protected abstract promptForVariable(
565575
variableName: string,
566-
context?: {
567-
type?: string;
568-
dateFormat?: string;
569-
defaultValue?: string;
570-
label?: string;
571-
description?: string;
572-
placeholder?: string;
573-
variableKey?: string;
574-
}
576+
context?: PromptContext,
575577
): Promise<string>;
576578

577579
protected abstract getTemplateContent(templatePath: string): Promise<string>;

src/preflight/RequirementCollector.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { App } from "obsidian";
22
import { GLOBAL_VAR_REGEX, TEMPLATE_REGEX, VARIABLE_REGEX } from "src/constants";
3-
import { Formatter } from "src/formatters/formatter";
3+
import { Formatter, type PromptContext } from "src/formatters/formatter";
44
import type { IChoiceExecutor } from "src/IChoiceExecutor";
55
import type QuickAdd from "src/main";
66
import { NLDParser } from "src/parsers/NLDParser";
@@ -205,15 +205,7 @@ export class RequirementCollector extends Formatter {
205205

206206
protected async promptForVariable(
207207
variableName?: string,
208-
context?: {
209-
type?: string;
210-
dateFormat?: string;
211-
defaultValue?: string;
212-
label?: string;
213-
description?: string;
214-
placeholder?: string;
215-
variableKey?: string;
216-
},
208+
context?: PromptContext,
217209
): Promise<string> {
218210
if (!variableName) return "";
219211
const key = context?.variableKey ?? variableName;

src/utils/valueSyntax.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
buildValueVariableKey,
4+
parseValueToken,
5+
} from "./valueSyntax";
6+
7+
describe("parseValueToken", () => {
8+
it("ignores empty label values", () => {
9+
const parsed = parseValueToken("title|label:");
10+
expect(parsed).not.toBeNull();
11+
expect(parsed?.label).toBeUndefined();
12+
expect(parsed?.variableKey).toBe("title");
13+
});
14+
15+
it("uses the last label when multiple are provided", () => {
16+
const parsed = parseValueToken("title|label:First|label:Second");
17+
expect(parsed?.label).toBe("Second");
18+
});
19+
20+
it("scopes list variables by label", () => {
21+
const parsed = parseValueToken("a,b|label:Priority");
22+
expect(parsed?.hasOptions).toBe(true);
23+
const expectedKey = buildValueVariableKey("a,b", "Priority", true);
24+
expect(parsed?.variableKey).toBe(expectedKey);
25+
});
26+
27+
it("treats bare label option as legacy default", () => {
28+
const parsed = parseValueToken("title|label");
29+
expect(parsed?.label).toBeUndefined();
30+
expect(parsed?.defaultValue).toBe("label");
31+
});
32+
33+
it("parses custom boolean values", () => {
34+
expect(parseValueToken("a,b|custom:")?.allowCustomInput).toBe(true);
35+
expect(parseValueToken("a,b|custom:false")?.allowCustomInput).toBe(false);
36+
expect(parseValueToken("a,b|custom:0")?.allowCustomInput).toBe(false);
37+
});
38+
39+
it("allows custom plus explicit default", () => {
40+
const parsed = parseValueToken("a,b|custom|default:High");
41+
expect(parsed?.allowCustomInput).toBe(true);
42+
expect(parsed?.defaultValue).toBe("High");
43+
});
44+
});

0 commit comments

Comments
 (0)