Skip to content

Commit

Permalink
Merge pull request #165 from OpenSmock/dev-164
Browse files Browse the repository at this point in the history
MolUtils: add required/offered types for a component contract interfa…
  • Loading branch information
labordep authored Feb 28, 2024
2 parents 20b14ee + baa4946 commit c8b5d0c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Molecule-Tests/MolUtilsTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ MolUtilsTest >> testLog2 [
MolUtils isLogActive: (activate).
]

{ #category : #tests }
MolUtilsTest >> testOfferedTypes [

"Not used services"
| types |
self assert: (MolUtils offeredTypes: nil) isEmpty. "test with nil"
self assert: (MolUtils offeredTypes: Trait) isEmpty. "test with no component interface object"
self assert: (MolUtils offeredTypes: MolUnusedServices) isEmpty. "test with unused interface"

"Type used services"
types := MolUtils offeredTypes: MolUsedServices.
self assert: types size equals: 2.
self assert: (types includes: MolCompleteComponent).
self assert: (types includes: MolCompleteComponent2).

"Type provide events"
types := MolUtils offeredTypes: MolUsedEvents.
self assert: types size equals: 2.
self assert: (types includes: MolCompleteComponent).
self assert: (types includes: MolCompleteComponent2).

"Type used parameters"
types := MolUtils offeredTypes: MolUsedParameters.
self assert: types size equals: 2.
self assert: (types includes: MolCompleteComponent).
self assert: (types includes: MolCompleteComponent2).
]

{ #category : #tests }
MolUtilsTest >> testPassivateComponentNamed [

Expand Down Expand Up @@ -395,6 +423,34 @@ MolUtilsTest >> testRemoveComponents [
equals: false
]

{ #category : #tests }
MolUtilsTest >> testRequiredTypes [

"Not used services"
| types |
self assert: (MolUtils requiredTypes: nil) isEmpty. "test with nil"
self assert: (MolUtils requiredTypes: Trait) isEmpty. "test with no component interface object"
self assert: (MolUtils requiredTypes: MolUnusedServices) isEmpty. "test with unused interface"

"Type used services"
types := MolUtils requiredTypes: MolUsedServices.
self assert: types size equals: 2.
self assert: (types includes: MolCompleteComponent).
self assert: (types includes: MolCompleteComponent2).

"Type provide events"
types := MolUtils requiredTypes: MolUsedEvents.
self assert: types size equals: 2.
self assert: (types includes: MolCompleteComponent).
self assert: (types includes: MolCompleteComponent2).

"Type used parameters"
types := MolUtils requiredTypes: MolUsedParameters.
self assert: types size equals: 2.
self assert: (types includes: MolCompleteComponent).
self assert: (types includes: MolCompleteComponent2).
]

{ #category : #tests }
MolUtilsTest >> testShowInformation [
MolUtils showInformation: nil.
Expand Down
52 changes: 52 additions & 0 deletions src/Molecule/MolUtils.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ MolUtils class >> log: aString [
self traceCr: '[Molecule] ' , string
]

{ #category : #'component contract' }
MolUtils class >> offeredTypes: aComponentContractInterface [
"Get types that are part of a component contract offered as Services, Events, or Parameters interface"

| list |
list := Set new.
aComponentContractInterface ifNil:[ ^ list asOrderedCollection ].
aComponentContractInterface isTrait ifFalse:[ ^ list asOrderedCollection ].
(aComponentContractInterface isComponentServices not and:[aComponentContractInterface isComponentEvents not and:[aComponentContractInterface isComponentParameters not]]) ifTrue:[ ^ list asOrderedCollection ].

"Collect all required"
MolComponentType users do:[ :type |
aComponentContractInterface isComponentServices ifTrue:[
(type isProvideServices: aComponentContractInterface) ifTrue:[ list add: type ].
] ifFalse:[
aComponentContractInterface isComponentEvents ifTrue:[
(type isProduceEvents: aComponentContractInterface) ifTrue:[ list add: type ].
] ifFalse:[
(type isProvideParameters: aComponentContractInterface) ifTrue:[ list add: type ].
].
].
].

^ list asOrderedCollection
]

{ #category : #'quick lifecycle' }
MolUtils class >> passivateComponent: aComponentClass named: aName [
"passivate quickly a component"
Expand Down Expand Up @@ -274,6 +300,32 @@ MolUtils class >> removeComponents: aComponentClassList [
].
]

{ #category : #'component contract' }
MolUtils class >> requiredTypes: aComponentContractInterface [
"Get types that are part of a component contract required as Services, Events, or Parameters interface"

| list |
list := Set new.
aComponentContractInterface ifNil:[ ^ list asOrderedCollection ].
aComponentContractInterface isTrait ifFalse:[ ^ list asOrderedCollection ].
(aComponentContractInterface isComponentServices not and:[aComponentContractInterface isComponentEvents not and:[aComponentContractInterface isComponentParameters not]]) ifTrue:[ ^ list asOrderedCollection ].

"Collect all required"
MolComponentType users do:[ :type |
aComponentContractInterface isComponentServices ifTrue:[
(type isUseServices: aComponentContractInterface) ifTrue:[ list add: type ].
] ifFalse:[
aComponentContractInterface isComponentEvents ifTrue:[
(type isConsumeEvents: aComponentContractInterface) ifTrue:[ list add: type ].
] ifFalse:[
(type isUseParameters: aComponentContractInterface) ifTrue:[ list add: type ].
].
].
].

^ list asOrderedCollection
]

{ #category : #log }
MolUtils class >> showInformation: aString [
"Inform the user with a message into the Pharo UI"
Expand Down

0 comments on commit c8b5d0c

Please sign in to comment.