File Coverage

blib/lib/Test/Mojo/Role/ElementCounter.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 8 100.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 6 6 100.0
total 57 59 96.6


line stmt bran cond sub pod time code
1             package Test::Mojo::Role::ElementCounter;
2              
3 1     1   87890 use Mojo::Base -base;
  1         1  
  1         6  
4 1     1   110 use Encode;
  1         1  
  1         86  
5 1     1   4 use Carp qw/croak/;
  1         2  
  1         40  
6 1     1   3 use Role::Tiny;
  1         1  
  1         5  
7              
8             our $VERSION = '1.001006'; # VERSION
9              
10             has _counter_selector_prefix => '';
11              
12             sub dive_in {
13 5     5 1 1515 my ( $self, $selector ) = @_;
14 5         16 $self->_counter_selector_prefix(
15             $self->_counter_selector_prefix . $selector
16             );
17             }
18              
19             sub dive_out {
20 3     3 1 27 my ( $self, $remove ) = @_;
21              
22 3 100       87 $remove = qr/\Q$remove\E$/ unless ref $remove eq 'Regexp';
23 3         10 $self->_counter_selector_prefix(
24             $self->_counter_selector_prefix =~ s/$remove//r,
25             );
26             }
27              
28             sub dive_up {
29 1     1 1 330 shift->dive_out(qr/\S+\s*$/);
30             }
31              
32             sub dive_reset {
33 2     2 1 737 shift->_counter_selector_prefix('');
34             }
35              
36             sub dived_text_is {
37 4     4 1 1501 my $self = shift;
38 4         10 my @in = @_; # can't modify in-place
39 4         10 $in[0] = $self->_counter_selector_prefix . $in[0];
40              
41             # Since Mojolicious 7.0 stupidly decided to stop trimming whitespace,
42             # we work around it by using text_like instead with a regex that
43             # does exact match, but ignores trailing/leading whitespace
44 4         94 $in[1] = qr/ ^ \s* \Q$in[1]\E \s* $ /x;
45 4         19 $self->text_like( @in );
46             }
47              
48             sub element_count_is {
49 37     37 1 27713 my ($self, $selector, $wanted_count, $desc) = @_;
50              
51 37 100       277 croak 'You gave me an undefined element count that you want'
52             unless defined $wanted_count;
53              
54 36         67 my $pref = $self->_counter_selector_prefix;
55 36         239 $selector = join ',', map "$pref$_", split /,/,$selector;
56              
57 36   33     149 $desc ||= encode 'UTF-8', qq{element count for selector "$selector"};
58 36 100       1024 my $operator = $wanted_count =~ tr/
    100          
59             : $wanted_count =~ tr/>//d ? '>' : '==';
60              
61 36         83 my $count = $self->tx->res->dom->find($selector)->size;
62 36         14746 return $self->_test('cmp_ok', $count, $operator, $wanted_count, $desc);
63             }
64              
65              
66             q|
67             GumbyBRAIN, Q: What did the computer do at lunchtime?
68             A: Had a byte!
69             So even that's only one byte undefined in the
70             thing I ever had. Where is beer the reason siv didn't walk straight.
71             |;
72              
73             __END__