File Coverage

blib/lib/Data/Rx/CoreType/str.pm
Criterion Covered Total %
statement 32 32 100.0
branch 18 18 100.0
condition 9 11 81.8
subroutine 7 7 100.0
pod 0 3 0.0
total 66 71 92.9


line stmt bran cond sub pod time code
1 1     1   12 use v5.12.0;
  1         3  
2 1     1   4 use warnings;
  1         3  
  1         42  
3             package Data::Rx::CoreType::str 0.200008;
4             # ABSTRACT: the Rx //str type
5              
6 1     1   5 use parent 'Data::Rx::CoreType';
  1         2  
  1         4  
7              
8 1     1   57 use Data::Rx::Util;
  1         2  
  1         386  
9              
10             sub guts_from_arg {
11 20     20 0 49 my ($class, $arg, $rx, $type) = @_;
12              
13 20 100       85 Carp::croak("unknown arguments to new")
14             unless Data::Rx::Util->_x_subset_keys_y($arg, { length => 1, value => 1});
15              
16             # XXX: We should be able to reject num values, too. :( -- rjbs, 2008-08-25
17 19 100       67 if (exists $arg->{value}) {
18 3         10 my $val = $arg->{value};
19 3 100 66     20 if (
20             (! defined $val)
21             or ref $val
22             ) {
23 1   50     134 Carp::croak(sprintf(
24             'invalid value (%s) for //str',
25             $val // 'undef',
26             ));
27             }
28             }
29              
30 18         31 my $guts = {};
31              
32             $guts->{length_check} = Data::Rx::Util->_make_range_check($arg->{length})
33 18 100       68 if $arg->{length};
34              
35 18 100       60 $guts->{value} = $arg->{value} if defined $arg->{value};
36              
37 18         39 return $guts;
38             }
39              
40             sub assert_valid {
41 235     235 0 79154 my ($self, $value) = @_;
42              
43 235 100       611 unless (defined $value) {
44 6         53 $self->fail({
45             error => [ qw(type) ],
46             message => "found value is undef",
47             value => $value,
48             });
49             }
50              
51             # XXX: This is insufficiently precise. It's here to keep us from believing
52             # that JSON::XS::Boolean objects, which end up looking like 0 or 1, are
53             # integers. -- rjbs, 2008-07-24
54 229 100       584 if (ref $value) {
55 112         596 $self->fail({
56             error => [ qw(type) ],
57             message => "found value is a reference",
58             value => $value,
59             });
60             }
61              
62 117 100 100     423 if ($self->{length_check} && ! $self->{length_check}->(length $value)) {
63 15         327 $self->fail({
64             error => [ qw(length) ],
65             message => "length of value is outside allowed range",
66             value => $value,
67             });
68             }
69              
70 102 100 100     437 if (defined $self->{value} and $self->{value} ne $value) {
71 44         247 $self->fail({
72             error => [ qw(value) ],
73             message => "found value is not the required value",
74             value => $value,
75             });
76             }
77              
78             # XXX: Really, we need a way to know whether (say) the JSON was one of the
79             # following: { "foo": 1 } or { "foo": "1" }
80             # Only one of those is providing a string. -- rjbs, 2008-07-27
81 58         147 return 1;
82             }
83              
84 70     70 0 233 sub subname { 'str' }
85              
86             1;
87              
88             __END__