Commit | Line | Data |
1285de5c |
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' ); |
c6d685f7 |
33 | is( $path, $files, 'termcap_path() should find default files' ); |
1285de5c |
34 | |
35 | SKIP: { |
36 | # this is ugly, but -f $0 really *ought* to work |
b4419834 |
37 | skip("-f $0 fails, some tests difficult now", 2) unless -f $0; |
1285de5c |
38 | |
b4419834 |
39 | $ENV{TERMCAP} = $ENV{TERMPATH} = $0; |
40 | ok( grep($0, Term::Cap::termcap_path()), |
c6d685f7 |
41 | 'termcap_path() should find file from $ENV{TERMCAP}' ); |
1285de5c |
42 | |
43 | $ENV{TERMCAP} = (grep { $^O eq $_ } qw( os2 MSWin32 dos )) ? 'a:/' : '/'; |
b4419834 |
44 | ok( grep($0, Term::Cap::termcap_path()), |
c6d685f7 |
45 | 'termcap_path() should find file from $ENV{TERMPATH}' ); |
1285de5c |
46 | } |
47 | |
1285de5c |
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 |
c6d685f7 |
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' ); |
1285de5c |
59 | |
60 | $t->{PADDING} = 2; |
c6d685f7 |
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' ); |
1285de5c |
63 | |
c6d685f7 |
64 | is( $t->Tputs('PADDING'), 2, 'Tputs() should return existing value' ); |
65 | is( $t->Tputs('pc', 2), 'pc', 'Tputs() should delegate to Tpad()' ); |
1285de5c |
66 | $t->Tputs('pc', 1, *OUT); |
c6d685f7 |
67 | is( $t->{pc}, 'pc', 'Tputs() should cache pc value when asked' ); |
68 | is( $out->read(), 'pc', 'Tputs() should write to filehandle when passed' ); |
1285de5c |
69 | |
70 | eval { $t->Trequire( 'pc' ) }; |
c6d685f7 |
71 | is( $@, '', 'Trequire() should finds existing cap' ); |
1285de5c |
72 | eval { $t->Trequire( 'nonsense' ) }; |
c6d685f7 |
73 | like( $@, qr/support: \(nonsense\)/, |
74 | 'Trequire() should croak with unsupported cap' ); |
1285de5c |
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) }; |
c6d685f7 |
85 | like( $@, qr/TERM not set/, 'Tgetent() should croaks without TERM' ); |
86 | like( $warn, qr/OSPEED was not set/, 'Tgetent() should set default OSPEED' ); |
1285de5c |
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) }; |
c6d685f7 |
93 | is( $warn, '', 'Tgetent() should not work if OSPEED is provided' ); |
94 | is( $vals->{PADDING}, 200, 'Tgetent() should set slow PADDING when needed' ); |
1285de5c |
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) }; |
c6d685f7 |
101 | isn't( $@, '', 'Tgetent() should catch bad termcap file' ); |
1285de5c |
102 | |
b4419834 |
103 | # if there's no valid termcap file found, it should croak |
1285de5c |
104 | $vals->{TERM} = ''; |
b4419834 |
105 | $ENV{TERMPATH} = $0; |
1285de5c |
106 | eval { $t = Term::Cap->Tgetent($vals) }; |
b4419834 |
107 | like( $@, qr/failed termcap lookup/, 'Tgetent() should dies with bad termcap' ); |
1285de5c |
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) }; |
c6d685f7 |
118 | like( $@, qr/failed termcap loop/, 'Tgetent() should catch deep recursion'); |
1285de5c |
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); |
c6d685f7 |
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' ); |
1285de5c |
129 | |
130 | # and it should have set these two fields |
c6d685f7 |
131 | is( $t->{_pc}, "\0", 'should set _pc field correctly' ); |
132 | is( $t->{_bc}, "\b", 'should set _bc field correctly' ); |
1285de5c |
133 | } |
134 | |
135 | # Tgoto has comments on the expected formats |
136 | $t->{_test} = "a%d"; |
c6d685f7 |
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' ); |
1285de5c |
139 | |
140 | $t->{_test} = "a%."; |
c6d685f7 |
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' ); |
1285de5c |
144 | |
145 | $t->{_test} = 'a%+'; |
c6d685f7 |
146 | like( $t->Tgoto('test', '', 1), qr/a\x01/, 'Tgoto() shoudl handle %+' ); |
1285de5c |
147 | $t->{_test} = 'a%+a'; |
c6d685f7 |
148 | is( $t->Tgoto('test', '', 1), 'ab', 'Tgoto() should handle %+char' ); |
1285de5c |
149 | $t->{_test} .= 'a' x 99; |
c6d685f7 |
150 | like( $t->Tgoto('test', '', 1), qr/ba{98}/, |
151 | 'Tgoto() should substr()s %+ if needed' ); |
1285de5c |
152 | |
153 | $t->{_test} = '%ra%d'; |
c6d685f7 |
154 | is( $t->Tgoto('test', 1, ''), 'a1', 'Tgoto() should swaps params with %r' ); |
1285de5c |
155 | |
156 | $t->{_test} = 'a%>11bc'; |
c6d685f7 |
157 | is( $t->Tgoto('test', '', 1), 'abc', 'Tgoto() should unpack args with %>' ); |
1285de5c |
158 | |
159 | $t->{_test} = 'a%21'; |
c6d685f7 |
160 | is( $t->Tgoto('test'), 'a001', 'Tgoto() should format with %2' ); |
1285de5c |
161 | |
162 | $t->{_test} = 'a%31'; |
c6d685f7 |
163 | is( $t->Tgoto('test'), 'a0001', 'Tgoto() should also formats with %3' ); |
1285de5c |
164 | |
165 | $t->{_test} = '%ia%21'; |
c6d685f7 |
166 | is( $t->Tgoto('test', '', 1), 'a021', 'Tgoto() should increment args with %i' ); |
1285de5c |
167 | |
168 | $t->{_test} = '%z'; |
c6d685f7 |
169 | is( $t->Tgoto('test'), 'OOPS', 'Tgoto() should catch invalid args' ); |
1285de5c |
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 |