Skip to content

Commit fcf6c00

Browse files
authored
Merge pull request #10 from HossamSaberr/master
Refactor: Clean up dead code and align copy with standard Pharo lifecycle
2 parents 2fbbb85 + e31b3b8 commit fcf6c00

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,29 @@ CTKeyedTreeTest >> testCopy [
215215
self assert: (c includesKey: #new)
216216
]
217217

218+
{ #category : 'tests' }
219+
CTKeyedTreeTest >> testDeepCopyStateNotShared [
220+
| original clone |
221+
original := CTKeyedTree new
222+
at: #config put: (CTKeyedTree new
223+
at: #color put: 'red';
224+
yourself);
225+
yourself.
226+
227+
clone := original copy.
228+
229+
"Verify they have equal values but are different objects in memory before mutation"
230+
self assert: original equals: clone.
231+
self deny: original == clone.
232+
233+
(clone at: #config) at: #color put: 'blue'.
234+
235+
self assert: (original atPath: #(config color)) equals: 'red'.
236+
self assert: (clone atPath: #(config color)) equals: 'blue'.
237+
238+
self deny: (original at: #config) == (clone at: #config).
239+
]
240+
218241
{ #category : 'tests' }
219242
CTKeyedTreeTest >> testDepth [
220243

src/Containers-KeyedTree/CTKeyedTree.class.st

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,6 @@ CTKeyedTree >> collect: aBlock [
268268
^ result
269269
]
270270

271-
{ #category : 'copying' }
272-
CTKeyedTree >> copy [
273-
274-
"Answer a deep copy of the receiver including all subtrees."
275-
276-
| result |
277-
result := self species new.
278-
self keysAndValuesDo: [ :key :value |
279-
result at: key put: (
280-
(value isKindOf: self class)
281-
ifTrue: [ value copy ]
282-
ifFalse: [ value ] ) ].
283-
^ result
284-
]
285-
286271
{ #category : 'accessing' }
287272
CTKeyedTree >> depth [
288273

@@ -496,16 +481,10 @@ CTKeyedTree >> pathsAndValuesDo: aBlock currentPath: pathArray [
496481

497482
{ #category : 'copying' }
498483
CTKeyedTree >> postCopy [
499-
500-
"Ensure proper deep copying of associations and subtrees."
501484

502-
array := array collect: [ :assoc |
503-
assoc ifNotNil: [
504-
Association
505-
key: assoc key
506-
value: ((assoc value isKindOf: self class)
507-
ifTrue: [ assoc value copy ]
508-
ifFalse: [ assoc value ]) ] ]
485+
super postCopy.
486+
self keysAndValuesDo: [ :key :value |
487+
self at: key put: value copy ]
509488
]
510489

511490
{ #category : 'printing' }

0 commit comments

Comments
 (0)