Document Actions

spam

Up one level

Document Actions

Test so you have less code to test

by rbp posted at 2007-05-16 03:28 last modified 2008-01-23 19:27

More than one year ago, Danilo Sato and I started a pet project to practice TDD. We ended up not making all that much progress so far, codewise (over one year! So much for "agile"...), but it's been a great learning exercise. It's a Python test-driven implementation of the Stratego game, and Danilo has documented our early programming sessions on his blog (in Portuguese, I'm waiting for him to catch up to start bitching for an English translation, but I'll post stuff about it here as well). We've recently resumed work on it and it should show up both here and at Danilo's.

Anyway, I have since developed a taste for testing (a "teste", you could say) and, when I started cleaning up nnebs, I found myself writing tests for everything as I went through the code (no, it had no tests before; yes, it was my graduation project; anyone can have a Comp. Sci. B.Sc. these days...). Doctests, even, but more on that later.

One thing that struck me as interesting, in this process, came up while I was writing tests for some state-related classes. Nnebs is basically a spam filter, and I had decided its classifiers (as well as a few other classes) should be stateful, that is, they should be able to easily represent a particular configuration and switch back to it when requested. Since a few different classes would be stateful, I created a Stateful class from which classes wishing to be stateful should inherit. So far, so good. All these classes needed to do was inherit from Stateful and define which attributes would make up their states.

To describe a state itself, I created another class (aptly named "State") to hold attributes and their default values. It defined methods to add, delete, set and query attributes. It also defined that two state objects would be equal if their attributes and values were the same.

The State class was about 30 lines of code and worked quite well. However, while writing its tests I noticed they seemed a bit too trivial, which struck me as odd. I was having to write tests like

>>> State(attr1=val1, attr2=val2) == State(attr1=val1, attr2=val2)
True
>>> State(attr1=val1).get('attr1')
val1

That just doesn't seem right :)

I figured I could delegate some of State's behaviour by making it a subclass of "dict". Great, there goes my "get" implementation. And my equality implementation. And my __del__ implementation. And my initialization implementation. And, well, I assume you can take it from here.

So State is now simply a dict. Running the tests confirmed that everything worked as before. My code is 30 lines of bug-food (a.k.a. "code") shorter. And I'm a lot happier with nnebs and more confident in its code.


Speak of the devil...

by rbp posted at 2007-05-16 15:18 last modified 2008-01-23 19:27

I started refactoring and doctesting nnebs, today. Last time I coded for it I was in such a hurry, in order to meet my graduation project's deadline, that, well, let's just say I'm not too proud of how it was delivered :P

Anyway, it's nice to play with nnebs once again, and I should put it online again soon.


Test so you have less code to test

Posted by Bits of rbp at 2007-08-15 14:22
More than one year ago, Danilo Sato and I started a pet project to practice TDD . We ended up not making all that much pro...

Eggs, baked beans, bacon and...

by rbp posted at 2007-05-16 15:26 last modified 2008-01-23 19:26

Ok, third spam comment in a row. Time to integrate coreblog2 with akismet (and maybe later with nnebs). Should be up soon.


A bug and a feature

by rbp posted at 2007-05-30 22:08 last modified 2008-01-23 19:26

I've re-ordered my pet-project priorities to 1) fix coreblog; 2) refactor and test zzbot; and 3) continue refactoring, testing and turning nnebs into an actual spam filter (and not a spam filtering testbed). I've also decided to get involved in python (the interpreter) development and have started reviewing patches, let's see how I'll fit this into the aforementioned priorities.

I put coreblog at the top because I've found myself reluctant to post before fixing some of the issues I encountered. I'm upgrading Plone today as a first step to start digging into coreblog, so expect something on that front soon.

On the other hand, I just realized a nice side-effect of being unable to receive trackbacks: no trackback spam!

I still get the email warning, though, so that won't keep me comfortable for too long :)


[isnomore.net]
software
blog
completely different things
Google Reader shared items
Google Reader shared items rss feed
bê do érre
ali ckel
cybershark
Recent entries
pyconbrasil[3][1] rbp 2007-10-06
Fun for the whole family! rbp 2007-09-27
Not equal not not equal. By default. rbp 2007-09-10
pyconbrasil[3].pictures rbp 2007-09-09
pyconbrasil[3][0] rbp 2007-09-08
Recent comments
Re:pyconbrasil[3][1] Danilo Sato 2007-10-06
Re:pyconbrasil[3][1] rbp 2007-10-06
Re:Fun for the whole family! rbp 2007-10-02
Re:Fun for the whole family! Anonymous User 2007-10-02
Re:You vicious, heartless bastard! Anonymous User 2007-08-15
Categories
python (13)
meta (8)
english (19)
portugues (0)
OLPC (1)
spam (4)
agile (2)
nnebs (3)
coreblog (2)
community (6)
pyconbrasil3 (5)
About this blog
rbp's random ramblings (and alliterations, as always)
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: