Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Containers-KeyedTree-Tests/CTKeyedTreeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,29 @@ 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.

"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'.
self assert: (clone atPath: #(config color)) equals: 'blue'.

self deny: (original at: #config) == (clone at: #config).
]

{ #category : 'tests' }
CTKeyedTreeTest >> testDepth [

Expand Down
27 changes: 3 additions & 24 deletions src/Containers-KeyedTree/CTKeyedTree.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 [

Expand Down Expand Up @@ -496,16 +481,10 @@ 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 |
self at: key put: value copy ]
]

{ #category : 'printing' }
Expand Down
Loading