Commit | Line | Data |
bbd9de80 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' unless -d 't'; |
5 | @INC = '../lib'; |
6 | } |
7 | |
8 | use warnings; |
9 | use Test::More tests => 8; |
10 | use vars qw( $Term::Complete::complete $complete ); |
fc81c880 |
11 | my $restore; |
12 | |
bbd9de80 |
13 | |
cd3eb553 |
14 | SKIP: { |
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 |
27 | use_ok( 'Term::Complete' ); |
28 | |
29 | *complete = \$Term::Complete::complete; |
30 | |
31 | my $in = tie *STDIN, 'FakeIn', "fro\t"; |
32 | my $out = tie *STDOUT, 'FakeOut'; |
33 | my @words = ( 'frobnitz', 'frobozz', 'frostychocolatemilkshakes' ); |
34 | |
35 | Complete('', \@words); |
36 | my $data = get_expected('fro', @words); |
37 | |
38 | # there should be an \a after our word |
39 | like( $$out, qr/fro\a/, 'found bell character' ); |
40 | |
41 | # now remove the \a -- there should be only one |
42 | is( $out->scrub(), 1, '(single) bell removed'); |
43 | |
44 | # 'fro' should match all three words |
45 | like( $$out, qr/$data/, 'all three words possible' ); |
46 | $out->clear(); |
47 | |
48 | # should only find 'frobnitz' and 'frobozz' |
49 | $in->add('frob'); |
50 | Complete('', @words); |
51 | $out->scrub(); |
52 | is( $$out, get_expected('frob', 'frobnitz', 'frobozz'), 'expected frob*' ); |
53 | $out->clear(); |
54 | |
55 | # should only do 'frobozz' |
56 | $in->add('frobo'); |
57 | Complete('', @words); |
58 | $out->scrub(); |
59 | is( $$out, get_expected( 'frobo', 'frobozz' ), 'only frobozz possible' ); |
60 | $out->clear(); |
61 | |
62 | # change the completion character |
63 | $complete = "!"; |
64 | $in->add('frobn'); |
65 | Complete('prompt:', @words); |
66 | $out->scrub(); |
67 | like( $$out, qr/prompt:frobn/, 'prompt is okay' ); |
68 | |
69 | # now remove the prompt and we should be okay |
70 | $$out =~ s/prompt://g; |
71 | is( $$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 |
77 | sub get_expected { |
78 | my $word = shift; |
79 | return join('.', $word, @_, $word, '.'); |
80 | } |
81 | |
82 | package FakeIn; |
83 | |
84 | sub TIEHANDLE { |
85 | my ($class, $text) = @_; |
86 | $text .= "$main::complete\025"; |
87 | bless(\$text, $class); |
88 | } |
89 | |
90 | sub add { |
91 | my ($self, $text) = @_; |
92 | $$self = $text . "$main::complete\025"; |
93 | } |
94 | |
95 | sub GETC { |
96 | my $self = shift; |
97 | return length $$self ? substr($$self, 0, 1, '') : "\r"; |
98 | } |
99 | |
100 | package FakeOut; |
101 | |
102 | sub TIEHANDLE { |
103 | bless(\(my $text), $_[0]); |
104 | } |
105 | |
106 | sub clear { |
107 | ${ $_[0] } = ''; |
108 | } |
109 | |
110 | # remove the bell character |
111 | sub scrub { |
112 | ${ $_[0] } =~ tr/\a//d; |
113 | } |
114 | |
115 | # must shift off self |
116 | sub PRINT { |
117 | my $self = shift; |
118 | ($$self .= join('', @_)) =~ s/\s+/./gm; |
119 | } |