Skip to content

Commit

Permalink
feat(*): detect class declaration type change (contributes to accordp…
Browse files Browse the repository at this point in the history
…roject#442)

Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone authored and Simon Stone committed Aug 5, 2022
1 parent 2e17bf1 commit 958074b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
15 changes: 3 additions & 12 deletions packages/concerto-analysis/src/compare-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,9 @@ export type CompareConfig = {
export const defaultCompareConfig: CompareConfig = {
comparerFactories,
rules: {
'asset-added': CompareResult.MINOR,
'asset-removed': CompareResult.MAJOR,
'concept-added': CompareResult.MINOR,
'concept-removed': CompareResult.MAJOR,
'enum-added': CompareResult.MINOR,
'enum-removed': CompareResult.MAJOR,
'event-added': CompareResult.MINOR,
'event-removed': CompareResult.MAJOR,
'participant-added': CompareResult.MINOR,
'participant-removed': CompareResult.MAJOR,
'transaction-added': CompareResult.MINOR,
'transaction-removed': CompareResult.MAJOR,
'class-declaration-added': CompareResult.MINOR,
'class-declaration-removed': CompareResult.MAJOR,
'class-declaration-type-changed': CompareResult.MAJOR,
'required-field-added': CompareResult.MAJOR,
'optional-field-added': CompareResult.PATCH,
'field-removed': CompareResult.MAJOR,
Expand Down
24 changes: 21 additions & 3 deletions packages/concerto-analysis/src/comparers/class-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const classDeclarationAdded: ComparerFactory = (context) => ({
if (!a && b) {
const type = getClassDeclarationType(b);
context.report({
key: `${type}-added`,
key: 'class-declaration-added',
message: `The ${type} "${b.getName()}" was added`,
element: b
});
Expand All @@ -33,12 +33,30 @@ const classDeclarationRemoved: ComparerFactory = (context) => ({
if (a && !b) {
const type = getClassDeclarationType(a);
context.report({
key: `${type}-removed`,
key: 'class-declaration-removed',
message: `The ${type} "${a.getName()}" was removed`,
element: a
});
}
}
});

export const classDeclarationComparerFactories = [classDeclarationAdded, classDeclarationRemoved];
const classDeclarationTypeChanged: ComparerFactory = (context) => ({
compareClassDeclaration: (a, b) => {
if (!a || !b) {
return;
}
const aType = getClassDeclarationType(a);
const bType = getClassDeclarationType(b);
if (aType === bType) {
return;
}
context.report({
key: 'class-declaration-type-changed',
message: `The ${aType} "${a.getName()}" changed type from ${aType} to ${bType}`,
element: a
});
}
});

export const classDeclarationComparerFactories = [classDeclarationAdded, classDeclarationRemoved, classDeclarationTypeChanged];
19 changes: 17 additions & 2 deletions packages/concerto-analysis/test/unit/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('should detect a change of namespace', async () => {
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: `${type}-added`,
key: 'class-declaration-added',
message: `The ${type} "Thing" was added`
})
]));
Expand All @@ -70,7 +70,7 @@ test('should detect a change of namespace', async () => {
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: `${type}-removed`,
key: 'class-declaration-removed',
message: `The ${type} "Thing" was removed`
})
]));
Expand Down Expand Up @@ -125,3 +125,18 @@ test('should detect an optional field being removed', async () => {
]));
expect(results.result).toBe(CompareResult.MAJOR);
});


[{ from: 'asset', to: 'concept'}, { from: 'enum', to: 'event' }, { from: 'participant', to: 'transaction'}].forEach(({ from, to }) => {
test(`should detect a change of declaration type (${from} to ${to})`, async () => {
const [a, b] = await getModelFiles(`${from}-added.cto`, `${to}-added.cto`);
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'class-declaration-type-changed',
message: `The ${from} "Thing" changed type from ${from} to ${to}`
})
]));
expect(results.result).toBe(CompareResult.MAJOR);
});
});

0 comments on commit 958074b

Please sign in to comment.