Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Extract abstract assertion from test into postcondition
Last updated at 12:28 pm UTC on 17 January 2006
The trick is, to _keep_ the tests, though they don't have any assertions inside them, as long as their directly tested methods have some assertions, they are still tests.
And suddenly one test might not only test one but several assertions. And our assertions are called in somehow unexpected circumstances. And they document the code.

An example

In Squeak 3.7 we have the following
Random >> next
	"Answer a random Float in the interval [0 to 1)."
	^ (seed:=self nextValue) / m

One could replace the comment with some active code from
RandomTest >> testNext
	10000 timesRepeat: [
			| next |
			next := gen next.
			self assert: (next >= 0).
			self assert: (next < 1)]

and end up with:
Random >> next
	|aResult|
	aResult:=	(seed:=self nextValue) / m.
	self assert: [aResult >= 0].
	self assert: [aResult < 1].
	^aResult

and
RandomTest >> testNext
	10000 timesRepeat: [gen next]


If you use Vassili's code for assertions, that will become :
Random >> next
	|aResult|
	aResult:=	(seed:=self nextValue) / m.
	[aResult >= 0] assert.
	[aResult < 1] assert.
	^aResult

Incidentely I now understand better your Extract to result stuff ... see the power
of "explanation examples" ? ;-) – Romain