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