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