Skip to content

Commit

Permalink
Fix bug in protocol management of Traits
Browse files Browse the repository at this point in the history
It happens currently that some methods copied from traits are not well categorized with their protocols.

This happens because we add the Traits methods in the class we are compiling before we copy the old protocols of the class.

I'm fixing the problem be copying the old protocols sooner and I'm adding a regression test that was failing before and is now passing.

This should fix the CI failures of pharo-spec/Spec#1565 and pharo-spec/Spec#1573
  • Loading branch information
jecisc committed Jul 23, 2024
1 parent 4da0589 commit b4b9e6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Shift-ClassBuilder/ShiftClassBuilder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ ShiftClassBuilder >> build [
self createMetaclass.
self createClass.

self oldClass ifNotNil: [
self copyProtocols.
self newClass commentSourcePointer: self oldClass commentSourcePointer ].

self createSharedVariables.

self installSlotsAndVariables.
Expand Down Expand Up @@ -236,6 +232,11 @@ ShiftClassBuilder >> createClass [
slots: (self withAdditionalSlots: self slots).

newClass environment: self installingEnvironment.

"Since Traits might add some methods in #classCreated:, we need to copy the protocols before we add them."
self oldClass ifNotNil: [
self copyProtocols.
self newClass commentSourcePointer: self oldClass commentSourcePointer ].

self builderEnhancer classCreated: self
]
Expand Down
20 changes: 20 additions & 0 deletions src/Traits-Tests/ShTraitInstallerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ Class {
#tag : 'ShiftClassInstaller'
}

{ #category : 'tests' }
ShTraitInstallerTest >> testAddingATraitToAClassHasRightProtocolsOnMetaclass [
"Regression test. Some methods were copied on the method dic of a metaclass but the protocols were lost"

newClass := ShiftClassInstaller make: [ :builder |
builder
name: #SHClass;
package: self generatedClassesPackageName ].

newClass := ShiftClassInstaller make: [ :builder |
builder
name: #SHClass;
traits: { TViewModelMock };
package: self generatedClassesPackageName ].

self assert: (newClass methods allSatisfy: [ :method | method protocol isNotNil ]).
self assert: (newClass class methods allSatisfy: [ :method | method protocol isNotNil ]).
self assert: (newClass class class methods allSatisfy: [ :method | method protocol isNotNil ])
]

{ #category : 'tests' }
ShTraitInstallerTest >> testCreatingFullTraitHasAllElements [

Expand Down

0 comments on commit b4b9e6b

Please sign in to comment.