File Coverage

blib/lib/Test2/Tools/DOM/Check.pm
Criterion Covered Total %
statement 71 71 100.0
branch 32 32 100.0
condition 7 10 70.0
subroutine 13 13 100.0
pod 3 6 50.0
total 126 132 95.4


line stmt bran cond sub pod time code
1             package Test2::Tools::DOM::Check;
2              
3 2     2   28 use v5.20;
  2         10  
4 2     2   11 use warnings;
  2         3  
  2         121  
5 2     2   13 use experimental 'signatures';
  2         3  
  2         11  
6              
7 2     2   276 use Test2::Util ();
  2         3  
  2         75  
8 2     2   1499 use Mojo::DOM58;
  2         110248  
  2         154  
9              
10             our $VERSION = '0.100';
11              
12 2     2   936 use parent 'Test2::Compare::Base';
  2         563  
  2         12  
13              
14 6     6 1 243 sub name { '' }
15              
16 45     45 1 18567 sub verify ( $self, %params ) { !!$params{exists} }
  45         133  
  45         145  
  45         86  
  45         228  
17              
18 44     44 0 1295 sub init ( $self ) { $self->{calls} = [] }
  44         79  
  44         74  
  44         133  
19              
20 65     65 0 2369 sub add_call ( $self, $call, $check, $name, $context, @ ) {
  65         114  
  65         110  
  65         114  
  65         110  
  65         108  
  65         87  
21 65 100 66     197 $name
    100          
22             ||= ref $call eq 'ARRAY' ? $call->[0]
23             : ref $call eq 'CODE' ? '\&CODE'
24             : $call;
25              
26 65   50     109 push @{ $self->{calls} } => [ $call, $check, $name, $context || 'scalar' ];
  65         417  
27             }
28              
29 45     45 1 267 sub deltas ( $self, %params ) {
  45         76  
  45         124  
  45         119  
30 45         69 my @deltas;
31 45         141 my ( $got, $convert, $seen ) = @params{qw( got convert seen )};
32              
33 45 100       263 $self->{dom} = ref $got eq 'Mojo::DOM58' ? $got : Mojo::DOM58->new($got);
34              
35 45 100       12288 if ( $self->{dom}->type eq 'root' ) {
36             # Keep root in scope.
37             # See https://github.com/mojolicious/mojo/issues/1924
38 24         403 $self->{root} = $self->{dom};
39              
40             # For usability's sake, if we received the root of the DOM, we move
41             # to its first child (if one exists). In the contexts where this
42             # module will be used, this will in most cases be what people expect.
43 24 100       87 if ( my $top = $self->{dom}->children->first ) {
44 23         2604 $self->{dom} = $top;
45             }
46             }
47              
48 45         588 my $dom = $self->{dom};
49              
50 45   50     77 for (@{ $self->{calls} // [] }) {
  45         183  
51 66         1999 my ( $method, $check, $name, $context ) = @$_;
52              
53 66         194 my @args;
54 66 100       225 ( $method, @args ) = @$method if ref $method eq 'ARRAY';
55              
56 66         186 $check = $convert->($check);
57              
58 66 100       8094 $method = ref $method eq 'CODE'
59             ? $method
60             : $dom->can($name);
61              
62 66 100       541 Carp::croak "Cannot call '$name' on an object of type " . ref $dom
63             unless $method;
64              
65              
66 65         106 my $value;
67             my ( $ok, $err ) = Test2::Util::try {
68 65 100   65   1127 $value = $context eq 'list' ? [ $dom->$method(@args) ] :
    100          
69             $context eq 'hash' ? { $dom->$method(@args) } :
70             $dom->$method(@args);
71 65         568 };
72              
73 65 100       10486 if ($ok) {
74 64         453 my %args = (
75             id => [ METHOD => $name ],
76             seen => $seen,
77             convert => $convert,
78             got => $value,
79             exists => defined $method,
80             );
81              
82             # Support HTML/XML logic for element attributes
83 64 100 100     320 if ( @args && $name eq 'attr' ) {
    100          
84 16         51 my $exists = exists $dom->attr->{ $args[0] };
85 16 100       302 $args{got} = $exists if $check->name =~ /^(?:TRUE|FALSE)$/;
86 16 100       166 $args{exists} = $exists if $check->name =~ /EXIST/;
87             }
88             # If the element is not found, it does not exist
89             elsif ( $name eq 'at' ) {
90 10 100       44 $args{exists} = defined $value if $check->name =~ /EXIST/;
91             }
92              
93 64         445 push @deltas => $check->run(%args);
94             }
95             else {
96 1         10 push @deltas => $check->delta_class->new(
97             id => [ METHOD => $name ],
98             check => $check,
99             exception => $err,
100             got => undef,
101             verified => undef,
102             );
103             }
104             }
105              
106 44         4175 return @deltas;
107             }
108              
109 3     3 0 2142 sub stringify_got { 1 }
110              
111             1;