Tordek
Tordek

Reputation: 10872

(Mis)Understanding Smalltalk and TDD

I'm trying to learn Smalltalk by doing, so I'm getting a grip on the syntax and style by buiding a simple "Matrix" class.

First of all, I'd be grateful if linked to a good Smalltak tutorial (although this is totally optional), preferably one that doesn't involve using the GUIs (I'd rather type my .sts than fish around the hierarchy explorer to put the methods in their place).

Then, on TDD: For this project I'm calling gst-sunit -f matrix.st -f matrix-test.st BaseMatrixTests, and there's bound to be a better way than that. Is there?

And finally, on asserts: I'm trying to write a method and put asserts within, eg.:

Matrix>>multiplyBy: anotherMatrix [
    [ self isNotEmpty ] assert.
    "Do Multiplication"
    [ result dimensions = (self height)@(anotherMatrix width) ] assert.
]

How can I do that kind of asserts?

Edit: Questions marked explicitly.

Upvotes: 7

Views: 2517

Answers (10)

Alan Lovejoy
Alan Lovejoy

Reputation:

Smalltalk Primer is an introduction to The Essentials of Message-Oriented Programming with Smalltalk

Upvotes: 0

Kristoff vdH
Kristoff vdH

Reputation: 234

I'd rather type my .sts than fish around the hierarchy explorer to put the methods in their place).

You say this now and I felt the same way until I invested some time into learning how to use these tools.

And I say this as a current user of Vim and former user of Emacs. I really have become quite productive using the class browser and other tools in Pharo now that I've learned the ropes.

That being said, if you really want to go your own way, GNU Smalltalk is the right choice for you.

I would like to point out at this point though that at least Pharo has a Matrix class which you can use for inspiration. Squeak should, too. Perhaps you will learn how useful the class browser is examining Matrix. ;)

Upvotes: 0

Germán Arduino
Germán Arduino

Reputation: 373

http://stores.lulu.com/store.php?fAcctID=2748699 (Also you will find the link to download the pdf)

Upvotes: 0

akuhn
akuhn

Reputation: 27793

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.

Upvotes: 1

Paolo Bonzini
Paolo Bonzini

Reputation: 1930

Regarding asserts, please look at the other question recently posted.

Regarding TDD, yes, calling gst-sunit -f matrix.st -f matrix-test.st BaseMatrixTests is sort of the best way. Everything else just builds on that, for example these could be thee alternatives:

  • make all TestCase subclasses for your package inherit from a phony subclass so that you can say AllMatrixTests* on gst-sunit's command line (when you add more tests).
  • file in matrix.st from matrix-test.st, thus eliminating one -f option.
  • create a Makefile and package.xml file to create a .star file for your package, as described here. Then you can do just gst-sunit -pMatrix.

Upvotes: 1

nes1983
nes1983

Reputation: 15756

So, about asserts, Squeak Smalltalk already brings Object>>assert: So, I suppose you can do:

self assert: self isNotEmpty.
self assert: result dimensions equal: (self height)@(anotherMatrix width)

If you are using GNU smalltalk, this might answer how to do assertions there: Smalltalk and Assertions

Niko

Upvotes: 1

jarober
jarober

Reputation: 855

If you downloaded Cincom Smalltalk Non-Commercial, there are a number of online tutorials. Start here:

http://www.cincomsmalltalk.com/userblogs/cincom/blogView?content=tutorials

If you downloaded Squeak, start here:

http://wiki.squeak.org/squeak/792

And yes, you really do need to use the IDE to work effectively with Smalltalk.

On testing, load SUnit. In Cincom Smalltalk, it's a loadable component; I've covered loading (and using) it in the video tutorials linked above. I'm not entirely sure how to load it for Squeak, or whether it's part of the base there, but it's certainly available for it.

Upvotes: 6

Charlie Martin
Charlie Martin

Reputation: 112366

Okay, several pieces here.

First, I agree with markusQ, although I completely sympathize: I'd rather be able to write my code in EMACS directly. Bt one of the things about Smalltalk is that it really is very unforgiving of people who don't want to do things the Smalltalk Way. In this case, the Smalltalk Way is to use the browsers.

Second, part of the reason that this is the Smalltalk Way is that Smalltalk is, in a lot of ways, not like other languages. There is really, for all practical purposes, no way to make a "separate" Smalltalk executable: all you can do is make an image of Smalltalk with some relatively small fragments of your own code added in. When you write code using an external editor, as with the syntax you show, you're literally just hand typing an import/export format that is somewhat easier to hand type than XML. But only somewhat.

The moral is, do it the Smalltalk way, with the browsers.

There are some fairly good tutorials for Smalltalk about. I usually use Squeak, so the ones I've seen are using Squeak, as here.

On TDD, you're in luck because Smalltalk was the first place to get xUnit. For Smalltalk, it's called SUnit, and there's a good tutorial here.

You're using assertions there in what appears to be basically a "design by contract" approach. There has been work done on adding design by contract to Smalltalk, as here. For simple assertions, you can add code as in this SO question.

assert: aBlock 
    "Throw an assertion error if aBlock does not evaluates to true."
    aBlock value
        ifFalse: [AssertionFailure signal: 'Assertion failed']

Upvotes: 11

MarkusQ
MarkusQ

Reputation: 21950

I hate to say it ('cause you clearly indicated you don't want to hear it), but get into the IDE. Trying to get your head around smalltalk without using the IDE is like going to Paris and eating at McDonalds. Sure, you're in Paris, but you aren't really exposing yourself to what it's all about.

The key thing about smalltalk is that it's all objects. All the way down (integers and characters are objects) and all the way up (classes, methods, the browsers, the IDE itself) are all objects. If you insist on fighting it you'll have about as much luck as someone wanting to learn how to write C by dragging and dropping things.

Upvotes: 9

Steve Rowe
Steve Rowe

Reputation: 19413

Other than a request for a tutorial, I don't see a question here. Could you clarify what it is you want to know?

A good list of resources for a beginner can be found in this question: https://stackoverflow.com/questions/481976/is-there-a-dive-into-smalltalk-book

Upvotes: 3

Related Questions