File Coverage

blib/lib/Test/Deep/JType.pm
Criterion Covered Total %
statement 86 86 100.0
branch 24 24 100.0
condition 3 4 75.0
subroutine 32 32 100.0
pod 6 6 100.0
total 151 152 99.3


line stmt bran cond sub pod time code
1 2     2   80461 use strict;
  2         13  
  2         49  
2 2     2   9 use warnings;
  2         4  
  2         64  
3             package Test::Deep::JType 0.008;
4             # ABSTRACT: Test::Deep helpers for JSON::Typist data
5              
6 2     2   615 use JSON::PP ();
  2         11819  
  2         33  
7 2     2   399 use JSON::Typist ();
  2         21  
  2         47  
8 2     2   11 use Test::Deep 1.126 (); # LeafWrapper, as_test_deep_cmp
  2         36  
  2         38  
9              
10 2     2   8 use Exporter 'import';
  2         3  
  2         512  
11             our @EXPORT = qw( jcmp_deeply jstr jnum jbool jtrue jfalse );
12              
13             #pod =head1 OVERVIEW
14             #pod
15             #pod L is a very useful library for testing data structures.
16             #pod Test::Deep::JType extends it with routines for testing
17             #pod L-annotated data.
18             #pod
19             #pod By default, Test::Deep's C will interpret plain numbers and strings
20             #pod as shorthand for C tests, meaning that the corresponding input
21             #pod data will also need to be a plain number or string. That means that this test
22             #pod won't work:
23             #pod
24             #pod my $json = q[ { "key": "value" } ];
25             #pod my $data = decode_json($json);
26             #pod my $typed = JSON::Typist->new->apply_types( $data );
27             #pod
28             #pod cmp_deeply($typed, { key => "value" });
29             #pod
30             #pod ...because C<"value"> will refuse to match an object. You I wrap each
31             #pod string or number to be compared in C or C respectively, but this
32             #pod can be a hassle, as well as a lot of clutter.
33             #pod
34             #pod C is exported by Test::Deep::JType, and behaves just like
35             #pod C, but plain numbers and strings are wrapped in C tests
36             #pod rather than shallow ones, so they always compare with C.
37             #pod
38             #pod To test that the input data matches the right type, other routines are exported
39             #pod that check type as well as content.
40             #pod
41             #pod =cut
42              
43             #pod =func jcmp_deeply
44             #pod
45             #pod This behaves just like Test::Deep's C but wraps plain scalar and
46             #pod number expectations in C, meaning they're compared with C only,
47             #pod instead of also asserting that the found value must not be an object.
48             #pod
49             #pod =cut
50              
51             sub jcmp_deeply {
52 9     9 1 7265 local $Test::Builder::Level = $Test::Builder::Level + 1;
53 7     7   18244 local $Test::Deep::LeafWrapper = sub { Test::Deep::JType::_String->new(@_) },
54 9         46 Test::Deep::cmp_deeply(@_);
55             }
56              
57             #pod =func jstr
58             #pod
59             #pod =func jnum
60             #pod
61             #pod =func jbool
62             #pod
63             #pod =func jtrue
64             #pod
65             #pod =func jfalse
66             #pod
67             #pod These routines are plain old Test::Deep-style assertions that check not only
68             #pod for data equivalence, but also that the data is the right type.
69             #pod
70             #pod C, C, and C take arguments, which are passed to the non-C
71             #pod version of the test used in building the C-style version. In other words,
72             #pod you can write:
73             #pod
74             #pod jcmp_deeply(
75             #pod $got,
76             #pod {
77             #pod name => jstr("Ricardo"),
78             #pod age => jnum(38.2, 0.01),
79             #pod calm => jbool(1),
80             #pod cool => jbool(),
81             #pod collected => jfalse(),
82             #pod },
83             #pod );
84             #pod
85             #pod If no argument is given, then the wrapped value isn't inspected. C just
86             #pod makes sure the value was a JSON string, without comparing it to anything.
87             #pod
88             #pod C and C are shorthand for C and C,
89             #pod respectively.
90             #pod
91             #pod As long as they've got a specific value to test for (that is, you called
92             #pod C and not C, the tests produced by these routines will
93             #pod serialize via a C-enabled JSON encode into the appropriate
94             #pod types. This makes it convenient to use these routines for building JSON as
95             #pod well as testing it.
96             #pod
97             #pod =cut
98              
99             my $STRING = Test::Deep::obj_isa('JSON::Typist::String');
100             my $NUMBER = Test::Deep::obj_isa('JSON::Typist::Number');
101             my $BOOL = Test::Deep::any(
102             Test::Deep::obj_isa('JSON::XS::Boolean'),
103             Test::Deep::obj_isa('JSON::PP::Boolean'),
104             );
105              
106 4     4 1 742 sub jstr { Test::Deep::JType::jstr->new(@_); }
107 4     4 1 2814 sub jnum { Test::Deep::JType::jnum->new(@_); }
108 7     7 1 26 sub jbool { Test::Deep::JType::jbool->new(@_); }
109              
110             my $TRUE = jbool(1);
111             my $FALSE = jbool(0);
112              
113 3     3 1 10 sub jtrue { $TRUE }
114 3     3 1 7 sub jfalse { $FALSE }
115              
116             {
117             package
118             Test::Deep::JType::_String;
119              
120 2     2   745 use Test::Deep::Cmp;
  2         1142  
  2         8  
121              
122             sub init
123             {
124 7     7   39 my $self = shift;
125              
126 7         64 $self->{val} = shift;
127             }
128              
129             sub descend
130             {
131 7     7   97 my $self = shift;
132 7         9 my $got = shift;
133              
134             # Stringify what we got for test output purposes. Otherwise,
135             # string overloading won't be called on $got, and we'll end up
136             # with 'JSON::Typist::String=SCALAR(0x...) in our test output
137 7 100       57 $self->data->{got} = $got . "" if defined $got;
138              
139             # If either is undef but not both this is a failure where
140             # as Test::Deep::String would just stringify the undef,
141             # throw a warning, and pass
142 7 100 75     67 if (defined($got) xor defined($self->{val})) {
143 4         9 return 0;
144             }
145              
146 3         8 return $got eq $self->{val};
147             }
148              
149             sub diag_message
150             {
151 5     5   2328 my $self = shift;
152              
153 5         7 my $where = shift;
154              
155 5         12 return "Comparing $where as a string";
156             }
157             }
158              
159             {
160             package Test::Deep::JType::jstr 0.008;
161              
162             use overload
163             '""' => sub {
164             Carp::confess("can't use valueless jstr() as a string")
165 4 100   4   4496 unless defined ${ $_[0] };
  4         249  
166 1         3 return ${ $_[0] };
  1         9  
167             },
168 2     2   394 fallback => 1;
  2         4  
  2         17  
169              
170 2     2   505 BEGIN { our @ISA = 'JSON::Typist::String'; }
171             sub TO_JSON {
172             Carp::confess("can't use valueless jstr() test as JSON data")
173 2 100   2   3196 unless defined ${ $_[0] };
  2         98  
174 1         2 return "${ $_[0] }";
  1         3  
175             }
176              
177             sub as_test_deep_cmp {
178 3     3   2584 my ($self) = @_;
179 3         28 my $value = $$self;
180 3 100       11 return defined $value ? Test::Deep::all($STRING, Test::Deep::str($value))
181             : $STRING;
182             }
183             }
184              
185             {
186             package Test::Deep::JType::jnum 0.008;
187              
188             use overload
189             '0+' => sub {
190             Carp::confess("can't use valueless jnum() as a number")
191 4 100   4   4268 unless defined ${ $_[0] };
  4         246  
192 1         3 return ${ $_[0] };
  1         9  
193             },
194 2     2   11 fallback => 1;
  2         5  
  2         15  
195              
196 2     2   469 BEGIN { our @ISA = 'JSON::Typist::Number'; }
197             sub TO_JSON {
198             Carp::confess("can't use valueless jnum() test as JSON data")
199 2 100   2   903 unless defined ${ $_[0] };
  2         176  
200 1         2 return 0 + ${ $_[0] };
  1         2  
201             }
202              
203             sub as_test_deep_cmp {
204 3     3   1387 my ($self) = @_;
205 3         30 my $value = $$self;
206 3 100       10 return defined $value ? Test::Deep::all($NUMBER, Test::Deep::num($value))
207             : $NUMBER;
208             }
209             }
210              
211             {
212             package Test::Deep::JType::jbool 0.008;
213              
214             use overload
215             'bool' => sub {
216             Carp::confess("can't use valueless jbool() as a bool")
217 5 100   5   5182 unless defined ${ $_[0] };
  5         272  
218 2         3 return ${ $_[0] };
  2         6  
219             },
220 2     2   11 fallback => 1;
  2         3  
  2         17  
221              
222             sub TO_JSON {
223             Carp::confess("can't use valueless jbool() test as JSON data")
224 3 100   3   3392 unless defined ${ $_[0] };
  3         89  
225 2 100       4 return ${ $_[0] } ? \1 : \0;
  2         6  
226             }
227              
228             sub new {
229 7     7   14 my ($class, $value) = @_;
230 7         20 bless \$value, $class;
231             }
232              
233             sub as_test_deep_cmp {
234 8     8   3113 my ($self) = @_;
235 8         28 my $value = $$self;
236 8 100       23 return defined $value ? Test::Deep::all($BOOL, Test::Deep::bool($value))
237             : $BOOL;
238             }
239             }
240              
241             1;
242              
243             __END__