File Coverage

blib/lib/Test2/Tools/DOM/Check.pm
Criterion Covered Total %
statement 64 65 98.4
branch 17 20 85.0
condition 4 5 80.0
subroutine 12 12 100.0
pod 3 5 60.0
total 100 107 93.4


line stmt bran cond sub pod time code
1             package Test2::Tools::DOM::Check;
2              
3 2     2   22 use v5.20;
  2         12  
4 2     2   10 use warnings;
  2         2  
  2         49  
5 2     2   9 use experimental 'signatures';
  2         3  
  2         10  
6              
7 2     2   178 use Test2::Util ();
  2         3  
  2         37  
8 2     2   558 use Mojo::DOM58;
  2         39795  
  2         113  
9              
10             our $VERSION = '0.001';
11              
12 2     2   833 use parent 'Test2::Compare::Base';
  2         509  
  2         11  
13              
14 3     3 1 13 sub name { '' }
15              
16 34     34 1 9918 sub verify ( $self, %params ) { !!$params{exists} }
  34         46  
  34         406  
  34         44  
  34         88  
17              
18 33     33 0 673 sub init ( $self ) { $self->{calls} = [] }
  33         43  
  33         39  
  33         75  
19              
20 49     49 0 890 sub add_call ( $self, $method, $check, $args ) {
  49         52  
  49         57  
  49         48  
  49         57  
  49         48  
21 49         58 push @{ $self->{calls} } => [ $method, $check, $args ];
  49         161  
22             }
23              
24 34     34 1 153 sub deltas ( $self, %params ) {
  34         40  
  34         60  
  34         39  
25 34         37 my @deltas;
26 34         73 my ( $got, $convert, $seen ) = @params{qw( got convert seen )};
27              
28 34 100       118 $self->{dom} = ref $got eq 'Mojo::DOM58' ? $got : Mojo::DOM58->new($got);
29              
30 34 100       6146 if ( $self->{dom}->type eq 'root' ) {
31             # Keep root in scope.
32             # See https://github.com/mojolicious/mojo/issues/1924
33 17         189 $self->{root} = $self->{dom};
34              
35             # For usability's sake, if we received the root of the DOM, we move
36             # to its first child (if one exists). In the contexts where this
37             # module will be used, this will in most cases be what people expect.
38 17 50       38 if ( my $top = $self->{dom}->children->first ) {
39 17         1259 $self->{dom} = $top;
40             }
41             }
42              
43 34         234 my $dom = $self->{dom};
44              
45 34   50     45 for my $call (@{ $self->{calls} // [] }) {
  34         87  
46 50         698 my ( $name, $args, $check ) = @$call;
47              
48 50         106 $check = $convert->($check);
49              
50 50 50       3827 my $method = $dom->can($name)
51             or Carp::croak "Cannot call $name on an object of type " . ref $dom;
52              
53 50         64 my $value;
54 50     50   224 my ( $ok, $err ) = Test2::Util::try { $value = $dom->$method(@$args) };
  50         496  
55              
56 50 50       5097 if ($ok) {
57 50         229 my %args = (
58             id => [ METHOD => $name ],
59             seen => $seen,
60             convert => $convert,
61             got => $value,
62             exists => $method,
63             );
64              
65             # Support HTML/XML logic for element attributes
66 50 100 100     189 if ( @$args && $name eq 'attr' ) {
    100          
67 14         30 my $exists = exists $dom->attr->{ $args->[0] };
68 14 100       183 $args{got} = $exists if $check->name =~ /^(?:TRUE|FALSE)$/;
69 14 100       100 $args{exists} = $exists if $check->name =~ /EXIST/;
70             }
71             # If the element is not found, it does not exist
72             elsif ( $name eq 'at' ) {
73 10 100       32 $args{exists} = defined $value if $check->name =~ /EXIST/;
74             }
75              
76 50         285 push @deltas => $check->run(%args);
77             }
78             else {
79 0         0 push @deltas => $check->delta_class->new(
80             id => [ METHOD => $name ],
81             check => $check,
82             exception => $err,
83             got => undef,
84             verified => undef,
85             );
86             }
87             }
88              
89 34         1997 return @deltas;
90             }
91              
92             1;