From d947902f2f7e19c4b2c39ca5a7c51d45963357f2 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Wed, 25 Feb 2026 01:49:46 +0200 Subject: [PATCH 1/3] Refactor: standardize copy/postCopy lifecycle --- src/Containers-KeyedTree/CTKeyedTree.class.st | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/Containers-KeyedTree/CTKeyedTree.class.st b/src/Containers-KeyedTree/CTKeyedTree.class.st index 38cb423..d5959be 100644 --- a/src/Containers-KeyedTree/CTKeyedTree.class.st +++ b/src/Containers-KeyedTree/CTKeyedTree.class.st @@ -268,21 +268,6 @@ CTKeyedTree >> collect: aBlock [ ^ result ] -{ #category : 'copying' } -CTKeyedTree >> copy [ - - "Answer a deep copy of the receiver including all subtrees." - - | result | - result := self species new. - self keysAndValuesDo: [ :key :value | - result at: key put: ( - (value isKindOf: self class) - ifTrue: [ value copy ] - ifFalse: [ value ] ) ]. - ^ result -] - { #category : 'accessing' } CTKeyedTree >> depth [ @@ -496,16 +481,11 @@ CTKeyedTree >> pathsAndValuesDo: aBlock currentPath: pathArray [ { #category : 'copying' } CTKeyedTree >> postCopy [ - - "Ensure proper deep copying of associations and subtrees." - array := array collect: [ :assoc | - assoc ifNotNil: [ - Association - key: assoc key - value: ((assoc value isKindOf: self class) - ifTrue: [ assoc value copy ] - ifFalse: [ assoc value ]) ] ] + super postCopy. + self keysAndValuesDo: [ :key :value | + (value isKindOf: self class) + ifTrue: [ self at: key put: value copy ] ] ] { #category : 'printing' } From 0389e2d28706692c2c75a579be17a63f752f2857 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Thu, 26 Feb 2026 17:51:35 +0200 Subject: [PATCH 2/3] Test: Add deep copy state isolation test --- .../CTKeyedTreeTest.class.st | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st b/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st index 55aac9f..f3fd7ee 100644 --- a/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st +++ b/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st @@ -215,6 +215,25 @@ CTKeyedTreeTest >> testCopy [ self assert: (c includesKey: #new) ] +{ #category : 'tests' } +CTKeyedTreeTest >> testDeepCopyStateNotShared [ + | original clone | + original := CTKeyedTree new + at: #config put: (CTKeyedTree new + at: #color put: 'red'; + yourself); + yourself. + + clone := original copy. + + (clone at: #config) at: #color put: 'blue'. + + self assert: (original atPath: #(config color)) equals: 'red'. + self assert: (clone atPath: #(config color)) equals: 'blue'. + + self deny: (original at: #config) == (clone at: #config). +] + { #category : 'tests' } CTKeyedTreeTest >> testDepth [ From e31b3b820bca3b7a7a3ba9afe630739df39bb6d0 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Wed, 18 Mar 2026 15:32:46 +0200 Subject: [PATCH 3/3] Refactor: Remove type checking --- src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st | 4 ++++ src/Containers-KeyedTree/CTKeyedTree.class.st | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st b/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st index f3fd7ee..11c363e 100644 --- a/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st +++ b/src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st @@ -226,6 +226,10 @@ CTKeyedTreeTest >> testDeepCopyStateNotShared [ clone := original copy. + "Verify they have equal values but are different objects in memory before mutation" + self assert: original equals: clone. + self deny: original == clone. + (clone at: #config) at: #color put: 'blue'. self assert: (original atPath: #(config color)) equals: 'red'. diff --git a/src/Containers-KeyedTree/CTKeyedTree.class.st b/src/Containers-KeyedTree/CTKeyedTree.class.st index d5959be..4fc0b39 100644 --- a/src/Containers-KeyedTree/CTKeyedTree.class.st +++ b/src/Containers-KeyedTree/CTKeyedTree.class.st @@ -484,8 +484,7 @@ CTKeyedTree >> postCopy [ super postCopy. self keysAndValuesDo: [ :key :value | - (value isKindOf: self class) - ifTrue: [ self at: key put: value copy ] ] + self at: key put: value copy ] ] { #category : 'printing' }