11 # let VMS whack all versions
12 1 while unlink('tcout');
17 # these names are hardcoded in Term::Cap
20 ( $ENV{HOME} . '/.termcap', # we assume pretty UNIXy system anyway
22 '/usr/share/misc/termcap' );
23 my $terminfo = `infocmp -C 2>/dev/null`;
24 unless( $files || $terminfo || $^O eq 'VMS' ) {
25 plan skip_all => 'no termcap available to test';
31 use_ok( 'Term::Cap' );
34 my $out = tie *OUT, 'TieOut';
37 if (open(TCOUT, ">tcout")) {
44 # termcap_path -- the names are hardcoded in Term::Cap
46 my $path = join '', Term::Cap::termcap_path();
47 is( $path, $files, 'termcap_path() should find default files' );
50 # this is ugly, but -f $0 really *ought* to work
51 skip("-f $file fails, some tests difficult now", 2) unless -f $file;
53 $ENV{TERMCAP} = $ENV{TERMPATH} = $file;
54 ok( grep($file, Term::Cap::termcap_path()),
55 'termcap_path() should find file from $ENV{TERMCAP}' );
58 ok( grep($file, Term::Cap::termcap_path()),
59 'termcap_path() should find file from $ENV{TERMPATH}' );
62 # make a Term::Cap "object"
67 bless($t, 'Term::Cap' );
70 is( $t->Tpad(), undef, 'Tpad() should return undef with no arguments' );
71 is( $t->Tpad('x'), 'x', 'Tpad() should return strings verbatim with no match' );
72 is( $t->Tpad( '1*a', 2 ), 'apcpc', 'Tpad() should pad paddable strings' );
75 is( $t->Tpad( '1*a', 3, *OUT ), 'apcpc', 'Tpad() should perform pad math' );
76 is( $out->read(), 'apcpc', 'Tpad() should write to filehandle when passed' );
78 is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' );
79 is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' );
80 $t->Tputs('pc', 1, *OUT);
81 is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' );
82 is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' );
84 eval { $t->Trequire( 'pc' ) };
85 is( $@, '', 'Trequire() should finds existing cap' );
86 eval { $t->Trequire( 'nonsense' ) };
87 like( $@, qr/support: \(nonsense\)/,
88 'Trequire() should croak with unsupported cap' );
91 local $SIG{__WARN__} = sub {
95 # test the first few features by forcing Tgetent() to croak (line 156)
98 eval { local $^W = 1; $t = Term::Cap->Tgetent($vals) };
99 like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' );
100 like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' );
102 is( $vals->{PADDING}, 10000/9600, 'Default OSPEED implies default PADDING' );
105 eval { local $^W = 0; $t = Term::Cap->Tgetent($vals) };
106 is($warn,'xxxx',"Tgetent() doesn't carp() without warnings on");
108 # check values for very slow speeds
111 eval { $t = Term::Cap->Tgetent($vals) };
112 is( $warn, '', 'Tgetent() should not work if OSPEED is provided' );
113 is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' );
117 skip('Tgetent() bad termcap test, since using a fixed termcap',1)
119 # now see if lines 177 or 180 will fail
121 $ENV{TERMPATH} = '!';
123 eval { $t = Term::Cap->Tgetent($vals) };
124 isn't( $@, '', 'Tgetent() should catch bad termcap file' );
128 skip( "Can't write 'tcout' file for tests", 9 ) unless $writable;
130 # it won't find the termtype in this fake file, so it should croak
131 $vals->{TERM} = 'quux';
132 $ENV{TERMPATH} = 'tcout';
133 eval { $t = Term::Cap->Tgetent($vals) };
134 like( $@, qr/failed termcap/, 'Tgetent() should die with bad termcap' );
136 # it shouldn't try to read one file more than 32(!) times
137 # see __END__ for a really awful termcap example
138 $ENV{TERMPATH} = join(' ', ('tcout') x 33);
139 $vals->{TERM} = 'bar';
140 eval { $t = Term::Cap->Tgetent($vals) };
141 like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion');
143 # now let it read a fake termcap file, and see if it sets properties
144 $ENV{TERMPATH} = 'tcout';
145 $vals->{TERM} = 'baz';
146 $t = Term::Cap->Tgetent($vals);
147 is( $t->{_f1}, 1, 'Tgetent() should set a single field correctly' );
148 is( $t->{_f2}, 1, 'Tgetent() should set another field on the same line' );
149 is( $t->{_no}, '', 'Tgetent() should set a blank field correctly' );
150 is( $t->{_k1}, 'v1', 'Tgetent() should set a key value pair correctly' );
151 like( $t->{_k2}, qr/v2\\\n2/, 'Tgetent() should set and translate pairs' );
153 # and it should have set these two fields
154 is( $t->{_pc}, "\0", 'should set _pc field correctly' );
155 is( $t->{_bc}, "\b", 'should set _bc field correctly' );
161 skip("QNX's termcap database does not contain an entry for dumb terminals",
169 my $foo = Term::Cap->Tgetent();
170 is($foo->{TERM} ,'dumb','Windows gets "dumb" by default');
173 # Tgoto has comments on the expected formats
175 is( $t->Tgoto('test', '', 1, *OUT), 'a1', 'Tgoto() should handle %d code' );
176 is( $out->read(), 'a1', 'Tgoto() should print to filehandle if passed' );
179 like( $t->Tgoto('test', '', 1), qr/^a\x01/, 'Tgoto() should handle %.' );
180 if (ord('A') == 193) { # EBCDIC platform
181 like( $t->Tgoto('test', '', 0), qr/\x81\x01\x16/,
182 'Tgoto() should handle %. and magic' );
183 } else { # ASCII platform
184 like( $t->Tgoto('test', '', 0), qr/\x61\x01\x08/,
185 'Tgoto() should handle %. and magic' );
189 like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() should handle %+' );
190 $t->{_test} = 'a%+a';
191 is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' );
192 $t->{_test} .= 'a' x 99;
193 like( $t->Tgoto('test', '', 1), qr/ba{98}/,
194 'Tgoto() should substr()s %+ if needed' );
196 $t->{_test} = '%ra%d';
197 is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' );
199 $t->{_test} = 'a%>11bc';
200 is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' );
202 $t->{_test} = 'a%21';
203 is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' );
205 $t->{_test} = 'a%31';
206 is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' );
208 $t->{_test} = '%ia%21';
209 is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' );
212 is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' );
214 # and this is pretty standard
218 bless( \(my $self), $_[0] );
223 $$self .= join('', @_);
228 substr( $$self, 0, length($$self), '' );