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
starteg.MusicPlayer
loadeg/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.

showtotalsongsinlibrary
checktotalsongsinlibrary37

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 track1
showfieldsname 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.

checkfieldsname 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 track2
checkfieldsname, 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 track2
play
checkplayer statusloading
wait2
checkplayer statusloading

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.

wait2
checkplayer statusplaying
checkcurrent tracknameAmerican Tango
checkcurrent tracktime3:42
wait3
checktime remaining3:39
pause

Here we've just pushed the pause button on the player. This puts us into limbo: time isn't passing.

checkplayer statuspaused
wait60
noteThis advances us 60 seconds of simulated external time, but it doesn't change the time remaining on the track since it's paused.
noteThis is also an example of the note command: these two comments are table rows, not paragraphs interspersed between the tables
checktime remaining3:39
play
wait60
checkplayer statusplaying
checktime remaining2:39
wait fortrack complete
checkplayer statusunloading
wait4
select track4
play

Now we're going to pop up a (virtual) dialog box

failload jam
checkmessageload jammed
pressok

That will return us to the regular player interface. Note the way it's coded in the program.

checkplayer statusloading

This ends the part that deals with the operation of the music player. Now let's look at the search and display facilities

select track2
find samealbum
checksearch statussearching
wait forsearch complete
checksearch statusready
checktotalsongsinselection2

Now we're going to display the selected songs. It could definitely be formatted better, but that's simply a matter of Html touchup.

displayName, 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
NameArtistAlbumYearTimeTrack
Scarlet WomanWeather ReportMysterious Traveller19745:436 of 7
American TangoWeather ReportMysterious Traveller19743:422 of 7

Now we want to do a different selection. The first thing is to select the contents.

select all
wait forsearch complete
checktotalsongsinselection37
select track3
checkfieldsartist
  • Artist: James Taylor
find sameartist
wait forsearch complete
checktotalsongsinselection5
displayName, Artist, Album, Year, Time and Track

 

verify
NameAlbumGenreSizeDate
Another Grey MorningJTPop32841999/7/02 11:32 PM
AnanasHourglassPop68974509/7/02 11:47 PM
CopperlineNew Moon ShinePop52480879/7/02 9:52 PM
Handy ManJTPop39769569/7/02 11:36 PM
Sailing To PhiladelphiaOctober RosePop 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.