Commit | Line | Data |
1285de5c |
1 | #!./perl |
2 | |
6eca1408 |
3 | my $file; |
1285de5c |
4 | BEGIN { |
6eca1408 |
5 | $file = $0; |
1285de5c |
6 | chdir 't' if -d 't'; |
7 | @INC = '../lib'; |
8 | } |
9 | |
10 | END { |
11 | # let VMS whack all versions |
12 | 1 while unlink('tcout'); |
13 | } |
14 | |
15 | use Test::More tests => 43; |
16 | |
cdfc6b8a |
17 | # these names are hardcoded in Term::Cap |
18 | my $files = join '', grep { -f $_ } ( $ENV{HOME} . '/.termcap', '/etc/termcap', |
19 | '/usr/share/misc/termcap' ); |
20 | unless ($files) { |
21 | SKIP: { |
22 | skip('no termcap available to test', 43); |
23 | } |
24 | exit; |
25 | } |
26 | |
1285de5c |
27 | use_ok( 'Term::Cap' ); |
28 | |
29 | local (*TCOUT, *OUT); |
30 | my $out = tie *OUT, 'TieOut'; |
31 | my $writable = 1; |
32 | |
33 | if (open(TCOUT, ">tcout")) { |
34 | print TCOUT <DATA>; |
35 | close TCOUT; |
36 | } else { |
37 | $writable = 0; |
38 | } |
39 | |
cdfc6b8a |
40 | # termcap_path |
1285de5c |
41 | $ENV{TERMCAP} = ''; |
42 | my $path = join '', Term::Cap::termcap_path(); |
c6d685f7 |
43 | is( $path, $files, 'termcap_path() should find default files' ); |
1285de5c |
44 | |
45 | SKIP: { |
46 | # this is ugly, but -f $0 really *ought* to work |
6eca1408 |
47 | skip("-f $file fails, some tests difficult now", 2) unless -f $file; |
1285de5c |
48 | |
6eca1408 |
49 | $ENV{TERMCAP} = $ENV{TERMPATH} = $file; |
50 | ok( grep($file, Term::Cap::termcap_path()), |
c6d685f7 |
51 | 'termcap_path() should find file from $ENV{TERMCAP}' ); |
1285de5c |
52 | |
26ca33de |
53 | $ENV{TERMCAP} = '/'; |
6eca1408 |
54 | ok( grep($file, Term::Cap::termcap_path()), |
c6d685f7 |
55 | 'termcap_path() should find file from $ENV{TERMPATH}' ); |
1285de5c |
56 | } |
57 | |
1285de5c |
58 | # make a Term::Cap "object" |
59 | my $t = { |
60 | PADDING => 1, |
61 | _pc => 'pc', |
62 | }; |
63 | bless($t, 'Term::Cap' ); |
64 | |
65 | # see if Tpad() works |
c6d685f7 |
66 | is( $t->Tpad(), undef, 'Tpad() should return undef with no arguments' ); |
67 | is( $t->Tpad('x'), 'x', 'Tpad() should return strings verbatim with no match' ); |
68 | is( $t->Tpad( '1*a', 2 ), 'apcpc', 'Tpad() should pad paddable strings' ); |
1285de5c |
69 | |
70 | $t->{PADDING} = 2; |
c6d685f7 |
71 | is( $t->Tpad( '1*a', 3, *OUT ), 'apcpc', 'Tpad() should perform pad math' ); |
72 | is( $out->read(), 'apcpc', 'Tpad() should write to filehandle when passed' ); |
1285de5c |
73 | |
c6d685f7 |
74 | is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' ); |
75 | is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' ); |
1285de5c |
76 | $t->Tputs('pc', 1, *OUT); |
c6d685f7 |
77 | is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' ); |
78 | is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' ); |
1285de5c |
79 | |
80 | eval { $t->Trequire( 'pc' ) }; |
c6d685f7 |
81 | is( $@, '', 'Trequire() should finds existing cap' ); |
1285de5c |
82 | eval { $t->Trequire( 'nonsense' ) }; |
c6d685f7 |
83 | like( $@, qr/support: \(nonsense\)/, |
84 | 'Trequire() should croak with unsupported cap' ); |
1285de5c |
85 | |
86 | my $warn; |
87 | local $SIG{__WARN__} = sub { |
88 | $warn = $_[0]; |
89 | }; |
90 | |
91 | # test the first few features by forcing Tgetent() to croak (line 156) |
92 | undef $ENV{TERM}; |
93 | my $vals = {}; |
94 | eval { $t = Term::Cap->Tgetent($vals) }; |
c6d685f7 |
95 | like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' ); |
96 | like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' ); |
1285de5c |
97 | is( $vals->{PADDING}, 10000/9600, 'Default OSPEED implies default PADDING' ); |
98 | |
99 | # check values for very slow speeds |
100 | $vals->{OSPEED} = 1; |
101 | $warn = ''; |
102 | eval { $t = Term::Cap->Tgetent($vals) }; |
c6d685f7 |
103 | is( $warn, '', 'Tgetent() should not work if OSPEED is provided' ); |
104 | is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' ); |
1285de5c |
105 | |
106 | # now see if lines 177 or 180 will fail |
107 | $ENV{TERM} = 'foo'; |
108 | $ENV{TERMPATH} = '!'; |
109 | $ENV{TERMCAP} = ''; |
110 | eval { $t = Term::Cap->Tgetent($vals) }; |
c6d685f7 |
111 | isn't( $@, '', 'Tgetent() should catch bad termcap file' ); |
1285de5c |
112 | |
1285de5c |
113 | SKIP: { |
6eca1408 |
114 | skip( "Can't write 'tcout' file for tests", 9 ) unless $writable; |
115 | |
116 | # it won't find the termtype in this fake file, so it should croak |
117 | $vals->{TERM} = 'quux'; |
118 | $ENV{TERMPATH} = 'tcout'; |
119 | eval { $t = Term::Cap->Tgetent($vals) }; |
120 | like( $@, qr/failed termcap/, 'Tgetent() should die with bad termcap' ); |
1285de5c |
121 | |
122 | # it shouldn't try to read one file more than 32(!) times |
123 | # see __END__ for a really awful termcap example |
1285de5c |
124 | $ENV{TERMPATH} = join(' ', ('tcout') x 33); |
125 | $vals->{TERM} = 'bar'; |
126 | eval { $t = Term::Cap->Tgetent($vals) }; |
c6d685f7 |
127 | like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion'); |
1285de5c |
128 | |
129 | # now let it read a fake termcap file, and see if it sets properties |
130 | $ENV{TERMPATH} = 'tcout'; |
131 | $vals->{TERM} = 'baz'; |
132 | $t = Term::Cap->Tgetent($vals); |
c6d685f7 |
133 | is( $t->{_f1}, 1, 'Tgetent() should set a single field correctly' ); |
134 | is( $t->{_f2}, 1, 'Tgetent() should set another field on the same line' ); |
135 | is( $t->{_no}, '', 'Tgetent() should set a blank field correctly' ); |
136 | is( $t->{_k1}, 'v1', 'Tgetent() should set a key value pair correctly' ); |
137 | like( $t->{_k2}, qr/v2\\\n2/, 'Tgetent() should set and translate pairs' ); |
1285de5c |
138 | |
139 | # and it should have set these two fields |
c6d685f7 |
140 | is( $t->{_pc}, "\0", 'should set _pc field correctly' ); |
141 | is( $t->{_bc}, "\b", 'should set _bc field correctly' ); |
1285de5c |
142 | } |
143 | |
144 | # Tgoto has comments on the expected formats |
145 | $t->{_test} = "a%d"; |
c6d685f7 |
146 | is( $t->Tgoto('test', '', 1, *OUT), 'a1', 'Tgoto() should handle %d code' ); |
147 | is( $out->read(), 'a1', 'Tgoto() should print to filehandle if passed' ); |
1285de5c |
148 | |
149 | $t->{_test} = "a%."; |
c6d685f7 |
150 | like( $t->Tgoto('test', '', 1), qr/^a\x01/, 'Tgoto() should handle %.' ); |
151 | like( $t->Tgoto('test', '', 0), qr/\x61\x01\x08/, |
152 | 'Tgoto() should handle %. and magic' ); |
1285de5c |
153 | |
154 | $t->{_test} = 'a%+'; |
c6d685f7 |
155 | like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() shoudl handle %+' ); |
1285de5c |
156 | $t->{_test} = 'a%+a'; |
c6d685f7 |
157 | is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' ); |
1285de5c |
158 | $t->{_test} .= 'a' x 99; |
c6d685f7 |
159 | like( $t->Tgoto('test', '', 1), qr/ba{98}/, |
160 | 'Tgoto() should substr()s %+ if needed' ); |
1285de5c |
161 | |
162 | $t->{_test} = '%ra%d'; |
c6d685f7 |
163 | is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' ); |
1285de5c |
164 | |
165 | $t->{_test} = 'a%>11bc'; |
c6d685f7 |
166 | is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' ); |
1285de5c |
167 | |
168 | $t->{_test} = 'a%21'; |
c6d685f7 |
169 | is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' ); |
1285de5c |
170 | |
171 | $t->{_test} = 'a%31'; |
c6d685f7 |
172 | is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' ); |
1285de5c |
173 | |
174 | $t->{_test} = '%ia%21'; |
c6d685f7 |
175 | is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' ); |
1285de5c |
176 | |
177 | $t->{_test} = '%z'; |
c6d685f7 |
178 | is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' ); |
1285de5c |
179 | |
180 | # and this is pretty standard |
181 | package TieOut; |
182 | |
183 | sub TIEHANDLE { |
184 | bless( \(my $self), $_[0] ); |
185 | } |
186 | |
187 | sub PRINT { |
188 | my $self = shift; |
189 | $$self .= join('', @_); |
190 | } |
191 | |
192 | sub read { |
193 | my $self = shift; |
194 | substr( $$self, 0, length($$self), '' ); |
195 | } |
196 | |
197 | __END__ |
198 | bar: :tc=bar: \ |
199 | baz: \ |
200 | :f1: :f2: \ |
201 | :no@ \ |
202 | :k1#v1\ |
203 | :k2=v2\\n2 |