File Coverage

blib/lib/Bash/Completion/Plugin/Test.pm
Criterion Covered Total %
statement 53 56 94.6
branch 7 10 70.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 74 80 92.5


line stmt bran cond sub pod time code
1             ## no critic (RequireUseStrict)
2             package Bash::Completion::Plugin::Test;
3             {
4             $Bash::Completion::Plugin::Test::VERSION = '0.01';
5             }
6              
7             ## use critic (RequireUseStrict)
8 1     1   23279 use strict;
  1         2  
  1         33  
9 1     1   6 use warnings;
  1         2  
  1         27  
10              
11 1     1   6 use Carp qw(croak);
  1         2  
  1         79  
12 1     1   876 use Bash::Completion::Request;
  1         402  
  1         27  
13 1     1   996 use Test::More;
  1         6000  
  1         9  
14              
15             sub new {
16 28     28 1 69297 my ( $class, %params ) = @_;
17              
18 28         68 my $plugin = delete $params{'plugin'};
19              
20 28 100       153 croak 'plugin parameter required' unless defined $plugin;
21              
22 27 50       98 if(my @bad_keys = keys %params) {
23 0         0 croak "invalid parameters: " . join(' ', @bad_keys);
24             }
25              
26 27         76 $class->_load_plugin($plugin);
27              
28 26         143 return bless {
29             plugin_class => $plugin,
30             }, $class;
31             }
32              
33             sub check_completions {
34 26     26 1 150 my ( $self, $command_line, $expected_completions, $name ) = @_;
35              
36 26         41 local $Test::Builder::Level = $Test::Builder::Level + 1;
37              
38 26         59 my $req = $self->_create_request($command_line);
39 26         763 my $plugin = $self->_create_plugin;
40              
41 26         262 $plugin->complete($req);
42              
43 26         2190 my @got_completions = $req->candidates;
44              
45 26         293 is_deeply [ sort @got_completions ], [ sort @$expected_completions ],
46             $name;
47             }
48              
49             sub _cursor_character {
50 26     26   49 return '^';
51             }
52              
53             sub _extract_cursor {
54 26     26   40 my ( $self, $command_line ) = @_;
55            
56 26         52 my $cursor_char = $self->_cursor_character;
57              
58 26         54 my $index = index $command_line, $cursor_char;
59              
60 26 50       52 if($index == -1) {
61 0         0 croak "Failed to find cursor character in command line";
62             }
63 26         113 my $replacements = $command_line =~ s/\Q$cursor_char\E//g;
64              
65 26 50       64 if($replacements > 1) {
66 0         0 croak "More than one cursor character in command line";
67             }
68              
69 26         73 return ( $command_line, $index );
70             }
71              
72             sub _create_request {
73 26     26   37 my ( $self, $command_line ) = @_;
74              
75 26         30 my $cursor_index;
76 26         49 ( $command_line, $cursor_index ) = $self->_extract_cursor($command_line);
77              
78 26         151 local $ENV{'COMP_LINE'} = $command_line;
79 26         104 local $ENV{'COMP_POINT'} = $cursor_index;
80              
81 26         102 return Bash::Completion::Request->new;
82             }
83              
84             sub _create_plugin {
85 26     26   42 my ( $self ) = @_;
86              
87 26         40 my $plugin_class = $self->{'plugin_class'};
88              
89 26         107 return $plugin_class->new;
90             }
91              
92             sub _load_plugin {
93 27     27   47 my ( $self, $plugin_class ) = @_;
94              
95 27         39 my $plugin_path = $plugin_class;
96              
97 27         132 $plugin_path =~ s{::}{/}g;
98 27         39 $plugin_path .= '.pm';
99              
100 27         34 my $ok = eval {
101 27         1549 require $plugin_path;
102             };
103 27 100       8075 unless($ok) {
104 1         26 croak "Could not load plugin '$plugin_class': $@";
105             }
106             }
107              
108             1;
109              
110              
111              
112             =pod
113              
114             =head1 NAME
115              
116             Bash::Completion::Plugin::Test - Module for testing Bash::Completion plugins
117              
118             =head1 VERSION
119              
120             version 0.01
121              
122             =head1 SYNOPSIS
123              
124             my $tester = Bash::Completion::Plugin::Test->new(
125             plugin => $PLUGIN_NAME,
126             );
127              
128             $test->check_completions('my-command ^', \@expected_completeions,
129             $opt_name);
130              
131             =head1 DESCRIPTION
132              
133             L is a module for testing
134             L plugins.
135              
136             =head1 METHODS
137              
138             =head2 Bash::Completion::Plugin::Test->new(%params)
139              
140             Creates a new tester object. C<%params> is a hash of named parameters;
141             currently, the only supported one is C, which is the name of the
142             plugin to test, and is required.
143              
144             =head2 $tester->check_completions($command, \@expected, $name)
145              
146             Runs the current completion plugin against C<$command>, and verifies
147             that the results it returns are the same as those in C<@expected>.
148             The order of the items in C<@expected> does not matter. C<$name> is
149             an optional name for the test. The carat character '^' must be present
150             in C<$command>; it is removed and represents the location of the cursor
151             when completion occurs.
152              
153             =head1 SEE ALSO
154              
155             L
156              
157             =head1 AUTHOR
158              
159             Rob Hoelz
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is copyright (c) 2012 by Rob Hoelz.
164              
165             This is free software; you can redistribute it and/or modify it under
166             the same terms as the Perl 5 programming language system itself.
167              
168             =head1 BUGS
169              
170             Please report any bugs or feature requests on the bugtracker website
171             https://github.com/hoelzro/bash-completion-plugin-test/issues
172              
173             When submitting a bug or request, please include a test-file or a
174             patch to an existing test-file that illustrates the bug or desired
175             feature.
176              
177             =cut
178              
179              
180             __END__