Skip to content

Commit

Permalink
Merge pull request #46 from ditrit/improvement/definition_models
Browse files Browse the repository at this point in the history
Improvement/definition models
  • Loading branch information
Zorin95670 authored Aug 23, 2022
2 parents afa137a + 3b27fe7 commit 7e27eaa
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 48 deletions.
12 changes: 12 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Install `bin-pack` library.
- Add definition model for ComponentLink
- Add properties in ComponentAttributeDefiniton:
- `linkType` used for specify the type of link (`Default` or `Reverse`)
- `ContainerRef` used for specify the reference of valid container type
- `definedAttributes` used to specify definition of subattribute in case of `Object` type

### Changed

- Improve `setComponentPosition` method with `bin-pack` library.
- Rename `linkTypes` to `linkRef` in ComponentAttributeDefinition
- Rename method `getComponentDefinitions` to `getDefinitions` in DefaultMetadata.
- Method `getDefinition` will return an object with component and link definitions.

### Removed

- Remove attributes `required` and `displayable` in ComponentDefinition

### Fixed

Expand Down
21 changes: 12 additions & 9 deletions src/metadata/DefaultMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
class DefaultMetadata {
/**
* Default constructor.
* @param {Object} metadata - Metadata provide by leto-modelizer.
* @param {Array} resources - Resources object that contains all Metadata object.
*/
constructor(metadata) {
constructor(resources) {
/**
* Metadata object.
* @type {Object}
* Resources object that contains all Metadata object.
* @type {Array}
*/
this.metadata = metadata;
this.resources = resources;
}

/**
Expand All @@ -24,11 +24,14 @@ class DefaultMetadata {
}

/**
* Get all component definitions from metadata.
* @return {ComponentDefinition[]} - Array of component definitions.
* Get all component/link definitions from metadata.
* @return {Object} - Object that contains component/link definitions.
*/
getComponentDefinitions() {
return [];
getDefinitions() {
return {
components: [],
links: [],
};
}
}

Expand Down
37 changes: 31 additions & 6 deletions src/models/ComponentAttributeDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class ComponentAttributeDefinition {
*
* @param {String} [props.name] - Attribute name.
* @param {String} [props.type] - Attribute type,
* valid types are String/Boolean/Number/Array/Object/Link.
* @param {String[]} [props.linkTypes=[]] - Define list of Component that can be linked with this.
* valid types are String/Boolean/Number/Array/Object/Link/Reference.
* @param {String} [props.linkType] - Type of link, valid types are Default/Reverse.
* @param {String[]} [props.linkRef=[]] - Reference of accepted component for link.
* @param {String[]} [props.containerRef=[]] - Reference of accepted component for container.
* @param {Boolean} [props.required=false] - Attribute is required.
* @param {ComponentAttributeDefinition[]} [props.definedAttributes=[]] - Defined attributes for
* this type.
* @param {Object} [props.rules={}] - Rules of this type of Attribute.
* @param {Array} [props.rules.values] - Default values of attribute.
* @param {Number} [props.rules.min] - Minimum value of Attribute.
Expand All @@ -19,8 +23,11 @@ class ComponentAttributeDefinition {
constructor(props = {
name: null,
type: null,
linkTypes: [],
linkType: null,
linkRef: [],
containerRef: [],
required: false,
definedAttributes: [],
rules: {
values: null,
min: null,
Expand All @@ -31,7 +38,10 @@ class ComponentAttributeDefinition {
const {
name,
type,
linkTypes,
linkType,
linkRef,
containerRef,
definedAttributes,
required,
rules,
} = props;
Expand All @@ -41,15 +51,30 @@ class ComponentAttributeDefinition {
*/
this.name = name || null;
/**
* Attribute type, valid types are String/Boolean/Number/Array/Object/Link.
* Attribute type, valid types are String/Boolean/Number/Array/Object/Link/Reference.
* @type {String}
*/
this.type = type || null;
/**
* Type of link, valid types are Default/Reverse.
* @type {String}
*/
this.linkType = linkType || null;
/**
* Define list of Component that can be linked with this.
* @type {String[]}
*/
this.linkTypes = linkTypes || [];
this.linkRef = linkRef || [];
/**
* Define list of Component that can be the container of this component.
* @type {String[]}
*/
this.containerRef = containerRef || [];
/**
* Defined attributes for this type.
* @type {ComponentAttributeDefinition[]}
*/
this.definedAttributes = definedAttributes || [];
/**
* Attribute is required.
* @type {Boolean}
Expand Down
16 changes: 0 additions & 16 deletions src/models/ComponentDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class ComponentDefinition {
* @param {ComponentAttributeDefinition[]} [props.definedAttributes=[]] - Defined attributes for
* this type.
* @param {Boolean} [props.isContainer=false] - Boolean means if this type can be a parent.
* @param {Boolean} [props.displayable=true] - Boolean means if this type can be drawn.
* @param {Boolean} [props.required=false] - Boolean means if this need to exist in all
* instantiated components.
*/
constructor(props = {
Expand All @@ -23,8 +21,6 @@ class ComponentDefinition {
parentTypes: [],
definedAttributes: [],
isContainer: false,
displayable: true,
required: false,
}) {
const {
type,
Expand All @@ -33,8 +29,6 @@ class ComponentDefinition {
parentTypes,
definedAttributes,
isContainer,
displayable,
required,
} = props;
/**
* The type of the associated component.
Expand Down Expand Up @@ -66,16 +60,6 @@ class ComponentDefinition {
* @type {Boolean}
*/
this.isContainer = isContainer === undefined ? false : isContainer;
/**
* Boolean means if this type can be a draw.
* @type {Boolean}
*/
this.displayable = displayable === undefined ? true : displayable;
/**
* Boolean means if this need to exist in all instantiated components.
* @type {Boolean}
*/
this.required = required === undefined ? false : required;
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/models/ComponentLinkDefinition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition of the link between components.
*/
class ComponentLinkDefinition {
/**
* Default constructor.
*
* @param {String} [props.sourceRef] - Reference of component can be the source in a link.
* @param {String} [props.targetRef] - Reference of component can be the target of the link.
* @param {String} [props.type] - Representation of the link.
*/
constructor(props = {
sourceRef: null,
targetRef: null,
type: null,
}) {
const {
sourceRef,
targetRef,
type,
} = props;
/**
* Reference of component can be the source in a link.
* @type {String}
*/
this.sourceRef = sourceRef || null;
/**
* Reference of component can be the target of the link.
* @type {String}
*/
this.targetRef = targetRef || null;
/**
* Representation of the link.
* @type {String}
*/
this.type = type || null;
}
}

export default ComponentLinkDefinition;
9 changes: 6 additions & 3 deletions tests/unit/metadata/DefaultMetadata.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ describe('Test Class: DefaultMetadata()', () => {
});
});

describe('Test method: getComponentDefinitions', () => {
it('Should return empty array', () => {
describe('Test method: getDefinitions', () => {
it('Should return object with component and link definitions', () => {
const defaultMetadata = new DefaultMetadata();
expect(defaultMetadata.getComponentDefinitions()).toEqual([]);
expect(defaultMetadata.getDefinitions()).toEqual({
components: [],
links: [],
});
});
});
});
Expand Down
29 changes: 23 additions & 6 deletions tests/unit/models/ComponentAttributeDefinition.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ describe('Test class: ComponentAttributeDefinition', () => {

expect(definedAttribute.name).toBeNull();
expect(definedAttribute.type).toBeNull();
expect(definedAttribute.linkTypes).toEqual([]);
expect(definedAttribute.linkType).toBeNull();
expect(definedAttribute.linkRef).toEqual([]);
expect(definedAttribute.containerRef).toEqual([]);
expect(definedAttribute.definedAttributes).toEqual([]);
expect(definedAttribute.required).toEqual(false);
expect(definedAttribute.rules.values).toBeNull();
expect(definedAttribute.rules.min).toBeNull();
Expand All @@ -20,7 +23,10 @@ describe('Test class: ComponentAttributeDefinition', () => {

expect(definedAttribute.name).toBeNull();
expect(definedAttribute.type).toBeNull();
expect(definedAttribute.linkTypes).toEqual([]);
expect(definedAttribute.linkType).toBeNull();
expect(definedAttribute.linkRef).toEqual([]);
expect(definedAttribute.containerRef).toEqual([]);
expect(definedAttribute.definedAttributes).toEqual([]);
expect(definedAttribute.required).toEqual(false);
expect(definedAttribute.rules.values).toBeNull();
expect(definedAttribute.rules.min).toBeNull();
Expand All @@ -33,7 +39,10 @@ describe('Test class: ComponentAttributeDefinition', () => {

expect(definedAttribute.name).toBeNull();
expect(definedAttribute.type).toBeNull();
expect(definedAttribute.linkTypes).toEqual([]);
expect(definedAttribute.linkType).toBeNull();
expect(definedAttribute.linkRef).toEqual([]);
expect(definedAttribute.containerRef).toEqual([]);
expect(definedAttribute.definedAttributes).toEqual([]);
expect(definedAttribute.required).toEqual(false);
expect(definedAttribute.rules.values).toBeNull();
expect(definedAttribute.rules.min).toBeNull();
Expand All @@ -45,7 +54,10 @@ describe('Test class: ComponentAttributeDefinition', () => {
const definedAttribute = new ComponentAttributeDefinition({
name: 'name',
type: 'type',
linkTypes: ['linkTypes'],
linkType: 'Default',
linkRef: ['linkRef'],
containerRef: ['containerRef'],
definedAttributes: [new ComponentAttributeDefinition()],
required: true,
rules: {
values: ['value'],
Expand All @@ -57,8 +69,13 @@ describe('Test class: ComponentAttributeDefinition', () => {

expect(definedAttribute.name).toEqual('name');
expect(definedAttribute.type).toEqual('type');
expect(definedAttribute.linkTypes.length).toEqual(1);
expect(definedAttribute.linkTypes[0]).toEqual('linkTypes');
expect(definedAttribute.linkType).toEqual('Default');
expect(definedAttribute.linkRef.length).toEqual(1);
expect(definedAttribute.linkRef[0]).toEqual('linkRef');
expect(definedAttribute.containerRef.length).toEqual(1);
expect(definedAttribute.containerRef[0]).toEqual('containerRef');
expect(definedAttribute.definedAttributes.length).toEqual(1);
expect(definedAttribute.definedAttributes[0]).not.toBeNull();
expect(definedAttribute.required).toEqual(true);
expect(definedAttribute.rules.values).toEqual(['value']);
expect(definedAttribute.rules.min).toEqual(1);
Expand Down
8 changes: 0 additions & 8 deletions tests/unit/models/ComponentDefinition.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ describe('Test class: ComponentDefinition', () => {
expect(componentDefinition.parentTypes).toEqual([]);
expect(componentDefinition.definedAttributes).toEqual([]);
expect(componentDefinition.isContainer).toBeFalsy();
expect(componentDefinition.displayable).toBeTruthy();
expect(componentDefinition.required).toBeFalsy();
});

it('Check passing undefined variables to constructor', () => {
Expand All @@ -24,8 +22,6 @@ describe('Test class: ComponentDefinition', () => {
expect(componentDefinition.parentTypes).toEqual([]);
expect(componentDefinition.definedAttributes).toEqual([]);
expect(componentDefinition.isContainer).toBeFalsy();
expect(componentDefinition.displayable).toBeTruthy();
expect(componentDefinition.required).toBeFalsy();
});

it('Check passing variable to constructor', () => {
Expand All @@ -36,8 +32,6 @@ describe('Test class: ComponentDefinition', () => {
parentTypes: ['type'],
definedAttributes: ['attribute'],
isContainer: true,
displayable: false,
required: true,
});

expect(componentDefinition.type).toEqual('type');
Expand All @@ -48,8 +42,6 @@ describe('Test class: ComponentDefinition', () => {
expect(componentDefinition.definedAttributes.length).toEqual(1);
expect(componentDefinition.definedAttributes[0]).toEqual('attribute');
expect(componentDefinition.isContainer).toBeTruthy();
expect(componentDefinition.displayable).toBeFalsy();
expect(componentDefinition.required).toBeTruthy();
});
});
});
33 changes: 33 additions & 0 deletions tests/unit/models/ComponentLinkDefinition.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ComponentLinkDefinition from 'src/models/ComponentLinkDefinition';

describe('Test class: ComponentLinkDefinition', () => {
describe('Test constructor', () => {
it('Check variable instantiation', () => {
const link = new ComponentLinkDefinition();

expect(link.sourceRef).toBeNull();
expect(link.targetRef).toBeNull();
expect(link.type).toBeNull();
});

it('Check passing undefined variables to constructor', () => {
const link = new ComponentLinkDefinition({});

expect(link.sourceRef).toBeNull();
expect(link.targetRef).toBeNull();
expect(link.type).toBeNull();
});

it('Check passing variable to constructor', () => {
const link = new ComponentLinkDefinition({
sourceRef: 'id1',
targetRef: 'id2',
type: 'type',
});

expect(link.sourceRef).toEqual('id1');
expect(link.targetRef).toEqual('id2');
expect(link.type).toEqual('type');
});
});
});

0 comments on commit 7e27eaa

Please sign in to comment.