Unicode properties: fix L& (the #12319 didn't allow L&,
[p5sagit/p5-mst-13.2.git] / lib / Term / Complete.t
CommitLineData
bbd9de80 1#!./perl
2
3BEGIN {
4 chdir 't' unless -d 't';
5 @INC = '../lib';
6}
7
8use warnings;
9use Test::More tests => 8;
10use vars qw( $Term::Complete::complete $complete );
fc81c880 11my $restore;
12
bbd9de80 13
cd3eb553 14SKIP: {
15 skip('PERL_SKIP_TTY_TEST', 8) if $ENV{PERL_SKIP_TTY_TEST};
16
17 my $TTY;
18 if ($^O eq 'rhapsody' && -c "/dev/ttyp0") { $TTY = "/dev/ttyp0" }
19 elsif (-c "/dev/tty") { $TTY = "/dev/tty" }
20 if (defined $TTY) {
21 open(TTY, $TTY) or die "open $TTY failed: $!";
22 skip("$TTY not a tty", 8) if defined $TTY && ! -t TTY;
fc81c880 23 $restore = `stty -g`;
24 skip("Can't reliably restore $TTY", 8) if $?;
25 }
cd3eb553 26
bbd9de80 27use_ok( 'Term::Complete' );
28
29*complete = \$Term::Complete::complete;
30
31my $in = tie *STDIN, 'FakeIn', "fro\t";
32my $out = tie *STDOUT, 'FakeOut';
33my @words = ( 'frobnitz', 'frobozz', 'frostychocolatemilkshakes' );
34
35Complete('', \@words);
36my $data = get_expected('fro', @words);
37
38# there should be an \a after our word
39like( $$out, qr/fro\a/, 'found bell character' );
40
41# now remove the \a -- there should be only one
42is( $out->scrub(), 1, '(single) bell removed');
43
44# 'fro' should match all three words
45like( $$out, qr/$data/, 'all three words possible' );
46$out->clear();
47
48# should only find 'frobnitz' and 'frobozz'
49$in->add('frob');
50Complete('', @words);
51$out->scrub();
52is( $$out, get_expected('frob', 'frobnitz', 'frobozz'), 'expected frob*' );
53$out->clear();
54
55# should only do 'frobozz'
56$in->add('frobo');
57Complete('', @words);
58$out->scrub();
59is( $$out, get_expected( 'frobo', 'frobozz' ), 'only frobozz possible' );
60$out->clear();
61
62# change the completion character
63$complete = "!";
64$in->add('frobn');
65Complete('prompt:', @words);
66$out->scrub();
67like( $$out, qr/prompt:frobn/, 'prompt is okay' );
68
69# now remove the prompt and we should be okay
70$$out =~ s/prompt://g;
71is( $$out, get_expected('frobn', 'frobnitz' ), 'works with new $complete' );
fc81c880 72`stty $restore`;
73
74} # end of SKIP, end of tests
bbd9de80 75
76# easier than matching space characters
77sub get_expected {
78 my $word = shift;
79 return join('.', $word, @_, $word, '.');
80}
81
82package FakeIn;
83
84sub TIEHANDLE {
85 my ($class, $text) = @_;
86 $text .= "$main::complete\025";
87 bless(\$text, $class);
88}
89
90sub add {
91 my ($self, $text) = @_;
92 $$self = $text . "$main::complete\025";
93}
94
95sub GETC {
96 my $self = shift;
97 return length $$self ? substr($$self, 0, 1, '') : "\r";
98}
99
100package FakeOut;
101
102sub TIEHANDLE {
103 bless(\(my $text), $_[0]);
104}
105
106sub clear {
107 ${ $_[0] } = '';
108}
109
110# remove the bell character
111sub scrub {
112 ${ $_[0] } =~ tr/\a//d;
113}
114
115# must shift off self
116sub PRINT {
117 my $self = shift;
118 ($$self .= join('', @_)) =~ s/\s+/./gm;
119}