| 1 | ## no critic (RequireUseStrict) | ||||||||||
| 2 | package Bash::Completion::Plugin::Test; | ||||||||||
| 3 | { | ||||||||||
| 4 | 2 | $Bash::Completion::Plugin::Test::VERSION = '0.01'; | |||||||||
| 5 | } | ||||||||||
| 6 | |||||||||||
| 7 | ## use critic (RequireUseStrict) | ||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Bash::Completion::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 28 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 16 | 28 | my ( $class, %params ) = @_; | |||||||||
| 17 | |||||||||||
| 18 | 28 | my $plugin = delete $params{'plugin'}; | |||||||||
| 19 | |||||||||||
| 20 | 28 | ▶ | croak 'plugin parameter required' unless defined $plugin; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 27 | ▶ | if(my @bad_keys = keys %params) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | croak "invalid parameters: " . join(' ', @bad_keys); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 27 | $class->_load_plugin($plugin); | |||||||||
| 27 | |||||||||||
| 28 | 26 | return bless { | |||||||||
| 29 | plugin_class => $plugin, | ||||||||||
| 30 | }, $class; | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 26 | ▶ | sub check_completions { | ||||||||
|
Subroutine
check_completions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 26 | my ( $self, $command_line, $expected_completions, $name ) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 26 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||
| 37 | |||||||||||
| 38 | 26 | my $req = $self->_create_request($command_line); | |||||||||
| 39 | 26 | my $plugin = $self->_create_plugin; | |||||||||
| 40 | |||||||||||
| 41 | 26 | $plugin->complete($req); | |||||||||
| 42 | |||||||||||
| 43 | 26 | my @got_completions = $req->candidates; | |||||||||
| 44 | |||||||||||
| 45 | 26 | is_deeply [ sort @got_completions ], [ sort @$expected_completions ], | |||||||||
| 46 | $name; | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 26 | ▶ | sub _cursor_character { | ||||||||
|
Subroutine
_cursor_character: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 26 | return '^'; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 26 | ▶ | sub _extract_cursor { | ||||||||
|
Subroutine
_extract_cursor: called CC=3 SCAR=11.8
| |||||||||||
| 54 | 26 | my ( $self, $command_line ) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 26 | my $cursor_char = $self->_cursor_character; | |||||||||
| 57 | |||||||||||
| 58 | 26 | my $index = index $command_line, $cursor_char; | |||||||||
| 59 | |||||||||||
| 60 | 26 | ▶ | if($index == -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | croak "Failed to find cursor character in command line"; | |||||||||
| 62 | } | ||||||||||
| 63 | 26 | my $replacements = $command_line =~ s/\Q$cursor_char\E//g; | |||||||||
| 64 | |||||||||||
| 65 | 26 | ▶ | if($replacements > 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | croak "More than one cursor character in command line"; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 26 | return ( $command_line, $index ); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 26 | ▶ | sub _create_request { | ||||||||
|
Subroutine
_create_request: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 26 | my ( $self, $command_line ) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 26 | my $cursor_index; | |||||||||
| 76 | 26 | ( $command_line, $cursor_index ) = $self->_extract_cursor($command_line); | |||||||||
| 77 | |||||||||||
| 78 | 26 | local $ENV{'COMP_LINE'} = $command_line; | |||||||||
| 79 | 26 | local $ENV{'COMP_POINT'} = $cursor_index; | |||||||||
| 80 | |||||||||||
| 81 | 26 | return Bash::Completion::Request->new; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 26 | ▶ | sub _create_plugin { | ||||||||
|
Subroutine
_create_plugin: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 26 | my ( $self ) = @_; | |||||||||
| 86 | |||||||||||
| 87 | 26 | my $plugin_class = $self->{'plugin_class'}; | |||||||||
| 88 | |||||||||||
| 89 | 26 | return $plugin_class->new; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 27 | ▶ | sub _load_plugin { | ||||||||
|
Subroutine
_load_plugin: called CC=2 SCAR=6.9
| |||||||||||
| 93 | 27 | my ( $self, $plugin_class ) = @_; | |||||||||
| 94 | |||||||||||
| 95 | 27 | my $plugin_path = $plugin_class; | |||||||||
| 96 | |||||||||||
| 97 | 27 | $plugin_path =~ s{::}{/}g; | |||||||||
| 98 | 27 | $plugin_path .= '.pm'; | |||||||||
| 99 | |||||||||||
| 100 | 27 | my $ok = eval { | |||||||||
| 101 | 27 | require $plugin_path; | |||||||||
| 102 | }; | ||||||||||
| 103 | 27 | ▶ | unless($ok) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 1 | croak "Could not load plugin '$plugin_class': $@"; | |||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 2 | 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<Bash::Completion::Plugin::Test> is a module for testing | ||||||||||
| 134 | L<Bash::Completion> 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<plugin>, 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<Bash::Completion> | ||||||||||
| 156 | |||||||||||
| 157 | =head1 AUTHOR | ||||||||||
| 158 | |||||||||||
| 159 | Rob Hoelz <rob@hoelz.ro> | ||||||||||
| 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__ | ||||||||||