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;
|
|
|
|
|
2016-07-04 11:28:09 -07:00
|
|
|
zci answer_type => "rgb_color";
|
2016-07-05 01:29:42 -07:00
|
|
|
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 01:29:42 -07:00
|
|
|
|
2016-07-05 03:45:40 -07:00
|
|
|
$answer{options} //= {};
|
2016-07-05 01:29:42 -07:00
|
|
|
return $answer{text_answer},
|
2016-07-04 10:57:54 -07:00
|
|
|
structured_answer => {
|
|
|
|
|
2016-07-05 01:29:42 -07:00
|
|
|
data => $answer{data},
|
2016-07-04 10:57:54 -07:00
|
|
|
|
|
|
|
templates => {
|
2016-07-05 01:52:14 -07:00
|
|
|
group => "text",
|
|
|
|
options => {
|
2016-07-05 02:58:39 -07:00
|
|
|
title_content => 'DDH.rgb_color.title_content',
|
2016-07-05 03:45:40 -07:00
|
|
|
%{$answer{options}},
|
2016-07-05 01:52:14 -07:00
|
|
|
},
|
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(
|
2016-07-05 00:52:33 -07:00
|
|
|
[qw( DDG::Goodie::RgbColor )],
|
2016-07-05 01:29:42 -07:00
|
|
|
# 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,
|
2016-07-05 03:25:25 -07:00
|
|
|
# Invalid queries
|
|
|
|
'color' => undef,
|
|
|
|
'color ffffff' => undef,
|
|
|
|
'color picker' => undef,
|
|
|
|
'color picker ffffff' => undef,
|
2016-07-04 10:57:54 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
done_testing;
|