Skip to content

Commit 3642a62

Browse files
committed
fixed-failing-tests
1 parent e09755a commit 3642a62

2 files changed

Lines changed: 60 additions & 20 deletions

File tree

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ CTKeyedTreeTest >> testAtPathEmpty [
8989
self should: [ emptyTree atPath: #(1) ] raise: self defaultTestError.
9090
]
9191

92-
{ #category : #'tests - at' }
92+
{ #category : #tests }
9393
CTKeyedTreeTest >> testAtPathIfAbsent [
94+
"Test accessing elements using a path with an ifAbsent: block."
95+
9496
self assert: (tree atPath: #(1) ifAbsent: [ #missing ]) equals: firstLevelOneSubTree.
9597
self assert: (tree atPath: #(1 two) ifAbsent: [ #missing ]) equals: 'One-Two'.
9698
self assert: (tree atPath: #(1 three) ifAbsent: [ #missing ]) equals: 'One-Three'.
9799
self assert: (tree atPath: #(2) ifAbsent: [ #missing ]) equals: 'Two'.
98100
self assert: (tree atPath: #(2 4) ifAbsent: [ #missing ]) equals: #missing.
99101
self assert: (tree atPath: #(1 two three) ifAbsent: [ #missing ]) equals: #missing.
100-
self assert: (tree atPath: #(3) ifAbsent: [ #missing ]) equals: #missing.
102+
self assert: (tree atPath: #(3) ifAbsent: [ #missing ]) equals: #missing
101103
]
102104

103105
{ #category : #'tests - at' }
@@ -206,17 +208,21 @@ CTKeyedTreeTest >> testPostCopy [
206208
self deny: (copy at: 1) == subTree.
207209
]
208210

209-
{ #category : #'tests - printing' }
211+
{ #category : #tests }
210212
CTKeyedTreeTest >> testPutFormattedTextOnLevelIndentString [
213+
"Test formatted text output with a custom indent string."
214+
211215
| stream |
212216
stream := String new writeStream.
213217
tree putFormattedTextOn: stream level: 1 indentString: '>>'.
214-
self assert: stream contents equals:
215-
'>>1
218+
self
219+
assert: stream contents
220+
equals:
221+
'>>1
216222
>> #three : ''One-Three''
217223
>> #two : ''One-Two''
218224
>>2 : ''Two''
219-
'.
225+
'
220226
]
221227

222228
{ #category : #'tests - removing' }
@@ -239,16 +245,18 @@ CTKeyedTreeTest >> testRemovePathIfAbsent [
239245
self assert: (tree atPath: #(2) ifAbsent: [ #missing ]) equals: #missing.
240246
]
241247

242-
{ #category : #'tests - operation' }
248+
{ #category : #tests }
243249
CTKeyedTreeTest >> testSortBlock [
250+
"Test the sort block for keys."
251+
244252
| treeWithMixedKeys sortedKeys |
245253
treeWithMixedKeys := CTKeyedTree new
246254
at: 2 put: 'Two';
247255
at: #a put: 'A';
248256
at: 1 put: 'One';
249257
yourself.
250258
sortedKeys := treeWithMixedKeys keys asSortedCollection: treeWithMixedKeys sortBlock.
251-
self assert: sortedKeys asArray equals: #(1 2 #a).
259+
self assert: sortedKeys asArray equals: #(#a 1 2)
252260
]
253261

254262
{ #category : #'tests - operation' }

src/Containers-KeyedTree/CTKeyedTree.class.st

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ CTKeyedTree >> atPath: anArray [
5454

5555
{ #category : #accessing }
5656
CTKeyedTree >> atPath: anArray ifAbsent: aBlock [
57-
"Answer the element referenced by the given key path. Answer the value of aBlock if not found."
5857
| element |
5958
element := self.
60-
anArray do: [ :key | element := element at: key ifAbsent: [ ^aBlock value ] ].
59+
anArray do: [ :key |
60+
(element isKindOf: self class)
61+
ifTrue: [ element := element at: key ifAbsent: [ ^aBlock value ] ]
62+
ifFalse: [ ^aBlock value ] ].
6163
^ element
6264
]
6365

@@ -117,15 +119,25 @@ CTKeyedTree >> postCopy [
117119

118120
{ #category : #printing }
119121
CTKeyedTree >> putFormattedTextOn: aStream level: indentLevel indentString: aString [
120-
"Place a description of the receiver on the given stream with the given indentation level."
121-
(self keys asSortedCollection: self sortBlock) do: [ :k | | v |
122-
indentLevel timesRepeat: [ aStream nextPutAll: aString ].
123-
aStream nextPutAll: k printString.
124-
v := self at: k.
125-
(v isKindOf: self class)
126-
ifTrue: [ aStream cr.
122+
"Write a textual representation of the receiver to aStream, indenting to indentLevel using aString as the indent string."
123+
124+
(self keys asSortedCollection: self sortBlock)
125+
do: [ :k |
126+
| v |
127+
indentLevel = 0
128+
ifFalse: [
129+
indentLevel = 1
130+
ifTrue: [ aStream nextPutAll: aString ]
131+
ifFalse: [ indentLevel - 1 timesRepeat: [ aStream nextPutAll: aString ]. aStream nextPutAll: ' ' ] ].
132+
aStream nextPutAll: k printString.
133+
v := self at: k.
134+
(v isKindOf: self class)
135+
ifTrue: [ aStream cr.
127136
v putFormattedTextOn: aStream level: indentLevel + 1 indentString: aString ]
128-
ifFalse: [ aStream nextPutAll: ' : '; nextPutAll: v printString. aStream cr ] ]
137+
ifFalse: [ aStream
138+
nextPutAll: ' : ';
139+
nextPutAll: v printString.
140+
aStream cr ] ]
129141
]
130142

131143
{ #category : #removing }
@@ -146,12 +158,32 @@ CTKeyedTree >> removePath: anArray ifAbsent: aBlock [
146158

147159
{ #category : #accessing }
148160
CTKeyedTree >> sortBlock [
149-
"Answer the block to sort tree keys with."
150-
^ [ :a :b | [ a <= b ] on: Error do: [ a class name <= b class name ] ]
161+
"Answer a sort block that can be used to sort the keys of the receiver."
162+
163+
^ [ :a :b |
164+
a class = b class
165+
ifTrue: [ a <= b ]
166+
ifFalse: [
167+
(a isSymbol and: [ b isNumber ])
168+
ifTrue: [ true ]
169+
ifFalse: [
170+
(b isSymbol and: [ a isNumber ])
171+
ifTrue: [ false ]
172+
ifFalse: [ a class name <= b class name ] ] ] ]
151173
]
152174

153175
{ #category : #accessing }
154176
CTKeyedTree >> subtrees [
155177
"Answer the subtrees of the receiver."
156178
^ (self select: [ :v | v isKindOf: CTKeyedTree ]) values
179+
]
180+
181+
{ #category : #tests }
182+
CTKeyedTreeTest >> testFormattedText [
183+
"Test the formatted text representation of the tree."
184+
185+
self assert: self t13 formattedText equals:
186+
'1 : ''1-3-1''
187+
2 : ''1-3-2''
188+
'
157189
]

0 commit comments

Comments
 (0)