| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Basic functions used by most of the test scripts. |
| 4 |
# |
| 5 |
|
| 6 |
sub run_tests { |
| 7 |
my ($testref) = @_; |
| 8 |
my @tests = @$testref; |
| 9 |
my ($nt, $i); |
| 10 |
|
| 11 |
$nt = scalar @tests; # number of sub-tests |
| 12 |
|
| 13 |
foreach $i (1 .. $nt) { |
| 14 |
my $testref = shift @tests; |
| 15 |
my ($cols, $rows, $text, @output) = @$testref; |
| 16 |
my ($ncols, $nrows, $row, $col, $settings); |
| 17 |
my ($line, $aline, $alineref, $galine, $passed); |
| 18 |
|
| 19 |
$settings = undef; |
| 20 |
if (ref $cols) { |
| 21 |
($settings, $cols, $rows, $text, @output) = @$testref; |
| 22 |
} |
| 23 |
|
| 24 |
print "$i..$nt\n"; |
| 25 |
|
| 26 |
my $vt = Term::VT102->new ('cols' => $cols, 'rows' => $rows); |
| 27 |
|
| 28 |
($ncols, $nrows) = $vt->size (); |
| 29 |
|
| 30 |
if (($cols != $ncols) or ($rows != $nrows)) { |
| 31 |
print "not ok $i\n"; |
| 32 |
warn "returned size: $ncols x $nrows, wanted $cols x $rows\n"; |
| 33 |
next; |
| 34 |
} |
| 35 |
|
| 36 |
if (defined $settings) { |
| 37 |
foreach (keys %$settings) { |
| 38 |
if ( |
| 39 |
!defined $vt->option_set ($_,$settings->{$_}) |
| 40 |
) { |
| 41 |
print "not ok $i\n"; |
| 42 |
warn "failed to set option: $_"; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
$vt->process ($text); |
| 48 |
|
| 49 |
$row = 0; |
| 50 |
$passed = 1; |
| 51 |
|
| 52 |
while ($#output > 0) { |
| 53 |
$line = shift @output; |
| 54 |
if (ref $output[0]) { |
| 55 |
$alineref = shift @output; |
| 56 |
$aline = ""; |
| 57 |
foreach (@$alineref) { |
| 58 |
$aline .= $vt->attr_pack (@$_); |
| 59 |
} |
| 60 |
} else { |
| 61 |
$alineref = undef; |
| 62 |
} |
| 63 |
$row ++; |
| 64 |
if ($vt->row_text ($row) ne $line) { |
| 65 |
$passed = 0; |
| 66 |
print STDERR "test $i: row $row incorrect, got '" . |
| 67 |
show_text ($vt->row_text ($row)) . "', expected '" . |
| 68 |
show_text ($line) . "'\n"; |
| 69 |
next; |
| 70 |
} |
| 71 |
next if (not defined $alineref); |
| 72 |
$galine = $vt->row_attr ($row); |
| 73 |
for ($col = 0; $col < $cols; $col ++) { |
| 74 |
if (substr ($aline, 2 * $col, 2) ne substr ($galine, 2 * $col, 2)) { |
| 75 |
$passed = 0; |
| 76 |
print STDERR "test $i: row $row col " . ($col + 1) . |
| 77 |
" attributes incorrect, got '" . |
| 78 |
show_attr ($vt, substr ($galine, 2 * $col, 2)) . |
| 79 |
"', expected '" . |
| 80 |
show_attr ($vt, substr ($aline, 2 * $col, 2)) . "'\n"; |
| 81 |
next; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ($passed == 0) { |
| 87 |
print "not ok $i\n"; |
| 88 |
print STDERR "screen contents ($cols x $rows):\n"; |
| 89 |
for (my $dumprow = 1; $dumprow <= $rows; $dumprow++) { |
| 90 |
print STDERR "[" . $vt->row_plaintext ($dumprow) . "]\n"; |
| 91 |
} |
| 92 |
} else { |
| 93 |
print "ok $i\n"; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
sub show_text { |
| 100 |
my ($text) = @_; |
| 101 |
return "" if (not defined $text); |
| 102 |
$text =~ s/([^\040-\176])/sprintf ("\\%o", ord ($1))/ge; |
| 103 |
return $text; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
sub show_attr { |
| 108 |
my ($vt, $attr) = @_; |
| 109 |
my ($fg,$bg,$bo,$fa,$st,$ul,$bl,$rv) = $vt->attr_unpack ($attr); |
| 110 |
my $str = "$fg-$bg"; |
| 111 |
$str .= "b" if ($bo != 0); |
| 112 |
$str .= "f" if ($fa != 0); |
| 113 |
$str .= "s" if ($st != 0); |
| 114 |
$str .= "u" if ($ul != 0); |
| 115 |
$str .= "F" if ($bl != 0); |
| 116 |
$str .= "r" if ($rv != 0); |
| 117 |
return $str . "-" . sprintf ("%04X", unpack ('S', $attr)); |
| 118 |
} |
| 119 |
|
| 120 |
1; |
| 121 |
# EOF |