| 1 | package DDG::Test::Block; | ||||||||||||||||||||||||||||
| 2 | 8 | our $AUTHORITY = 'cpan:DDG'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Adds a function to easily test L<DDG::Block>. | ||||||||||||||||||||||||||||
| 4 | 8 | $DDG::Test::Block::VERSION = '1016'; | |||||||||||||||||||||||||||
| 5 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Test::More; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Class::Load ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use DDG::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use DDG::Block::Words; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use DDG::Block::Regexp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use DDG::Test::Location; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use DDG::Test::Language; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use Package::Stash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub import { | ||||||||||||||||||||||||||||
| 18 | 9 | ▶ | my ( $class, %params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 9 | my $target = caller; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 9 | my $stash = Package::Stash->new($target); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $stash->add_symbol('&block_test',sub { | ||||||||||||||||||||||||||||
| 25 | 7 | ▶ | my $result_callback = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.9
| |||||||||||||||||||||||||||||
| 26 | 7 | my $plugins_ref = shift; | |||||||||||||||||||||||||||
| 27 | 7 | my @plugins = @{$plugins_ref}; | |||||||||||||||||||||||||||
| 28 | 7 | my @regexp; my @words; | |||||||||||||||||||||||||||
| 29 | 7 | foreach my $plugin (@plugins) { | |||||||||||||||||||||||||||
| 30 | 15 | load_class($plugin); | |||||||||||||||||||||||||||
| 31 | 15 | ▶ | if ($plugin->triggers_block_type eq 'Words') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | 13 | push @words, $plugin; | |||||||||||||||||||||||||||
| 33 | } elsif ($plugin->triggers_block_type eq 'Regexp') { | ||||||||||||||||||||||||||||
| 34 | 2 | push @regexp, $plugin; | |||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||
| 36 | 0 | croak "Unknown plugin type"; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 7 | ▶ | my $words_block = @words ? DDG::Block::Words->new( plugins => [@words]) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 7 | ▶ | my $regexp_block = @regexp ? DDG::Block::Regexp->new( plugins => [@regexp]) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 19 | my $query = shift; | |||||||||||||||||||||||||||
| 43 | 19 | my $request; | |||||||||||||||||||||||||||
| 44 | 19 | ▶ | if (ref $query eq 'DDG::Request') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 5 | $request = $query; | |||||||||||||||||||||||||||
| 46 | 5 | $query = $request->query_raw; | |||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 14 | $request = DDG::Request->new( | |||||||||||||||||||||||||||
| 49 | query_raw => $query, | ||||||||||||||||||||||||||||
| 50 | location => test_location('us'), | ||||||||||||||||||||||||||||
| 51 | language => test_language('us'), | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 19 | my $target = shift; | |||||||||||||||||||||||||||
| 55 | 19 | my $answer = undef; | |||||||||||||||||||||||||||
| 56 | 19 | ▶ | ( $answer ) = $words_block->request($request) if $words_block; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 19 | ▶ | ( $answer ) = $regexp_block->request($request) if $regexp_block && !$answer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$regexp_block and not $answer
MC/DC100%2/2
$regexp_block and not $answer
$regexp_block
not $answer
Truth table100%3/3
A and not(B)
A= $regexp_blockB= not $answer
| |||||||||||||||||||||||||||||
| 58 | 19 | ▶ | if ( defined $target ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 19 | for ($answer) { | |||||||||||||||||||||||||||
| 60 | 19 | $result_callback->($query,$answer,$target); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | is($answer,$target,'Checking for not matching on '.$query); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 9 | }); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 8 | 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||