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