Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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.

Comment thread
jordanmontt marked this conversation as resolved.
(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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to type check in here? assoc value isKindOf: self class is type checking

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, i carried it over from the old dead postCopy code to ensure I didn't unintentionally break any legacy behavior

ig we can fix that using Polymorphism by Just send #copy to everything: self keysAndValuesDo: [ :key :value | self at: key put: value copy ]

ifTrue: [ self at: key put: value copy ] ]
]

{ #category : 'printing' }
Expand Down
Loading