File Coverage

blib/lib/DTL/Fast/Expression/Operator/Binary/In.pm
Criterion Covered Total %
statement 31 48 64.5
branch 18 28 64.2
condition 18 32 56.2
subroutine 6 6 100.0
pod 0 1 0.0
total 73 115 63.4


line stmt bran cond sub pod time code
1             package DTL::Fast::Expression::Operator::Binary::In;
2 3     3   2304 use strict; use utf8; use warnings FATAL => 'all';
  3     3   6  
  3     3   80  
  3         16  
  3         5  
  3         20  
  3         141  
  3         6  
  3         126  
3 3     3   21 use parent 'DTL::Fast::Expression::Operator::Binary';
  3         6  
  3         18  
4              
5             $DTL::Fast::OPS_HANDLERS{'in'} = __PACKAGE__;
6              
7 3     3   815 use DTL::Fast::Expression::Operator::Binary::Eq;
  3         6  
  3         1497  
8              
9             sub dispatch
10             {
11 13     13 0 27 my( $self, $arg1, $arg2, $context) = @_;
12 13         29 my ($arg1_type, $arg2_type) = (ref $arg1, ref $arg2);
13 13         15 my $result = undef;
14              
15 13 100 100     101 if( not $arg2_type and not $arg1_type ) # substring checking
    100 100        
    100 66        
    50 33        
    50 33        
16             {
17 4 100       15 $result = index($arg2, $arg1) > -1 ? 1: 0;
18             }
19             elsif( not $arg1_type and $arg2_type eq 'ARRAY') # operand in array
20             {
21 4         6 $result = 0;
22 4         8 foreach my $a2 (@$arg2)
23             {
24             # @todo: deep comparision, shouldn't it be optional?
25 8 100       21 if( DTL::Fast::Expression::Operator::Binary::Eq::dispatch($self, $arg1, $a2) )
26             {
27 2         3 $result = 1;
28 2         8 last;
29             }
30             }
31             }
32             elsif( not $arg1_type and $arg2_type eq 'HASH') # exists synonim
33             {
34 4 100       11 $result = exists $arg2->{$arg1} ? 1: 0;
35             }
36             elsif( $arg2_type eq 'ARRAY' and $arg1_type eq 'ARRAY') # second operand is an ARRAY
37             {
38 0         0 $result = 1;
39 0         0 foreach my $a1 (@$arg1)
40             {
41 0         0 my $found = 0;
42 0         0 foreach my $a2 (@$arg2)
43             {
44             # @todo: deep comparision, shouldn't it be optional?
45 0 0       0 if( DTL::Fast::Expression::Operator::Binary::Eq::dispatch($self, $a1, $a2) )
46             {
47 0         0 $found = 1;
48 0         0 last;
49             }
50             }
51              
52 0 0       0 if( not $found )
53             {
54 0         0 $result = 0;
55 0         0 last;
56             }
57             }
58             }
59             elsif( $arg2_type eq 'HASH' and $arg1_type eq 'HASH' ) # hash contains hash
60             {
61 0         0 $result = 1;
62 0         0 foreach my $key1 (keys %$arg1)
63             {
64 0 0 0     0 if(
65             not exists $arg2->{$key1}
66             # @todo: deep comparision, shouldn't it be optional?
67             or not DTL::Fast::Expression::Operator::Binary::Eq::dispatch($self, $arg1->{$key1}, $arg2->{$key1})
68             )
69             {
70 0         0 $result = 0;
71 0         0 last;
72             }
73             }
74             }
75              
76             # `in` method implementation
77 13 50 66     107 if(
78             not defined $result
79             and UNIVERSAL::can($arg1, 'in')
80             )
81             {
82 0         0 $result = $arg1->in($arg2);
83             }
84              
85             # `contains` method implementation
86 13 50 66     44 if(
87             not defined $result
88             and UNIVERSAL::can($arg2, 'contains')
89             )
90             {
91 0         0 $result = $arg2->contains($arg1);
92             }
93              
94             # still nothing
95 13 100       34 if( not defined $result )
96             {
97 1   50     22 die $self->get_render_error(
      50        
      50        
      50        
98             $context,
99             sprintf("Don't know how to check that %s (%s) is in %s (%s)"
100             , $arg1 // 'undef'
101             , $arg1_type || 'SCALAR'
102             , $arg2 // 'undef'
103             , $arg2_type || 'SCALAR'
104             )
105             );
106             }
107              
108 12         44 return $result;
109             }
110              
111             1;