From 1c7ec483935674946f93af85a8b7409d28006707 Mon Sep 17 00:00:00 2001 From: Carolina Hernandez Date: Thu, 9 Nov 2023 06:37:42 -0300 Subject: [PATCH] Implementation and tests for add and remove classes from orEnvironments --- .../ScopeNodesPresenter.class.st | 2 +- .../ScopeRequestLabel.class.st | 3 + .../ScopesPresenter.class.st | 4 +- .../ScClassA1.class.st | 6 ++ .../ScClassA2.class.st | 6 ++ .../package.st | 1 + .../ScClassB1.class.st | 6 ++ .../package.st | 1 + .../ScopesManagesOperationsTest.class.st | 59 +++++++++++++++++++ src/NewTools-Scopes-Tests/package.st | 1 + .../RBCompositeEnvironment.extension.st | 52 ++++++++++++++-- src/NewTools-Scopes/ScopesManager.class.st | 36 +++++------ 12 files changed, 153 insertions(+), 24 deletions(-) create mode 100644 src/NewTools-Scopes-Resources-A-Tests/ScClassA1.class.st create mode 100644 src/NewTools-Scopes-Resources-A-Tests/ScClassA2.class.st create mode 100644 src/NewTools-Scopes-Resources-A-Tests/package.st create mode 100644 src/NewTools-Scopes-Resources-B-Tests/ScClassB1.class.st create mode 100644 src/NewTools-Scopes-Resources-B-Tests/package.st create mode 100644 src/NewTools-Scopes-Tests/ScopesManagesOperationsTest.class.st create mode 100644 src/NewTools-Scopes-Tests/package.st diff --git a/src/NewTools-Scopes-Editor/ScopeNodesPresenter.class.st b/src/NewTools-Scopes-Editor/ScopeNodesPresenter.class.st index ce8304ba5..36bd4c96d 100644 --- a/src/NewTools-Scopes-Editor/ScopeNodesPresenter.class.st +++ b/src/NewTools-Scopes-Editor/ScopeNodesPresenter.class.st @@ -1,5 +1,5 @@ " -I display a tree with all the available packages and classes to create a scope +I display a tree with all the available packages and classes to create or edit a scope " Class { #name : 'ScopeNodesPresenter', diff --git a/src/NewTools-Scopes-Editor/ScopeRequestLabel.class.st b/src/NewTools-Scopes-Editor/ScopeRequestLabel.class.st index 8c0bf8fcc..d1e539854 100644 --- a/src/NewTools-Scopes-Editor/ScopeRequestLabel.class.st +++ b/src/NewTools-Scopes-Editor/ScopeRequestLabel.class.st @@ -1,3 +1,6 @@ +" +I am a presenter where users enter the name of a new scope +" Class { #name : 'ScopeRequestLabel', #superclass : 'SpRequestTextDialog', diff --git a/src/NewTools-Scopes-Editor/ScopesPresenter.class.st b/src/NewTools-Scopes-Editor/ScopesPresenter.class.st index 77673eeca..a5c4e3f05 100644 --- a/src/NewTools-Scopes-Editor/ScopesPresenter.class.st +++ b/src/NewTools-Scopes-Editor/ScopesPresenter.class.st @@ -1,6 +1,6 @@ " -I am the main Scope Browser presenter. -I show the list of existing scopes in the system and allow to create, edit and delete scopes. +I am the main Scopes Editor presenter. +I show the list of existing scopes in the system and allow users to create, edit and delete scopes. " Class { #name : 'ScopesPresenter', diff --git a/src/NewTools-Scopes-Resources-A-Tests/ScClassA1.class.st b/src/NewTools-Scopes-Resources-A-Tests/ScClassA1.class.st new file mode 100644 index 000000000..6bfa99595 --- /dev/null +++ b/src/NewTools-Scopes-Resources-A-Tests/ScClassA1.class.st @@ -0,0 +1,6 @@ +Class { + #name : 'ScClassA1', + #superclass : 'Object', + #category : 'NewTools-Scopes-Resources-A-Tests', + #package : 'NewTools-Scopes-Resources-A-Tests' +} diff --git a/src/NewTools-Scopes-Resources-A-Tests/ScClassA2.class.st b/src/NewTools-Scopes-Resources-A-Tests/ScClassA2.class.st new file mode 100644 index 000000000..a747ef699 --- /dev/null +++ b/src/NewTools-Scopes-Resources-A-Tests/ScClassA2.class.st @@ -0,0 +1,6 @@ +Class { + #name : 'ScClassA2', + #superclass : 'Object', + #category : 'NewTools-Scopes-Resources-A-Tests', + #package : 'NewTools-Scopes-Resources-A-Tests' +} diff --git a/src/NewTools-Scopes-Resources-A-Tests/package.st b/src/NewTools-Scopes-Resources-A-Tests/package.st new file mode 100644 index 000000000..1e4876d70 --- /dev/null +++ b/src/NewTools-Scopes-Resources-A-Tests/package.st @@ -0,0 +1 @@ +Package { #name : 'NewTools-Scopes-Resources-A-Tests' } diff --git a/src/NewTools-Scopes-Resources-B-Tests/ScClassB1.class.st b/src/NewTools-Scopes-Resources-B-Tests/ScClassB1.class.st new file mode 100644 index 000000000..ea35f29b7 --- /dev/null +++ b/src/NewTools-Scopes-Resources-B-Tests/ScClassB1.class.st @@ -0,0 +1,6 @@ +Class { + #name : 'ScClassB1', + #superclass : 'Object', + #category : 'NewTools-Scopes-Resources-B-Tests', + #package : 'NewTools-Scopes-Resources-B-Tests' +} diff --git a/src/NewTools-Scopes-Resources-B-Tests/package.st b/src/NewTools-Scopes-Resources-B-Tests/package.st new file mode 100644 index 000000000..d683d678c --- /dev/null +++ b/src/NewTools-Scopes-Resources-B-Tests/package.st @@ -0,0 +1 @@ +Package { #name : 'NewTools-Scopes-Resources-B-Tests' } diff --git a/src/NewTools-Scopes-Tests/ScopesManagesOperationsTest.class.st b/src/NewTools-Scopes-Tests/ScopesManagesOperationsTest.class.st new file mode 100644 index 000000000..0c5d79769 --- /dev/null +++ b/src/NewTools-Scopes-Tests/ScopesManagesOperationsTest.class.st @@ -0,0 +1,59 @@ +Class { + #name : 'ScopesManagesOperationsTest', + #superclass : 'TestCase', + #category : 'NewTools-Scopes-Tests', + #package : 'NewTools-Scopes-Tests' +} + +{ #category : 'tests' } +ScopesManagesOperationsTest >> testAddClassAndFillPackageInOrEnvironment [ + + | packageEnv classEnvA1 orEnv | + + "class environment missing one class to complete the full package" + classEnvA1 := RBBrowserEnvironment new forClasses: { ScClassA1 }. + + "empty package environment" + packageEnv := RBBrowserEnvironment new forPackages: { }. + + orEnv := RBOrEnvironment onEnvironment: classEnvA1 or: packageEnv. + + orEnv addClass: ScClassA2. + orEnv addClass: ScClassA2 class. + + self assert: orEnv allClasses equals: { ScClassA1 . ScClassA2 }. + self assert: orEnv packages equals: { ScClassA1 package } +] + +{ #category : 'tests' } +ScopesManagesOperationsTest >> testRemoveClassInClassEnvironmentFromOrEnvironment [ + + | classEnvA1 packageEnvB orEnv | + + classEnvA1 := RBBrowserEnvironment new forClasses: { ScClassA1 }. + packageEnvB := RBBrowserEnvironment new forPackages: { ScClassB1 package }. + + orEnv := RBOrEnvironment onEnvironment: classEnvA1 or: packageEnvB. + orEnv removeClass: ScClassA1. + orEnv removeClass: ScClassA1 class. + + self assert: orEnv allClasses asSet equals: ScClassB1 package definedClasses +] + +{ #category : 'tests' } +ScopesManagesOperationsTest >> testRemoveClassInPackageEnvironmentFromOrEnvironment [ + + | packageEnvA classEnv orEnv | + + "empty class environment" + classEnv := RBBrowserEnvironment new forClasses: { }. + + "environment packageEnvA has two classes: ScClassA1 and ScClassA2" + packageEnvA := RBBrowserEnvironment new forPackages: { ScClassA1 package }. + + orEnv := RBOrEnvironment onEnvironment: classEnv or: packageEnvA. + orEnv removeClass: ScClassA1. + orEnv removeClass: ScClassA1 class. + + self assert: orEnv allClasses equals: { ScClassA2 } +] diff --git a/src/NewTools-Scopes-Tests/package.st b/src/NewTools-Scopes-Tests/package.st new file mode 100644 index 000000000..146f63239 --- /dev/null +++ b/src/NewTools-Scopes-Tests/package.st @@ -0,0 +1 @@ +Package { #name : 'NewTools-Scopes-Tests' } diff --git a/src/NewTools-Scopes/RBCompositeEnvironment.extension.st b/src/NewTools-Scopes/RBCompositeEnvironment.extension.st index bd632c063..00543ad2c 100644 --- a/src/NewTools-Scopes/RBCompositeEnvironment.extension.st +++ b/src/NewTools-Scopes/RBCompositeEnvironment.extension.st @@ -2,8 +2,31 @@ Extension { #name : 'RBCompositeEnvironment' } { #category : '*NewTools-Scopes' } RBCompositeEnvironment >> addClass: aClass [ - self environment addClass: aClass. - self otherEnvironment addClass: aClass + + | envs classEnv packaEnv siblings | + self isTerminal ifFalse: [ + self environment addClass: aClass. + self otherEnvironment addClass: aClass. + ^ self ]. + + "I am terminal. This method assumes that I have one classEnv and one packageEnv" + envs := { + self environment. + self otherEnvironment }. + + classEnv := (envs select: [ :env | env isClassEnvironment ]) anyOne. + packaEnv := (envs select: [ :env | env isPackageEnvironment ]) anyOne. + + "Get the classes in the same package as aClass (siblings)" + siblings := classEnv allClasses select: [ :class | + class package = aClass package ]. + "If the package is now full, then add the package and remove the siblings" + (siblings asSet add: aClass; yourself) size = aClass package definedClasses size ifTrue: [ + siblings do: [ :each | classEnv removeClass: each ]. + packaEnv addPackage: aClass package. + ^ self ]. + + classEnv addClass: aClass ] { #category : '*NewTools-Scopes' } @@ -14,8 +37,29 @@ RBCompositeEnvironment >> addPackage: aPackageName [ { #category : '*NewTools-Scopes' } RBCompositeEnvironment >> removeClass: aClass [ - self environment removeClass: aClass. - self otherEnvironment removeClass: aClass + + | envs classEnv packaEnv packagesToRemove classesToAdd | + self isTerminal ifFalse: [ + self environment removeClass: aClass. + self otherEnvironment removeClass: aClass. + ^ self ]. + + "I am terminal. This method assumes that I have one classEnv and one packageEnv" + envs := { self environment . self otherEnvironment }. + + "Remove aClass from my classEnvironment" + classEnv := (envs select: [ :env | env isClassEnvironment ]) anyOne. + classEnv removeClass: aClass. + + packaEnv := (envs select: [ :env | env isPackageEnvironment ]) anyOne. + + "Packages containing aClass must be removed from the package environment" + packagesToRemove := packaEnv packages select: [ :package | package definedClasses includes: aClass ]. + packagesToRemove do: [ :p | + packaEnv removePackage: p. + classesToAdd := p definedClasses reject: [ :class | class = aClass ]. + classesToAdd do: [ :class | classEnv addClass: class ] ] + ] { #category : '*NewTools-Scopes' } diff --git a/src/NewTools-Scopes/ScopesManager.class.st b/src/NewTools-Scopes/ScopesManager.class.st index 790776e41..87ed85317 100644 --- a/src/NewTools-Scopes/ScopesManager.class.st +++ b/src/NewTools-Scopes/ScopesManager.class.st @@ -193,34 +193,36 @@ ScopesManager >> defaultScopeLabel [ ] { #category : 'editing' } -ScopesManager >> editScope: aScope withNodes: aCollection [ - | classesInScope packagesInScope classes packages fullPackages orphanClasses addedClasses removedClasses addedPackages removedPackages | - - classesInScope := aScope classes. +ScopesManager >> editScope: aScope withNodes: aCollection [ + + | classesInScope packagesInScope classes packages fullPackages orphanClasses addedClasses removedClasses addedPackages removedPackages fullPackagesInScope orphanClassesInScope | + + classesInScope := aScope classes reject: [ :each | each isMeta ]. packagesInScope := aScope packages. - classes := (aCollection select: [ :each | each isClassOrTrait ]) reject: [ - :each | each isMeta ]. - classes := aScope classes asSet addAll: classes; yourself. + fullPackagesInScope := self fullPackagesFrom: classesInScope. + orphanClassesInScope := self orphanClassesIn: classesInScope havingFullPackages: fullPackagesInScope. + classes := (aCollection select: [ :each | each isClassOrTrait ]) + reject: [ :each | each isMeta ]. packages := aCollection select: [ :each | each isKindOf: RPackage ]. - packages := aScope packages asSet addAll: packages; yourself. - + fullPackages := self fullPackagesFrom: classes. orphanClasses := self orphanClassesIn: classes havingFullPackages: fullPackages. - + addedClasses := orphanClasses reject: [ :aClass | classesInScope includes: aClass ]. removedClasses := classesInScope reject: [ :aClass | orphanClasses includes: aClass ]. - - addedPackages := fullPackages reject: [ :aPackage | packagesInScope includes: aPackage ]. - removedPackages := packagesInScope reject: [ :aPackage | fullPackages includes: aPackage ]. - + + addedPackages := fullPackages reject: [ :aPackage | fullPackagesInScope includes: aPackage ]. + removedPackages := fullPackagesInScope reject: [ :aPackage | fullPackages includes: aPackage ]. + addedClasses do: [ :aClass | aScope addClass: aClass ]. removedClasses do: [ :aClass | aScope removeClass: aClass ]. - + addedPackages do: [ :aPackage | aScope addPackage: aPackage name ]. - removedPackages do: [ :aPackage | aScope removePackage: aPackage name ]. - + removedPackages do: [ :aPackage | + aScope removePackage: aPackage name ]. + ^ aScope ]