File Coverage

blib/lib/Test/Object.pm
Criterion Covered Total %
statement 33 33 100.0
branch 2 4 50.0
condition n/a
subroutine 11 11 100.0
pod 0 2 0.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             package Test::Object;
2              
3             =pod
4            
5             =head1 NAME
6            
7             Test::Object - Thoroughly testing objects via registered handlers
8            
9             =head1 SYNOPSIS
10            
11             ###################################################################
12             # In your test module, register test handlers again class names #
13             ###################################################################
14            
15             package My::ModuleTester;
16            
17             use Test::More;
18             use Test::Object;
19            
20             # Foo::Bar is a subclass of Foo
21             Test::Object->register(
22             class => 'Foo',
23             tests => 5,
24             code => \&foo_ok,
25             );
26             Test::Object->register(
27             class => 'Foo::Bar',
28             # No fixed number of tests
29             code => \&foobar_ok,
30             );
31            
32             sub foo_ok {
33             my $object = shift;
34             ok( $object->foo, '->foo returns true' );
35             }
36            
37             sub foobar_ok {
38             my $object = shift;
39             is( $object->foo, 'bar', '->foo returns "bar"' );
40             }
41            
42             1;
43            
44            
45            
46             ###################################################################
47             # In test script, test object against all registered classes #
48             ###################################################################
49            
50             #!/usr/bin/perl -w
51            
52             use Test::More 'no_plan';
53             use Test::Object;
54             use My::ModuleTester;
55            
56             my $object = Foo::Bar->new;
57             isa_ok( $object, 'Foo::Bar' );
58             object_ok( $object );
59            
60             =head1 DESCRIPTION
61            
62             In situations where you have deep trees of classes, there is a common
63             situation in which you test a module 4 or 5 subclasses down, which should
64             follow the correct behaviour of not just the subclass, but of all the
65             parent classes.
66            
67             This should be done to ensure that the implementation of a subclass has
68             not somehow "broken" the object's behaviour in a more general sense.
69            
70             C<Test::Object> is a testing package designed to allow you to easily test
71             what you believe is a valid object against the expected behaviour of B<all>
72             of the classes in its inheritance tree in one single call.
73            
74             To do this, you "register" tests (in the form of CODE or function
75             references) with C<Test::Object>, with each test associated with a
76             particular class.
77            
78             When you call C<object_ok> in your test script, C<Test::Object> will check
79             the object against all registered tests. For each class that your object
80             responds to C<$object-E<gt>isa($class)> for, the appropriate testing
81             function will be called.
82            
83             Doing it this way allows adapter objects and other things that respond
84             to C<isa> differently that the default to still be tested against the
85             classes that it is advertising itself as correctly.
86            
87             This also means that more than one test might be "counted" for each call
88             to C<object_ok>. You should account for this correctly in your expected
89             test count.
90            
91             =cut
92              
93 2     2   33813 use 5.005;
  2         8  
  2         66  
94 2     2   10 use strict;
  2         5  
  2         61  
95 2     2   19 use Carp ();
  2         4  
  2         25  
96 2     2   9 use Exporter ();
  2         3  
  2         30  
97 2     2   13 use Test::More ();
  2         2  
  2         30  
98 2     2   10 use Scalar::Util ();
  2         2  
  2         33  
99 2     2   2647 use Test::Object::Test ();
  2         5  
  2         44  
100              
101 2     2   12 use vars qw{$VERSION @ISA @EXPORT};
  2         3  
  2         149  
102             BEGIN {
103 2     2   4 $VERSION = '0.07';
104 2         34 @ISA     = 'Exporter';
105 2         288 @EXPORT  = 'object_ok';
106             }
107              
108              
109              
110              
111              
112             #####################################################################
113             # Registration and Planning
114              
115             my @TESTS = ();
116              
117             sub register {
118 1     1 0 25 my $class = shift;
119 1         11 push @TESTS, Test::Object::Test->new( @_ );
120             }
121              
122              
123              
124              
125              
126             #####################################################################
127             # Testing Functions
128              
129             sub object_ok {
130 1 50   1 0 579 my $object = Scalar::Util::blessed($_[0]) ? shift
131             : Carp::croak("Did not provide an object to object_ok");
132              
133             # Iterate over the tests and run any we ->isa
134 1         3 foreach my $test ( @TESTS ) {
135 1 50       3 $test->run( $object ) if $object->isa( $test->class );
136             }
137              
138 1         396 1;
139             }
140              
141             1;
142              
143             =pod
144            
145             =head1 SUPPORT
146            
147             Bugs should be submitted via the CPAN bug tracker, located at
148            
149             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Object>
150            
151             For other issues, contact the author.
152            
153             =head1 AUTHOR
154            
155             Adam Kennedy E<lt>cpan@ali.asE<gt>
156            
157             =head1 SEE ALSO
158            
159             L<http://ali.as/>, L<Test::More>, L<Test::Builder::Tester>, L<Test::Class>
160            
161             =head1 COPYRIGHT
162            
163             Copyright 2005, 2006 Adam Kennedy. All rights reserved.
164            
165             This program is free software; you can redistribute
166             it and/or modify it under the same terms as Perl itself.
167            
168             The full text of the license can be found in the
169             LICENSE file included with this module.
170            
171             =cut
172