File Coverage

blib/lib/Data/Rx/CoreType/str.pm
Criterion Covered Total %
statement 32 33 96.9
branch 16 20 80.0
condition 7 9 77.7
subroutine 7 7 100.0
pod 0 3 0.0
total 62 72 86.1


line stmt bran cond sub pod time code
1 1     1   4 use strict;
  1         1  
  1         25  
2 1     1   3 use warnings;
  1         2  
  1         30  
3             package Data::Rx::CoreType::str;
4             # ABSTRACT: the Rx //str type
5             $Data::Rx::CoreType::str::VERSION = '0.200007';
6 1     1   4 use parent 'Data::Rx::CoreType';
  1         1  
  1         3  
7              
8 1     1   43 use Data::Rx::Util;
  1         1  
  1         230  
9              
10             sub guts_from_arg {
11 18     18 0 25 my ($class, $arg, $rx, $type) = @_;
12              
13 18 50       69 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 18 100       51 if (exists $arg->{value}) {
18 2         4 my $val = $arg->{value};
19 2 50 33     14 if (
20             (! defined $val)
21             or ref $val
22             ) {
23 0 0       0 Carp::croak(sprintf(
24             'invalid value (%s) for //str',
25             defined $val ? $val : 'undef',
26             ));
27             }
28             }
29              
30 18         24 my $guts = {};
31              
32 18 100       37 $guts->{length_check} = Data::Rx::Util->_make_range_check($arg->{length})
33             if $arg->{length};
34              
35 18 100       60 $guts->{value} = $arg->{value} if defined $arg->{value};
36              
37 18         41 return $guts;
38             }
39              
40             sub assert_valid {
41 235     235 0 30610 my ($self, $value) = @_;
42              
43 235 100       396 unless (defined $value) {
44 6         55 $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       393 if (ref $value) {
55 112         492 $self->fail({
56             error => [ qw(type) ],
57             message => "found value is a reference",
58             value => $value,
59             });
60             }
61              
62 117 100 100     369 if ($self->{length_check} && ! $self->{length_check}->(length $value)) {
63 15         298 $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     390 if (defined $self->{value} and $self->{value} ne $value) {
71 44         192 $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         121 return 1;
82             }
83              
84 54     54 0 121 sub subname { 'str' }
85              
86             1;
87              
88             __END__