1 package TAP::Parser::ResultFactory;
4 use vars qw($VERSION @ISA %CLASS_FOR);
7 use TAP::Parser::Result::Bailout ();
8 use TAP::Parser::Result::Comment ();
9 use TAP::Parser::Result::Plan ();
10 use TAP::Parser::Result::Pragma ();
11 use TAP::Parser::Result::Test ();
12 use TAP::Parser::Result::Unknown ();
13 use TAP::Parser::Result::Version ();
14 use TAP::Parser::Result::YAML ();
18 ##############################################################################
22 TAP::Parser::ResultFactory - Factory for creating TAP::Parser output objects
26 use TAP::Parser::ResultFactory;
28 my $factory = TAP::Parser::ResultFactory->new;
29 my $result = $factory->make_result( $token );
41 This is a simple factory class which returns a L<TAP::Parser::Result> subclass
42 representing the current bit of test data from TAP (usually a single line).
43 It is used primarily by L<TAP::Parser::Grammar>. Unless you're subclassing,
44 you probably won't need to use this module directly.
52 Creates a new factory class.
53 I<Note:> You currently don't need to instantiate a factory in order to use it.
57 Returns an instance the appropriate class for the test token passed in.
59 my $result = TAP::Parser::ResultFactory->make_result($token);
61 Can also be called as an instance method.
66 my ( $proto, $token ) = @_;
67 my $type = $token->{type};
68 return $proto->class_for($type)->new($token);
73 Takes one argument: C<$type>. Returns the class for this $type, or C<croak>s
76 =head3 C<register_type>
78 Takes two arguments: C<$type>, C<$class>
80 This lets you override an existing type with your own custom type, or register
81 a completely new type, eg:
83 # create a custom result type:
87 @ISA = 'TAP::Parser::Result';
89 # register with the factory:
90 TAP::Parser::ResultFactory->register_type( 'my_type' => __PACKAGE__ );
93 my $r = TAP::Parser::ResultFactory->( { type => 'my_type' } );
95 Your custom type should then be picked up automatically by the L<TAP::Parser>.
101 plan => 'TAP::Parser::Result::Plan',
102 pragma => 'TAP::Parser::Result::Pragma',
103 test => 'TAP::Parser::Result::Test',
104 comment => 'TAP::Parser::Result::Comment',
105 bailout => 'TAP::Parser::Result::Bailout',
106 version => 'TAP::Parser::Result::Version',
107 unknown => 'TAP::Parser::Result::Unknown',
108 yaml => 'TAP::Parser::Result::YAML',
113 my ( $class, $type ) = @_;
115 # return target class:
116 return $CLASS_FOR{$type} if exists $CLASS_FOR{$type};
120 Carp::croak("Could not determine class for result type '$type'");
124 my ( $class, $type, $rclass ) = @_;
126 # register it blindly, assume they know what they're doing
127 $CLASS_FOR{$type} = $rclass;
135 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
137 There are a few things to bear in mind when creating your own
144 The factory itself is never instantiated (this I<may> change in the future).
145 This means that C<_initialize> is never called.
149 C<TAP::Parser::Result-E<gt>new> is never called, $tokens are reblessed.
150 This I<will> change in a future version!
154 L<TAP::Parser::Result> subclasses will register themselves with
155 L<TAP::Parser::ResultFactory> directly:
158 TAP::Parser::ResultFactory->register_type( foo => __PACKAGE__ );
160 Of course, it's up to you to decide whether or not to ignore them.
166 package MyResultFactory;
172 use TAP::Parser::ResultFactory;
174 @ISA = qw( TAP::Parser::ResultFactory );
176 # force all results to be 'MyResult'
186 L<TAP::Parser::Result>,
187 L<TAP::Parser::Grammar>