zeroclickinfo-goodies/t/RgbColor.t

181 lines
4.2 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}->{hex},
data => {
subtitle_prefix => 'Mix ',
%params,
}
2016-07-05 03:45:40 -07:00
);
}
sub build_answer_random {
my %params = @_;
2016-07-05 03:45:40 -07:00
return (
text_answer => re($color_re),
data => {
result_color => superhashof({
hex => re($color_re)
}),
subtitle_prefix => 'Random color between ',
%params,
2016-07-05 03:45:40 -07:00
},
);
}
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);
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',
subtitle_content => 'DDH.rgb_color.sub_list',
},
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 #
################
my $black = {
hex => '#000000',
name => 'black',
};
my $white = {
hex => '#ffffff',
name => 'white',
};
my $grey = {
hex => '#7f7f7f',
name => '',
};
my $pink = {
hex => '#ffc0cb',
name => 'pink',
};
my $blue = {
hex => '#0000ff',
name => 'blue',
};
my $orange = {
hex => '#ffa500',
name => 'orange',
};
my $bluish_orange = {
hex => '#7f527f',
name => '',
};
my $pinkish_blue = {
hex => '#7f60e5',
name => '',
};
my $tc_mix_black_white = build_test('mix',
input_colors => [$black, $white],
result_color => $grey,
2016-07-05 03:49:19 -07:00
);
my $tc_mix_pink_blue = build_test('mix',
input_colors => [$pink, $blue],
result_color => $pinkish_blue,
);
my $tc_mix_blue_orange = build_test('mix',
input_colors => [$blue, $orange],
result_color => $bluish_orange,
);
my $tc_random_black_white = build_test('random',
input_colors => [$black, $white],
);
my $tc_random_white_black = build_test('random',
input_colors => [$white, $black],
);
2016-07-04 10:57:54 -07:00
ddg_goodie_test(
[qw( DDG::Goodie::RgbColor )],
# Random colors
'random color' => $tc_random_black_white,
'rand color' => $tc_random_black_white,
# # With bounds
'random color between white and black' => $tc_random_white_black,
# # # W/o 'and'
'random color between black white' => $tc_random_black_white,
2016-07-05 01:38:04 -07:00
# Using 'colour'
'random colour' => $tc_random_black_white,
2016-07-05 03:45:40 -07:00
# Mixing colors
'mix 000000 ffffff' => $tc_mix_black_white,
2016-07-05 03:49:19 -07:00
# # With leading '#'
'mix #000000 #ffffff' => $tc_mix_black_white,
2016-07-05 03:50:31 -07:00
# # 'and'
'mix 000000 and ffffff' => $tc_mix_black_white,
# # Using names
'mix black and white' => $tc_mix_black_white,
# Sample queries (from checking query suggestions)
'mix pink and blue what color do you get' => $tc_mix_pink_blue,
'what do you get if you mix blue and orange' => $tc_mix_blue_orange,
# Invalid queries
'color' => undef,
'color ffffff' => undef,
'color picker' => undef,
'color picker ffffff' => undef,
'mix' => undef,
# # From sample queries
'random color names' => undef,
'mix colors to make black' => undef,
'how to mix concrete' => undef,
'blue and gold dress' => undef,
'opposite of blue raining jane lyrics' => undef,
'pictures of blue rain' => undef,
'blue hex color' => undef,
# # With potential to trigger in the future
'blue and gold' => undef,
'opposite of blue' => undef,
"what's opposite of blue on the color wheel" => undef,
2016-07-04 10:57:54 -07:00
);
done_testing;