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   156564 use Mojo::Base -base;
  1         3  
  1         7  
4 1     1   193 use Encode;
  1         3  
  1         119  
5 1     1   8 use Carp qw/croak/;
  1         2  
  1         45  
6 1     1   7 use Role::Tiny;
  1         2  
  1         7  
7              
8             our $VERSION = '1.001008'; # VERSION
9              
10             has _counter_selector_prefix => '';
11              
12             sub dive_in {
13 5     5 1 1675 my ( $self, $selector ) = @_;
14 5         14 $self->_counter_selector_prefix(
15             $self->_counter_selector_prefix . $selector
16             );
17             }
18              
19             sub dive_out {
20 3     3 1 29 my ( $self, $remove ) = @_;
21              
22 3 100       25 $remove = qr/\Q$remove\E$/ unless ref $remove eq 'Regexp';
23 3         9 $self->_counter_selector_prefix(
24             $self->_counter_selector_prefix =~ s/$remove//r,
25             );
26             }
27              
28             sub dive_up {
29 1     1 1 332 shift->dive_out(qr/\S+\s*$/);
30             }
31              
32             sub dive_reset {
33 2     2 1 836 shift->_counter_selector_prefix('');
34             }
35              
36             sub dived_text_is {
37 4     4 1 2035 my $self = shift;
38 4         12 my @in = @_; # can't modify in-place
39 4         14 $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         75 $in[1] = qr/ ^ \s* \Q$in[1]\E \s* $ /x;
45 4         23 $self->text_like( @in );
46             }
47              
48             sub element_count_is {
49 37     37 1 39945 my ($self, $selector, $wanted_count, $desc) = @_;
50              
51 37 100       400 croak 'You gave me an undefined element count that you want'
52             unless defined $wanted_count;
53              
54 36         88 my $pref = $self->_counter_selector_prefix;
55 36         361 $selector = join ',', map "$pref$_", split /,/,$selector;
56              
57 36   33     225 $desc ||= encode 'UTF-8', qq{element count for selector "$selector"};
58 36 100       1804 my $operator = $wanted_count =~ tr/
    100          
59             : $wanted_count =~ tr/>//d ? '>' : '==';
60              
61 36         107 my $count = $self->tx->res->dom->find($selector)->size;
62 36         24944 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__