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