zeroclickinfo-goodies/t/RgbColor.t

95 lines
2.1 KiB
Perl
Raw Normal View History

2016-07-04 10:57:54 -07:00
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Deep;
use DDG::Test::Goodie;
zci answer_type => "rgb_color";
zci is_cached => 0;
2016-07-04 10:57:54 -07:00
2016-07-05 03:45:40 -07:00
###################
# Test Builders #
###################
my %test_builders = (
mix => \&build_answer_mix,
random => \&build_answer_random,
);
my $color_re = qr/^#\p{XDigit}{6}$/i;
sub build_answer_mix {
my %params = @_;
return (
text_answer => $params{result_color},
data => \%params,
options => {
subtitle_content => 'DDH.rgb_color.mix',
},
);
}
sub build_answer_random {
return (
text_answer => re($color_re),
data => {
result_color => re($color_re),
subtitle => 'Random color',
},
);
}
2016-07-04 10:57:54 -07:00
2016-07-05 03:45:40 -07:00
sub build_structured_answer {
my ($type, %test_params) = @_;
my $builder = $test_builders{$type};
my %answer = $builder->(%test_params);
2016-07-05 03:45:40 -07:00
$answer{options} //= {};
return $answer{text_answer},
2016-07-04 10:57:54 -07:00
structured_answer => {
data => $answer{data},
2016-07-04 10:57:54 -07:00
templates => {
group => "text",
options => {
title_content => 'DDH.rgb_color.title_content',
2016-07-05 03:45:40 -07:00
%{$answer{options}},
},
2016-07-04 10:57:54 -07:00
}
};
}
sub build_test { test_zci(build_structured_answer(@_)) }
2016-07-05 03:45:40 -07:00
################
# Test Cases #
################
2016-07-05 03:49:19 -07:00
my $tc_mix_white_black = build_test('mix',
input_colors => ['#000000', '#ffffff'],
result_color => '#7f7f7f',
);
2016-07-04 10:57:54 -07:00
ddg_goodie_test(
[qw( DDG::Goodie::RgbColor )],
# Random colors
2016-07-05 03:45:40 -07:00
'random color' => build_test('random'),
'rand color' => build_test('random'),
2016-07-05 01:38:04 -07:00
# Using 'colour'
2016-07-05 03:45:40 -07:00
'random colour' => build_test('random'),
# Mixing colors
2016-07-05 03:49:19 -07:00
'mix 000000 ffffff' => $tc_mix_white_black,
# # With leading '#'
'mix #000000 #ffffff' => $tc_mix_white_black,
# Invalid queries
'color' => undef,
'color ffffff' => undef,
'color picker' => undef,
'color picker ffffff' => undef,
2016-07-04 10:57:54 -07:00
);
done_testing;