Skip to content

Commit

Permalink
- add tests for deletion of slots from a Trait: #testChangingDeleteSl…
Browse files Browse the repository at this point in the history
…ot and #testChangingDeleteSlot2Slots

- TaRemoveSlot: change TaRemoveSlot to use a collection of vars to delete from the Trait (as it can define multiple slots)
- add CDTraitSlotRemoveNode to make the classparser work

fixes pharo-project#8503
  • Loading branch information
MarcusDenker committed Feb 9, 2021
1 parent 391cb13 commit bcb92a1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/ClassParser/CDAbstractTraitCompositionNode.class.st
Expand Up @@ -23,6 +23,14 @@ CDAbstractTraitCompositionNode >> - aCollection [
exclusions: aCollection
]

{ #category : #composing }
CDAbstractTraitCompositionNode >> -- aCollection [

^ CDTraitSlotRemoveNode new
subject: self;
removedSlots: aCollection
]

{ #category : #composing }
CDAbstractTraitCompositionNode >> @ aCollection [

Expand Down
28 changes: 28 additions & 0 deletions src/ClassParser/CDTraitSlotRemoveNode.class.st
@@ -0,0 +1,28 @@
"
MyTrait -- {#slotName}
"
Class {
#name : #CDTraitSlotRemoveNode,
#superclass : #CDTraitCompositionNode,
#instVars : [
'removedSlots'
],
#category : #'ClassParser-Model'
}

{ #category : #accessing }
CDTraitSlotRemoveNode >> removedSlots [

^ removedSlots
]

{ #category : #accessing }
CDTraitSlotRemoveNode >> removedSlots: anObject [

removedSlots := anObject
]

{ #category : #printing }
CDTraitSlotRemoveNode >> toString [
^ subject name ,' -- ', removedSlots asString.
]
8 changes: 8 additions & 0 deletions src/ClassParser/CDTraitSlotRenameNode.class.st
@@ -1,3 +1,6 @@
"
MyTrait @@ {#ivar -> ivar2 }
"
Class {
#name : #CDTraitSlotRenameNode,
#superclass : #CDTraitCompositionNode,
Expand All @@ -18,3 +21,8 @@ CDTraitSlotRenameNode >> renames: anObject [

renames := anObject
]

{ #category : #printing }
CDTraitSlotRenameNode >> toString [
^ subject name ,' @@ ', renames asString.
]
40 changes: 40 additions & 0 deletions src/TraitsV2-Tests/T2TraitWithAliasTest.class.st
Expand Up @@ -39,6 +39,46 @@ T2TraitWithAliasTest >> testChangingAnAliasedMethodIsUpdated [

]

{ #category : #tests }
T2TraitWithAliasTest >> testChangingDeleteSlot [

| t1 c1 |

t1 := self newTrait: #T1 with: #(ivar).
t1 compile: 'ivar ^ ivar'.

c1 := self newClass: #C1 with: #() uses: t1 -- {#ivar}.

"we got the method #ivar from the Trait"
self assert: (c1 includesSelector: #ivar).
"the slot that we got from the trait is now removed"
self assert: c1 slotNames equals: #().
"make sure to not modify the Trait"
self assert: t1 slotNames equals: #(ivar).
]

{ #category : #tests }
T2TraitWithAliasTest >> testChangingDeleteSlot2Slots [

| t1 c1 |

t1 := self newTrait: #T1 with: #(ivar1 ivar2).
t1 compile: 'ivar2 ^ ivar2'.
t1 compile: 'ivar1 ^ ivar1'.

c1 := self newClass: #C1 with: #() uses: t1 -- { #ivar1 . #ivar2 }.

"we got the methods from the Trait"
self assert: (c1 includesSelector: #ivar1).
self assert: (c1 includesSelector: #ivar2).

"the slot that we got from the trait is now removed"
self assert: c1 slotNames equals: #().
"make sure to not modify the Trait"
self assert: t1 slotNames equals: #(ivar1 ivar2).

]

{ #category : #tests }
T2TraitWithAliasTest >> testChangingRenamedSlot [

Expand Down
4 changes: 2 additions & 2 deletions src/TraitsV2/TaAbstractComposition.class.st
Expand Up @@ -41,11 +41,11 @@ TaAbstractComposition >> - anArrayOrASymbol [
]

{ #category : #operations }
TaAbstractComposition >> -- anSlotName [
TaAbstractComposition >> -- anArrayOfSlotNames [

"I create a new trait composition element, from me but without the slot named as the parameter"

^ TaRemoveSlot named: anSlotName from: self.
^ TaRemoveSlot remove: anArrayOfSlotNames from: self.
]

{ #category : #comparing }
Expand Down
29 changes: 17 additions & 12 deletions src/TraitsV2/TaRemoveSlot.class.st
Expand Up @@ -7,42 +7,47 @@ Class {
#name : #TaRemoveSlot,
#superclass : #TaSingleComposition,
#instVars : [
'slotName',
'renames'
'removedSlots'
],
#category : #'TraitsV2-Compositions'
}

{ #category : #'instance creation' }
TaRemoveSlot class >> named: anSlotName from: aTrait [
TaRemoveSlot class >> remove: anArrayOfSlotNames from: aTrait [
^ self new
slotName: anSlotName;
removedSlots: anArrayOfSlotNames;
inner: aTrait;
yourself.
]

{ #category : #copying }
TaRemoveSlot >> copyTraitExpression [
^ self class named: slotName from: inner.
^ self class remove: removedSlots from: inner
]

{ #category : #accessing }
TaRemoveSlot >> slotName [
^ slotName
TaRemoveSlot >> removedSlots [

^ removedSlots
]

{ #category : #accessing }
TaRemoveSlot >> slotName: anObject [
slotName := anObject
TaRemoveSlot >> removedSlots: anObject [

removedSlots := anObject
]

{ #category : #accessing }
TaRemoveSlot >> slots [
^ inner slots reject: [ :e | e name = slotName ]
^ inner slots reject: [ :e | removedSlots includes: e name ]
]

{ #category : #printing }
TaRemoveSlot >> traitCompositionExpression [

^ (self inner traitCompositionExpressionWithParens , ' -- ' , slotName printString) asString
^ String
streamContents: [ :s |
s
nextPutAll: self inner traitCompositionExpressionWithParens;
nextPutAll: ' -- '.
removedSlots printAsSelfEvaluatingFormOn: s ]
]
4 changes: 2 additions & 2 deletions src/TraitsV2/Trait.class.st
Expand Up @@ -200,9 +200,9 @@ Trait >> - anArray [
]

{ #category : #accessing }
Trait >> -- aSlotName [
Trait >> -- aSlotNameCollection [

^ self asTraitComposition -- aSlotName
^ self asTraitComposition -- aSlotNameCollection
]

{ #category : #accessing }
Expand Down

0 comments on commit bcb92a1

Please sign in to comment.