File Coverage

blib/lib/DDG/Test/Spice.pm
Criterion Covered Total %
statement 56 66 84.8
branch 2 6 33.3
condition n/a
subroutine 15 16 93.7
pod n/a
total 73 88 82.9


line stmt bran cond sub pod time code
1             package DDG::Test::Spice;
2             our $AUTHORITY = 'cpan:DDG';
3             # ABSTRACT: Adds keywords to easily test Spice plugins.
4             $DDG::Test::Spice::VERSION = '1016';
5 3     14   16618 use strict;
  3         3  
  3         76  
6 3     3   12 use warnings;
  3         3  
  3         71  
7 3     3   9 use Carp;
  3         4  
  3         161  
8 3     3   10 use Test::More;
  3         3  
  3         18  
9 3     3   1172 use DDG::Test::Block;
  3         5  
  3         12  
10 3     3   1082 use DDG::ZeroClickInfo::Spice;
  3         7  
  3         88  
11 3     3   1153 use DDG::Meta::ZeroClickInfoSpice;
  3         8  
  3         94  
12 3     3   19 use Package::Stash;
  3         4  
  3         53  
13 3     3   11 use Class::Load 'load_class';
  3         4  
  3         1186  
14              
15              
16             sub import {
17 3     3   26 my ( $class, %params ) = @_;
18 3         7 my $target = caller;
19 3         34 my $stash = Package::Stash->new($target);
20              
21              
22 3         7 my %spice_params = (
23             call_type => 'include',
24             );
25              
26             $stash->add_symbol('&test_spice', sub {
27 11     11   208 my $call = shift;
28             ref $_[0] eq 'HASH'
29 11 50       155 ? DDG::ZeroClickInfo::Spice->new(%spice_params, %{$_[0]}, call => $call )
  0         0  
30             : DDG::ZeroClickInfo::Spice->new(%spice_params, @_, call => $call )
31 3         45 });
32              
33              
34             $stash->add_symbol('&spice', sub {
35 0 0   0   0 if (ref $_[0] eq 'HASH') {
36 0         0 for (keys %{$_[0]}) {
  0         0  
37 0         0 $spice_params{$_} = $_[0]->{$_};
38             }
39             } else {
40 0         0 while (@_) {
41 0         0 my $key = shift;
42 0         0 my $value = shift;
43 0         0 $spice_params{$key} = $value;
44             }
45             }
46 3         21 });
47              
48              
49             $stash->add_symbol('&ddg_spice_test', sub { block_test(sub {
50 11     11   11 my $query = shift;
51 11         9 my $answer = shift;
52 11         7 my $spice = shift;
53 11 50       13 if ($answer) {
54 11         39 is_deeply($answer,$spice,'Testing query '.$query);
55             } else {
56 0         0 fail('Expected result but dont get one on '.$query);
57             }
58 3     1   15 },@_)});
  1     1   12  
59              
60              
61             $stash->add_symbol('&alt_to_test', sub {
62 1     1   500 my ($spice, $alt_tos) = @_;
63              
64 1         6 load_class($spice);
65              
66 1         84 my $rewrites = $spice->alt_rewrites;
67 1         7 ok($rewrites, "$spice has rewrites");
68              
69 1         186 ok($spice =~ /^(DDG.+::)/, "Extract base from $spice");
70 1         169 my $base = $1;
71              
72 1         3 for my $alt (@$alt_tos){
73 2         161 my ($cb, $path) = @{DDG::Meta::ZeroClickInfoSpice::params_from_target("$base$alt")};
  2         8  
74 2         4 my $rw = $rewrites->{$alt};
75 2         6 ok($rw, "$alt exists");
76 2         330 ok($rw->callback eq $cb, "$alt callback");
77 2         323 ok($rw->path eq $path, "$alt path");
78             }
79 3         77 });
80             }
81              
82             1;
83              
84             __END__