Merge pull request #2692 from duckduckgo/gd/cheatsheet-tests-aliases

CheatSheets: Add alias trigger tests
master
Zach Thompson 2016-03-07 11:51:00 -07:00
commit 7380c1c8c6
3 changed files with 59 additions and 21 deletions

View File

@ -23,6 +23,9 @@
# #
# Where "trigger_type" is one of start, end, startend, or any. # Where "trigger_type" is one of start, end, startend, or any.
# #
# NOTE: The use of 'any' is highly discouraged as it is unstable and
# may not perform as expected.
#
# NOTE: Categories should be in alphabetical (lexical) order (and preferably the # NOTE: Categories should be in alphabetical (lexical) order (and preferably the
# triggers and trigger types as well) - this ensures it is as easy as possible # triggers and trigger types as well) - this ensures it is as easy as possible
# to find relevant categories. # to find relevant categories.

View File

@ -37,6 +37,26 @@ sub id_to_file_name {
return $id . '.json'; return $id . '.json';
} }
sub check_aliases_for_triggers {
my ($aliases, $trigger_types) = @_;
my @aliases = @$aliases;
while (my ($trigger_type, $triggers) = each %{$trigger_types}) {
my @triggers = @$triggers;
foreach my $alias (@aliases) {
my $trigger;
if (
($trigger_type =~ /^start/
&& ($trigger = first { $alias =~ /^$_/ } @triggers))
|| ($trigger_type =~ /end$/
&& ($trigger = first { $alias =~ /$_$/ } @triggers))
) {
return ($alias, $trigger);
}
}
}
return;
}
# Iterate over all Cheat Sheet JSON files... # Iterate over all Cheat Sheet JSON files...
foreach my $path (glob("$json_dir/*.json")){ foreach my $path (glob("$json_dir/*.json")){
next if $ARGV[0] && $path ne "$json_dir/$ARGV[0].json"; next if $ARGV[0] && $path ne "$json_dir/$ARGV[0].json";
@ -63,12 +83,6 @@ foreach my $path (glob("$json_dir/*.json")){
$temp_pass = (!exists $json->{description} && !$json->{description})? 0 : 1; $temp_pass = (!exists $json->{description} && !$json->{description})? 0 : 1;
push(@tests, {msg => "has description (optional but suggested)", critical => 0, pass => $temp_pass}); push(@tests, {msg => "has description (optional but suggested)", critical => 0, pass => $temp_pass});
### ID tests ###
if (my $cheat_id = $json->{id}) {
$temp_pass = id_to_file_name($cheat_id) eq $file_name;
push(@tests, {msg => "Invalid file name ($file_name) for ID ($cheat_id)", critical => 1, pass => $temp_pass});
}
### Template Type tests ### ### Template Type tests ###
$temp_pass = (exists $json->{template_type} && $json->{template_type})? 1 : 0; $temp_pass = (exists $json->{template_type} && $json->{template_type})? 1 : 0;
push(@tests, {msg => 'must specify a template type', critical => 1, pass => $temp_pass}); push(@tests, {msg => 'must specify a template type', critical => 1, pass => $temp_pass});
@ -76,21 +90,54 @@ foreach my $path (glob("$json_dir/*.json")){
$temp_pass = (exists $template_map->{$template_type}); $temp_pass = (exists $template_map->{$template_type});
push(@tests, {msg => "Invalid template_type '$template_type'", critical => 1, pass => $temp_pass}); push(@tests, {msg => "Invalid template_type '$template_type'", critical => 1, pass => $temp_pass});
### Trigger tests ### my %categories;
if (my $cheat_id = $json->{id}) { if (my $cheat_id = $json->{id}) {
### ID tests ###
$temp_pass = id_to_file_name($cheat_id) eq $file_name;
push(@tests, {msg => "Invalid file name ($file_name) for ID ($cheat_id)", critical => 1, pass => $temp_pass});
### Trigger tests ###
if (my $custom = $triggers_yaml->{custom_triggers}->{$cheat_id}) { if (my $custom = $triggers_yaml->{custom_triggers}->{$cheat_id}) {
%categories = map { $_ => 1 } @{$custom->{additional_categories}};
# Duplicate triggers # Duplicate triggers
foreach my $trigger (flat_triggers($custom)) { foreach my $trigger (flat_triggers($custom)) {
$temp_pass = $triggers{$trigger}++ ? 0 : 1; $temp_pass = $triggers{$trigger}++ ? 0 : 1;
push(@tests, {msg => "trigger '$trigger' already in use", critical => 1, pass => $temp_pass}); push(@tests, {msg => "trigger '$trigger' already in use", critical => 1, pass => $temp_pass});
} }
# Re-adding category # Re-adding category
foreach my $category (@{$custom->{additional_categories}}) { foreach my $category (keys %categories) {
$temp_pass = none { $_ eq $category } @{$template_map->{$template_type}}; $temp_pass = none { $_ eq $category } @{$template_map->{$template_type}};
push(@tests, {msg => "Category '$category' already assigned", critical => 1, pass => $temp_pass}); push(@tests, {msg => "Category '$category' already assigned", critical => 1, pass => $temp_pass});
} }
} }
### Alias tests ###
if (my $aliases = $json->{aliases}) {
my @aliases = @{$aliases};
if ("@aliases" =~ /[[:upper:]]/) {
push(@tests, {msg => "uppercase detected in alias - aliases should be lowercase", critical => 1});
} }
if (first { lc $_ eq $defaultName } @aliases) {
push(@tests, {msg => "aliases should not contain the cheat sheet name ($defaultName)", critical => 1});
}
# Make sure aliases don't contain any category triggers.
while (my ($category, $trigger_types) = each %{$triggers_yaml->{categories}}) {
my $critical = $categories{$category};
if (my ($alias, $trigger) = check_aliases_for_triggers(\@aliases, $trigger_types)) {
push(@tests, {msg => "alias ($alias) contains a trigger ($trigger) defined in the '$category' category", critical => $critical});
}
}
# Make sure aliases don't contain any custom triggers for the cheat sheet.
if (my $custom = $triggers_yaml->{custom_triggers}{$cheat_id}) {
if (my ($alias, $trigger) = check_aliases_for_triggers(\@aliases, $custom)) {
push(@tests, {msg => "alias ($alias) contains a custom trigger ($trigger)", critical => 1});
}
}
}
}
%categories = (%categories, map { $_ => 1 } @{$template_map->{$template_type}});
### Metadata tests ### ### Metadata tests ###
my $has_meta = exists $json->{metadata}; my $has_meta = exists $json->{metadata};
@ -107,18 +154,6 @@ foreach my $path (glob("$json_dir/*.json")){
$temp_pass = (my $url = $json->{metadata}{sourceUrl})? 1 : 0; $temp_pass = (my $url = $json->{metadata}{sourceUrl})? 1 : 0;
push(@tests, {msg => "sourceUrl is not undef $name", critical => 1, pass => $temp_pass, skip => 1});; push(@tests, {msg => "sourceUrl is not undef $name", critical => 1, pass => $temp_pass, skip => 1});;
### Alias tests ###
if (my $aliases = $json->{aliases}) {
my @aliases = @{$aliases};
if ("@aliases" =~ /[[:upper:]]/) {
push(@tests, {msg => "uppercase detected in alias - aliases should be lowercase", critical => 1});
}
if (first { lc $_ eq $defaultName } @aliases) {
push(@tests, {msg => "aliases should not contain the cheat sheet name ($defaultName)", critical => 1});
}
}
### Sections tests ### ### Sections tests ###
$temp_pass = (my $order = $json->{section_order})? 1 : 0; $temp_pass = (my $order = $json->{section_order})? 1 : 0;
push(@tests, {msg => 'has section_order', critical => 1, pass => $temp_pass}); push(@tests, {msg => 'has section_order', critical => 1, pass => $temp_pass});