Skip to content

Commit

Permalink
feat(*): detect enum value add/remove (contributes to accordproject#442)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone authored and Simon Stone committed Aug 5, 2022
1 parent 958074b commit b7d6cc7
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 75 deletions.
11 changes: 7 additions & 4 deletions packages/concerto-analysis/src/compare-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ export const defaultCompareConfig: CompareConfig = {
'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,
'namespace-changed': CompareResult.ERROR
'required-property-added': CompareResult.MAJOR,
'optional-property-added': CompareResult.PATCH,
'required-property-removed': CompareResult.MAJOR,
'optional-property-removed': CompareResult.MAJOR,
'namespace-changed': CompareResult.ERROR,
'enum-value-added': CompareResult.PATCH,
'enum-value-removed': CompareResult.MAJOR,
}
};
14 changes: 13 additions & 1 deletion packages/concerto-analysis/src/compare-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* limitations under the License.
*/

import { ClassDeclaration } from '@accordproject/concerto-core';
import { ClassDeclaration, EnumValueDeclaration, Field, Property, RelationshipDeclaration } from '@accordproject/concerto-core';

export function getClassDeclarationType(classDeclaration: ClassDeclaration) {
if (classDeclaration.isAsset()) {
Expand All @@ -31,3 +31,15 @@ export function getClassDeclarationType(classDeclaration: ClassDeclaration) {
throw new Error(`unknown class declaration type "${classDeclaration}"`);
}
}

export function getPropertyType(property: Property) {
if (property instanceof Field) {
return 'field';
} else if (property instanceof RelationshipDeclaration) {
return 'relationship';
} else if (property instanceof EnumValueDeclaration) {
return 'enum value';
} else {
throw new Error(`unknown property type "${property}"`);
}
}
62 changes: 0 additions & 62 deletions packages/concerto-analysis/src/comparers/fields.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/concerto-analysis/src/comparers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*/

import { classDeclarationComparerFactories } from './class-declarations';
import { fieldComparerFactories } from './fields';
import { modelFileComparerFactories } from './model-files';
import { propertyComparerFactories } from './properties';

export const comparerFactories = [
...classDeclarationComparerFactories,
...fieldComparerFactories,
...propertyComparerFactories,
...modelFileComparerFactories
];
87 changes: 87 additions & 0 deletions packages/concerto-analysis/src/comparers/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { EnumValueDeclaration, Field } from '@accordproject/concerto-core';
import { getClassDeclarationType, getPropertyType } from '../compare-utils';
import { ComparerFactory } from '../comparer';

const propertyAdded: ComparerFactory = (context) => ({
compareProperty: (a, b) => {
if (a || !b) {
return;
}
const classDeclarationType = getClassDeclarationType(b.getParent());
if (b instanceof EnumValueDeclaration) {
context.report({
key: 'enum-value-added',
message: `The enum value "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`,
element: b,
});
return;
}
const isOptional = b.isOptional();
const hasDefault = b instanceof Field ? !!b.getDefaultValue() : false;
const required = !isOptional && !hasDefault;
const type = getPropertyType(b);
if (required) {
context.report({
key: 'required-property-added',
message: `The required ${type} "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`,
element: b,
});
} else {
context.report({
key: 'optional-property-added',
message: `The optional ${type} "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`,
element: b,
});
}
}
});

const propertyRemoved: ComparerFactory = (context) => ({
compareProperty: (a, b) => {
if (!a || b) {
return;
}
const classDeclarationType = getClassDeclarationType(a.getParent());
if (a instanceof EnumValueDeclaration) {
context.report({
key: 'enum-value-removed',
message: `The enum value "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`,
element: b,
});
return;
}
const isOptional = a.isOptional();
const hasDefault = a instanceof Field ? !!a.getDefaultValue() : false;
const required = !isOptional && !hasDefault;
const type = getPropertyType(a);
if (required) {
context.report({
key: 'required-property-removed',
message: `The required ${type} "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`,
element: a
});
} else {
context.report({
key: 'optional-property-removed',
message: `The optional ${type} "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`,
element: a
});
}
}
});

export const propertyComparerFactories = [propertyAdded, propertyRemoved];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace [email protected]

enum Thing {
o FOO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace [email protected]

enum Thing {
o FOO
o BAR
}
36 changes: 30 additions & 6 deletions packages/concerto-analysis/test/unit/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test('should detect a required field being added', async () => {
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'required-field-added',
key: 'required-property-added',
message: 'The required field "value" was added to the concept "Thing"'
})
]));
Expand All @@ -95,8 +95,8 @@ test('should detect a required field being removed', async () => {
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'field-removed',
message: 'The field "value" was removed from the concept "Thing"'
key: 'required-property-removed',
message: 'The required field "value" was removed from the concept "Thing"'
})
]));
expect(results.result).toBe(CompareResult.MAJOR);
Expand All @@ -107,7 +107,7 @@ test('should detect an optional field being added', async () => {
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'optional-field-added',
key: 'optional-property-added',
message: 'The optional field "value" was added to the concept "Thing"'
})
]));
Expand All @@ -119,8 +119,8 @@ test('should detect an optional field being removed', async () => {
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'field-removed',
message: 'The field "value" was removed from the concept "Thing"'
key: 'optional-property-removed',
message: 'The optional field "value" was removed from the concept "Thing"'
})
]));
expect(results.result).toBe(CompareResult.MAJOR);
Expand All @@ -140,3 +140,27 @@ test('should detect an optional field being removed', async () => {
expect(results.result).toBe(CompareResult.MAJOR);
});
});

test('should detect a enum value being added', async () => {
const [a, b] = await getModelFiles('enum-value-added-a.cto', 'enum-value-added-b.cto');
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'enum-value-added',
message: 'The enum value "BAR" was added to the enum "Thing"'
})
]));
expect(results.result).toBe(CompareResult.PATCH);
});

test('should detect an enum value being removed', async () => {
const [a, b] = await getModelFiles('enum-value-added-b.cto', 'enum-value-added-a.cto');
const results = new Compare().compare(a, b);
expect(results.findings).toEqual(expect.arrayContaining([
expect.objectContaining({
key: 'enum-value-removed',
message: 'The enum value "BAR" was removed from the enum "Thing"'
})
]));
expect(results.result).toBe(CompareResult.MAJOR);
});

0 comments on commit b7d6cc7

Please sign in to comment.