Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Refactored DDG::Test::Goodie to Package::Stash
Browse files Browse the repository at this point in the history
  • Loading branch information
Getty committed May 23, 2012
1 parent 1bc9bb5 commit 75b6d3f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 60 deletions.
65 changes: 32 additions & 33 deletions lib/DDG/Test.pm
Expand Up @@ -8,48 +8,47 @@ use Class::Load ':all';
use DDG::Request;
use DDG::Block::Words;
use DDG::Block::Regexp;
use Package::Stash;

sub import {
my ( $class, %params ) = @_;
my $target = caller;

{
no strict "refs";

*{"${target}::block_test"} = sub {
my $result_callback = shift;
my $plugins_ref = shift;
my @plugins = @{$plugins_ref};
my @regexp; my @words;
for (@plugins) {
load_class($_);
if ($_->triggers_block_type eq 'Words') {
push @words, $_;
} elsif ($_->triggers_block_type eq 'Regexp') {
push @regexp, $_;
} else {
croak "Unknown plugin type";
}
my $stash = Package::Stash->new($target);
$stash->add_symbol('&block_test',sub {
my $result_callback = shift;
my $plugins_ref = shift;
my @plugins = @{$plugins_ref};
my @regexp; my @words;
for (@plugins) {
load_class($_);
if ($_->triggers_block_type eq 'Words') {
push @words, $_;
} elsif ($_->triggers_block_type eq 'Regexp') {
push @regexp, $_;
} else {
croak "Unknown plugin type";
}
my $words_block = @words ? DDG::Block::Words->new( plugins => [@words]) : undef;
my $regexp_block = @regexp ? DDG::Block::Regexp->new( plugins => [@regexp]) : undef;
while (@_) {
my $query = shift;
my $target = shift;
my $request = DDG::Request->new({ query_raw => $query });
my $answer = undef;
( $answer ) = $words_block->request($request) if $words_block;
( $answer ) = $regexp_block->request($request) if $regexp_block && !$answer;
if ( defined $target ) {
for ($answer) {
$result_callback->($query,$answer,$target);
}
} else {
is($answer,$target,'Checking for not matching on '.$query);
}
my $words_block = @words ? DDG::Block::Words->new( plugins => [@words]) : undef;
my $regexp_block = @regexp ? DDG::Block::Regexp->new( plugins => [@regexp]) : undef;
while (@_) {
my $query = shift;
my $target = shift;
my $request = DDG::Request->new({ query_raw => $query });
my $answer = undef;
( $answer ) = $words_block->request($request) if $words_block;
( $answer ) = $regexp_block->request($request) if $regexp_block && !$answer;
if ( defined $target ) {
for ($answer) {
$result_callback->($query,$answer,$target);
}
} else {
is($answer,$target,'Checking for not matching on '.$query);
}
}
}
});

}

1;
51 changes: 25 additions & 26 deletions lib/DDG/Test/Goodie.pm
Expand Up @@ -10,38 +10,37 @@ use DDG::ZeroClickInfo;
sub import {
my ( $class, %params ) = @_;
my $target = caller;
my $stash = Package::Stash->new($target);

{
no strict "refs";
my %zci_params;

my %zci_params;
$stash->add_symbol('&test_zci', sub {
my $answer = shift;
ref $_[0] eq 'HASH' ?
DDG::ZeroClickInfo->new(%zci_params, %{$_[0]}, answer => $answer ) :
DDG::ZeroClickInfo->new(%zci_params, @_, answer => $answer )
};

*{"${target}::test_zci"} = sub {
my $answer = shift;
ref $_[0] eq 'HASH' ?
DDG::ZeroClickInfo->new(%zci_params, %{$_[0]}, answer => $answer ) :
DDG::ZeroClickInfo->new(%zci_params, @_, answer => $answer )
};

*{"${target}::zci"} = sub {
if (ref $_[0] eq 'HASH') {
for (keys %{$_[0]}) {
$zci_params{$_} = $_[0]->{$_};
}
} else {
while (@_) {
my $key = shift;
my $value = shift;
$zci_params{$key} = $value;
}
$stash->add_symbol('&zci', sub {
if (ref $_[0] eq 'HASH') {
for (keys %{$_[0]}) {
$zci_params{$_} = $_[0]->{$_};
}
} else {
while (@_) {
my $key = shift;
my $value = shift;
$zci_params{$key} = $value;
}
};
}
});

*{"${target}::ddg_goodie_test"} = sub { block_test(sub {
$stash->add_symbol('&ddg_goodie_test', sub { block_test(sub {
my $query = shift;
my $answer = shift;
my $zci = shift;
if ($answer) {
fail('Doesnt expected result but get one on '.$query) unless defined $zci;
if (ref $zci->answer eq 'Regexp') {
like($answer->answer,$zci->answer,'Regexp check against text for '.$query);
$zci->{answer} = $answer->answer;
Expand All @@ -52,10 +51,10 @@ sub import {
}
is_deeply($answer,$zci,'Testing query '.$query);
} else {
fail('Expected result but dont get one on '.$query);
fail('Expected result but dont get one on '.$query) unless defined $answer;
}
},@_)};
}
},@_)
});

}

Expand Down
2 changes: 1 addition & 1 deletion t/15-request.t
Expand Up @@ -39,7 +39,7 @@ BEGIN {
words => [qw( bang test-test )],
triggers => {
0 => [qw( !bang bang )],
2 => [qw( test test-test testtest )],
2 => ["test", "test test", "test-test", "testtest"],
},
},
'other !bang test' => {
Expand Down

0 comments on commit 75b6d3f

Please sign in to comment.