Skip to content

Commit 0e954b8

Browse files
committed
add test case
1 parent 4ea7753 commit 0e954b8

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

packages/langium-cli/test/generator/ast-generator.test.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,51 @@ describe('Ast generator', () => {
186186
}
187187
`);
188188

189+
testGeneratedInterface('optionalProperties option shall generate properties as optional in interfaces', `
190+
grammar TestGrammar
191+
192+
interface A {
193+
str: string
194+
strOpt?: string
195+
strArray: string[]
196+
bool: boolean
197+
boolOpt?: boolean
198+
boolArray: bool[]
199+
ref: @A
200+
refOpt?: @A
201+
refArray: @A[]
202+
ctn: A
203+
ctnOpt?: A
204+
ctnArray: A[]
205+
}
206+
207+
hidden terminal WS: /\\s+/;
208+
terminal ID: /[_a-zA-Z][\\w_]*/;
209+
`, expandToString`
210+
export interface A extends langium.AstNode {
211+
readonly $container: A;
212+
readonly $type: 'A';
213+
bool: boolean;
214+
boolArray: Array<unknown>;
215+
boolOpt: boolean;
216+
ctn?: A;
217+
ctnArray: Array<A>;
218+
ctnOpt?: A;
219+
ref?: langium.Reference<A>;
220+
refArray: Array<langium.Reference<A>>;
221+
refOpt?: langium.Reference<A>;
222+
str?: string;
223+
strArray: Array<string>;
224+
strOpt?: string;
225+
}
226+
227+
export const A = 'A';
228+
229+
export function isA(item: unknown): item is A {
230+
return reflection.isInstance(item, A);
231+
}
232+
`, true);
233+
189234
testGeneratedAst('should generate checker functions for datatype rules of type number', `
190235
grammar TestGrammar
191236
@@ -456,8 +501,8 @@ async function testTerminalConstants(grammar: string, expected: string) {
456501
expect(relevantPart).toEqual(expectedPart);
457502
}
458503

459-
function testGeneratedInterface(name: string, grammar: string, expected: string): void {
460-
testGenerated(name, grammar, expected, 'export interface', 'export type testAstType');
504+
function testGeneratedInterface(name: string, grammar: string, expected: string, optionalProperties = false): void {
505+
testGenerated(name, grammar, expected, 'export interface', 'export type testAstType', 0, optionalProperties);
461506
}
462507

463508
function testGeneratedAst(name: string, grammar: string, expected: string): void {
@@ -468,13 +513,14 @@ function testTypeMetaData(name: string, grammar: string, expected: string): void
468513
testGenerated(name, grammar, expected, 'getTypeMetaData', 'export const reflection');
469514
}
470515

471-
function testGenerated(name: string, grammar: string, expected: string, start: string, end: string, startCount = 0): void {
516+
function testGenerated(name: string, grammar: string, expected: string, start: string, end: string, startCount = 0, optionalProperties = false): void {
472517
test(name, async () => {
473518
const result = (await parse(grammar)).parseResult;
474519
const config: LangiumConfig = {
475520
[RelativePath]: './',
476521
projectName: 'test',
477-
languages: []
522+
languages: [],
523+
optionalProperties: optionalProperties
478524
};
479525
const expectedPart = normalizeEOL(expected).trim();
480526
const typesFileContent = generateAst(services.grammar, [result.value], config);

0 commit comments

Comments
 (0)