<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Compare::Array;
use strict;
use warnings;

use base 'Test2::Compare::Base';

our $VERSION = '0.000162';

use Test2::Util::HashBase qw/inref meta ending items order for_each/;

use Carp qw/croak confess/;
use Scalar::Util qw/reftype looks_like_number/;

sub init {
    my $self = shift;

    if( defined( my $ref = $self-&gt;{+INREF}) ) {
        croak "Cannot specify both 'inref' and 'items'" if $self-&gt;{+ITEMS};
        croak "Cannot specify both 'inref' and 'order'" if $self-&gt;{+ORDER};
        croak "'inref' must be an array reference, got '$ref'" unless reftype($ref) eq 'ARRAY';
        my $order = $self-&gt;{+ORDER} = [];
        my $items = $self-&gt;{+ITEMS} = {};
        for (my $i = 0; $i &lt; @$ref; $i++) {
            push @$order =&gt; $i;
            $items-&gt;{$i} = $ref-&gt;[$i];
        }
    }
    else {
        $self-&gt;{+ITEMS} ||= {};
        croak "All indexes listed in the 'items' hashref must be numeric"
            if grep { !looks_like_number($_) } keys %{$self-&gt;{+ITEMS}};

        $self-&gt;{+ORDER} ||= [sort { $a &lt;=&gt; $b } keys %{$self-&gt;{+ITEMS}}];
        croak "All indexes listed in the 'order' arrayref must be numeric"
            if grep { !(looks_like_number($_) || (ref($_) &amp;&amp; reftype($_) eq 'CODE')) } @{$self-&gt;{+ORDER}};
    }

    $self-&gt;{+FOR_EACH} ||= [];

    $self-&gt;SUPER::init();
}

sub name { '&lt;ARRAY&gt;' }

sub meta_class  { 'Test2::Compare::Meta' }

sub verify {
    my $self = shift;
    my %params = @_;

    return 0 unless $params{exists};
    my $got = $params{got};
    return 0 unless defined $got;
    return 0 unless ref($got);
    return 0 unless reftype($got) eq 'ARRAY';
    return 1;
}

sub add_prop {
    my $self = shift;
    $self-&gt;{+META} = $self-&gt;meta_class-&gt;new unless defined $self-&gt;{+META};
    $self-&gt;{+META}-&gt;add_prop(@_);
}

sub top_index {
    my $self = shift;
    my @order = @{$self-&gt;{+ORDER}};

    while(@order) {
        my $idx = pop @order;
        next if ref $idx;
        return $idx;
    }

    return undef; # No indexes
}

sub add_item {
    my $self = shift;
    my $check = pop;
    my ($idx) = @_;

    my $top = $self-&gt;top_index;

    croak "elements must be added in order!"
        if $top &amp;&amp; $idx &amp;&amp; $idx &lt;= $top;

    $idx = defined($top) ? $top + 1 : 0
        unless defined($idx);

    push @{$self-&gt;{+ORDER}} =&gt; $idx;
    $self-&gt;{+ITEMS}-&gt;{$idx} = $check;
}

sub add_filter {
    my $self = shift;
    my ($code) = @_;
    croak "A single coderef is required"
        unless @_ == 1 &amp;&amp; $code &amp;&amp; ref $code &amp;&amp; reftype($code) eq 'CODE';

    push @{$self-&gt;{+ORDER}} =&gt; $code;
}

sub add_for_each {
    my $self = shift;
    push @{$self-&gt;{+FOR_EACH}} =&gt; @_;
}

sub deltas {
    my $self = shift;
    my %params = @_;
    my ($got, $convert, $seen) = @params{qw/got convert seen/};

    my @deltas;
    my $state = 0;
    my @order = @{$self-&gt;{+ORDER}};
    my $items = $self-&gt;{+ITEMS};
    my $for_each = $self-&gt;{+FOR_EACH};

    my $meta     = $self-&gt;{+META};
    push @deltas =&gt; $meta-&gt;deltas(%params) if defined $meta;

    # Make a copy that we can munge as needed.
    my @list = @$got;

    while (@order) {
        my $idx = shift @order;
        my $overflow = 0;
        my $val;

        # We have a filter, not an index
        if (ref($idx)) {
            @list = $idx-&gt;(@list);
            next;
        }

        confess "Internal Error: Stacks are out of sync (state &gt; idx)"
            if $state &gt; $idx + 1;

        while ($state &lt;= $idx) {
            $overflow = !@list;
            $val = shift @list;

            # check-all goes here so we hit each item, even unspecified ones.
            for my $check (@$for_each) {
                last if $overflow; # avoid doing 'for each' checks beyond array bounds
                $check = $convert-&gt;($check);
                push @deltas =&gt; $check-&gt;run(
                    id      =&gt; [ARRAY =&gt; $state],
                    convert =&gt; $convert,
                    seen    =&gt; $seen,
                    exists  =&gt; !$overflow,
                    $overflow ? () : (got =&gt; $val),
                );
            }

            $state++;
        }

        confess "Internal Error: Stacks are out of sync (state != idx + 1)"
            unless $state == $idx + 1;

        my $check = $convert-&gt;($items-&gt;{$idx});

        push @deltas =&gt; $check-&gt;run(
            id      =&gt; [ARRAY =&gt; $idx],
            convert =&gt; $convert,
            seen    =&gt; $seen,
            exists  =&gt; !$overflow,
            $overflow ? () : (got =&gt; $val),
        );
    }

    while (@list &amp;&amp; (@$for_each || $self-&gt;{+ENDING})) {
        my $item = shift @list;

        for my $check (@$for_each) {
            $check = $convert-&gt;($check);
            push @deltas =&gt; $check-&gt;run(
                id      =&gt; [ARRAY =&gt; $state],
                convert =&gt; $convert,
                seen    =&gt; $seen,
                got     =&gt; $item,
                exists  =&gt; 1,
            );
        }

        # if items are left over, and ending is true, we have a problem!
        if ($self-&gt;{+ENDING}) {
            push @deltas =&gt; $self-&gt;delta_class-&gt;new(
                dne      =&gt; 'check',
                verified =&gt; undef,
                id       =&gt; [ARRAY =&gt; $state],
                got      =&gt; $item,
                check    =&gt; undef,

                $self-&gt;{+ENDING} eq 'implicit' ? (note =&gt; 'implicit end') : (),
            );
        }

        $state++;
    }

    return @deltas;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test2::Compare::Array - Internal representation of an array comparison.

=head1 DESCRIPTION

This module is an internal representation of an array for comparison purposes.

=head1 METHODS

=over 4

=item $ref = $arr-&gt;inref()

If the instance was constructed from an actual array, this will return the
reference to that array.

=item $bool = $arr-&gt;ending

=item $arr-&gt;set_ending($bool)

Set this to true if you would like to fail when the array being validated has
more items than the check. That is, if you check indexes 0-3 but the array has
values for indexes 0-4, it will fail and list that last item in the array as
unexpected. If set to false then it is assumed you do not care about extra
items.

=item $hashref = $arr-&gt;items()

Returns the hashref of C&lt;&lt; key =&gt; val &gt;&gt; pairs to be checked in the
array.

=item $arr-&gt;set_items($hashref)

Accepts a hashref to permit indexes to be skipped if desired.

B&lt;Note:&gt; that there is no validation when using C&lt;set_items&gt;, it is better to
use the C&lt;add_item&gt; interface.

=item $arrayref = $arr-&gt;order()

Returns an arrayref of all indexes that will be checked, in order.

=item $arr-&gt;set_order($arrayref)

Sets the order in which indexes will be checked.

B&lt;Note:&gt; that there is no validation when using C&lt;set_order&gt;, it is better to
use the C&lt;add_item&gt; interface.

=item $name = $arr-&gt;name()

Always returns the string C&lt;&lt; "&lt;ARRAY&gt;" &gt;&gt;.

=item $bool = $arr-&gt;verify(got =&gt; $got, exists =&gt; $bool)

Check if C&lt;$got&gt; is an array reference or not.

=item $idx = $arr-&gt;top_index()

Returns the topmost index which is checked. This will return undef if there
are no items, or C&lt;0&gt; if there is only 1 item.

=item $arr-&gt;add_item($item)

Push an item onto the list of values to be checked.

=item $arr-&gt;add_item($idx =&gt; $item)

Add an item to the list of values to be checked at the specified index.

=item $arr-&gt;add_filter(sub { ... })

Add a filter sub. The filter receives all remaining values of the array being
checked, and should return the values that should still be checked. The filter
will be run between the last item added and the next item added.

=item @deltas = $arr-&gt;deltas(got =&gt; $got, convert =&gt; \&amp;convert, seen =&gt; \%seen)

Find the differences between the expected array values and those in the C&lt;$got&gt;
arrayref.

=back

=head1 SOURCE

The source code repository for Test2-Suite can be found at
F&lt;https://github.com/Test-More/Test2-Suite/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2018 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>