in reply to Test Driven Development example + design question
I haven't read the book that you read, so might advice might differ a bit from what the book said.
As another disclaimer, Perl is much more expressive than Java, so we can make some APIs simpler, and still have flexibility under the hood.
So, what do you know about the problem? It involves reading of some files, computations, and writing of files. What's the simplest possible API for reading files?
my $data = read_file($filename); # or maybe the reader needs to know the file format too? my $data = read_file($filename, $fileformat);
Maybe you'll object "but that's not object oriented!". So what? Your job is to get stuff done. You can still do some OO stuff behind the scence if you really want, but for now you should focus on making the APIs as simple as possible. Oh, and you probably return some kind of object from read_file, so there's your OO part.
Anyway, now that you have a simple API for a part of your program, you can start to write tests.
use 5.010; use Test tests => 2; use YourModule qw/read_file/; my $data = read_file('t/data/ExampleData'); # now you have to ask yourself, what kind of data # will you need for processing the spreadsheets? # I know nothing about your application, so # I'm just making up some stuff here: is join(',', $data->headings), 'time,pressure,temperature', 'can get column headings from $data'; is $data->temperature( time => "0.01"), 273.018, 'can retrieve temperature by time';
So, now you need to implement it far enough to get the tests passing.
Next you can do the same for processing the data, and for writing a file. The API of each of those can always be a single subroutine call.
If you need some more flexiblity later on, those subroutines can call some constructors on your classes and use polymorphism and whatever buzzword you want to be compatible with. But always remember that the user should seee as little complexity as possible. That way the tests also see very little complexity, and it becomes very easy to test each piece of functionality in isolation from the others.
Once you have all of this in place, you will probably want some higher level abstraction, for example something that reads several input files, passes them to a routine that does the processing, and then writes output files. Again the API for this can be a simple function, and thus it can be easy to call and test.
read_process_write( inputfiles => [ 't/data/TestInput1', 't/data/TestInput2'], merger => sub { ... }, outputfile => 't/data/temp/TestResult', );
Again, write the tests for it and implement it. Then be happy.
Note that all this time I didn't talk about how to structure roles and classes. That's because it's not really central to the working of your program, and it is often obvious. Once you know that an API is my $data = read_file($path);, it is obvious that $data must contain an object that knows everything interesting about the data in file $path (or contains other objects that know it).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Test Driven Development example + design question
by mascip (Pilgrim) on Jul 01, 2012 at 12:49 UTC | |
by mascip (Pilgrim) on Jul 01, 2012 at 13:11 UTC | |
Re^2: Test Driven Development example + design question
by mascip (Pilgrim) on Jul 01, 2012 at 18:29 UTC | |
Re^2: Test Driven Development example + design question
by mascip (Pilgrim) on Jul 01, 2012 at 19:46 UTC |