Skip to content
Permalink
Browse files

Implemented part of the missing API and implemented corresponding tests.

  • Loading branch information...
juliendelplanque committed Jun 25, 2019
1 parent 8ab2c3b commit 5c91facd019e952b993750725bd187f2346db6d9
Showing with 97 additions and 13 deletions.
  1. +51 −0 src/Commander2-Tests/CmCommandGroupTest.class.st
  2. +46 −13 src/Commander2/CmCommandGroup.class.st
@@ -145,6 +145,57 @@ CmCommandGroupTest >> testRegisterCommandWithContextBlock [
self assert: error entryName equals: 'second command' ]
]

{ #category : #test }
CmCommandGroupTest >> testRegisterFirst [
| command oldFirst |
command := CmBlockCommand new
basicName: 'added first command';
block: [ :x | x ];
context: [ 1 ];
yourself.

self deny: group entries first equals: command.
oldFirst := group entries first.

group registerFirst: command.
self assert: group entries first equals: command.
self assert: group entries second equals: oldFirst.
]

{ #category : #test }
CmCommandGroupTest >> testRegisterInsteadOf [
| command |
command := CmBlockCommand new
basicName: 'added first command';
block: [ :x | x ];
context: [ 1 ];
yourself.

self assert: group entries second equals: subGroup1.

group register: command insteadOf: subGroup1.

self assert: group entries second equals: command.
self deny: (group hasGroup: subGroup1)
]

{ #category : #test }
CmCommandGroupTest >> testRegisterLast [
| command oldLast |
command := CmBlockCommand new
basicName: 'last first command';
block: [ :x | x ];
context: [ 1 ];
yourself.

self deny: group entries last equals: command.
oldLast := group entries last.

group registerLast: command.
self assert: group entries last equals: command.
self assert: group entries allButLast last equals: oldLast.
]

{ #category : #test }
CmCommandGroupTest >> testRegisterSubRegister [
| subGroup3 |
@@ -61,11 +61,34 @@ CmCommandGroup >> commands [
^ entries select: [ :entry | entry isCommand ]
]

{ #category : #private }
CmCommandGroup >> ensureNotDuplicated: aCommandOrGroup [
"Check that aCommandOrGroup's #basicName is not already used by one of my entries.
If it is, raises a CmDuplicatedEntryName error.
"
(self hasEntryNamed: aCommandOrGroup basicName)
ifTrue: [ CmDuplicatedEntryName signalEntryNamed: aCommandOrGroup basicName ]
]

{ #category : #'public-api' }
CmCommandGroup >> entries [
^ entries
]

{ #category : #private }
CmCommandGroup >> entriesIndexOf: aCommandOrGroup [
"Returns the index of aCommandOrGroup in my entries.
If aCommandOrGroup is not included in my entries, raises a NotFound error.
"
^ entries
indexOf: aCommandOrGroup
ifAbsent:[
NotFound new
collection: self;
object: aCommandOrGroup;
signal: 'Command or group to replace is not found in the group.' ]
]

{ #category : #'public-api' }
CmCommandGroup >> groups [
^ entries select: [ :entry | entry isGroup ]
@@ -84,6 +107,11 @@ CmCommandGroup >> hasEntryNamed: aString [
^ entries anySatisfy: [ :any | any basicName = aString ]
]

{ #category : #testing }
CmCommandGroup >> hasGroup: aCommandGroup [
^ self groups includes: aCommandGroup
]

{ #category : #initialization }
CmCommandGroup >> initialize [
super initialize.
@@ -93,36 +121,41 @@ CmCommandGroup >> initialize [
]

{ #category : #'public-api' }
CmCommandGroup >> register: aCmCommandsOrGroup [
(self hasEntryNamed: aCmCommandsOrGroup basicName)
ifTrue: [ CmDuplicatedEntryName signalEntryNamed: aCmCommandsOrGroup basicName ].

entries add: aCmCommandsOrGroup
CmCommandGroup >> register: aCommandOrGroup [
self registerLast: aCommandOrGroup
]

{ #category : #'public-api' }
CmCommandGroup >> register: aCmCommandsOrGroup after: anotherLtCommandsOrGroup [
CmCommandGroup >> register: aCommandOrGroup after: anotherCommandOrGroup [
self shouldBeImplemented; flag: #TODO
]

{ #category : #'public-api' }
CmCommandGroup >> register: aCmCommandsOrGroup before: anotherLtCommandsOrGroup [
CmCommandGroup >> register: aCommandOrGroup before: anotherCommandOrGroup [
self shouldBeImplemented; flag: #TODO
]

{ #category : #'public-api' }
CmCommandGroup >> register: aCmCommandsOrGroup insteadOf: anotherLtCommandsOrGroup [
self shouldBeImplemented; flag: #TODO
CmCommandGroup >> register: aCommandOrGroup insteadOf: anotherCommandOrGroup [
| commandToReplaceIndex |
commandToReplaceIndex := self entriesIndexOf: anotherCommandOrGroup.

((self commands collect: #basicName) \ { (entries at: commandToReplaceIndex) } includes: aCommandOrGroup name)
ifTrue: [ CmDuplicatedEntryName signalEntryNamed: aCommandOrGroup name ].

entries at: commandToReplaceIndex put: aCommandOrGroup
]

{ #category : #'public-api' }
CmCommandGroup >> registerFirst: aCmCommandsOrGroup [
self shouldBeImplemented; flag: #TODO
CmCommandGroup >> registerFirst: aCommandOrGroup [
self ensureNotDuplicated: aCommandOrGroup.
entries addFirst: aCommandOrGroup
]

{ #category : #'public-api' }
CmCommandGroup >> registerLast: aCmCommandsOrGroup [
self shouldBeImplemented; flag: #TODO
CmCommandGroup >> registerLast: aCommandOrGroup [
self ensureNotDuplicated: aCommandOrGroup.
entries addLast: aCommandOrGroup
]

{ #category : #'public-api' }

0 comments on commit 5c91fac

Please sign in to comment.
You can’t perform that action at this time.