9 # let VMS whack all versions
10 1 while unlink('tcout');
13 use Test::More tests => 43;
15 use_ok( 'Term::Cap' );
18 my $out = tie *OUT, 'TieOut';
21 if (open(TCOUT, ">tcout")) {
28 # termcap_path -- the names are hardcoded in Term::Cap
30 my $path = join '', Term::Cap::termcap_path();
31 my $files = join '', grep { -f $_ } ( $ENV{HOME} . '/.termcap', '/etc/termcap',
32 '/usr/share/misc/termcap' );
33 is( $path, $files, 'termcap_path() should find default files' );
36 # this is ugly, but -f $0 really *ought* to work
37 skip("-f $0 fails, some tests difficult now", 2) unless -f $0;
39 $ENV{TERMCAP} = $ENV{TERMPATH} = $0;
40 ok( grep($0, Term::Cap::termcap_path()),
41 'termcap_path() should find file from $ENV{TERMCAP}' );
43 $ENV{TERMCAP} = (grep { $^O eq $_ } qw( os2 MSWin32 dos )) ? 'a:/' : '/';
44 ok( grep($0, Term::Cap::termcap_path()),
45 'termcap_path() should find file from $ENV{TERMPATH}' );
48 # make a Term::Cap "object"
53 bless($t, 'Term::Cap' );
56 is( $t->Tpad(), undef, 'Tpad() should return undef with no arguments' );
57 is( $t->Tpad('x'), 'x', 'Tpad() should return strings verbatim with no match' );
58 is( $t->Tpad( '1*a', 2 ), 'apcpc', 'Tpad() should pad paddable strings' );
61 is( $t->Tpad( '1*a', 3, *OUT ), 'apcpc', 'Tpad() should perform pad math' );
62 is( $out->read(), 'apcpc', 'Tpad() should write to filehandle when passed' );
64 is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' );
65 is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' );
66 $t->Tputs('pc', 1, *OUT);
67 is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' );
68 is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' );
70 eval { $t->Trequire( 'pc' ) };
71 is( $@, '', 'Trequire() should finds existing cap' );
72 eval { $t->Trequire( 'nonsense' ) };
73 like( $@, qr/support: \(nonsense\)/,
74 'Trequire() should croak with unsupported cap' );
77 local $SIG{__WARN__} = sub {
81 # test the first few features by forcing Tgetent() to croak (line 156)
84 eval { $t = Term::Cap->Tgetent($vals) };
85 like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' );
86 like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' );
87 is( $vals->{PADDING}, 10000/9600, 'Default OSPEED implies default PADDING' );
89 # check values for very slow speeds
92 eval { $t = Term::Cap->Tgetent($vals) };
93 is( $warn, '', 'Tgetent() should not work if OSPEED is provided' );
94 is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' );
96 # now see if lines 177 or 180 will fail
100 eval { $t = Term::Cap->Tgetent($vals) };
101 isn't( $@, '', 'Tgetent() should catch bad termcap file' );
103 # if there's no valid termcap file found, it should croak
106 eval { $t = Term::Cap->Tgetent($vals) };
107 like( $@, qr/failed termcap lookup/, 'Tgetent() should dies with bad termcap' );
110 skip( "Can't write 'tcout' file for tests", 8 ) unless $writable;
112 # it shouldn't try to read one file more than 32(!) times
113 # see __END__ for a really awful termcap example
115 $ENV{TERMPATH} = join(' ', ('tcout') x 33);
116 $vals->{TERM} = 'bar';
117 eval { $t = Term::Cap->Tgetent($vals) };
118 like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion');
120 # now let it read a fake termcap file, and see if it sets properties
121 $ENV{TERMPATH} = 'tcout';
122 $vals->{TERM} = 'baz';
123 $t = Term::Cap->Tgetent($vals);
124 is( $t->{_f1}, 1, 'Tgetent() should set a single field correctly' );
125 is( $t->{_f2}, 1, 'Tgetent() should set another field on the same line' );
126 is( $t->{_no}, '', 'Tgetent() should set a blank field correctly' );
127 is( $t->{_k1}, 'v1', 'Tgetent() should set a key value pair correctly' );
128 like( $t->{_k2}, qr/v2\\\n2/, 'Tgetent() should set and translate pairs' );
130 # and it should have set these two fields
131 is( $t->{_pc}, "\0", 'should set _pc field correctly' );
132 is( $t->{_bc}, "\b", 'should set _bc field correctly' );
135 # Tgoto has comments on the expected formats
137 is( $t->Tgoto('test', '', 1, *OUT), 'a1', 'Tgoto() should handle %d code' );
138 is( $out->read(), 'a1', 'Tgoto() should print to filehandle if passed' );
141 like( $t->Tgoto('test', '', 1), qr/^a\x01/, 'Tgoto() should handle %.' );
142 like( $t->Tgoto('test', '', 0), qr/\x61\x01\x08/,
143 'Tgoto() should handle %. and magic' );
146 like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() shoudl handle %+' );
147 $t->{_test} = 'a%+a';
148 is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' );
149 $t->{_test} .= 'a' x 99;
150 like( $t->Tgoto('test', '', 1), qr/ba{98}/,
151 'Tgoto() should substr()s %+ if needed' );
153 $t->{_test} = '%ra%d';
154 is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' );
156 $t->{_test} = 'a%>11bc';
157 is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' );
159 $t->{_test} = 'a%21';
160 is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' );
162 $t->{_test} = 'a%31';
163 is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' );
165 $t->{_test} = '%ia%21';
166 is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' );
169 is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' );
171 # and this is pretty standard
175 bless( \(my $self), $_[0] );
180 $$self .= join('', @_);
185 substr( $$self, 0, length($$self), '' );