]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Component.pm
9a7ba2bb69ed3bb75c4cc2559533c15564542be0
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Component.pm
1 package Test::Valgrind::Component;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Component - Base class for Test::Valgrind components.
9
10 =head1 VERSION
11
12 Version 1.13
13
14 =cut
15
16 our $VERSION = '1.13';
17
18 =head1 DESCRIPTION
19
20 This class is the base for all others that act as components that can be started and stopped.
21
22 =cut
23
24 use Scalar::Util ();
25
26 use base qw<Test::Valgrind::Carp>;
27
28 =head1 METHODS
29
30 =head2 C<new>
31
32 Basic constructor.
33
34 =cut
35
36 sub new {
37  my $self = shift;
38
39  my $class = $self;
40  if (Scalar::Util::blessed($self)) {
41   $class = ref $self;
42   if ($self->isa(__PACKAGE__)) {
43    $self->{started} = undef;
44    return $self;
45   }
46  }
47
48  bless {
49   started => undef,
50  }, $class;
51 }
52
53 =head2 C<started [ $bool ]>
54
55 Specifies whether the component is running (C<1>), stopped (C<0>) or was never started (C<undef>).
56
57 =cut
58
59 sub started { @_ <= 1 ? $_[0]->{started} : ($_[0]->{started} = $_[1] ? 1 : 0) }
60
61 =head2 C<start>
62
63 Marks the component as started, and throws an exception if it was already.
64 Returns its self object.
65
66 =cut
67
68 sub start {
69  my ($self) = @_;
70
71  $self->_croak(ref($self) . ' component already started') if $self->started;
72  $self->started(1);
73
74  $self;
75 }
76
77 =head2 C<finish>
78
79 Marks the component as stopped, and throws an exception if it wasn't started.
80 Returns its self object.
81
82 =cut
83
84 sub finish {
85  my ($self) = @_;
86
87  $self->_croak(ref($self) . ' component is not started') unless $self->started;
88  $self->started(0);
89
90  $self;
91 }
92
93 =head1 SEE ALSO
94
95 L<Test::Valgrind>.
96
97 =head1 AUTHOR
98
99 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
100
101 You can contact me by mail or on C<irc.perl.org> (vincent).
102
103 =head1 BUGS
104
105 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
106 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
107
108 =head1 SUPPORT
109
110 You can find documentation for this module with the perldoc command.
111
112     perldoc Test::Valgrind::Component
113
114 =head1 COPYRIGHT & LICENSE
115
116 Copyright 2009,2010,2011 Vincent Pit, all rights reserved.
117
118 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
119
120 =cut
121
122 1; # End of Test::Valgrind::Component