Added basic version of bitsum goodie.

master
kste 2015-06-11 14:45:35 +00:00
parent 292277c9bc
commit b36e5bee99
2 changed files with 75 additions and 0 deletions

53
lib/DDG/Goodie/Bitsum.pm Normal file
View File

@ -0,0 +1,53 @@
package DDG::Goodie::Bitsum;
# ABSTRACT: Write an abstract here
# Start at https://duck.co/duckduckhack/goodie_overview if you are new
# to instant answer development
use DDG::Goodie;
use strict;
zci answer_type => "bitsum";
zci is_cached => 1;
# Metadata. See https://duck.co/duckduckhack/metadata for help in filling out this section.
name "Bitsum";
description "Succinct explanation of what this instant answer does";
primary_example_queries "first example query", "second example query";
secondary_example_queries "optional -- demonstrate any additional triggers";
# Uncomment and complete: https://duck.co/duckduckhack/metadata#category
# category "";
# Uncomment and complete: https://duck.co/duckduckhack/metadata#topics
# topics "";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Bitsum.pm";
attribution github => ["GitHubAccount", "Friendly Name"],
twitter => "twitterhandle";
# Triggers
triggers start => "bitsum", "hammingweight";
# Handle statement
handle remainder => sub {
# optional - regex guard
# return unless qr/^\w+/;
return unless $_; # Guard against "no answer"
my $n = $_;
my $c;
for ($c = 0; $n; $n >>= 1) {
$c += $n & 1;
}
my $result = $c;
return $result,
structured_answer => {
input => [html_enc($_)],
operation => 'Hamming Weight',
result => html_enc($result),
};
};
1;

22
t/Bitsum.t Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "bitsum";
zci is_cached => 1;
ddg_goodie_test(
[qw( DDG::Goodie::Bitsum )],
# At a minimum, be sure to include tests for all:
# - primary_example_queries
# - secondary_example_queries
'example query' => test_zci('query'),
# Try to include some examples of queries on which it might
# appear that your answer will trigger, but does not.
'bad example query' => undef,
);
done_testing;