views:

302

answers:

4

Tryng out some smalltalk + TDD + "good practices" I've run into a kinda ugly block:

How do I do an assertion in GNU Smalltalk?

I'm just looking for a simple ifFalse: [Die] kind of thing

+2  A: 

This is the code for assert: from Squeak (which I recommend you use rather than GNU):

assert: aBlock 
    "Throw an assertion error if aBlock does not evaluates to true."
    aBlock value
     ifFalse: [AssertionFailure signal: 'Assertion failed']
anon
Why would you not recommend the GNU version?
Eyvind
Lack of support - squeak has a more involved community, plus Squeak is more fun.
anon
More support comes from more people using it. And GNU Smalltalk is actually evolving nicely. There are quite some people behind, with Paolo Bonzini as main force.
Janko Mivšek
Dont listen to the die-hard Squeakers, they just haven't seen a file system in years :) You can have fun with any Smalltalk.
Adrian
A: 

It is simple. In your test methods you write:

self assert: 1 + 1 = 2

But first you need to create a test class as a subclass of TestCase (in Squeak), for example:

TestCase subclass: #MyTest

Here you write testing methods, which names must always start with 'test', for instance :

testBasicArithmetics

self assert: 1 + 1 = 2
Janko Mivšek
Plainly doing that reports that "Matrix does not understand #assert:".
Tordek
Yes, you need to make a test class of course and put assertions in test methods there. See my update.
Janko Mivšek
Sorry, it was a misunderstanding: I don't mean unit-test assertions, but design-by-contract assertions. (IE, pre/post-condition checking within the method.)
Tordek
+2  A: 

as well as self assert: [ ... some block ]

works for blocks & non-blocks, since sending #value to Object returns self.

Igor Stasenko
A: 

It has been suggested above to add #assert: to Object, but rather I'd add #assert to BlockClosure (or whatever [] class is in GNU Smalltalk).

assert
    this value ifFalse: [AssertionFailure signal: 'Assertion failed']

and thus use as in

[ value notNil ] assert.
[ value > 0 ] assert.
[ list isEmpty not ] assert.

etcetera.

Adrian