'From Squeak3.8alpha of ''17 July 2004'' [latest update: #6302] on 7 October 2004 at 4:27:12 pm'! "Change Set: ifNotEmptyDo-md v3 Date: 7 October 2004 Author: Marcus Denker v3: added unit tests for new empty-related methods (st) v2: added ifNotEmptyDo:ifEmpty and ifEmpty:ifNotEmptyDo:, for consistency this adds ifNil:ifNotNilDo: ifNotNilDo:ifNil: to Object and UndfinedObject Requires: ValueWithPossibleArg-md. In the spirit of ifNotNilDo: this adds an ifEmptyDo: to be used as an alternative to ifNotEmpty:. Fixes the comments of ifNotEmpty:, ifNotEmpy:ifEmpty and ifEmpty:ifNotEmpty: to describe that it can take an 1-arg block. The interface of ifNotNil and variants will be fixed after we have the new compiler, as those are compiled inline and need a compiler change ifNotEmpty: and the rest are changed to use valueWithPossibleArgument:, which should be somewhat faster. " ! TestCase subclass: #CollectionTest instanceVariableNames: 'empty nonEmpty' classVariableNames: '' poolDictionaries: '' category: 'Collections-Abstract-Tests'! !CollectionTest commentStamp: '' prior: 0! A TestCase is a Command representing the future running of a test case. Create one with the class method #selector: aSymbol, passing the name of the method to be run when the test case runs. When you discover a new fixture, subclass TestCase, declare instance variables for the objects in the fixture, override #setUp to initialize the variables, and possibly override# tearDown to deallocate any external resources allocated in #setUp. When you are writing a test case method, send #assert: aBoolean when you want to check for an expected value. For example, you might say "self assert: socket isOpen" to test whether or not a socket is open at a point in a test.! !Object methodsFor: 'accessing' stamp: 'md 10/7/2004 15:43'! ifNil: nilBlock ifNotNilDo: aBlock "Evaluate aBlock with the receiver as its argument." ^ aBlock value: self ! ! !Object methodsFor: 'accessing' stamp: 'md 10/7/2004 15:43'! ifNotNilDo: aBlock ifNil: nilBlock "Evaluate aBlock with the receiver as its argument." ^ aBlock value: self ! ! !Collection methodsFor: 'testing' stamp: 'md 10/7/2004 14:49'! ifEmpty: emptyBlock ifNotEmpty: notEmptyBlock "Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise" " If the notEmptyBlock has an argument, eval with the receiver as its argument" ^ self isEmpty ifTrue: emptyBlock ifFalse: [notEmptyBlock valueWithPossibleArgument: self]! ! !Collection methodsFor: 'testing' stamp: 'md 10/7/2004 15:36'! ifEmpty: emptyBlock ifNotEmptyDo: notEmptyBlock "Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise" "Evaluate the notEmptyBlock with the receiver as its argument" ^ self isEmpty ifTrue: emptyBlock ifFalse: [notEmptyBlock value: self]! ! !Collection methodsFor: 'testing' stamp: 'md 10/7/2004 14:58'! ifNotEmpty: aBlock "Evaluate the given block unless the receiver is empty. If the block has an argument, eval with the receiver as its argument, but it might be better to use ifNotEmptyDo: to make the code easier to understand" ^self isEmpty ifFalse: [aBlock valueWithPossibleArgument: self]. ! ! !Collection methodsFor: 'testing' stamp: 'md 10/7/2004 14:48'! ifNotEmpty: notEmptyBlock ifEmpty: emptyBlock "Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise If the notEmptyBlock has an argument, eval with the receiver as its argument" ^ self isEmpty ifFalse: [notEmptyBlock valueWithPossibleArgument: self] ifTrue: emptyBlock! ! !Collection methodsFor: 'testing' stamp: 'md 10/7/2004 14:28'! ifNotEmptyDo: aBlock "Evaluate the given block with the receiver as its argument." ^self isEmpty ifFalse: [aBlock value: self]. ! ! !Collection methodsFor: 'testing' stamp: 'md 10/7/2004 15:36'! ifNotEmptyDo: notEmptyBlock ifEmpty: emptyBlock "Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise Evaluate the notEmptyBlock with the receiver as its argument" ^ self isEmpty ifFalse: [notEmptyBlock value: self] ifTrue: emptyBlock! ! !CollectionTest methodsFor: 'initialize-release' stamp: 'st 10/7/2004 16:23'! setUp empty := Set new. nonEmpty := OrderedCollection with: #x! ! !CollectionTest methodsFor: 'testing' stamp: 'st 10/7/2004 16:23'! testIfEmptyifNotEmpty self assert: (empty ifEmpty: [true] ifNotEmpty: [false]). self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]). self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x])! ! !CollectionTest methodsFor: 'testing' stamp: 'st 10/7/2004 16:23'! testIfEmptyifNotEmptyDo self assert: (empty ifEmpty: [true] ifNotEmptyDo: [:s | false]). self assert: (nonEmpty ifEmpty: [false] ifNotEmptyDo: [:s | s first = #x])! ! !CollectionTest methodsFor: 'testing' stamp: 'st 10/7/2004 16:24'! testIfNotEmpty empty ifNotEmpty: [self assert: false]. self assert: (nonEmpty ifNotEmpty: [self]) == self. self assert: (nonEmpty ifNotEmpty: [:s | s first]) = #x ! ! !CollectionTest methodsFor: 'testing' stamp: 'st 10/7/2004 16:24'! testIfNotEmptyDo empty ifNotEmptyDo: [:s | self assert: false]. self assert: (nonEmpty ifNotEmptyDo: [:s | s first]) = #x ! ! !CollectionTest methodsFor: 'testing' stamp: 'st 10/7/2004 16:24'! testIfNotEmptyDoifNotEmpty self assert: (empty ifNotEmptyDo: [:s | false] ifEmpty: [true]). self assert: (nonEmpty ifNotEmptyDo: [:s | s first = #x] ifEmpty: [false])! ! !CollectionTest methodsFor: 'testing' stamp: 'st 10/7/2004 16:24'! testIfNotEmptyifEmpty self assert: (empty ifEmpty: [true] ifNotEmpty: [false]). self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]). self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x])! ! !UndefinedObject methodsFor: 'testing' stamp: 'md 10/7/2004 15:41'! ifNil: nilBlock ifNotNilDo: ifNotNilBlock "Evaluate the block for nil because I'm == nil" ^ nilBlock value! ! !UndefinedObject methodsFor: 'testing' stamp: 'md 10/7/2004 15:39'! ifNotNilDo: ifNotNilBlock ifNil: nilBlock "If I got here, I am nil, so evaluate the block nilBlock" ^ nilBlock value! ! CollectionTest removeSelector: #tearDown!