[Newbies] Creating a SUnit Test for A Morphic Object

squeak414 at free.fr squeak414 at free.fr
Tue Jun 3 20:24:09 UTC 2008


Quoting Jason Burke <burkej71 at gmail.com>:

> Hello All,
>
> I'm new to the list here, and I have a question on creating SUnit tests for
> Morphs. In my latest bi-annual "Squeak Peek", something clicked that hadn't
> in the past 3 years of periodically checking into Squeak: It started to make
> sense (I usually give up in frustration after a week or so, but the, "Squeak
> by Example" book has really helped this time. Kudos to the writers).
>
> So, I started digging around in the Morphic classes, and I stumbled across,
> what appeared to be, a bug in LedDigitMorph. I played around with it until I
> felt satisfied that it was indeed a bug, and then submitted a report to
> bugs.squeak.org. The powers that be agreed that this looked like a bug, and
> asked me to do a little more footwork on it (I had submitted my fix with the
> report). However, I'm not quite sure how to accomplish what was requested.
>
> Here's the bug (mantis ID: 0007067), and a test case:
>
> LedDigitMorph does not change its state after receiving a digit: message.
> You have to click on the morph before it changes. The test case is:
>
> myDigit := LedDigitMorph new openInWorld.
> myDigit digit: 2.
>
> The morph will not show the number 2 until you click on it.
>
> I solved this bug in my image by changing the digit: message to this:
>
> digit: anInteger
>
>     digit _ anInteger \\ 10. "make sure it stays between 0 and 9"
>     self changed.
>
> The response I got back asked me to create a SUnit test for this issue (One
> that fails before the patch and passes afterwards).
>
> Unfortunately, I'm not sure how to do this (though I'd be happy to continue
> to work on it). Can someone give me some guidance on how to go about
> creating SUnit test cases for this Morphic object?
>
> Thanks,
>
> Jason

Hi Jason, I'm a newbie too, but thought I'd have a go at this.

Looking at the changed method, it passes up through its owner morphs the
rectangle that needs redrawing, which are then checked by the WorldState. So you
can test that the world has a redraw needed:

DigitTest >> testDigit
	| myDigit worldState |
	worldState := WorldState allInstances first.
	myDigit := LedDigitMorph new openInWorld.
	worldState doOneSubCycleFor: World world.  "clear out any redraws needed"
	myDigit digit: 2.
	self
		should: [worldState checkIfUpdateNeeded].

This works. It will need someone more experienced than me to judge its
(in)elegance, or whether it risks doing anything nasty in particular cases.

Cheers,    ...Stan






More information about the Beginners mailing list