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

Commit

Permalink
HexToDec: Convert to text template (#3054)
Browse files Browse the repository at this point in the history
* HexToDec: Converting to text template

* HexToDec: Refactoring tests
  • Loading branch information
mintsoft authored and GuiltyDolphin committed May 15, 2016
1 parent c2250e1 commit 0cc555f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 64 deletions.
12 changes: 8 additions & 4 deletions lib/DDG/Goodie/HexToDec.pm
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ handle query_raw => sub {
my $decimal = Math::BigInt->from_hex($hex);

return "$hex base 16 = $decimal base 10", structured_answer => {
input => ['0x' . $hex],
operation => 'Hex to decimal',
result => "$decimal", # Quoted for display precision as string.
data => {
title => "$decimal", # Quoted for display precision as string.
subtitle => "Hex to decimal: 0x" . $hex
},
templates => {
group => 'text',
}
};
};

0x41414141;
0x01;
91 changes: 31 additions & 60 deletions t/HexToDec.t
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,43 @@ use DDG::Test::Goodie;
zci answer_type => 'hex_to_dec';
zci is_cached => 1;

sub build_test
{
my ($text, $title, $subtitle) = @_;

return test_zci($text, structured_answer => {
data => {
title => $title,
subtitle => "Hex to decimal: $subtitle"
},
templates => {
group => 'text'
}
});
}

ddg_goodie_test(
[qw( DDG::Goodie::HexToDec )],
'0xd1038d2e07b42569' => test_zci(
'0xd1038d2e07b42569' => build_test(
'd1038d2e07b42569 base 16 = 15061036807694329193 base 10',
structured_answer => {
input => ['0xd1038d2e07b42569'],
operation => 'Hex to decimal',
result => 15061036807694329193
}
),
'0x44696f21' => test_zci(
'44696f21 base 16 = 1147760417 base 10',
structured_answer => {
input => ['0x44696f21'],
operation => 'Hex to decimal',
result => 1147760417,
}
),
'0xffffffffffffffffffffff' => test_zci(
'ffffffffffffffffffffff base 16 = 309485009821345068724781055 base 10',
structured_answer => {
input => ['0xffffffffffffffffffffff'],
operation => 'Hex to decimal',
result => "309485009821345068724781055",
}
),
'0xff in decimal' => test_zci(
'ff base 16 = 255 base 10',
structured_answer => {
input => ['0xff'],
operation => 'Hex to decimal',
result => "255",
}
),
'0xff hex to dec' => test_zci(
'ff base 16 = 255 base 10',
structured_answer => {
input => ['0xff'],
operation => 'Hex to decimal',
result => "255",
}
),
'0xff as base 10' => test_zci(
'ff base 16 = 255 base 10',
structured_answer => {
input => ['0xff'],
operation => 'Hex to decimal',
result => "255",
}
15061036807694329193,
'0xd1038d2e07b42569'
),
'0xff in base-10' => test_zci(
'ff base 16 = 255 base 10',
structured_answer => {
input => ['0xff'],
operation => 'Hex to decimal',
result => "255",
}
'0x44696f21' => build_test(
'44696f21 base 16 = 1147760417 base 10',
1147760417,
'0x44696f21'
),
'0xff hex to base10' => test_zci(
'ff base 16 = 255 base 10',
structured_answer => {
input => ['0xff'],
operation => 'Hex to decimal',
result => "255",
}
'0xffffffffffffffffffffff' => build_test(
'ffffffffffffffffffffff base 16 = 309485009821345068724781055 base 10',
"309485009821345068724781055",
'0xffffffffffffffffffffff'
),
'0xff in decimal' => build_test('ff base 16 = 255 base 10', "255", '0xff'),
'0xff hex to dec' => build_test('ff base 16 = 255 base 10', "255", '0xff'),
'0xff as base 10' => build_test('ff base 16 = 255 base 10', "255", '0xff'),
'0xff in base-10' => build_test('ff base 16 = 255 base 10', "255", '0xff'),
'0xff hex to base10' => build_test('ff base 16 = 255 base 10', "255", '0xff'),
'0x44696f2Z' => undef,
'ascii 0x74657374' => undef,
'0x255 hex' => undef,
Expand Down

0 comments on commit 0cc555f

Please sign in to comment.