Re: [PATCH mg.c gv.c and others] ${^TAINT}
[p5sagit/p5-mst-13.2.git] / lib / Term / Cap.t
CommitLineData
1285de5c 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8END {
9 # let VMS whack all versions
10 1 while unlink('tcout');
11}
12
13use Test::More tests => 43;
14
15use_ok( 'Term::Cap' );
16
17local (*TCOUT, *OUT);
18my $out = tie *OUT, 'TieOut';
19my $writable = 1;
20
21if (open(TCOUT, ">tcout")) {
22 print TCOUT <DATA>;
23 close TCOUT;
24} else {
25 $writable = 0;
26}
27
28# termcap_path -- the names are hardcoded in Term::Cap
29$ENV{TERMCAP} = '';
30my $path = join '', Term::Cap::termcap_path();
31my $files = join '', grep { -f $_ } ( $ENV{HOME} . '/.termcap', '/etc/termcap',
32 '/usr/share/misc/termcap' );
c6d685f7 33is( $path, $files, 'termcap_path() should find default files' );
1285de5c 34
35SKIP: {
36 # this is ugly, but -f $0 really *ought* to work
b4419834 37 skip("-f $0 fails, some tests difficult now", 2) unless -f $0;
1285de5c 38
b4419834 39 $ENV{TERMCAP} = $ENV{TERMPATH} = $0;
40 ok( grep($0, Term::Cap::termcap_path()),
c6d685f7 41 'termcap_path() should find file from $ENV{TERMCAP}' );
1285de5c 42
43 $ENV{TERMCAP} = (grep { $^O eq $_ } qw( os2 MSWin32 dos )) ? 'a:/' : '/';
b4419834 44 ok( grep($0, Term::Cap::termcap_path()),
c6d685f7 45 'termcap_path() should find file from $ENV{TERMPATH}' );
1285de5c 46}
47
1285de5c 48# make a Term::Cap "object"
49my $t = {
50 PADDING => 1,
51 _pc => 'pc',
52};
53bless($t, 'Term::Cap' );
54
55# see if Tpad() works
c6d685f7 56is( $t->Tpad(), undef, 'Tpad() should return undef with no arguments' );
57is( $t->Tpad('x'), 'x', 'Tpad() should return strings verbatim with no match' );
58is( $t->Tpad( '1*a', 2 ), 'apcpc', 'Tpad() should pad paddable strings' );
1285de5c 59
60$t->{PADDING} = 2;
c6d685f7 61is( $t->Tpad( '1*a', 3, *OUT ), 'apcpc', 'Tpad() should perform pad math' );
62is( $out->read(), 'apcpc', 'Tpad() should write to filehandle when passed' );
1285de5c 63
c6d685f7 64is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' );
65is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' );
1285de5c 66$t->Tputs('pc', 1, *OUT);
c6d685f7 67is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' );
68is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' );
1285de5c 69
70eval { $t->Trequire( 'pc' ) };
c6d685f7 71is( $@, '', 'Trequire() should finds existing cap' );
1285de5c 72eval { $t->Trequire( 'nonsense' ) };
c6d685f7 73like( $@, qr/support: \(nonsense\)/,
74 'Trequire() should croak with unsupported cap' );
1285de5c 75
76my $warn;
77local $SIG{__WARN__} = sub {
78 $warn = $_[0];
79};
80
81# test the first few features by forcing Tgetent() to croak (line 156)
82undef $ENV{TERM};
83my $vals = {};
84eval { $t = Term::Cap->Tgetent($vals) };
c6d685f7 85like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' );
86like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' );
1285de5c 87is( $vals->{PADDING}, 10000/9600, 'Default OSPEED implies default PADDING' );
88
89# check values for very slow speeds
90$vals->{OSPEED} = 1;
91$warn = '';
92eval { $t = Term::Cap->Tgetent($vals) };
c6d685f7 93is( $warn, '', 'Tgetent() should not work if OSPEED is provided' );
94is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' );
1285de5c 95
96# now see if lines 177 or 180 will fail
97$ENV{TERM} = 'foo';
98$ENV{TERMPATH} = '!';
99$ENV{TERMCAP} = '';
100eval { $t = Term::Cap->Tgetent($vals) };
c6d685f7 101isn't( $@, '', 'Tgetent() should catch bad termcap file' );
1285de5c 102
b4419834 103# if there's no valid termcap file found, it should croak
1285de5c 104$vals->{TERM} = '';
b4419834 105$ENV{TERMPATH} = $0;
1285de5c 106eval { $t = Term::Cap->Tgetent($vals) };
b4419834 107like( $@, qr/failed termcap lookup/, 'Tgetent() should dies with bad termcap' );
1285de5c 108
109SKIP: {
110 skip( "Can't write 'tcout' file for tests", 8 ) unless $writable;
111
112 # it shouldn't try to read one file more than 32(!) times
113 # see __END__ for a really awful termcap example
114
115 $ENV{TERMPATH} = join(' ', ('tcout') x 33);
116 $vals->{TERM} = 'bar';
117 eval { $t = Term::Cap->Tgetent($vals) };
c6d685f7 118 like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion');
1285de5c 119
120 # now let it read a fake termcap file, and see if it sets properties
121 $ENV{TERMPATH} = 'tcout';
122 $vals->{TERM} = 'baz';
123 $t = Term::Cap->Tgetent($vals);
c6d685f7 124 is( $t->{_f1}, 1, 'Tgetent() should set a single field correctly' );
125 is( $t->{_f2}, 1, 'Tgetent() should set another field on the same line' );
126 is( $t->{_no}, '', 'Tgetent() should set a blank field correctly' );
127 is( $t->{_k1}, 'v1', 'Tgetent() should set a key value pair correctly' );
128 like( $t->{_k2}, qr/v2\\\n2/, 'Tgetent() should set and translate pairs' );
1285de5c 129
130 # and it should have set these two fields
c6d685f7 131 is( $t->{_pc}, "\0", 'should set _pc field correctly' );
132 is( $t->{_bc}, "\b", 'should set _bc field correctly' );
1285de5c 133}
134
135# Tgoto has comments on the expected formats
136$t->{_test} = "a%d";
c6d685f7 137is( $t->Tgoto('test', '', 1, *OUT), 'a1', 'Tgoto() should handle %d code' );
138is( $out->read(), 'a1', 'Tgoto() should print to filehandle if passed' );
1285de5c 139
140$t->{_test} = "a%.";
c6d685f7 141like( $t->Tgoto('test', '', 1), qr/^a\x01/, 'Tgoto() should handle %.' );
142like( $t->Tgoto('test', '', 0), qr/\x61\x01\x08/,
143 'Tgoto() should handle %. and magic' );
1285de5c 144
145$t->{_test} = 'a%+';
c6d685f7 146like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() shoudl handle %+' );
1285de5c 147$t->{_test} = 'a%+a';
c6d685f7 148is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' );
1285de5c 149$t->{_test} .= 'a' x 99;
c6d685f7 150like( $t->Tgoto('test', '', 1), qr/ba{98}/,
151 'Tgoto() should substr()s %+ if needed' );
1285de5c 152
153$t->{_test} = '%ra%d';
c6d685f7 154is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' );
1285de5c 155
156$t->{_test} = 'a%>11bc';
c6d685f7 157is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' );
1285de5c 158
159$t->{_test} = 'a%21';
c6d685f7 160is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' );
1285de5c 161
162$t->{_test} = 'a%31';
c6d685f7 163is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' );
1285de5c 164
165$t->{_test} = '%ia%21';
c6d685f7 166is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' );
1285de5c 167
168$t->{_test} = '%z';
c6d685f7 169is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' );
1285de5c 170
171# and this is pretty standard
172package TieOut;
173
174sub TIEHANDLE {
175 bless( \(my $self), $_[0] );
176}
177
178sub PRINT {
179 my $self = shift;
180 $$self .= join('', @_);
181}
182
183sub read {
184 my $self = shift;
185 substr( $$self, 0, length($$self), '' );
186}
187
188__END__
189bar: :tc=bar: \
190baz: \
191:f1: :f2: \
192:no@ \
193:k1#v1\
194:k2=v2\\n2