Skip to content

Commit

Permalink
Use keyword for description, rules for everything else
Browse files Browse the repository at this point in the history
When producing FSH for an Invariant, use the Description keyword for the
description element. For all other metadata elements, use assignment
rules instead. These rules will always come before any other rules on
the Invariant.
  • Loading branch information
mint-thompson committed Nov 22, 2023
1 parent 1c78ec0 commit bd3aca3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/exportable/ExportableInvariant.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { fshtypes } from 'fsh-sushi';
import { Exportable, ExportableAssignmentRule, ExportableInsertRule } from '.';
import { fshifyString } from './common';
import { EOL } from 'os';

export class ExportableInvariant extends fshtypes.Invariant implements Exportable {
rules: (ExportableAssignmentRule | ExportableInsertRule)[];

constructor(name: string) {
super(name);
}

metadataToFSH(): string {
const resultLines: string[] = [];
resultLines.push(`Invariant: ${this.name}`);
if (this.description) {
// Description can be a multiline string.
// If it contains newline characters, treat it as a multiline string.
if (this.description.indexOf('\n') > -1) {
resultLines.push(`Description: """${this.description}"""`);
} else {
resultLines.push(`Description: "${fshifyString(this.description)}"`);
}
}
if (this.severity) {
resultLines.push(`* severity = ${this.severity}`);
}
if (this.expression) {
resultLines.push(`* expression = "${fshifyString(this.expression)}"`);
}
if (this.xpath) {
resultLines.push(`* xpath = "${fshifyString(this.xpath)}"`);
}
return resultLines.join(EOL);
}
}
43 changes: 36 additions & 7 deletions test/exportable/ExportableInvariant.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EOL } from 'os';
import { fshtypes } from 'fsh-sushi';
import { ExportableInvariant } from '../../src/exportable';
import { ExportableAssignmentRule, ExportableInvariant } from '../../src/exportable';

describe('ExportableInvariant', () => {
it('should export the simplest invariant', () => {
Expand All @@ -21,9 +21,9 @@ describe('ExportableInvariant', () => {
const expectedResult = [
'Invariant: inv-2',
'Description: "This is an important condition."',
'Severity: #error',
'Expression: "requirement.exists()"',
'XPath: "f:requirement"'
'* severity = #error',
'* expression = "requirement.exists()"',
'* xpath = "f:requirement"'
].join(EOL);
const result = input.toFSH();
expect(result).toBe(expectedResult);
Expand All @@ -39,9 +39,38 @@ describe('ExportableInvariant', () => {
const expectedResult = [
'Invariant: inv-3',
'Description: """Please do this.\nPlease always do this with a \\ character."""',
'Severity: #warning',
'Expression: "requirement.contains(\\"\\\\\\")"',
'XPath: "f:requirement"'
'* severity = #warning',
'* expression = "requirement.contains(\\"\\\\\\")"',
'* xpath = "f:requirement"'
].join(EOL);
const result = input.toFSH();
expect(result).toBe(expectedResult);
});

it('should produce FSH for an invariant with additional rules', () => {
const input = new ExportableInvariant('inv-4');
input.description = 'This is an important condition.';
input.severity = new fshtypes.FshCode('error');
input.expression = 'requirement.exists()';
input.xpath = 'f:requirement';

const requirements = new ExportableAssignmentRule('requirements');
requirements.value = 'This is necessary because it is important.';
const extensionUrl = new ExportableAssignmentRule('human.extension[0].url');
extensionUrl.value = 'http://example.org/SomeExtension';
const extensionValue = new ExportableAssignmentRule('human.extension[0].valueString');
extensionValue.value = 'ExtensionValue';
input.rules.push(requirements, extensionUrl, extensionValue);

const expectedResult = [
'Invariant: inv-4',
'Description: "This is an important condition."',
'* severity = #error',
'* expression = "requirement.exists()"',
'* xpath = "f:requirement"',
'* requirements = "This is necessary because it is important."',
'* human.extension[0].url = "http://example.org/SomeExtension"',
'* human.extension[0].valueString = "ExtensionValue"'
].join(EOL);
const result = input.toFSH();
expect(result).toBe(expectedResult);
Expand Down

0 comments on commit bd3aca3

Please sign in to comment.