@@ -186,6 +186,51 @@ describe('Ast generator', () => {
186
186
}
187
187
` ) ;
188
188
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
+
189
234
testGeneratedAst ( 'should generate checker functions for datatype rules of type number' , `
190
235
grammar TestGrammar
191
236
@@ -456,8 +501,8 @@ async function testTerminalConstants(grammar: string, expected: string) {
456
501
expect ( relevantPart ) . toEqual ( expectedPart ) ;
457
502
}
458
503
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 ) ;
461
506
}
462
507
463
508
function testGeneratedAst ( name : string , grammar : string , expected : string ) : void {
@@ -468,13 +513,14 @@ function testTypeMetaData(name: string, grammar: string, expected: string): void
468
513
testGenerated ( name , grammar , expected , 'getTypeMetaData' , 'export const reflection' ) ;
469
514
}
470
515
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 {
472
517
test ( name , async ( ) => {
473
518
const result = ( await parse ( grammar ) ) . parseResult ;
474
519
const config : LangiumConfig = {
475
520
[ RelativePath ] : './' ,
476
521
projectName : 'test' ,
477
- languages : [ ]
522
+ languages : [ ] ,
523
+ optionalProperties : optionalProperties
478
524
} ;
479
525
const expectedPart = normalizeEOL ( expected ) . trim ( ) ;
480
526
const typesFileContent = generateAst ( services . grammar , [ result . value ] , config ) ;
0 commit comments