| 1 | package ICANN::RST::Spec; | ||
| 2 | # ABSTRACT: an object representing the RST test specifications. | ||
| 3 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Data::Mirror qw(mirror_file mirror_json); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use ICANN::RST::Case; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use ICANN::RST::ChangeLog; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use ICANN::RST::DataProvider; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use ICANN::RST::Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use ICANN::RST::Input; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use ICANN::RST::Plan; | ||
| 11 | use ICANN::RST::Resource; | ||
| 12 | use ICANN::RST::Suite; | ||
| 13 | use ICANN::RST::Text; | ||
| 14 | use List::Util qw(pairmap); | ||
| 15 | use URI; | ||
| 16 | use YAML::XS; | ||
| 17 | use constant SCHEMA_VERSION => q{1.14.0}; | ||
| 18 | use feature qw(say); | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | $YAML::XS::Boolean = q{JSON::PP}; | ||
| 22 | |||
| 23 | sub url_from_release { | ||
| 24 | my ($package, $release) = @_; | ||
| 25 | |||
| 26 | return URI->new(sprintf(q{https://github.com/icann/rst-test-specs/releases/download/%s/rst-test-specs.yaml}, $release)); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub current_version { shift->url_from_release(mirror_json(q{https://api.github.com/repos/icann/rst-test-specs/releases/latest})->{name}) } | ||
| 30 | |||
| 31 | sub new_from_version { | ||
| 32 | my ($package, $version) = @_; | ||
| 33 | my $file = mirror_file($package->url_from_release($version)); | ||
| 34 | return $file ? $package->new($file) : undef; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub new { | ||
| 38 | my ($package, $file) = @_; | ||
| 39 | |||
| 40 | $file ||= mirror_file($package->current_version); | ||
| 41 | |||
| 42 | my $self = bless( | ||
| 43 | { | ||
| 44 | 'spec' => YAML::XS::LoadFile($file), | ||
| 45 | 'file' => $file | ||
| 46 | }, | ||
| 47 | $package, | ||
| 48 | ); | ||
| 49 | |||
| 50 | my $v = version->parse($self->spec->{'RST-Test-Plan-Schema-Version'}); | ||
| 51 | croak(sprintf("Unsupported schema version '%s', must be >= '%s'", $v, SCHEMA_VERSION)) unless ($v >= SCHEMA_VERSION); | ||
| 52 | |||
| 53 | # | ||
| 54 | # precompile everything | ||
| 55 | # | ||
| 56 | $self->{'_preamble'} = ICANN::RST::Text->new($self->spec->{'Preamble'}); | ||
| 57 | $self->{'_plans'} = [ sort { $a->order <=> $b->order } pairmap { ICANN::RST::Plan -> new($a, $b, $self) } %{$self->spec->{'Test-Plans'}} ]; | ||
| 58 | $self->{'_suites'} = [ sort { $a->order <=> $b->order } pairmap { ICANN::RST::Suite -> new($a, $b, $self) } %{$self->spec->{'Test-Suites'}} ]; | ||
| 59 | $self->{'_cases'} = [ sort { $a->id cmp $b->id } pairmap { ICANN::RST::Case -> new($a, $b, $self) } %{$self->spec->{'Test-Cases'}} ]; | ||
| 60 | $self->{'_inputs'} = [ sort { $a->id cmp $b->id } pairmap { ICANN::RST::Input -> new($a, $b, $self) } %{$self->spec->{'Input-Parameters'}} ]; | ||
| 61 | $self->{'_resources'} = [ sort { $a->id cmp $b->id } pairmap { ICANN::RST::Resource -> new($a, $b, $self) } %{$self->spec->{'Resources'}} ]; | ||
| 62 | $self->{'_errors'} = [ sort { $a->id cmp $b->id } pairmap { ICANN::RST::Error -> new($a, $b, $self) } %{$self->spec->{'Errors'}} ]; | ||
| 63 | $self->{'_providers'} = [ sort { $a->id cmp $b->id } pairmap { ICANN::RST::DataProvider -> new($a, $b, $self) } %{$self->spec->{'Data-Providers'}} ]; | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub changelog { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my @log; | ||
| 72 | |||
| 73 | foreach my $date (reverse(sort(keys(%{$self->spec->{'ChangeLog'}})))) { | ||
| 74 | push(@log, ICANN::RST::ChangeLog->new($date, $self->spec->{'ChangeLog'}->{$date}, $self)); | ||
| 75 | } | ||
| 76 | |||
| 77 | return @log; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub spec { $_[0]->{'spec'} } | ||
| 81 | sub schemaVersion { $_[0]->spec->{'RST-Test-Plan-Schema-Version'} } | ||
| 82 | sub version { $_[0]->spec->{'Version'} } | ||
| 83 | sub lastUpdated { $_[0]->spec->{'Last-Updated'} } | ||
| 84 | sub contactName { $_[0]->spec->{'Contact'}->{'Name'} } | ||
| 85 | sub contactOrg { $_[0]->spec->{'Contact'}->{'Organization'} } | ||
| 86 | sub contactEmail { $_[0]->spec->{'Contact'}->{'Email'} } | ||
| 87 | sub preamble { $_[0]->{'_preamble'} } | ||
| 88 | sub plans { @{ $_[0]->{'_plans'} } } | ||
| 89 | sub suites { @{ $_[0]->{'_suites'} } } | ||
| 90 | sub cases { @{ $_[0]->{'_cases'} } } | ||
| 91 | sub inputs { @{ $_[0]->{'_inputs'} } } | ||
| 92 | sub resources { @{ $_[0]->{'_resources'} } } | ||
| 93 | sub errors { @{ $_[0]->{'_errors'} } } | ||
| 94 | sub providers { @{ $_[0]->{'_providers'} } } | ||
| 95 | sub plan { my ($self, $id) = @_ ; return $self->find($id, $self->plans) } | ||
| 96 | sub suite { my ($self, $id) = @_ ; return $self->find($id, $self->suites) } | ||
| 97 | sub case { my ($self, $id) = @_ ; return $self->find($id, $self->cases) } | ||
| 98 | sub resource { my ($self, $id) = @_ ; return $self->find($id, $self->resources) } | ||
| 99 | sub error { my ($self, $id) = @_ ; return $self->find($id, $self->errors) } | ||
| 100 | sub input { my ($self, $id) = @_ ; return $self->find($id, $self->inputs) } | ||
| 101 | sub provider { my ($self, $id) = @_ ; return $self->find($id, $self->providers) } | ||
| 102 | |||
| 103 | sub find { | ||
| 104 | my ($self, $needle, @haystack) = @_; | ||
| 105 | |||
| 106 | foreach my $stick (@haystack) { | ||
| 107 | return $stick if ($stick->id eq $needle); | ||
| 108 | } | ||
| 109 | |||
| 110 | carp("object '$needle' not found"); | ||
| 111 | |||
| 112 | return undef; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ | ||