11 # let VMS whack all versions
12 1 while unlink('tcout');
15 use Test::More tests => 43;
17 use_ok( 'Term::Cap' );
20 my $out = tie *OUT, 'TieOut';
23 if (open(TCOUT, ">tcout")) {
30 # termcap_path -- the names are hardcoded in Term::Cap
32 my $path = join '', Term::Cap::termcap_path();
33 my $files = join '', grep { -f $_ } ( $ENV{HOME} . '/.termcap', '/etc/termcap',
34 '/usr/share/misc/termcap' );
35 is( $path, $files, 'termcap_path() should find default files' );
38 # this is ugly, but -f $0 really *ought* to work
39 skip("-f $file fails, some tests difficult now", 2) unless -f $file;
41 $ENV{TERMCAP} = $ENV{TERMPATH} = $file;
42 ok( grep($file, Term::Cap::termcap_path()),
43 'termcap_path() should find file from $ENV{TERMCAP}' );
45 $ENV{TERMCAP} = (grep { $^O eq $_ } qw( os2 MSWin32 dos )) ? 'a:/' : '/';
46 ok( grep($file, Term::Cap::termcap_path()),
47 'termcap_path() should find file from $ENV{TERMPATH}' );
50 # make a Term::Cap "object"
55 bless($t, 'Term::Cap' );
58 is( $t->Tpad(), undef, 'Tpad() should return undef with no arguments' );
59 is( $t->Tpad('x'), 'x', 'Tpad() should return strings verbatim with no match' );
60 is( $t->Tpad( '1*a', 2 ), 'apcpc', 'Tpad() should pad paddable strings' );
63 is( $t->Tpad( '1*a', 3, *OUT ), 'apcpc', 'Tpad() should perform pad math' );
64 is( $out->read(), 'apcpc', 'Tpad() should write to filehandle when passed' );
66 is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' );
67 is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' );
68 $t->Tputs('pc', 1, *OUT);
69 is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' );
70 is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' );
72 eval { $t->Trequire( 'pc' ) };
73 is( $@, '', 'Trequire() should finds existing cap' );
74 eval { $t->Trequire( 'nonsense' ) };
75 like( $@, qr/support: \(nonsense\)/,
76 'Trequire() should croak with unsupported cap' );
79 local $SIG{__WARN__} = sub {
83 # test the first few features by forcing Tgetent() to croak (line 156)
86 eval { $t = Term::Cap->Tgetent($vals) };
87 like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' );
88 like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' );
89 is( $vals->{PADDING}, 10000/9600, 'Default OSPEED implies default PADDING' );
91 # check values for very slow speeds
94 eval { $t = Term::Cap->Tgetent($vals) };
95 is( $warn, '', 'Tgetent() should not work if OSPEED is provided' );
96 is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' );
98 # now see if lines 177 or 180 will fail
100 $ENV{TERMPATH} = '!';
102 eval { $t = Term::Cap->Tgetent($vals) };
103 isn't( $@, '', 'Tgetent() should catch bad termcap file' );
105 # if there's no valid termcap file found, it should croak
106 # (an empty string in $ENV{TERMPATH} and $ENV{TERM} counts as 'not found')
109 $ENV{TERMCAP} = '|:';
110 eval { $t = Term::Cap->Tgetent($vals) };
111 like( $@, qr/failed termcap lookup/, 'Tgetent() should die with bad termcap' );
114 skip( "Can't write 'tcout' file for tests", 8 ) unless $writable;
116 # it shouldn't try to read one file more than 32(!) times
117 # see __END__ for a really awful termcap example
119 $ENV{TERMPATH} = join(' ', ('tcout') x 33);
120 $vals->{TERM} = 'bar';
121 eval { $t = Term::Cap->Tgetent($vals) };
122 like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion');
124 # now let it read a fake termcap file, and see if it sets properties
125 $ENV{TERMPATH} = 'tcout';
126 $vals->{TERM} = 'baz';
127 $t = Term::Cap->Tgetent($vals);
128 is( $t->{_f1}, 1, 'Tgetent() should set a single field correctly' );
129 is( $t->{_f2}, 1, 'Tgetent() should set another field on the same line' );
130 is( $t->{_no}, '', 'Tgetent() should set a blank field correctly' );
131 is( $t->{_k1}, 'v1', 'Tgetent() should set a key value pair correctly' );
132 like( $t->{_k2}, qr/v2\\\n2/, 'Tgetent() should set and translate pairs' );
134 # and it should have set these two fields
135 is( $t->{_pc}, "\0", 'should set _pc field correctly' );
136 is( $t->{_bc}, "\b", 'should set _bc field correctly' );
139 # Tgoto has comments on the expected formats
141 is( $t->Tgoto('test', '', 1, *OUT), 'a1', 'Tgoto() should handle %d code' );
142 is( $out->read(), 'a1', 'Tgoto() should print to filehandle if passed' );
145 like( $t->Tgoto('test', '', 1), qr/^a\x01/, 'Tgoto() should handle %.' );
146 like( $t->Tgoto('test', '', 0), qr/\x61\x01\x08/,
147 'Tgoto() should handle %. and magic' );
150 like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() shoudl handle %+' );
151 $t->{_test} = 'a%+a';
152 is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' );
153 $t->{_test} .= 'a' x 99;
154 like( $t->Tgoto('test', '', 1), qr/ba{98}/,
155 'Tgoto() should substr()s %+ if needed' );
157 $t->{_test} = '%ra%d';
158 is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' );
160 $t->{_test} = 'a%>11bc';
161 is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' );
163 $t->{_test} = 'a%21';
164 is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' );
166 $t->{_test} = 'a%31';
167 is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' );
169 $t->{_test} = '%ia%21';
170 is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' );
173 is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' );
175 # and this is pretty standard
179 bless( \(my $self), $_[0] );
184 $$self .= join('', @_);
189 substr( $$self, 0, length($$self), '' );