9 if ( $ENV{PERL_CORE} ) {
15 # let VMS whack all versions
16 1 while unlink('tcout');
21 # these names are hardcoded in Term::Cap
24 ( $ENV{HOME} . '/.termcap', # we assume pretty UNIXy system anyway
26 '/usr/share/misc/termcap' );
27 unless( $files || $^O eq 'VMS') {
28 plan skip_all => 'no termcap available to test';
34 use_ok( 'Term::Cap' );
37 my $out = tie *OUT, 'TieOut';
40 if (open(TCOUT, ">tcout")) {
47 # termcap_path -- the names are hardcoded in Term::Cap
49 my $path = join '', Term::Cap::termcap_path();
50 is( $path, $files, 'termcap_path() should find default files' );
53 # this is ugly, but -f $0 really *ought* to work
54 skip("-f $file fails, some tests difficult now", 2) unless -f $file;
56 $ENV{TERMCAP} = $ENV{TERMPATH} = $file;
57 ok( grep($file, Term::Cap::termcap_path()),
58 'termcap_path() should find file from $ENV{TERMCAP}' );
61 ok( grep($file, Term::Cap::termcap_path()),
62 'termcap_path() should find file from $ENV{TERMPATH}' );
65 # make a Term::Cap "object"
70 bless($t, 'Term::Cap' );
73 is( $t->Tpad(), undef, 'Tpad() should return undef with no arguments' );
74 is( $t->Tpad('x'), 'x', 'Tpad() should return strings verbatim with no match' );
75 is( $t->Tpad( '1*a', 2 ), 'apcpc', 'Tpad() should pad paddable strings' );
78 is( $t->Tpad( '1*a', 3, *OUT ), 'apcpc', 'Tpad() should perform pad math' );
79 is( $out->read(), 'apcpc', 'Tpad() should write to filehandle when passed' );
81 is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' );
82 is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' );
83 $t->Tputs('pc', 1, *OUT);
84 is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' );
85 is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' );
87 eval { $t->Trequire( 'pc' ) };
88 is( $@, '', 'Trequire() should finds existing cap' );
89 eval { $t->Trequire( 'nonsense' ) };
90 like( $@, qr/support: \(nonsense\)/,
91 'Trequire() should croak with unsupported cap' );
94 local $SIG{__WARN__} = sub {
98 # test the first few features by forcing Tgetent() to croak (line 156)
101 eval { local $^W = 1; $t = Term::Cap->Tgetent($vals) };
102 like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' );
103 like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' );
105 is( $vals->{PADDING}, 10000/9600, 'Default OSPEED implies default PADDING' );
108 eval { local $^W = 0; $t = Term::Cap->Tgetent($vals) };
109 is($warn,'xxxx',"Tgetent() doesn't carp() without warnings on");
111 # check values for very slow speeds
114 eval { $t = Term::Cap->Tgetent($vals) };
115 is( $warn, '', 'Tgetent() should not work if OSPEED is provided' );
116 is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' );
120 skip('Tgetent() bad termcap test, since using a fixed termcap',1)
122 # now see if lines 177 or 180 will fail
124 $ENV{TERMPATH} = '!';
126 eval { $t = Term::Cap->Tgetent($vals) };
127 isn't( $@, '', 'Tgetent() should catch bad termcap file' );
131 skip( "Can't write 'tcout' file for tests", 9 ) unless $writable;
133 # it won't find the termtype in this fake file, so it should croak
134 $vals->{TERM} = 'quux';
135 $ENV{TERMPATH} = 'tcout';
136 eval { $t = Term::Cap->Tgetent($vals) };
137 like( $@, qr/failed termcap/, 'Tgetent() should die with bad termcap' );
139 # it shouldn't try to read one file more than 32(!) times
140 # see __END__ for a really awful termcap example
141 $ENV{TERMPATH} = join(' ', ('tcout') x 33);
142 $vals->{TERM} = 'bar';
143 eval { $t = Term::Cap->Tgetent($vals) };
144 like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion');
146 # now let it read a fake termcap file, and see if it sets properties
147 $ENV{TERMPATH} = 'tcout';
148 $vals->{TERM} = 'baz';
149 $t = Term::Cap->Tgetent($vals);
150 is( $t->{_f1}, 1, 'Tgetent() should set a single field correctly' );
151 is( $t->{_f2}, 1, 'Tgetent() should set another field on the same line' );
152 is( $t->{_no}, '', 'Tgetent() should set a blank field correctly' );
153 is( $t->{_k1}, 'v1', 'Tgetent() should set a key value pair correctly' );
154 like( $t->{_k2}, qr/v2\\\n2/, 'Tgetent() should set and translate pairs' );
156 # and it should have set these two fields
157 is( $t->{_pc}, "\0", 'should set _pc field correctly' );
158 is( $t->{_bc}, "\b", 'should set _bc field correctly' );
161 # Tgoto has comments on the expected formats
163 is( $t->Tgoto('test', '', 1, *OUT), 'a1', 'Tgoto() should handle %d code' );
164 is( $out->read(), 'a1', 'Tgoto() should print to filehandle if passed' );
167 like( $t->Tgoto('test', '', 1), qr/^a\x01/, 'Tgoto() should handle %.' );
168 like( $t->Tgoto('test', '', 0), qr/\x61\x01\x08/,
169 'Tgoto() should handle %. and magic' );
172 like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() should handle %+' );
173 $t->{_test} = 'a%+a';
174 is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' );
175 $t->{_test} .= 'a' x 99;
176 like( $t->Tgoto('test', '', 1), qr/ba{98}/,
177 'Tgoto() should substr()s %+ if needed' );
179 $t->{_test} = '%ra%d';
180 is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' );
182 $t->{_test} = 'a%>11bc';
183 is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' );
185 $t->{_test} = 'a%21';
186 is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' );
188 $t->{_test} = 'a%31';
189 is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' );
191 $t->{_test} = '%ia%21';
192 is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' );
195 is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' );
197 # and this is pretty standard
201 bless( \(my $self), $_[0] );
206 $$self .= join('', @_);
211 substr( $$self, 0, length($$self), '' );