| 1 | package Test::CPAN::Health::Result; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use autodie qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Carp qw(croak); | ||
| 8 | use Readonly; | ||
| 9 | use Params::Validate::Strict qw(validate_strict); | ||
| 10 | |||
| 11 | our $VERSION = '0.1.0'; | ||
| 12 | |||
| 13 | Readonly::Array my @VALID_STATUSES => qw(pass warn fail skip error); | ||
| 14 | Readonly::Hash my %STATUS_SET => map { $_ => 1 } @VALID_STATUSES; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Test::CPAN::Health::Result - Outcome of a single health check | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Test::CPAN::Health::Result; | ||
| 23 | |||
| 24 | my $result = Test::CPAN::Health::Result->new( | ||
| 25 | check_id => 'sem_ver', | ||
| 26 | status => 'pass', | ||
| 27 | score => 100, | ||
| 28 | summary => 'Version 1.2.3 follows semantic versioning', | ||
| 29 | ); | ||
| 30 | |||
| 31 | print $result->status; # 'pass' | ||
| 32 | print $result->score; # 100 | ||
| 33 | print $result->is_pass ? 'ok' : 'not ok'; | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | Encapsulates the outcome of one L<Test::CPAN::Health::Check> run. Each | ||
| 38 | Result carries a status (pass/warn/fail/skip/error), an optional 0-100 | ||
| 39 | score used in the weighted overall calculation, a human-readable summary, | ||
| 40 | an optional list of detail strings, an optional URL pointing to external | ||
| 41 | evidence, and an optional raw data hashref for machine-readable reporters. | ||
| 42 | |||
| 43 | =head1 LIMITATIONS | ||
| 44 | |||
| 45 | =over 4 | ||
| 46 | |||
| 47 | =item * C<score> is not validated to the range 0..100 at construction time; | ||
| 48 | enforcement is the caller's responsibility. | ||
| 49 | |||
| 50 | =back | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ($class, %args) = @_; | ||
| 56 | |||
| 57 | %args = %{ validate_strict( | ||
| 58 | schema => { | ||
| 59 | check_id => { type => 'string', min => 1 }, | ||
| 60 | status => { type => 'string', min => 1 }, | ||
| 61 | score => { type => 'integer', min => 0, max => 100, optional => 1 }, | ||
| 62 | summary => { type => 'string', optional => 1 }, | ||
| 63 | details => { type => 'arrayref', optional => 1 }, | ||
| 64 | url => { type => 'string', optional => 1 }, | ||
| 65 | data => { type => 'hashref', optional => 1 }, | ||
| 66 | }, | ||
| 67 | input => \%args, | ||
| 68 | ) }; | ||
| 69 | |||
| 70 | croak "Invalid status '$args{status}'; expected one of: " . join(', ', @VALID_STATUSES) | ||
| 71 | unless $STATUS_SET{ $args{status} }; | ||
| 72 | |||
| 73 | my $self = bless { | ||
| 74 | _check_id => $args{check_id}, | ||
| 75 | _status => $args{status}, | ||
| 76 | _score => $args{score}, | ||
| 77 | _summary => $args{summary} // '', | ||
| 78 | _details => $args{details} // [], | ||
| 79 | _url => $args{url}, | ||
| 80 | _data => $args{data} // {}, | ||
| 81 | }, $class; | ||
| 82 | |||
| 83 | return $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head2 check_id | ||
| 87 | |||
| 88 | =head3 PURPOSE | ||
| 89 | |||
| 90 | Returns the stable string identifier of the check that produced this result. | ||
| 91 | |||
| 92 | =head3 API SPECIFICATION | ||
| 93 | |||
| 94 | =head4 INPUT | ||
| 95 | |||
| 96 | None. | ||
| 97 | |||
| 98 | =head4 OUTPUT | ||
| 99 | |||
| 100 | Scalar string. | ||
| 101 | |||
| 102 | =head3 MESSAGES | ||
| 103 | |||
| 104 | Code | Severity | Message | Resolution | ||
| 105 | ------+----------+------------------------------------+--------------------- | ||
| 106 | | | | | ||
| 107 | |||
| 108 | =head3 FORMAL SPECIFICATION | ||
| 109 | |||
| 110 | -- Z schema (placeholder) -- | ||
| 111 | check_id : String | ||
| 112 | ------------------------------------------------------- | ||
| 113 | check_id /= "" | ||
| 114 | |||
| 115 | =head3 SIDE EFFECTS | ||
| 116 | |||
| 117 | None. | ||
| 118 | |||
| 119 | =head3 USAGE EXAMPLE | ||
| 120 | |||
| 121 | print $result->check_id; # 'sem_ver' | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub check_id { my ($self) = @_; return $self->{_check_id} } | ||
| 126 | |||
| 127 | =head2 status | ||
| 128 | |||
| 129 | =head3 PURPOSE | ||
| 130 | |||
| 131 | Returns the pass/warn/fail/skip/error status string. | ||
| 132 | |||
| 133 | =head3 API SPECIFICATION | ||
| 134 | |||
| 135 | =head4 INPUT | ||
| 136 | |||
| 137 | None. | ||
| 138 | |||
| 139 | =head4 OUTPUT | ||
| 140 | |||
| 141 | One of: C<pass>, C<warn>, C<fail>, C<skip>, C<error>. | ||
| 142 | |||
| 143 | =head3 MESSAGES | ||
| 144 | |||
| 145 | Code | Severity | Message | Resolution | ||
| 146 | ------+----------+------------------------------------+--------------------- | ||
| 147 | | | | | ||
| 148 | |||
| 149 | =head3 FORMAL SPECIFICATION | ||
| 150 | |||
| 151 | -- Z schema (placeholder) -- | ||
| 152 | status : {pass, warn, fail, skip, error} | ||
| 153 | |||
| 154 | =head3 SIDE EFFECTS | ||
| 155 | |||
| 156 | None. | ||
| 157 | |||
| 158 | =head3 USAGE EXAMPLE | ||
| 159 | |||
| 160 | print $result->status; # 'fail' | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub status { my ($self) = @_; return $self->{_status} } | ||
| 165 | sub score { my ($self) = @_; return $self->{_score} } | ||
| 166 | sub summary { my ($self) = @_; return $self->{_summary} } | ||
| 167 | sub details { my ($self) = @_; return $self->{_details} } | ||
| 168 | sub url { my ($self) = @_; return $self->{_url} } | ||
| 169 | sub data { my ($self) = @_; return $self->{_data} } | ||
| 170 | |||
| 171 | # Convenience predicates -- avoids scattered string comparisons in reporters | ||
| 172 | |||
| 173 | sub is_pass { my ($self) = @_; return $self->{_status} eq 'pass' } | ||
| 174 | sub is_warn { my ($self) = @_; return $self->{_status} eq 'warn' } | ||
| 175 | sub is_fail { my ($self) = @_; return $self->{_status} eq 'fail' } | ||
| 176 | sub is_skip { my ($self) = @_; return $self->{_status} eq 'skip' } | ||
| 177 | sub is_error { my ($self) = @_; return $self->{_status} eq 'error' } | ||
| 178 | |||
| 179 | =head2 as_hash | ||
| 180 | |||
| 181 | =head3 PURPOSE | ||
| 182 | |||
| 183 | Serialise the Result to a plain hashref, suitable for JSON encoding or | ||
| 184 | passing between processes. | ||
| 185 | |||
| 186 | =head3 API SPECIFICATION | ||
| 187 | |||
| 188 | =head4 INPUT | ||
| 189 | |||
| 190 | None. | ||
| 191 | |||
| 192 | =head4 OUTPUT | ||
| 193 | |||
| 194 | Hashref with keys: check_id, status, score, summary, details, url, data. | ||
| 195 | |||
| 196 | =head3 MESSAGES | ||
| 197 | |||
| 198 | Code | Severity | Message | Resolution | ||
| 199 | ------+----------+------------------------------------+--------------------- | ||
| 200 | | | | | ||
| 201 | |||
| 202 | =head3 FORMAL SPECIFICATION | ||
| 203 | |||
| 204 | -- Z schema (placeholder) -- | ||
| 205 | AsHashOp | ||
| 206 | result : Result | ||
| 207 | output : Hashref | ||
| 208 | ------------------------------------------------------- | ||
| 209 | dom(output) = {check_id, status, score, summary, details, url, data} | ||
| 210 | |||
| 211 | =head3 SIDE EFFECTS | ||
| 212 | |||
| 213 | None. | ||
| 214 | |||
| 215 | =head3 USAGE EXAMPLE | ||
| 216 | |||
| 217 | my $href = $result->as_hash; | ||
| 218 | my $json = encode_json($href); | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub as_hash { | ||
| 223 | my ($self) = @_; | ||
| 224 | |||
| 225 | return { | ||
| 226 | check_id => $self->{_check_id}, | ||
| 227 | status => $self->{_status}, | ||
| 228 | score => $self->{_score}, | ||
| 229 | summary => $self->{_summary}, | ||
| 230 | details => [ @{$self->{_details}} ], | ||
| 231 | url => $self->{_url}, | ||
| 232 | data => { %{$self->{_data}} }, | ||
| 233 | }; | ||
| 234 | } | ||
| 235 | |||
| 236 | =head1 AUTHOR | ||
| 237 | |||
| 238 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 239 | |||
| 240 | =head1 LICENSE AND COPYRIGHT | ||
| 241 | |||
| 242 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 243 | |||
| 244 | This program is free software; you can redistribute it and/or modify it | ||
| 245 | under the terms of the GNU General Public License as published by the Free | ||
| 246 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 247 | any later version. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | 1; | ||