File Coverage

blib/lib/Test/Lazy.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition 2 3 66.6
subroutine 10 10 100.0
pod 3 3 100.0
total 37 38 97.3


line stmt bran cond sub pod time code
1             package Test::Lazy;
2              
3 2     2   71158 use warnings;
  2         5  
  2         62  
4 2     2   10 use strict;
  2         4  
  2         160  
5              
6             =head1 NAME
7              
8             Test::Lazy - A quick and easy way to compose and run tests with useful output.
9              
10             =head1 VERSION
11              
12             Version 0.061
13              
14             =cut
15              
16             our $VERSION = '0.061';
17              
18             =head1 SYNOPSIS
19              
20             use Test::Lazy qw/check try/;
21              
22             # Will evaluate the code and check it:
23             try('qw/a/' => eq => 'a');
24             try('qw/a/' => ne => 'b');
25             try('qw/a/' => is => ['a']);
26              
27             # Don't evaluate, but still compare:
28             check(1 => is => 1);
29             check(0 => isnt => 1);
30             check(a => like => qr/[a-zA-Z]/);
31             check(0 => unlike => qr/a-zA-Z]/);
32             check(1 => '>' => 0);
33             check(0 => '<' => 1);
34              
35             # A failure example:
36              
37             check([qw/a b/] => is => [qw/a b c/]);
38              
39             # Failed test '['a','b'] is ['a','b','c']'
40             # Compared array length of $data
41             # got : array with 2 element(s)
42             # expect : array with 3 element(s)
43              
44              
45             # Custom test explanation:
46              
47             try('2 + 2' => '==' => 5, "Math is hard: %?");
48              
49             # Failed test 'Math is hard: 2 + 2 == 5'
50             # got: 4
51             # expected: 5
52              
53             =head1 DESCRIPTION
54              
55             Ever get tired of coming up with a witty test message? Think that the best explanation for a test is the code behind it? Test::Lazy is for you.
56             Test::Lazy will take a stringified piece of code, evaluate it, and use a comparator to match the result to an expectation. If the test fails, then Test::Lazy will use the
57             code as the test explanation so you can exactly what went wrong.
58              
59             You can even put in your own amendment to Test::Lazy's response, just use the '%?' marker in your explanation.
60              
61             =head1 COMPARISON
62              
63             If is an ARRAY or HASH reference, then Test::Lazy will do a structure comparison, using cmp_structure as opposed to cmp_scalar. Generally, this means using Test::Deep
64             to do the comparison.
65              
66             For try or check, should be one of the below:
67              
68             =head2 Scalar
69              
70             ok: Test::More::ok
71              
72             not_ok: ! Test::More::ok
73              
74             < > <= >= lt gt le ge == != eq ne: Test::More::cmp_ok
75              
76             is isnt like unlike: Test::More::{is,isnt,like,unlike}
77              
78             =head2 Structural
79              
80             ok: Test::More::ok
81              
82             not_ok: ! Test::More::ok
83              
84             bag same_bag samebag: Test::Deep::cmp_bag
85              
86             set same_set sameset: Test::Deep::cmp_set
87              
88             same is like eq ==: Test::Deep::cmp_deeply
89              
90             isnt unlink ne !=: Test::More::ok(!Test::Deep::eq_deeply)
91              
92             =cut
93              
94             BEGIN {
95 2     2   44 our @EXPORT_OK = qw/check try template/;
96 2     2   12 use base qw/Exporter/;
  2         6  
  2         179  
97             }
98              
99 2     2   938 use Test::Lazy::Tester;
  2         7  
  2         18  
100 2     2   83 use Test::Builder;
  2         3  
  2         434  
101              
102             {
103             my $singleton;
104             sub _singleton() {
105 45   66 45   232 return $singleton ||= Test::Lazy::Tester->new;
106             }
107             *singleton = \&_singleton
108             }
109              
110             =head1 EXPORTS
111              
112             =head2 check( , , , [ ] )
113              
114             Compare to using .
115             Optionally provide a to display on failure. If is not given,
116             then one will be automatically made from , , and .
117              
118             Note, if is an ARRAY or HASH, try will do structural comparison instead of scalar
119             comparison.
120              
121             check([qw/a b/] => is => [qw/a b c/]);
122              
123             # This will produce the following output:
124              
125             # Failed test '["a","b"] is ["a","b","c"]'
126             # at __FILE__ line __LINE__.
127             # got: '["a","b"]'
128             # expected: '["a","b","c"]'
129              
130             =cut
131              
132             sub check {
133 22     22 1 10768 local $Test::Builder::Level = $Test::Builder::Level + 1;
134 22         44 return _singleton->check(@_);
135             }
136              
137             =head2 try( , , , [ ] )
138              
139             Evaluate and compare the result to using .
140             Optionally provide a to display on failure. If is not given,
141             then one will be automatically made from , , and .
142              
143             C will also try to guess what representation is best for the result of
144             the statement, whether that be single value, ARRAY, or HASH. It'll do this based
145             on what is returned by the statement, and the type of .
146             See `perldoc -m Test::Lazy` for more detail.
147              
148             Note, if is an ARRAY or HASH, try will do structural comparison instead of scalar
149             comparison.
150              
151              
152             try("2 + 2" => '==' => 5);
153              
154             # This will produce the following output:
155              
156             # Failed test '2 + 2 == 5'
157             # at __FILE__ line __LINE__.
158             # got: '4'
159             # expected: '5'
160              
161             =cut
162              
163             sub try {
164 22     22 1 10141 local $Test::Builder::Level = $Test::Builder::Level + 1;
165 22         45 return _singleton->try(@_);
166             }
167              
168             =head2 template( ... )
169              
170             Convenience function for creating a C. All arguments are directly passed to
171             C.
172              
173             See L for more details.
174              
175             Returns a new L object.
176              
177             =cut
178              
179             sub template {
180 1     1 1 492 return _singleton->template(@_);
181             }
182              
183             =head1 METHODS
184              
185             =head2 Test::Lazy->singleton
186              
187             Access the underlying Test::Lazy::Tester object to customize comparators or renderers.
188              
189             Test::Lazy->singleton->cmp_scalar->{xyzzy} = sub {
190             Test::More::cmp_ok($_[0] => eq => "xyzzy", $_[2]);
191             };
192              
193             # ... meanwhile ...
194              
195             check("xyzy" => "is_xyzzy");
196              
197             # Failed test 'xyzy is_xyzzy'
198             # got: 'xyzy'
199             # expected: 'xyzzy'
200              
201             Returns a L object.
202              
203             =head1 AUTHOR
204              
205             Robert Krimen, C<< >>
206              
207             =head1 BUGS
208              
209             Please report any bugs or feature requests to
210             C, or through the web interface at
211             L.
212             I will be notified, and then you'll automatically be notified of progress on
213             your bug as I make changes.
214              
215             =head1 SUPPORT
216              
217             You can find documentation for this module with the perldoc command.
218              
219             perldoc Test::Lazy
220              
221             You can also look for information at:
222              
223             =over 4
224              
225             =item * AnnoCPAN: Annotated CPAN documentation
226              
227             L
228              
229             =item * CPAN Ratings
230              
231             L
232              
233             =item * RT: CPAN's request tracker
234              
235             L
236              
237             =item * Search CPAN
238              
239             L
240              
241             =back
242              
243             =head1 ACKNOWLEDGEMENTS
244              
245             =head1 COPYRIGHT & LICENSE
246              
247             Copyright 2007 Robert Krimen, all rights reserved.
248              
249             This program is free software; you can redistribute it and/or modify it
250             under the same terms as Perl itself.
251              
252             =cut
253              
254             1; # End of Test::Lazy