File Coverage

File:blib/lib/App/Test/Generator/Mutator.pm
Coverage:96.5%

linestmtbrancondsubtimecode
1package App::Test::Generator::Mutator;
2
3
15
15
117600
20
use 5.036;
4
15
15
15
873
28263
28
use autodie qw(:io);    # covers open/close/read/write; excludes system (which legitimately fails)
5
15
15
15
25311
26
261
use Carp qw(croak);
6
15
15
15
18
9
223
use Config;
7
15
15
15
2622
36617
346
use File::Copy::Recursive qw(dircopy);
8
15
15
15
32
10
105
use File::Spec;
9
15
15
15
666
10361
189
use File::Temp qw(tempdir);
10
15
15
15
2143
583517
158
use PPI;
11
15
15
15
533
3554
262
use Readonly;
12
13
15
15
15
1752
14
136
use App::Test::Generator::Mutation::BooleanNegation;
14
15
15
15
1580
14
119
use App::Test::Generator::Mutation::ConditionalInversion;
15
15
15
15
1545
16
137
use App::Test::Generator::Mutation::NumericBoundary;
16
15
15
15
1673
15
7365
use App::Test::Generator::Mutation::ReturnUndef;
17
18# --------------------------------------------------
19# Valid mutation level values
20# --------------------------------------------------
21Readonly my $LEVEL_FULL => 'full';
22Readonly my $LEVEL_FAST => 'fast';
23
24# --------------------------------------------------
25# Default values for optional constructor arguments
26# --------------------------------------------------
27Readonly my $DEFAULT_LIB_DIR        => 'lib';
28Readonly my $DEFAULT_MUTATION_LEVEL => $LEVEL_FULL;
29
30# --------------------------------------------------
31# Error message constants — named so test code can
32# match against them without duplicating the literal
33# --------------------------------------------------
34Readonly my $ERR_FILE_REQUIRED     => 'file required';
35Readonly my $ERR_WORKSPACE_NOT_SET => 'Workspace not prepared -- call prepare_workspace first';
36Readonly my $ERR_RELATIVE_NOT_SET  => 'Relative path not set -- call prepare_workspace first';
37
38our $VERSION = '0.52';
39
40=encoding utf8
41
42 - 198
=head1 NAME

App::Test::Generator::Mutator - Generate and apply mutation tests

=head1 VERSION

Version 0.52

=head1 SYNOPSIS

A "mutant" is a copy of your code with one small, deliberate mistake.
If your tests still pass, the mutant "survived" and your tests missed the
mistake.  If a test fails, the mutant was "killed", which is good.

Pattern 1: list the mutants for a file.  Nothing is changed on disk.

    use App::Test::Generator::Mutator;

    my $mutator = App::Test::Generator::Mutator->new(
        file    => 'lib/My/Module.pm',
        lib_dir => 'lib',
    );

    my $mutants = $mutator->generate_mutants();
    printf "Generated %d mutants\n", scalar @{$mutants};
    printf "line %d: %s\n", $_->line, $_->description for @{$mutants};

Pattern 2: test every mutant.  Run this from the top directory of the
project.  The mutated file is copied over the real file for the test run
and the real file is always put back.  This is what
C<bin/test-generator-mutate> does.

    my $file = 'lib/My/Module.pm';
    my $read = sub { open my $fh, '<', $_[0]; local $/; <$fh> };
    my $original = $read->($file);

    for my $m (@{$mutants}) {
        my $workspace = $mutator->prepare_workspace();    # clean copy each time
        $mutator->apply_mutant($m);
        my $mutated = $read->("$workspace/$file");

        { open my $fh, '>', $file; print $fh $mutated }
        my $survived = $mutator->run_tests();
        { open my $fh, '>', $file; print $fh $original }

        printf "%s: %s\n", ($survived ? 'SURVIVED' : 'KILLED'), $m->description;
    }

Pattern 3: use C<fast> mode to get fewer mutants and a shorter run.

    my $quick = App::Test::Generator::Mutator->new(
        file           => 'lib/My/Module.pm',
        mutation_level => 'fast',
    );
    my $fewer = $quick->generate_mutants();
    printf "fast: %d mutants, full: %d mutants\n",
        scalar @{$fewer}, scalar @{$mutants};

Pattern 4: protect lines of your own code from mutation.  Put these two
comment lines around them in the file to be mutated.  Each marker must
be alone on its line.

    ## MUTANT_SKIP_BEGIN
    return 1 if $n <= 1;    # no mutant will be made for this line
    ## MUTANT_SKIP_END

=head1 DESCRIPTION

B<App::Test::Generator::Mutator> is a mutation engine that programmatically
alters Perl source files to evaluate the effectiveness of a project's test
suite. It analyses modules, generates systematic code mutations (such as
conditional inversions, logical operator changes, and numeric boundary
flips), and applies them within an isolated workspace so tests can be
executed safely against each modified variant.

By tracking which mutants are killed (cause tests to fail) versus those that
survive (tests still pass), the module enables calculation of a mutation
score, providing a quantitative measure of how well the test suite detects
unintended behavioural changes.

=head2 new

Construct a new Mutator for a given source file.

    my $mutator = App::Test::Generator::Mutator->new(
        file           => 'lib/My/Module.pm',
        lib_dir        => 'lib',
        mutation_level => 'full',
    );

=head3 Arguments

=over 4

=item * C<file>

Path to the Perl source file to mutate. Required. Must exist on disk.

=item * C<lib_dir>

Root library directory. Optional - defaults to C<lib>.
Use a relative path. C<file> must begin with this path.

=item * C<mutation_level>

Controls the breadth of mutation. C<full> applies all mutations;
C<fast> deduplicates and removes redundant mutants first.
Optional - defaults to C<full>.

=back

=head3 Returns

A blessed hashref. Croaks if C<file> is missing or does not exist.
The file is only checked here. It is read later, by C<generate_mutants>.

=head3 API SPECIFICATION

=head4 input

    {
        file           => { type => SCALAR },
        lib_dir        => { type => SCALAR, optional => 1 },
        mutation_level => { type => SCALAR, optional => 1 },
    }

=head4 output

    {
        type => OBJECT,
        isa  => 'App::Test::Generator::Mutator',
    }

=head3 EXAMPLE

    my $m = App::Test::Generator::Mutator->new(
        file           => 'lib/Acme/Widget.pm',
        mutation_level => 'fast',
    );

=head3 MESSAGES

=over 4

=item C<file required>

C<file> was not supplied.

=item C<< file not found: PATH >>

C<file> was supplied but does not exist on disk.

=back

The mathematical description is in L</FORMAL SPECIFICATION>.

=cut
199
200sub new {
201
125
554710
        my ($class, %args) = @_;
202
203        # file is required and must exist on disk
204
125
298
        croak $ERR_FILE_REQUIRED unless defined $args{file};
205
119
559
        croak "file not found: $args{file}" unless -f $args{file};
206
207        return bless {
208                file           => $args{file},
209                lib_dir        => $args{lib_dir}        || $DEFAULT_LIB_DIR,
210
113
636
                mutation_level => $args{mutation_level} || $DEFAULT_MUTATION_LEVEL,
211
212                # Instantiate all registered mutation strategies
213                mutations => [
214                        App::Test::Generator::Mutation::BooleanNegation->new(),
215                        App::Test::Generator::Mutation::ReturnUndef->new(),
216                        App::Test::Generator::Mutation::NumericBoundary->new(),
217                        App::Test::Generator::Mutation::ConditionalInversion->new(),
218                ],
219        }, $class;
220}
221
222 - 307
=head2 generate_mutants

Parse the target file and generate all mutants by running each registered
mutation strategy against the PPI document.

    my $mutants = $mutator->generate_mutants();   # scalar context: arrayref
    for my $m (@{$mutants}) { ... }

    my @mutants = $mutator->generate_mutants();   # list context: flat list (backward-compat)

=head3 Arguments

None beyond C<$self>.

=head3 Returns

An arrayref of L<App::Test::Generator::Mutant> objects. In C<fast> mode,
redundant and duplicate mutants are removed before returning.
Lines within C<## MUTANT_SKIP_BEGIN> / C<## MUTANT_SKIP_END> annotation
blocks are excluded from the candidate list entirely.
After this method returns,
C<$self-E<gt>{skip_lines}> contains a hashref mapping excluded
line numbers to 1.

=head3 API SPECIFICATION

=head4 input

    {
        self => { type => OBJECT, isa => 'App::Test::Generator::Mutator' },
    }

=head4 output

    {
        type     => ARRAYREF,
        elements => { type => OBJECT, isa => 'App::Test::Generator::Mutant' },
    }

=head3 EXAMPLE

    my $mutants = $mutator->generate_mutants();
    printf "%d mutants generated\n", scalar @{$mutants};

    for my $m (@{$mutants}) {
        printf "line %d: %s\n", $m->line, $m->description;
    }

=head3 MESSAGES

=over 4

=item C<< Unable to parse FILE >>

PPI could not parse the source file (syntax error or unreadable file).
FILE is the path passed to C<new>.

=item C<< FILE: MUTANT_SKIP_BEGIN at line N with no prior MUTANT_SKIP_END >>

A C<## MUTANT_SKIP_BEGIN> marker was found while already inside a skip block.

=item C<< FILE: MUTANT_SKIP_END at line N with no matching MUTANT_SKIP_BEGIN >>

A C<## MUTANT_SKIP_END> marker was found with no preceding C<## MUTANT_SKIP_BEGIN>.

=item C<< FILE: MUTANT_SKIP_BEGIN at line N has no matching MUTANT_SKIP_END >>

The source file ended while still inside a skip block.

=back

The mathematical description is in L</FORMAL SPECIFICATION>.

=head3 PSEUDOCODE

    parse file with PPI
    scan lines for MUTANT_SKIP_BEGIN / MUTANT_SKIP_END pairs, giving skip_lines
    for each registered mutation strategy:
        skip if strategy does not apply_to(doc)
        for each mutant from strategy->mutate(doc):
            include unless mutant.line is in skip_lines
    if mutation_level == 'fast':
        deduplicate and remove redundant mutants
    return arrayref (or flat list in list context)

=cut
308
309sub generate_mutants {
310
71
34152
        my $self = $_[0];
311
312        # Parse the target file into a PPI document
313
71
335
        my $doc = PPI::Document->new($self->{file}) or croak "Unable to parse $self->{file}";
314
315        # Build set of lines excluded by ## MUTANT_SKIP_BEGIN / ## MUTANT_SKIP_END
316
71
318755
        my %skip_lines;
317
71
80
        my $in_skip  = 0;
318
71
54
        my $skip_start = 0;
319
71
59
        my $line_num = 0;
320
321
71
136
        for my $line (split /\n/, $doc->serialize()) {
322
1841
28719
                $line_num++;
323
324                # Match only lines where the annotation is the entire content —
325                # prevents false positives in comments or POD that mention the tag
326
1841
1043
                if($line =~ /^\s*##\s*MUTANT_SKIP_BEGIN\s*$/) {
327
10
18
                        croak "$self->{file}: MUTANT_SKIP_BEGIN at line $line_num with no prior MUTANT_SKIP_END"
328                                if $in_skip;
329
8
9
                        $in_skip    = 1;
330
8
3
                        $skip_start = $line_num;
331                }
332
1839
918
                $skip_lines{$line_num} = 1 if $in_skip;
333
334                # Match only lines where the annotation is the entire content —
335                # prevents false positives in comments or POD that mention the tag
336
1839
1051
                if($line =~ /^\s*##\s*MUTANT_SKIP_END\s*$/) {
337
6
20
                        croak "$self->{file}: MUTANT_SKIP_END at line $line_num with no matching MUTANT_SKIP_BEGIN"
338                                unless $in_skip;
339
3
3
                        $in_skip = 0;
340                }
341        }
342        # Unclosed MUTANT_SKIP_BEGIN is fatal
343
66
151
        croak "$self->{file}: MUTANT_SKIP_BEGIN at line $skip_start has no matching MUTANT_SKIP_END" if $in_skip;
344
345        # Store skip lines for use by the report generator
346
63
87
        $self->{skip_lines} = \%skip_lines;
347
348
63
43
        my @mutants;
349
350        # Run each registered mutation strategy against the document,
351        # excluding any candidates on skip-annotated lines. applies_to()
352        # is a cheap pre-filter -- skip the mutate() walk entirely for
353        # strategies that have nothing to match in this document.
354
63
63
63
61
        for my $mutation (@{$self->{mutations}}) {
355
252
560
                next unless $mutation->applies_to($doc);
356
194
795
281
529
                push @mutants, grep { !$skip_lines{$_->line} } $mutation->mutate($doc);
357        }
358
359        # In fast mode deduplicate and remove redundant mutants before returning.
360        # Returns arrayref in scalar context and a flat list in list context so
361        # existing callers (my @m = generate_mutants()) continue to work unchanged.
362
63
208
        my $result = $self->{mutation_level} eq $LEVEL_FAST
363                ? _dedup_mutants(\@mutants)
364                : \@mutants;
365
366
63
52
231
122
        return wantarray ? @{$result} : $result;
367}
368
369 - 443
=head2 prepare_workspace

Prepare an isolated temporary workspace for a single mutant.

The entire C<lib_dir> tree is copied into the workspace so that all module
dependencies resolve correctly when the test suite runs against the mutant.
Only after this copy is complete is the single target file overwritten by
C<apply_mutant>.

    my $workspace = $mutator->prepare_workspace();
    $mutator->apply_mutant($mutant);
    local $ENV{PERL5LIB} = "$workspace/lib";
    my $survived = (system('prove', 't') == 0);

=head3 Arguments

None beyond C<$self>.

=head3 Returns

A string containing the absolute path to the temporary directory created.
The directory is removed automatically when the program ends
(L<File::Temp>'s C<CLEANUP =E<gt> 1> behaviour). It is not removed earlier.

=head3 Side effects

Creates a temporary directory. Recursively copies C<lib_dir> into it.
Sets C<< $self->{_workspace} >>, C<< $self->{_relative} >>, and
C<< $self->{_lib_basename} >>. Does not modify C<< $self->{lib_dir} >>.

=head3 Notes

Call C<prepare_workspace> again before B<each> C<apply_mutant> call.
C<apply_mutant> does not undo the previous mutant, so two mutants applied
to the same workspace are both present in the file.

Each call makes a new directory and a new full copy of C<lib_dir>. The
object then points at the new one. Older workspaces stay on disk until the
program ends, so a long run needs disk space for one copy per call.

=head3 API SPECIFICATION

=head4 input

    {
        self => { type => OBJECT, isa => 'App::Test::Generator::Mutator' },
    }

=head4 output

    {
        type => SCALAR,
    }

=head3 EXAMPLE

    my $workspace = $mutator->prepare_workspace();
    # workspace is an absolute temp dir path
    # original lib_dir value is unchanged
    printf "original lib_dir still: %s\n", $mutator->{lib_dir};

=head3 MESSAGES

=over 4

=item C<dircopy failed: $!>

The C<lib_dir> tree could not be copied into the temporary workspace directory,
usually a permissions error.

=back

The mathematical description is in L</FORMAL SPECIFICATION>.

=cut
444
445sub prepare_workspace {
446
17
14007
        my $self = $_[0];
447
448        # Create a self-cleaning temporary directory
449
17
92
        my $tmp = tempdir(CLEANUP => 1);
450
451        # Normalise lib_dir to its final component so workspace paths
452        # are relative regardless of whether an absolute path was passed in
453
17
3271
        my $lib_basename = (File::Spec->splitdir($self->{lib_dir}))[-1];
454
455        # Derive the file's path relative to lib_dir for use by apply_mutant
456
17
21
        my $relative = $self->{file};
457
17
134
        $relative =~ s/^\Q$self->{lib_dir}\E\/?//;
458
459        # Copy the entire lib tree so all dependencies resolve in the workspace
460
17
147
        dircopy($self->{lib_dir}, File::Spec->catfile($tmp, $lib_basename)) or croak "dircopy failed: $!";
461
462        # Store normalised state under private keys — do NOT mutate lib_dir,
463        # which callers may inspect after this call expecting the original value.
464
16
20377
        $self->{_workspace}    = $tmp;
465
16
33
        $self->{_relative}     = $relative;
466
16
20
        $self->{_lib_basename} = $lib_basename;
467
468
16
36
        return $tmp;
469}
470
471 - 543
=head2 apply_mutant

Apply a single mutant's transform to the target file in the workspace.

    $mutator->apply_mutant($mutant);

=head3 Arguments

=over 4

=item * C<$mutant>

An L<App::Test::Generator::Mutant> object whose C<transform> closure
will be applied to the workspace copy of the target file.

=back

=head3 Returns

Nothing useful. Do not test the return value.
Modifies the workspace copy of the target file in place.

=head3 Side effects

Overwrites the target file in the workspace with the mutated version.
The real project file is never touched by this method.

=head3 API SPECIFICATION

=head4 input

    {
        self   => { type => OBJECT, isa => 'App::Test::Generator::Mutator' },
        mutant => { type => OBJECT, isa => 'App::Test::Generator::Mutant'  },
    }

=head4 output

    { type => UNDEF }

=head3 EXAMPLE

    for my $m (@{$mutants}) {
        my $workspace = $mutator->prepare_workspace();
        $mutator->apply_mutant($m);
        # "$workspace/lib/My/Module.pm" now holds exactly one mutation
    }

=head3 MESSAGES

=over 4

=item C<Workspace not prepared -- call prepare_workspace first>

C<apply_mutant> was called before C<prepare_workspace>.

=item C<Relative path not set -- call prepare_workspace first>

Internal: the relative-path field was not set by C<prepare_workspace>.

=item C<< Failed to parse TARGET >>

PPI could not parse the workspace copy of the target file.

=item C<Can't call method "transform" on an undefined value>

A Perl error, not a message from this module. C<$mutant> was C<undef>.

=back

The mathematical description is in L</FORMAL SPECIFICATION>.

=cut
544
545sub apply_mutant {
546
14
796
        my ($self, $mutant) = @_;
547
548        # Workspace must be prepared before applying any mutant
549        my $workspace = $self->{_workspace}
550
14
159
                or croak $ERR_WORKSPACE_NOT_SET;
551
552        my $relative  = $self->{_relative}
553
6
17
                or croak $ERR_RELATIVE_NOT_SET;
554
555        # Construct the full path to the file in the workspace
556        my $target = File::Spec->catfile(
557                $workspace,
558                $self->{_lib_basename},
559
6
36
                $relative,
560        );
561
562        # Parse the workspace copy and apply the mutation transform
563
6
41
        my $doc = PPI::Document->new($target) or croak "Failed to parse $target";
564
565
6
21799
        $mutant->transform->($doc);
566
567
6
114
        $doc->save($target);
568}
569
570 - 627
=head2 run_tests

Run the project's test suite and return whether all tests passed.

    my $survived = $mutator->run_tests();

=head3 Arguments

None beyond C<$self>.

=head3 Returns

A true value (1) if all tests passed (mutant survived). A false value
(the empty string, not 0) if any test failed or C<prove> could not be
started (mutant killed). Test it with C<if>, not with C<== 0>.

=head3 Side effects

Executes an external process running the test suite. The output of
C<prove> goes to the terminal.

=head3 Notes

Runs C<prove -l t> in the current directory. This tests the F<lib>
directory of the project, B<not> the workspace. To test a mutant, copy
the mutated file from the workspace over the project file first, and put
the original back afterwards. See Pattern 2 in the L</SYNOPSIS>.

The C<prove> program is looked for next to the running C<perl> first,
then in Perl's own C<bin> directory, and only then on C<PATH>.

There is no time limit. A mutant that causes an endless loop makes this
call wait for ever.

=head3 API SPECIFICATION

=head4 input

    {
        self => { type => OBJECT, isa => 'App::Test::Generator::Mutator' },
    }

=head4 output

    { type => SCALAR }

=head3 EXAMPLE

    my $survived = $mutator->run_tests();
    if($survived) {
        print "mutant survived\n";
    } else {
        print "mutant killed\n";
    }

The mathematical description is in L</FORMAL SPECIFICATION>.

=cut
628
629sub run_tests {
630
6
870
        my $self = $_[0];
631
632        # Derive prove from $^X so CPAN smokers that install to a non-PATH
633        # location still resolve the correct perl/prove pair.  Config{bin}
634        # is a reliable fallback; bare 'prove' is last resort only.
635
6
84
        my ($vol, $dir) = File::Spec->splitpath($^X);
636
6
56
        my $prove       = File::Spec->catpath($vol, $dir, 'prove');
637
6
67
        $prove          = File::Spec->catfile($Config{bin}, 'prove') unless -x $prove;
638
6
33
        $prove          = 'prove' unless -x $prove;
639
640
6
839753
        return system($prove, '-l', 't') == 0;
641}
642
643# --------------------------------------------------
644# _dedup_mutants
645#
646# Purpose:    Remove duplicate and redundant mutants
647#             from a list, used in fast mutation mode
648#             to reduce the number of mutants to run.
649#
650# Entry:      $mutants - arrayref of Mutant objects.
651#
652# Exit:       Returns an arrayref of deduplicated
653#             Mutant objects.
654#
655# Side effects: None.
656#
657# Notes:      Deduplication key uses line, original,
658#             and description rather than the transform
659#             coderef, which is not stable as a string.
660# --------------------------------------------------
661sub _dedup_mutants {
662
38
2230
        my ($mutants) = @_;
663
38
34
        my @rc;
664        my %seen;
665
666
38
38
25
41
        for my $m (@{$mutants}) {
667                # Build a stable key from metadata — not from the coderef
668
194
158
                my $key = join '|',
669                        $m->line        // '',
670                        $m->original    // '',
671                        $m->description // '';
672
673
194
183
                next if $seen{$key}++;
674
187
113
                next if _is_redundant_mutation($m);
675
676
175
124
                push @rc, $m;
677        }
678
679
38
46
        return \@rc;
680}
681
682# --------------------------------------------------
683# _is_redundant_mutation
684#
685# Return true if a mutant is considered
686#     redundant and should be skipped in fast
687#     mutation mode.
688#
689# Entry:      $m - a Mutant object.
690#
691# Exit:       Returns 1 if redundant, 0 otherwise.
692#
693# Notes:      Checks for arithmetic no-ops, double
694#             negation inside conditionals, boolean
695#             literal flips, mutations inside comments,
696#             and equivalent numeric comparisons.
697#             Does not compare transform coderefs —
698#             they are not meaningful as strings.
699# --------------------------------------------------
700sub _is_redundant_mutation {
701
216
142
        my ($m) = @_;
702
703
216
169
        my $orig = $m->original // '';
704
705        # Arithmetic no-ops add nothing to mutation coverage
706
216
168
        return 1 if $orig =~ /\+\s*0$/;
707
209
169
        return 1 if $orig =~ /-\s*0$/;
708
709        # Double negation inside conditionals forces boolean context
710        # in Perl and is not a meaningful mutation
711
203
145
        if($m->context && $m->context eq 'conditional') {
712
58
60
                return 1 if $orig =~ /^\!\!/;
713        }
714
715        # Boolean literal flip on a standalone 1 or 0 is trivial
716
200
198
        return 1 if $orig =~ /^\s*(?:1|0)\s*$/;
717
718        # Mutations inside comments are unreachable code
719
189
118
        return 1 if $m->line_content && $m->line_content =~ /^\s*#/;
720
721
185
118
        return 0;
722}
723
724 - 1096
=head1 COMMON PITFALLS

=over 4

=item Calling C<apply_mutant> before C<prepare_workspace>

C<apply_mutant> requires C<prepare_workspace> to have been called first. The
workspace holds the isolated copy of C<lib/> that receives mutations.

=item Passing an absolute path as C<lib_dir>

C<lib_dir> must be a B<relative> path (e.g. C<lib>). An absolute path causes
C<apply_mutant> to construct a doubled directory under the workspace and then
fail to find the target file.

=item Checking C<< $mutator->{workspace} >> after the refactor

Internal workspace state is stored in C<_workspace>, C<_relative>, and
C<_lib_basename> (note the underscore prefix). The original C<lib_dir> value
is never overwritten. Old code that reads C<< $mutator->{workspace} >> or
C<< $mutator->{relative} >> (without underscores) will not find these keys.

=item Forgetting that C<run_tests> drives C<prove> from C<$^X>

C<run_tests> resolves C<prove> from the same Perl binary used to run the script.
If you shell out to C<prove> directly in your own integration code, make sure
you are using the matching C<prove>.

=item C<generate_mutants> in list vs scalar context

C<generate_mutants> returns a flat list in list context and an arrayref in
scalar context.  Assign to an arrayref (C<my $m = $mutator-E<gt>generate_mutants()>)
to guarantee you always get a reference regardless of calling context.

=item Applying two mutants to one workspace

C<apply_mutant> changes the workspace file and leaves it changed. If you
apply a second mutant without a new C<prepare_workspace> call, the file
holds both mutations and your results are wrong.

=item Expecting C<run_tests> to test the workspace

C<run_tests> runs the tests against the project's own F<lib> directory.
If you only call C<apply_mutant> and then C<run_tests>, the tests load the
original code, every mutant "survives", and the kill rate is 0%.

=item An unknown C<mutation_level> is not an error

Only the exact text C<fast> turns on fast mode. Any other value, for
example C<Fast> or C<quick>, is accepted silently and works like C<full>.

=item How C<undef> and other false values are handled

=over 4

=item * C<< file => undef >> makes C<new> croak with C<file required>.
An empty string makes it croak with C<file not found>.

=item * C<lib_dir> and C<mutation_level> use their default when the value
is C<undef>, an empty string or C<0>. You cannot have a library directory
that is really named C<0>.

=item * C<apply_mutant(undef)> stops with a Perl error, after the
workspace check.

=item * C<run_tests> returns an empty string for "failed". It never
returns C<undef>.

=item * A mutant's C<line>, C<original> or C<description> may be C<undef>.
Fast mode treats each missing value as an empty string when it looks for
duplicates.

=back

=item Extra arguments to C<new> are ignored

A wrong spelling such as C<libdir> or C<level> is not reported. The
default value is used and no warning is given.

=item Skip markers must be alone on their line

C<## MUTANT_SKIP_BEGIN> and C<## MUTANT_SKIP_END> are only seen when
nothing else except spaces is on the same line. Markers cannot be nested.
The two marker lines themselves are also skipped.

=back

=head1 ENCODING

=over 4

=item * The C<file> and C<lib_dir> arguments

Paths are passed to the operating system unchanged. ASCII paths always
work. For non-ASCII directory or file names, pass the path as bytes in
the encoding of your file system (normally UTF-8).

=item * Source file contents

The file to mutate is read and written by L<PPI> as raw bytes. UTF-8
text, other non-ASCII text and emojis inside strings, comments and POD
are copied to the mutated file byte for byte. They are not damaged and
they do not need to be decoded first.

=item * Text returned by a mutant

C<< $mutant->original >>, C<< $mutant->description >> and
C<< $mutant->line_content >> can contain pieces of your source code.
These are byte strings. If the source is UTF-8 and you print them through
a UTF-8 file handle, decode them first with
C<Encode::decode('UTF-8', $text)>.

=item * Skip markers

The markers are ASCII. They are found correctly in a file of any
ASCII-compatible encoding, including UTF-8.

=item * Error messages

Messages contain the file path exactly as you gave it.

=back

=head1 LIMITATIONS

=over 4

=item * Single strategy registry

The four built-in mutation strategies are hardcoded in C<new>. There is no
plugin mechanism for registering additional strategies without subclassing.
A future version should accept a C<strategies> arrayref argument.

=item * No parallelism

C<run_tests> is synchronous. For large test suites or large mutant sets,
wall-clock time scales linearly. The in-place mutation strategy also
serialises all mutants behind a single file lock.

=item * PPI re-parse per apply_mutant call

C<apply_mutant> re-parses the workspace copy of the target file for every
mutant. For very large single-file modules the PPI parse time may dominate.

=item * apply_mutant does not restore on abnormal exit

If the process is killed between the write and restore in
C<bin/test-generator-mutate>, the project file is left mutated.
A C<git restore lib/...> recovers it.

=back

=head1 SEE ALSO

=over 4

=item C<bin/test-generator-mutate>

=item L<Devel::Mutator>

=back

=head1 AUTHOR

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

=head1 FORMAL SPECIFICATION

This section describes each method in Z notation.  It is for readers who
want an exact, mathematical statement.  You do not need it to use the module.

How to read it: a name ending in C<?> is an input, a name ending in C<!> is
an output, and a name ending in C<'> is the value after the call.
E<0x0394> before the state name means "this call may change the object".
E<0x039E> means "this call does not change the object".

    LEVEL == { 'full', 'fast' }

    â”Œâ”€ Mutator ─────────────────────────────────────────
    â”‚ file, lib_dir : PATH
    â”‚ mutation_level : STRING
    â”‚ skip_lines : â„™ ℕ₁ ∪ { undef }
    â”‚ workspace : PATH ∪ { undef }
    â”‚ relative : PATH ∪ { undef }
    â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    â”‚ workspace = undef ⇔ relative = undef
    â””───────────────────────────────────────────────────

=head2 new

    â”Œâ”€ New ─────────────────────────────────────────────
    â”‚ Mutator′
    â”‚ file? : VALUE
    â”‚ lib_dir?, mutation_level? : VALUE
    â”‚ result! : Mutator
    â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    â”‚ pre   file? ≠ undef ∧ file? names an existing plain file
    â”‚ post  file′ = file?
    â”‚       lib_dir′ = if lib_dir? is true
    â”‚                  then lib_dir? else 'lib'
    â”‚       mutation_level′ = if mutation_level? is true
    â”‚                         then mutation_level? else 'full'
    â”‚       skip_lines′ = undef ∧ workspace′ = undef ∧ relative′ = undef
    â”‚       result! = θ Mutator′
    â””───────────────────────────────────────────────────

    file? = undef                  â‡’ croak 'file required'
    file? is not an existing file  â‡’ croak 'file not found: ' ⁀ file?

=head2 generate_mutants

    â”Œâ”€ GenerateMutants ─────────────────────────────────
    â”‚ Δ Mutator
    â”‚ result! : seq Mutant
    â”‚ all : seq Mutant
    â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    â”‚ pre   file is valid Perl that PPI can parse
    â”‚       skip markers in file are paired and not nested
    â”‚ post  skip_lines′ = { n : ℕ₁ | line n of file lies between
    â”‚                       a BEGIN marker and its END marker,
    â”‚                       both marker lines included }
    â”‚       all = every mutant that the four strategies
    â”‚             produce for file, in strategy order
    â”‚       âˆ€ m : ran result! • m ∈ ran all ∧ m.line ∉ skip_lines′
    â”‚       mutation_level ≠ 'fast' ⇒
    â”‚           result! = all with skipped lines removed
    â”‚       mutation_level = 'fast' ⇒
    â”‚           (∀ i, j : dom result! • i ≠ j ⇒
    â”‚               key(result!(i)) ≠ key(result!(j)))
    â”‚           âˆ§ (∀ m : ran result! • ¬ redundant(m))
    â”‚       file′ = file ∧ lib_dir′ = lib_dir
    â”‚       workspace′ = workspace ∧ relative′ = relative
    â””───────────────────────────────────────────────────

    key(m) == (m.line, m.original, m.description)

    In list context the members of result! are returned as a list.

=head2 prepare_workspace

    â”Œâ”€ PrepareWorkspace ────────────────────────────────
    â”‚ Δ Mutator
    â”‚ result! : PATH
    â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    â”‚ pre   lib_dir names a directory that can be copied
    â”‚ post  result! names a directory created by this call
    â”‚       result! ≠ workspace
    â”‚       result!/basename(lib_dir) is a full copy of lib_dir
    â”‚       workspace′ = result!
    â”‚       relative′ = file with the leading lib_dir removed
    â”‚       file′ = file ∧ lib_dir′ = lib_dir
    â”‚       mutation_level′ = mutation_level
    â”‚       skip_lines′ = skip_lines
    â””───────────────────────────────────────────────────

    the copy fails  â‡’ croak 'dircopy failed: ' ⁀ reason

=head2 apply_mutant

    â”Œâ”€ ApplyMutant ─────────────────────────────────────
    â”‚ Ξ Mutator
    â”‚ mutant? : Mutant
    â”‚ target : PATH
    â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    â”‚ pre   workspace ≠ undef ∧ relative is true
    â”‚       mutant?.transform is a CODE reference
    â”‚       target = workspace/basename(lib_dir)/relative
    â”‚       target is valid Perl that PPI can parse
    â”‚ post  content′(target) = mutant?.transform(content(target))
    â”‚       content′(file) = content(file)
    â””───────────────────────────────────────────────────

    workspace = undef  â‡’ croak 'Workspace not prepared -- ...'
    relative is false  â‡’ croak 'Relative path not set -- ...'
    target not parsed  â‡’ croak 'Failed to parse ' ⁀ target

=head2 run_tests

    â”Œâ”€ RunTests ────────────────────────────────────────
    â”‚ Ξ Mutator
    â”‚ result! : { 1, '' }
    â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    â”‚ post  result! = 1 ⇔ the command 'prove -l t', run in the
    â”‚                     current directory, exits with status 0
    â””───────────────────────────────────────────────────

=head1 STATE DIAGRAM

The object has three states. The state says what has been prepared so far.

      new(file => undef) or new(file => missing file)
    +--------------------------------------------------+
    |  action: croak, no object                        |
    v                                                  |
  +-------------+                                      |
  |  NO OBJECT  |--------------------------------------+
  +-------------+
        |
        | new(file => existing file)
        | action: store file, lib_dir, mutation_level;
        |         build the four mutation strategies
        v
  +---------------------+  generate_mutants()
  |                     |-----------------------------------------+
  |     CONSTRUCTED     |  action: read file, set skip_lines,     |
  |  (no workspace yet) |  return the mutants (state stays same)  |
  |                     |<----------------------------------------+
  |                     |
  |                     |  run_tests()
  |                     |-----------------------------------------+
  |                     |  action: run 'prove -l t', return 1/''  |
  |                     |<----------------------------------------+
  |                     |
  |                     |  apply_mutant(m)
  |                     |-----------------------------------------+
  |                     |  action: croak 'Workspace not prepared' |
  |                     |<----------------------------------------+
  +---------------------+
        |
        | prepare_workspace()
        | action: make temp directory, copy lib_dir into it,
        |         remember workspace path and relative file path
        v
  +---------------------+  generate_mutants()   (as above)
  |                     |-----------------------------------------+
  |   WORKSPACE CLEAN   |<----------------------------------------+
  |  (copy is the same  |
  |   as the original)  |  run_tests()          (as above)
  |                     |-----------------------------------------+
  |                     |<----------------------------------------+
  |                     |
  |                     |  prepare_workspace()
  |                     |-----------------------------------------+
  |                     |  action: new directory, new clean copy  |
  |                     |<----------------------------------------+
  +---------------------+
        |            ^
        |            | prepare_workspace()
        |            | action: new directory, new clean copy;
        |            |         the mutated copy is left behind
        | apply_mutant(m)
        | action: rewrite the workspace copy of the file
        |         with one mutation
        v            |
  +---------------------+  apply_mutant(m2)
  |                     |-----------------------------------------+
  |  WORKSPACE MUTATED  |  action: a second mutation is added on  |
  |                     |  top of the first (usually a mistake)   |
  |                     |<----------------------------------------+
  |                     |
  |                     |  generate_mutants(), run_tests()
  |                     |-----------------------------------------+
  |                     |  action: as above, state stays the same |
  |                     |<----------------------------------------+
  +---------------------+

  From any state: when the program ends, every temporary workspace
  directory is deleted.

A call that croaks leaves the object in the state it was in. The
workspace file is only rewritten after the mutation has succeeded.

No method of this class changes the real project file.

=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
1097
10981;