PyFit version of Music Example
This rewrites the venerable Music Example using DoFixture to give a
head to head comparison between the styles using a fairly substantial
example that's still small enough to get one's head around.
Another part of the reason for doing this is to see how far
I could get without having to do a fixture. I consider this only
partially successful at that: it doesn't need anything from FIT
(although one of the type adapters does use Parse) but it also
does things that an application simply wouldn't do. In other words,
it's not entirely oblivious to the FIT environment.
The first thing we're going to do is invoke DoFixture, and
then start our music player. This is a three line table that we
slide to the right and grey out because it contains a bunch of
"technology facing" stuff that the end user shouldn't have to deal
with.
fitLib.DoFixture |
start | eg.MusicPlayer |
load | eg/Music/Music.txt |
The first line loads DoFixture, which is one of the standard
fixtures that comes with the Fit Library. DoFixture will control
the entire rest of the session.
The second line loads the Software Under Test, which is our
music player.
The third line loads the file containing all of the music
in the library. The load command is a method in the music
player.
Now we're going to check the total number of songs in the
library.
show | total | songs | in | library |
check | total | songs | in | library | 37
|
This line invokes the totalIn method with parameters of
"songs" and "library". This same method might be able to return,
for example, the total number of artists in the latest selection.
The italics are not something that DoFixture supplies: they are
the recommended markup to distinguish the syntax from the
parameters that the user fills in.
The next thing we want to do is display some of the
fields for the entries in the library.
select track | 1 |
show | fields | name and artist |
This shows what looks like an advanced feature: being able to take an English list. Well, it's not quite what it seems - we're going to write a type adapter to handle that list. It's going to be a fairly sophisticated little thing that can take the variations of standard English list syntax
The alternative is that we'd need a fieldsIn method, and a fieldsAndIn method, and probably a fieldsAndAndIn and fieldsAndAndAndIn method. That isn't the world's most intelligent design, especially since we might find a use for such a type adapter somewhere else.
It also shows that the result is displayed as a bulleted list. This isn't another DoFixture piece of magic; we're going to write a type adapter that takes a standard Python dictionary and displays it this way. It will also convert the other way: from a bulleted list with colon separated entries into a dictionary.
check | fields | name and artist |
- Name: Akila
- Artist: Toure Kunda
|
We can, of course, check the next one as well using the same command. Notice that the actual fields never appear in the method name; they're parameters to the command.
select track | 2 |
check | fields | name, artist, album, year, time and track |
- Name: American Tango
- Artist: Weather Report
- Album: Mysterious Traveller
- Year: 1974
- Time: 3:42
- Track: 2 of 7
|
This is pretty good, but it points out some things. One is that there are several data fields that aren't really character strings: the time and the track count are more complex quantities that need additional support. We're going to have to take that into account. We're also going to fix the time so it's in minutes and seconds rather than in minutes and fractions.
The next thing we're going to do is actually play some music.
select track | 2 |
play |
check | player status | loading |
wait | 2 |
check | player status | loading |
The wait command advances simulated external time by the requested amount, and does all the rest of the bookkeeping. It takes, by the way, four seconds to load a CD.
Another thing to notice is that the table doesn't look all that good. I fixed a small part of the problem with explicit colspan attributes in the HTML markup, but it's tedious to do that and it still doesn't look really good.
Presentation is important. One thing to make presentation a bit better is to use one table per line; at least that provides decent horizontal spacing in most browsers. Another thing to do is suppress the internal lines within the tables. Up to now, I've been leaving the line around the table. It helps set them off from the text. Most tests aren't going to be this chatty; it's a better idea to leave the tables entirely unmarked and mark comments in another way. I'm going to drop the borders and let the italics carry the distinctions. You decide on which style looks better.
check | player status | playing |
check | current track | name | American Tango |
check | current track | time | 3:42 |
Here we've just pushed the pause button on the player. This puts us into limbo: time isn't passing.
note | This advances us 60 seconds of simulated external time, but it doesn't change the time remaining on the track since it's paused. |
note | This is also an example of the note command: these two comments are table rows, not paragraphs interspersed between the tables |
check | player status | playing |
check | player status | unloading |
Now we're going to pop up a (virtual) dialog box
That will return us to the regular player interface. Note the way it's coded in the program.
check | player status | loading |
This ends the part that deals with the operation of the music player. Now let's look at the search and display facilities
check | search status | searching |
check | total | songs | in | selection | 2 |
Now we're going to display the selected songs. It could definitely be formatted better, but that's simply a matter of Html touchup.
display | Name, Artist, Album, Year, Time and Track |
However, what we probably want to do is check the contents of the selection. We have a variety of fixtures to do that: the Array, Row, Set and Subset. The next command is going to invoke the Row fixture to do the checks. I'm also going to put a border around the tables to emphasize that they're tables.
verify |
Name | Artist | Album | Year | Time | Track |
Scarlet Woman | Weather Report | Mysterious Traveller | 1974 | 5:43 | 6 of 7 |
American Tango | Weather Report | Mysterious Traveller | 1974 | 3:42 | 2 of 7 |
Now we want to do a different selection. The first thing is to select the contents.
check | total | songs | in | selection | 37 |
check | total | songs | in | selection | 5 |
display | Name, Artist, Album, Year, Time and Track |
verify |
Name | Album | Genre | Size | Date |
Another Grey Morning | JT | Pop | 3284199 | 9/7/02 11:32 PM |
Ananas | Hourglass | Pop | 6897450 | 9/7/02 11:47 PM |
Copperline | New Moon Shine | Pop | 5248087 | 9/7/02 9:52 PM |
Handy Man | JT | Pop | 3976956 | 9/7/02 11:36 PM |
Sailing To Philadelphia | October Rose | Pop | 6581911 | 9/7/02 10:45 PM |
That's it for this example. I hope it's been instructive for the differences in style. One thing you'll notice is that this style doesn't lend itself to the time tracking that the ActionFixture version did, however it flows better in several respects.