Skip to content

Commit

Permalink
Warn on empty strings (#1158)
Browse files Browse the repository at this point in the history
* Added utility added tests

* Added tests for profile, extension, logical, resource

* Update test/export/StructureDefinitionExporter.test.ts

Co-authored-by: Chris Moesel <[email protected]>

* Uncommented an expect statement

Co-authored-by: gthuran <Suma1-dumont>
Co-authored-by: Chris Moesel <[email protected]>
  • Loading branch information
guhanthuran and cmoesel authored Oct 20, 2022
1 parent c5a3f98 commit 38c14ac
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/export/CodeSystemExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export class CodeSystemExporter {
private setMetadata(codeSystem: CodeSystem, fshDefinition: FshCodeSystem): void {
codeSystem.setName(fshDefinition);
codeSystem.setId(fshDefinition);
if (fshDefinition.title == '') {
logger.warn(`Code system ${fshDefinition.name} has a title field that should not be empty.`);
}
if (fshDefinition.description == '') {
logger.warn(
`Code system ${fshDefinition.name} has a description field that should not be empty.`
);
}
if (fshDefinition.title) codeSystem.title = fshDefinition.title;
if (fshDefinition.description) codeSystem.description = fshDefinition.description;
// Version is set to value provided in config, will be overriden if reset by rules
Expand Down
8 changes: 8 additions & 0 deletions src/export/InstanceExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,14 @@ export class InstanceExporter implements Fishable {
const instanceOfStructureDefinition = StructureDefinition.fromJSON(json);
let instanceDef = new InstanceDefinition();
instanceDef._instanceMeta.name = fshDefinition.id; // This is name of the instance in the FSH
if (fshDefinition.title == '') {
logger.warn(`Instance ${fshDefinition.name} has a title field that should not be empty.`);
}
if (fshDefinition.description == '') {
logger.warn(
`Instance ${fshDefinition.name} has a description field that should not be empty.`
);
}
if (fshDefinition.title) {
instanceDef._instanceMeta.title = fshDefinition.title;
}
Expand Down
12 changes: 12 additions & 0 deletions src/export/StructureDefinitionExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ export class StructureDefinitionExporter implements Fishable {
delete structDef.identifier;
structDef.version = this.tank.config.version; // can be overridden using a rule
structDef.setName(fshDefinition);
if (fshDefinition.title == '') {
if (fshDefinition instanceof Profile) {
}
logger.warn(
`${fshDefinition.constructorName} ${fshDefinition.name} has a title field that should not be empty.`
);
}
if (fshDefinition.description == '') {
logger.warn(
`${fshDefinition.constructorName} ${fshDefinition.name} has a description field that should not be empty.`
);
}
if (fshDefinition.title) {
structDef.title = fshDefinition.title;
if (
Expand Down
8 changes: 8 additions & 0 deletions src/export/ValueSetExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export class ValueSetExporter {
private setMetadata(valueSet: ValueSet, fshDefinition: FshValueSet): void {
valueSet.setName(fshDefinition);
valueSet.setId(fshDefinition);
if (fshDefinition.title == '') {
logger.warn(`Value set ${fshDefinition.name} has a title field that should not be empty.`);
}
if (fshDefinition.description == '') {
logger.warn(
`Value set ${fshDefinition.name} has a description field that should not be empty.`
);
}
if (fshDefinition.title) {
valueSet.title = fshDefinition.title;
}
Expand Down
17 changes: 17 additions & 0 deletions test/export/CodeSystemExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ describe('CodeSystemExporter', () => {
});
});

it('should warn when title and/or description is an empty string', () => {
const codeSystem = new FshCodeSystem('MyCodeSystem');
codeSystem.title = '';
codeSystem.description = '';
doc.codeSystems.set(codeSystem.name, codeSystem);
const exported = exporter.export().codeSystems;
expect(exported.length).toBe(1);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Code system MyCodeSystem has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Code system MyCodeSystem has a description field that should not be empty.'
);
});

it('should log a message when the code system has an invalid id', () => {
const codeSystem = new FshCodeSystem('StrangeSystem')
.withFile('Strange.fsh')
Expand Down
19 changes: 19 additions & 0 deletions test/export/InstanceExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ describe('InstanceExporter', () => {
expect(loggerSpy.getLastMessage('error')).toMatch(/File: Incorrect\.fsh.*Line: 15 - 18\D*/s);
});

it('should warn when title and/or description is an empty string', () => {
const instance = new Instance('MyInstance');
instance.instanceOf = 'Patient';
instance.title = '';
instance.description = '';
doc.instances.set(instance.name, instance);
const exported = exporter.export().instances;

expect(exported.length).toBe(1);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Instance MyInstance has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Instance MyInstance has a description field that should not be empty.'
);
});

it('should export instances with InstanceOf FSHy profile', () => {
const profileFoo = new Profile('Foo');
profileFoo.parent = 'Patient';
Expand Down
74 changes: 74 additions & 0 deletions test/export/StructureDefinitionExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,80 @@ describe('StructureDefinitionExporter R4', () => {
expect(pkg.extensions.length).toBe(1);
});

it('should warn when the structDef is a profile and title and/or description is an empty string', () => {
const profile = new Profile('Foo');
profile.parent = 'Patient';
profile.title = '';
profile.description = '';
doc.profiles.set(profile.name, profile);
const exported = exporter.export();
expect(exported.profiles.length).toBe(1);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Profile Foo has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Profile Foo has a description field that should not be empty.'
);
});

it('should warn when the structDef is an extension and title and/or description is an empty string', () => {
const extension = new Extension('Bar');
extension.parent = 'Extension';
extension.title = '';
extension.description = '';
doc.extensions.set(extension.name, extension);
const exported = exporter.export();

expect(exported.extensions.length).toBe(1);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Extension Bar has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Extension Bar has a description field that should not be empty.'
);
});

it('should warn when the structDef is a logical and title and/or description is an empty string', () => {
const logical = new Logical('BackFooTheFuture');
logical.parent = 'Element';
logical.title = '';
logical.description = '';
doc.logicals.set(logical.name, logical);
const exported = exporter.export();

expect(exported.logicals.length).toBe(1);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Logical BackFooTheFuture has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Logical BackFooTheFuture has a description field that should not be empty.'
);
});

it('should warn when the structDef is a resource and title and/or description is an empty string', () => {
const resource = new Resource('SpidermanBarFromHome');
resource.parent = 'Resource';
resource.id = 'PatientResource';
resource.title = '';
resource.description = '';
doc.resources.set(resource.name, resource);
exporter.exportStructDef(resource);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Resource SpidermanBarFromHome has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Resource SpidermanBarFromHome has a description field that should not be empty.'
);
});

it('should log a message when the structure definition has an invalid id', () => {
const profile = new Profile('Wrong').withFile('Wrong.fsh').withLocation([1, 8, 4, 18]);
profile.id = 'will?not?work';
Expand Down
17 changes: 17 additions & 0 deletions test/export/ValueSetExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ describe('ValueSetExporter', () => {
});
});

it('should warn when title and/or description is an empty string', () => {
const valueSet = new FshValueSet('BreakfastVS');
valueSet.title = '';
valueSet.description = '';
doc.valueSets.set(valueSet.name, valueSet);
const exported = exporter.export().valueSets;
expect(exported.length).toBe(1);

expect(loggerSpy.getAllMessages('warn').length).toBe(2);
expect(loggerSpy.getFirstMessage('warn')).toMatch(
'Value set BreakfastVS has a title field that should not be empty.'
);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Value set BreakfastVS has a description field that should not be empty.'
);
});

it('should log a message when the value set has an invalid id', () => {
const valueSet = new FshValueSet('BreakfastVS')
.withFile('Breakfast.fsh')
Expand Down

0 comments on commit 38c14ac

Please sign in to comment.