From d6a62b118d71899c68a67c2555d76974fa0ef167 Mon Sep 17 00:00:00 2001 From: Pierre Laborde <49183340+labordep@users.noreply.github.com> Date: Wed, 2 Oct 2024 00:35:02 +0200 Subject: [PATCH] Implement #140 --- .../MolComponentImplTest.class.st | 29 +++++++++++++++++++ src/Molecule/MolComponentImpl.trait.st | 13 +++++++++ src/Molecule/MolUtils.class.st | 29 ++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/Molecule-Tests/MolComponentImplTest.class.st b/src/Molecule-Tests/MolComponentImplTest.class.st index 56d49ba..c1dca94 100644 --- a/src/Molecule-Tests/MolComponentImplTest.class.st +++ b/src/Molecule-Tests/MolComponentImplTest.class.st @@ -658,6 +658,21 @@ MolComponentImplTest >> testStart2 [ self assert: component componentName equals: #compA. ] +{ #category : #'tests - component creation' } +MolComponentImplTest >> testStartWithGeneratedName [ + + | instance instance2 | + instance := MolCompleteComponentImpl startWithGeneratedName. + self assert: instance componentName notNil. + self assert: instance componentName isSymbol. + + instance2 := MolCompleteComponentImpl startWithGeneratedName. + self assert: instance2 componentName notNil. + self assert: instance2 componentName isSymbol. + self deny: instance componentName equals: instance2 componentName. + +] + { #category : #'tests - component creation' } MolComponentImplTest >> testStartWithName [ @@ -706,6 +721,7 @@ MolComponentImplTest >> testStartWithWrongNames [ { #category : #'tests - component creation' } MolComponentImplTest >> testStop [ + MolCompleteComponentImpl start. MolCompleteComponentImpl stop. self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil. @@ -715,3 +731,16 @@ MolComponentImplTest >> testStop [ self assert: (MolUtils instanceOf: MolCompleteComponentImpl named: #compA) equals: nil. ] + +{ #category : #'tests - component creation' } +MolComponentImplTest >> testStopInstance [ + + | instance | + instance := MolCompleteComponentImpl start. + instance stop. + self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil. + + instance := MolCompleteComponentImpl start: #test. + instance stop. + self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil. +] diff --git a/src/Molecule/MolComponentImpl.trait.st b/src/Molecule/MolComponentImpl.trait.st index 6a210fe..fcde883 100644 --- a/src/Molecule/MolComponentImpl.trait.st +++ b/src/Molecule/MolComponentImpl.trait.st @@ -178,6 +178,12 @@ MolComponentImpl classSide >> start: aComponentName [ ^ MolUtils startComponent: self named: aComponentName ] +{ #category : #'start & stop' } +MolComponentImpl classSide >> startWithGeneratedName [ + + ^ self start: (MolUtils generateComponentNameForType: self componentType) +] + { #category : #'start & stop' } MolComponentImpl classSide >> stop [ @@ -449,3 +455,10 @@ MolComponentImpl >> servicesProviders [ ifNotNil: [ :e | e servicesProviders ] ifNil: [ MolComponentConnector defaultServicesProviders ] ] + +{ #category : #stop } +MolComponentImpl >> stop [ + "Stop me, whatever my name is" + + self class stop: self componentName +] diff --git a/src/Molecule/MolUtils.class.st b/src/Molecule/MolUtils.class.st index 42bee57..ed87158 100644 --- a/src/Molecule/MolUtils.class.st +++ b/src/Molecule/MolUtils.class.st @@ -96,7 +96,7 @@ MolUtils class >> createAllComponents [ components keysDo: [ :aClass | homeServices activateComponent: aClass ] ] -{ #category : #accessing } +{ #category : #name } MolUtils class >> defaultComponentName [ "Used when a component instanciation is not nammed" @@ -131,6 +131,33 @@ MolUtils class >> deployAndInitializeComponents: aComponentClassList [ ^ components ] +{ #category : #private } +MolUtils class >> doRandomComponentNameForType: aComponentType [ + + ^ (aComponentType printString, (Random new next * 10000000) rounded printString) asSymbol + +] + +{ #category : #name } +MolUtils class >> generateComponentNameForType: aComponentType [ + + | instances componentName usedNames found | + aComponentType ifNil: [ ^ nil ]. + componentName := self doRandomComponentNameForType: aComponentType. + found := false. + + "check if the name is not used" + [ found ] whileFalse: [ + instances := self allComponentInstancesOfType: aComponentType. + usedNames := instances collect: [ :c | c componentName ]. + componentName := self doRandomComponentNameForType: aComponentType. + found := usedNames isEmpty + ifTrue: [ true ] + ifFalse: [ (usedNames includes: componentName) not ] ]. + + ^ componentName +] + { #category : #accessing } MolUtils class >> instanceKindOf: aClass [