Unicode properties: fix L& (the #12319 didn't allow L&,
[p5sagit/p5-mst-13.2.git] / lib / Term / Cap.t
1 #!./perl
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6 }
7
8 END {
9         # let VMS whack all versions
10         1 while unlink('tcout');
11 }
12
13 use Test::More tests => 43;
14
15 use_ok( 'Term::Cap' );
16
17 local (*TCOUT, *OUT);
18 my $out = tie *OUT, 'TieOut';
19 my $writable = 1;
20
21 if (open(TCOUT, ">tcout")) {
22         print TCOUT <DATA>;
23         close TCOUT;
24 } else {
25         $writable = 0;
26 }
27
28 # termcap_path -- the names are hardcoded in Term::Cap
29 $ENV{TERMCAP} = '';
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' );
34
35 SKIP: {
36         # this is ugly, but -f $0 really *ought* to work
37         skip("-f $0 fails, some tests difficult now", 2) unless -f $0;
38
39         $ENV{TERMCAP} = $ENV{TERMPATH} = $0;
40         ok( grep($0, Term::Cap::termcap_path()), 
41                 'termcap_path() should find file from $ENV{TERMCAP}' );
42
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}' );
46 }
47
48 # make a Term::Cap "object"
49 my $t = {
50         PADDING => 1,
51         _pc => 'pc',
52 };
53 bless($t, 'Term::Cap' );
54
55 # see if Tpad() works
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' );
59
60 $t->{PADDING} = 2;
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' );
63
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' );
69
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' );
75
76 my $warn;
77 local $SIG{__WARN__} = sub {
78         $warn = $_[0];
79 };
80
81 # test the first few features by forcing Tgetent() to croak (line 156)
82 undef $ENV{TERM};
83 my $vals = {};
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' );
88
89 # check values for very slow speeds
90 $vals->{OSPEED} = 1;
91 $warn = '';
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' );
95
96 # now see if lines 177 or 180 will fail
97 $ENV{TERM} = 'foo';
98 $ENV{TERMPATH} = '!';
99 $ENV{TERMCAP} = '';
100 eval { $t = Term::Cap->Tgetent($vals) };
101 isn't( $@, '', 'Tgetent() should catch bad termcap file' );
102
103 # if there's no valid termcap file found, it should croak
104 $vals->{TERM} = '';
105 $ENV{TERMPATH} = $0;
106 eval { $t = Term::Cap->Tgetent($vals) };
107 like( $@, qr/failed termcap lookup/, 'Tgetent() should dies with bad termcap' );
108
109 SKIP: {
110         skip( "Can't write 'tcout' file for tests", 8 ) unless $writable;
111
112         # it shouldn't try to read one file more than 32(!) times
113         # see __END__ for a really awful termcap example
114
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');
119
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' );
129
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' );
133 }
134
135 # Tgoto has comments on the expected formats
136 $t->{_test} = "a%d";
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' );
139
140 $t->{_test} = "a%.";
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' );
144
145 $t->{_test} = 'a%+';
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' );
152
153 $t->{_test} = '%ra%d';
154 is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' );
155
156 $t->{_test} = 'a%>11bc';
157 is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' );
158
159 $t->{_test} = 'a%21';
160 is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' );
161
162 $t->{_test} = 'a%31';
163 is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' );
164
165 $t->{_test} = '%ia%21';
166 is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' );
167
168 $t->{_test} = '%z';
169 is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' );
170
171 # and this is pretty standard
172 package TieOut;
173
174 sub TIEHANDLE {
175         bless( \(my $self), $_[0] );
176 }
177
178 sub PRINT {
179         my $self = shift;
180         $$self .= join('', @_);
181 }
182
183 sub read {
184         my $self = shift;
185         substr( $$self, 0, length($$self), '' );
186 }
187
188 __END__
189 bar: :tc=bar: \
190 baz: \
191 :f1: :f2: \
192 :no@ \
193 :k1#v1\
194 :k2=v2\\n2