Skip to content

Commit ad2294b

Browse files
committed
adding weak chests
1 parent 8e41eb2 commit ad2294b

9 files changed

+233
-89
lines changed

Chest-Tests/ChestTest.class.st

+63-51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ Class {
77
#category : #'Chest-Tests'
88
}
99

10+
{ #category : #tests }
11+
ChestTest class >> chestClass [
12+
13+
^ Chest
14+
]
15+
16+
{ #category : #tests }
17+
ChestTest >> chestClass [
18+
19+
^ self class chestClass
20+
]
21+
1022
{ #category : #accessing }
1123
ChestTest >> firstChestNameAvailable [
1224

@@ -48,7 +60,7 @@ ChestTest >> tearDown [
4860

4961
{ #category : #tests }
5062
ChestTest >> testAddRemoveToChest [
51-
self testAddRemoveToChest: Chest new.
63+
self testAddRemoveToChest: self chestClass new.
5264
]
5365

5466
{ #category : #tests }
@@ -74,14 +86,14 @@ ChestTest >> testAddRemoveToChest: aChest [
7486
{ #category : #tests }
7587
ChestTest >> testAddRemoveToDefaultChest [
7688
"Tests that the Chest class itself works as a chest"
77-
self testAddRemoveToChest: Chest.
89+
self testAddRemoveToChest: self chestClass.
7890
]
7991

8092
{ #category : #tests }
8193
ChestTest >> testAddingAnObjectTwiceInSameChestWithDifferentNamesRemoveTheOldKeyForThisObject [
8294

8395
| chest object oldName newName |
84-
chest := Chest new.
96+
chest := self chestClass new.
8597
object := Object new.
8698
oldName := chest add: object.
8799

@@ -96,16 +108,16 @@ ChestTest >> testAddingAnObjectTwiceInSameChestWithDifferentNamesRemoveTheOldKey
96108
{ #category : #tests }
97109
ChestTest >> testAlwaysGiveFreshIDToNewChests [
98110
| c1 c2 c3 idList c4 |
99-
c1 := Chest new.
100-
c2 := Chest new.
101-
c3 := Chest new.
111+
c1 := self chestClass new.
112+
c2 := self chestClass new.
113+
c3 := self chestClass new.
102114
self assert: c1 name ~= c2 name.
103115
self assert: c2 name ~= c3 name.
104116
self assert: c1 name ~= c3 name.
105117
idList := OrderedCollection new.
106118
idList add: (c1 name); add: (c2 name); add: (c3 name).
107-
Chest removeChest: c3.
108-
c4 := Chest new.
119+
self chestClass removeChest: c3.
120+
c4 := self chestClass new.
109121
self assert: ((idList includes: c4 name) not).
110122

111123

@@ -115,7 +127,7 @@ ChestTest >> testAlwaysGiveFreshIDToNewChests [
115127
ChestTest >> testAtPutAddsObjectToChestWithCorrectName [
116128

117129
| chest object |
118-
chest := Chest new.
130+
chest := self chestClass new.
119131
object := 42.
120132

121133
self should: [chest at: 'toto'] raise: KeyNotFound.
@@ -129,7 +141,7 @@ ChestTest >> testAtPutAddsObjectToChestWithCorrectName [
129141
ChestTest >> testCannotNameChestWithInvalidName [
130142

131143
| chest |
132-
chest := Chest new.
144+
chest := self chestClass new.
133145

134146
self
135147
should: [ chest name: ' \n\t' ] raise: ChestInvalidNameError;
@@ -142,7 +154,7 @@ ChestTest >> testCannotNameChestWithInvalidName [
142154
ChestTest >> testCannotNameObjectInChestWithInvalidName [
143155

144156
| chest |
145-
chest := Chest new.
157+
chest := self chestClass new.
146158

147159
self
148160
should: [ chest at: ' \n\t' put: Object new ]
@@ -159,7 +171,7 @@ ChestTest >> testCannotNameObjectInChestWithInvalidName [
159171
ChestTest >> testCannotReNameObjectInChestWithInvalidName [
160172

161173
| chest object |
162-
chest := Chest new.
174+
chest := self chestClass new.
163175
object := Object new.
164176

165177
chest at: 'toto' put: object.
@@ -185,13 +197,13 @@ ChestTest >> testChestCreationWithCustomName [
185197
| firstNameAvailable chestWithCustomName |
186198
firstNameAvailable := self firstChestNameAvailable.
187199

188-
self should: [ Chest named: firstNameAvailable ] raise: KeyNotFound.
200+
self should: [ self chestClass named: firstNameAvailable ] raise: KeyNotFound.
189201

190-
chestWithCustomName := Chest newNamed: firstNameAvailable.
202+
chestWithCustomName := self chestClass newNamed: firstNameAvailable.
191203

192204
self assert: chestWithCustomName name equals: firstNameAvailable.
193205
self
194-
assert: (Chest named: firstNameAvailable)
206+
assert: (self chestClass named: firstNameAvailable)
195207
identicalTo: chestWithCustomName
196208
]
197209

@@ -234,21 +246,21 @@ ChestTest >> testChestCreationWithNameThatAlreadyExistsRaisesAnError [
234246
ChestTest >> testChestDictionaryProvidesCopyOfAllChests [
235247

236248
| chestDictionary firstNameAvailable |
237-
chestDictionary := Chest chestDictionary.
249+
chestDictionary := self chestClass chestDictionary.
238250
firstNameAvailable := self firstChestNameAvailable.
239251

240252
chestDictionary at: firstNameAvailable put: 42.
241253

242254
self
243-
should: [ Chest named: firstNameAvailable ]
255+
should: [ self chestClass named: firstNameAvailable ]
244256
raise: KeyNotFound
245257
]
246258

247259
{ #category : #tests }
248260
ChestTest >> testContentsProvidesCopyOfChestContent [
249261

250262
| chest object contents objectInCopy |
251-
chest := Chest new.
263+
chest := self chestClass new.
252264
object := 42.
253265
objectInCopy := 72.
254266

@@ -271,7 +283,7 @@ ChestTest >> testDefaultInstance [
271283
ChestTest >> testEmpty [
272284

273285
| chest |
274-
chest := Chest new.
286+
chest := self chestClass new.
275287
chest
276288
at: 'toto' put: 'toto';
277289
at: 'tata' put: 'tata';
@@ -292,7 +304,7 @@ ChestTest >> testEmpty [
292304
ChestTest >> testEmptyChest [
293305

294306
| chest |
295-
chest := Chest new.
307+
chest := self chestClass new.
296308
chest
297309
at: 'toto' put: 42;
298310
at: 'titi' put: 72;
@@ -308,8 +320,8 @@ ChestTest >> testEmptyChest [
308320
ChestTest >> testGettingChestFromID [
309321
"Tests that retrieving chests by their id works"
310322
| c1 c2 |
311-
c1 := Chest new.
312-
c2 := Chest new.
323+
c1 := self chestClass new.
324+
c2 := self chestClass new.
313325
self assert: (Chest named: c1 name) identicalTo: c1.
314326
self assert: (Chest named: c2 name) identicalTo: c2.
315327
"Tests that accessing a removed chest signals an error"
@@ -329,7 +341,7 @@ ChestTest >> testNotifications [
329341
self assert: l chestRemovedReceived not.
330342

331343
"Test the event on chest creation"
332-
c := Chest new.
344+
c := self chestClass new.
333345
self assert: l newChestReceived.
334346
self assert: l newChest identicalTo: c.
335347

@@ -341,7 +353,7 @@ ChestTest >> testNotifications [
341353
self assert: l newContentsOfUpdatedChest equals: c contents.
342354

343355
"Test the event on chest removal"
344-
Chest removeChest: c.
356+
self chestClass removeChest: c.
345357
self assert: l chestRemovedReceived.
346358
self assert: l removedChest identicalTo: c.
347359
]
@@ -350,47 +362,47 @@ ChestTest >> testNotifications [
350362
ChestTest >> testRemove [
351363

352364
| chest |
353-
chest := Chest new.
365+
chest := self chestClass new.
354366

355-
self assert: (Chest named: chest name) identicalTo: chest.
367+
self assert: (self chestClass named: chest name) identicalTo: chest.
356368

357369
chest remove.
358370

359-
self should: [ Chest named: chest name ] raise: KeyNotFound
371+
self should: [ self chestClass named: chest name ] raise: KeyNotFound
360372
]
361373

362374
{ #category : #tests }
363375
ChestTest >> testRemoveChestNamed [
364376

365377
| chest |
366-
chest := Chest new.
378+
chest := self chestClass new.
367379

368-
self assert: (Chest named: chest name) identicalTo: chest.
380+
self assert: (self chestClass named: chest name) identicalTo: chest.
369381

370-
Chest removeChestNamed: chest name.
382+
self chestClass removeChestNamed: chest name.
371383

372-
self should: [ Chest named: chest name ] raise: KeyNotFound
384+
self should: [ self chestClass named: chest name ] raise: KeyNotFound
373385
]
374386

375387
{ #category : #tests }
376388
ChestTest >> testRemoveChestNamedWithDefaultInstance [
377389

378390
| chest |
379-
chest := Chest defaultInstance.
391+
chest := self chestClass defaultInstance.
380392

381-
self assert: (Chest named: chest name) identicalTo: chest.
393+
self assert: (self chestClass named: chest name) identicalTo: chest.
382394

383-
Chest removeChestNamed: chest name.
395+
self chestClass removeChestNamed: chest name.
384396

385-
self should: [ Chest named: chest name ] raise: KeyNotFound.
386-
self deny: Chest defaultInstance identicalTo: chest
397+
self should: [ self chestClass named: chest name ] raise: KeyNotFound.
398+
self deny: self chestClass defaultInstance identicalTo: chest
387399
]
388400

389401
{ #category : #tests }
390402
ChestTest >> testRemoveObjectNamedWithExistingObject [
391403

392404
| chest |
393-
chest := Chest new.
405+
chest := self chestClass new.
394406
chest
395407
at: 'toto' put: 'toto';
396408
at: 'tata' put: 'tata';
@@ -407,7 +419,7 @@ ChestTest >> testRemoveObjectNamedWithExistingObject [
407419
ChestTest >> testRemoveObjectNamedWithObjectThatDoesNotExist [
408420

409421
| chest |
410-
chest := Chest new.
422+
chest := self chestClass new.
411423
chest
412424
at: 'toto' put: 'toto';
413425
at: 'tata' put: 'tata';
@@ -424,21 +436,21 @@ ChestTest >> testRemoveObjectNamedWithObjectThatDoesNotExist [
424436
ChestTest >> testRemoveWithDefaultInstance [
425437

426438
| chest |
427-
chest := Chest defaultInstance.
439+
chest := self chestClass defaultInstance.
428440

429441
self assert: (Chest named: chest name) identicalTo: chest.
430442

431443
chest remove.
432444

433-
self should: [ Chest named: chest name ] raise: KeyNotFound.
434-
self deny: Chest defaultInstance identicalTo: chest
445+
self should: [ self chestClass named: chest name ] raise: KeyNotFound.
446+
self deny: self chestClass defaultInstance identicalTo: chest
435447
]
436448

437449
{ #category : #tests }
438450
ChestTest >> testRenameObjectInto [
439451

440452
| chest object |
441-
chest := Chest new.
453+
chest := self chestClass new.
442454
object := Object new.
443455
chest at: 'toto' put: object.
444456

@@ -454,7 +466,7 @@ ChestTest >> testRenameObjectInto [
454466
ChestTest >> testRenameObjectIntoRaisesErrorWhenObjectIsNotInChest [
455467

456468
| chest objectToBeRenamed objectInChest |
457-
chest := Chest new.
469+
chest := self chestClass new.
458470
objectToBeRenamed := Object new.
459471
objectInChest := Object new.
460472
chest at: 'tata' put: objectInChest.
@@ -475,7 +487,7 @@ ChestTest >> testRenameObjectIntoRaisesErrorWhenObjectIsNotInChest [
475487
ChestTest >> testRenameObjectIntoRaisesErrorWhenObjectOfSameNameAlreadyExists [
476488

477489
| chest objectToBeRenamed objectAlreadyHavingNewName |
478-
chest := Chest new.
490+
chest := self chestClass new.
479491
objectToBeRenamed := Object new.
480492
objectAlreadyHavingNewName := Object new.
481493
chest at: 'tata' put: objectAlreadyHavingNewName.
@@ -500,33 +512,33 @@ ChestTest >> testRenameObjectIntoRaisesErrorWhenObjectOfSameNameAlreadyExists [
500512
ChestTest >> testRenamingChestChangesItsKeyInDictionary [
501513

502514
| chest firstNameAvailable oldName |
503-
chest := Chest new.
515+
chest := self chestClass new.
504516
oldName := chest name.
505517
firstNameAvailable := self firstChestNameAvailable.
506518

507519
chest name: firstNameAvailable.
508520

509521
self assert: chest name equals: firstNameAvailable.
510-
self assert: (Chest named: firstNameAvailable) identicalTo: chest.
511-
self should: [ Chest named: oldName ] raise: KeyNotFound
522+
self assert: (self chestClass named: firstNameAvailable) identicalTo: chest.
523+
self should: [ self chestClass named: oldName ] raise: KeyNotFound
512524
]
513525

514526
{ #category : #tests }
515527
ChestTest >> testRenamingChestRaisesErrorWhenChestOfSameNameAlreadyExists [
516528

517529
| chestToBeRenamed firstNameAvailable oldName chestAlreadyHavingNewName |
518-
chestToBeRenamed := Chest new.
530+
chestToBeRenamed := self chestClass new.
519531
oldName := chestToBeRenamed name.
520532
firstNameAvailable := self firstChestNameAvailable.
521-
chestAlreadyHavingNewName := Chest newNamed: firstNameAvailable.
533+
chestAlreadyHavingNewName := self chestClass newNamed: firstNameAvailable.
522534

523535
self
524536
should: [ chestToBeRenamed name: firstNameAvailable ]
525537
raise: ChestKeyAlreadyInUseError.
526538

527539
self assert: chestToBeRenamed name equals: oldName.
528540
self
529-
assert: (Chest named: firstNameAvailable)
541+
assert: (self chestClass named: firstNameAvailable)
530542
identicalTo: chestAlreadyHavingNewName.
531-
self assert: (Chest named: oldName) identicalTo: chestToBeRenamed
543+
self assert: (self chestClass named: oldName) identicalTo: chestToBeRenamed
532544
]

Chest-Tests/WeakChestTest.class.st

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Class {
2+
#name : #WeakChestTest,
3+
#superclass : #ChestTest,
4+
#category : #'Chest-Tests'
5+
}
6+
7+
{ #category : #tests }
8+
WeakChestTest >> chestClass [
9+
10+
^ WeakChest
11+
]
12+
13+
{ #category : #tests }
14+
WeakChestTest >> testAddObjectToWeakChest [
15+
16+
| chest object |
17+
chest := WeakChest new.
18+
object := Object new.
19+
20+
chest at: 'toto' put: object.
21+
22+
self assert: (chest at: 'toto') identicalTo: object.
23+
24+
object := nil.
25+
Smalltalk garbageCollect.
26+
27+
self assert: (chest at: 'toto') equals: nil
28+
]
29+
30+
{ #category : #tests }
31+
WeakChestTest >> testWeakDefaultInstance [
32+
33+
self assert: (WeakChest defaultInstance isKindOf: WeakChest)
34+
]

0 commit comments

Comments
 (0)