@@ -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
@@ -504,8 +549,8 @@ async function testTerminalConstants(grammar: string, expected: string) {
504549 expect ( relevantPart ) . toEqual ( expectedPart ) ;
505550}
506551
507- function testGeneratedInterface ( name : string , grammar : string , expected : string ) : void {
508- testGenerated ( name , grammar , expected , 'export interface' , 'export type testAstType' ) ;
552+ function testGeneratedInterface ( name : string , grammar : string , expected : string , optionalProperties = false ) : void {
553+ testGenerated ( name , grammar , expected , 'export interface' , 'export type testAstType' , 0 , optionalProperties ) ;
509554}
510555
511556function testGeneratedAst ( name : string , grammar : string , expected : string ) : void {
@@ -519,13 +564,14 @@ function testTypeMetaData(name: string, grammar: string, expected: string): void
519564function testReferenceType ( name : string , grammar : string , expected : string ) : void {
520565 testGenerated ( name , grammar , expected , 'getReferenceType' , 'getTypeMetaData' ) ;
521566}
522- function testGenerated ( name : string , grammar : string , expected : string , start : string , end : string , startCount = 0 ) : void {
567+ function testGenerated ( name : string , grammar : string , expected : string , start : string , end : string , startCount = 0 , optionalProperties = false ) : void {
523568 test ( name , async ( ) => {
524569 const result = ( await parse ( grammar ) ) . parseResult ;
525570 const config : LangiumConfig = {
526571 [ RelativePath ] : './' ,
527572 projectName : 'test' ,
528- languages : [ ]
573+ languages : [ ] ,
574+ optionalProperties : optionalProperties
529575 } ;
530576 const expectedPart = normalizeEOL ( expected ) . trim ( ) ;
531577 const typesFileContent = generateAst ( services . grammar , [ result . value ] , config ) ;
0 commit comments