File Coverage

blib/lib/DDG/Test/Block.pm
Criterion Covered Total %
statement 63 65 96.9
branch 12 16 75.0
condition 3 3 100.0
subroutine 13 13 100.0
pod n/a
total 91 97 93.8


line stmt bran cond sub pod time code
1             package DDG::Test::Block;
2             our $AUTHORITY = 'cpan:DDG';
3             # ABSTRACT: Adds a function to easily test L.
4             $DDG::Test::Block::VERSION = '1016';
5 8     8   28 use strict;
  8         11  
  8         175  
6 8     8   23 use warnings;
  8         9  
  8         133  
7 8     8   31 use Carp;
  8         9  
  8         325  
8 8     8   24 use Test::More;
  8         7  
  8         49  
9 8     8   3728 use Class::Load ':all';
  8         93872  
  8         996  
10 8     8   2004 use DDG::Request;
  8         22  
  8         211  
11 8     8   2887 use DDG::Block::Words;
  8         21  
  8         200  
12 8     8   2357 use DDG::Block::Regexp;
  8         13  
  8         163  
13 8     8   2699 use DDG::Test::Location;
  8         13  
  8         34  
14 8     8   2703 use DDG::Test::Language;
  8         12  
  8         32  
15 8     8   42 use Package::Stash;
  8         10  
  8         2129  
16              
17             sub import {
18 9     9   20 my ( $class, %params ) = @_;
19 9         14 my $target = caller;
20              
21 9         67 my $stash = Package::Stash->new($target);
22              
23              
24             $stash->add_symbol('&block_test',sub {
25 7     7   13 my $result_callback = shift;
26 7         7 my $plugins_ref = shift;
27 7         10 my @plugins = @{$plugins_ref};
  7         15  
28 7         10 my @regexp; my @words;
29 7         15 foreach my $plugin (@plugins) {
30 15         37 load_class($plugin);
31 15 100       2589 if ($plugin->triggers_block_type eq 'Words') {
    50          
32 13         27 push @words, $plugin;
33             } elsif ($plugin->triggers_block_type eq 'Regexp') {
34 2         4 push @regexp, $plugin;
35             } else {
36 0         0 croak "Unknown plugin type";
37             }
38             }
39 7 50       100 my $words_block = @words ? DDG::Block::Words->new( plugins => [@words]) : undef;
40 7 100       75 my $regexp_block = @regexp ? DDG::Block::Regexp->new( plugins => [@regexp]) : undef;
41 7         21 while (@_) {
42 19         14631 my $query = shift;
43 19         20 my $request;
44 19 100       37 if (ref $query eq 'DDG::Request') {
45 5         6 $request = $query;
46 5         13 $query = $request->query_raw;
47             } else {
48 14         39 $request = DDG::Request->new(
49             query_raw => $query,
50             location => test_location('us'),
51             language => test_language('us'),
52             );
53             }
54 19         6218 my $target = shift;
55 19         20 my $answer = undef;
56 19 50       349 ( $answer ) = $words_block->request($request) if $words_block;
57 19 100 100     100 ( $answer ) = $regexp_block->request($request) if $regexp_block && !$answer;
58 19 50       26 if ( defined $target ) {
59 19         27 for ($answer) {
60 19         47 $result_callback->($query,$answer,$target);
61             }
62             } else {
63 0           is($answer,$target,'Checking for not matching on '.$query);
64             }
65             }
66 9         346 });
67              
68             }
69              
70             1;
71              
72             __END__