File Coverage

File:blib/lib/App/Test/Generator/Provenance.pm
Coverage:69.8%

linestmtbrancondsubtimecode
1package App::Test::Generator::Provenance;
2
3
168
168
168
364
101
1243
use strict;
4
168
168
168
175
100
2268
use warnings;
5
6
168
168
168
172
121
4569
use Exporter qw(import);
7
8our $VERSION = '0.52';
9
10our @EXPORT_OK = qw(
11        SOURCE_VALIDATOR SOURCE_INPUT_SPEC SOURCE_POD
12        SOURCE_CODE SOURCE_SIGNATURE SOURCE_FIELD
13        sources documented_sources type_checks_source
14        source_is_valid source_is_documented source_checks_types
15);
16
17 - 202
=head1 NAME

App::Test::Generator::Provenance - The allowed values of a schema parameter's C<_source> marker

=head1 VERSION

Version 0.52

=head1 SYNOPSIS

    use App::Test::Generator::Provenance qw(
        SOURCE_POD source_is_documented source_is_valid
    );

    # When the extractor records where a parameter came from
    $spec->{_source} = SOURCE_POD;

    # When the harness decides whether a constraint is documentation
    $is_hint = source_is_documented($spec->{_source});

=head1 DESCRIPTION

Every generated schema parameter carries a C<_source> marker saying where
its type and constraints came from.  The extractor writes the marker and
the generated harness reads it, so both must agree on the same set of
values.  Defining that set here, once, stops the two from drifting apart:
the extractor can only emit a value from L</sources>, and the harness only
treats a value from L</documented_sources> as documentation.

The markers are:

    value        meaning
    validator    the routine's own validator confirmed the type
    input_spec   an input specification in the POD declared the type
    pod          ordinary POD prose declared the type or a constraint
    code         the routine's code revealed the parameter
    signature    a subroutine signature declared the parameter
    field        a field declaration declared the parameter

A documented marker (C<input_spec> or C<pod>) describes valid input; it
is not a check the routine was seen to make.  C<validator> is the marker
that says the routine checks types itself.  The other markers say only
that the parameter exists.

=head1 CONSTANTS

=head2 SOURCE_VALIDATOR, SOURCE_INPUT_SPEC, SOURCE_POD, SOURCE_CODE, SOURCE_SIGNATURE, SOURCE_FIELD

The six allowed marker values, exported on request.

=head1 FUNCTIONS

=head2 sources

Returns the allowed marker values as a list.  Use it to validate a marker
read from a schema.

=head3 API SPECIFICATION

=head4 input

    {}

=head4 output

    {}

A list of strings, not a reference.  No type is declared because the schema
format has no type for a returned list.

=head2 documented_sources

Returns the markers that describe documentation rather than an enforced
check: C<pod> and C<input_spec>.

=head3 API SPECIFICATION

=head4 input

    {}

=head4 output

    {}

A list of strings, not a reference.  No type is declared because the schema
format has no type for a returned list.

=head2 type_checks_source

Returns the marker that says the routine checks types itself:
C<validator>.

=head3 API SPECIFICATION

=head4 input

    {}

=head4 output

    { type => SCALAR }

=head2 source_is_valid

    $ok = source_is_valid($source);

True when the marker is one of L</sources>.

=head3 API SPECIFICATION

=head4 input

    {
        source => { type => 'any', optional => 1 },
    }

An undefined value or a reference is never a marker and gives 0; nothing
is thrown.

=head4 output

    { type => SCALAR }

1 or 0.

=head2 source_is_documented

    $hint = source_is_documented($source);

True when the marker is one of L</documented_sources>.  An undefined or
unknown marker is not documentation.

=head3 API SPECIFICATION

=head4 input

    {
        source => { type => 'any', optional => 1 },
    }

An undefined value or a reference is never a marker and gives 0; nothing
is thrown.

=head4 output

    { type => SCALAR }

1 or 0.

=head2 source_checks_types

    $checked = source_checks_types($source);

True when the marker is L</type_checks_source>.

=head3 API SPECIFICATION

=head4 input

    {
        source => { type => 'any', optional => 1 },
    }

An undefined value or a reference is never a marker and gives 0; nothing
is thrown.

=head4 output

    { type => SCALAR }

1 or 0.

=head1 AUTHOR

Nigel Horne, C<< <njh at nigelhorne.com> >>

=head1 LICENCE AND COPYRIGHT

Copyright 2026 Nigel Horne.

Usage is subject to the terms of GPL2.
If you use it,
please let me know.

=cut
203
204
168
168
168
184
99
2816
use constant SOURCE_VALIDATOR  => 'validator';
205
168
168
168
170
497
1962
use constant SOURCE_INPUT_SPEC => 'input_spec';
206
168
168
168
244
148
1710
use constant SOURCE_POD        => 'pod';
207
168
168
168
175
85
1497
use constant SOURCE_CODE       => 'code';
208
168
168
168
174
118
1585
use constant SOURCE_SIGNATURE  => 'signature';
209
168
168
168
183
108
2439
use constant SOURCE_FIELD      => 'field';
210
211
168
1882
use constant _SOURCES => [
212        SOURCE_VALIDATOR,
213        SOURCE_INPUT_SPEC,
214        SOURCE_POD,
215        SOURCE_CODE,
216        SOURCE_SIGNATURE,
217        SOURCE_FIELD,
218
168
168
201
82
];
219
220
168
13723
use constant _DOCUMENTED_SOURCES => [
221        SOURCE_POD,
222        SOURCE_INPUT_SPEC,
223
168
168
160
99
];
224
225sub sources
226{
227
2
2
1
3
        return @{_SOURCES()};
228}
229
230sub documented_sources
231{
232
183
183
1892
517
        return @{_DOCUMENTED_SOURCES()};
233}
234
235sub type_checks_source
236{
237
183
904
        return SOURCE_VALIDATOR();
238}
239
240sub source_is_valid
241{
242
2
592
        my ($source) = @_;
243
244
2
5
        return 0 unless defined($source) && !ref($source) && length($source);
245
2
12
4
8
        return scalar grep { $_ eq $source } sources();
246}
247
248sub source_is_documented
249{
250
0
        my ($source) = @_;
251
252
0
        return 0 unless defined($source) && !ref($source);
253
0
0
        return scalar grep { $_ eq $source } documented_sources();
254}
255
256sub source_checks_types
257{
258
0
        my ($source) = @_;
259
260
0
        return 0 unless defined($source) && !ref($source);
261
0
        return ($source eq SOURCE_VALIDATOR()) ? 1 : 0;
262}
263
2641;