Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 67 lines (53 sloc) 2.09 KB
#!/usr/bin/env perl
use Test::More;
use HTTP::Tiny;
use JSON;
my $json;
die "\nUsage:\n\n ./cheatsheet_check /path/to/cheatsheet.json\n\n" unless scalar @ARGV;
subtest 'file' => sub {
BAIL_OUT("Error reading file $ARGV[0]") unless -e $ARGV[0];
ok 1, 'file exists'; # checked ^
my @filepath = split '/', $ARGV[0];
my $filename = pop @filepath;
like $filename, qr /^[^\s|_]+?\.json$/,'filename is appropriate';
ok my $content = do { local $/ = undef;<> }, 'file content can be read';
ok $json = decode_json($content), 'content is valid JSON';
};
subtest 'headers' => sub {
ok exists $json->{id} && $json->{id}, 'has id';
ok exists $json->{name} && $json->{name}, 'has name';
ok exists $json->{description} && $json->{description}, 'has description';
};
subtest 'metadata' => sub {
ok exists $json->{metadata}, 'has metadata';
ok exists $json->{metadata}{sourceName}, 'has metadata sourceName';
ok exists $json->{metadata}{sourceUrl}, 'has metadata sourceUrl';
ok my $url = $json->{metadata}{sourceUrl}, 'sourceUrl is not undef';
SKIP: {
skip 'sourceUrl is missing, unable to check it', 1 unless $url;
ok(HTTP::Tiny->new->get($url)->{success}, 'fetch sourceUrl');
};
};
subtest 'sections' => sub {
ok my $order = $json->{section_order}, 'has section_order';
is ref $order, 'ARRAY', 'section_order is an array of section names';
ok my $sections = $json->{sections}, 'has sections';
is ref $sections, 'HASH', 'sections is a hash of section key/pairs';
for my $section_name (@$order)
{
ok my $section = $sections->{$section_name}, "'$section_name' exists in sections";
}
for my $section_name (keys %$sections)
{
ok grep(/\Q$section_name\E/, @$order), "'$section_name' exists in section_order";
is ref $sections->{$section_name}, 'ARRAY', "'$section_name' is an array";
my $entry_count = 0;
for my $entry (@{$sections->{$section_name}})
{
ok exists $entry->{key}, "'$section_name' entry: $entry_count has a key";
ok exists $entry->{val}, "'$section_name' entry: $entry_count has a val";
$entry_count++;
}
}
};
done_testing;