Next Automated Testing 16

Test code does not have to be robust

        unless (open F, "<", $db_file) {
          die "Couldn't open file '$db_file': $!; aborting";
        }
        my @matches;
        while (<F>) {
          chomp;
          my ($u) = split /:/;
          push @matches, $_ if $u eq $username;
        }
        close F;
        my ($rec, @extra) = @matches;
        if (@extra) {
          die "Multiple records for user '$username' in file '$db_file'\n";
        } elsif (not defined $rec) {
          die "User '$username' not found in file '$db_file'\n";
        }
        my (@field) = split /:/, $rec;
        unless (@field == 5) {
          die "Malformed record for user '$username' in file '$db_file':\n\t$rec\n";
        }
        # Now do something with $field[3] ...
     my ($rec) = qx{ grep '^netmon:' $db_file };
     is((split(/:/, $rec))[3], "expired", "netmon user was expired");

Next Copyright © 2004 Mark Dominus