Skip to content
Open
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
19 changes: 19 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,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 [

Expand Down
28 changes: 4 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,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' }
Expand Down