File Coverage

blib/lib/Test/Roo/Class.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 6 100.0
condition 5 5 100.0
subroutine 12 12 100.0
pod 4 4 100.0
total 59 59 100.0


line stmt bran cond sub pod time code
1 9     9   9878 use 5.008001;
  9         176  
  9         918  
2 9     9   212 use strictures;
  9         17  
  9         61  
3              
4             package Test::Roo::Class;
5             # ABSTRACT: Base class for Test::Roo test classes
6             our $VERSION = '1.004'; # VERSION
7              
8 9     9   1574 use Moo;
  9         19  
  9         61  
9 9     9   17174 use MooX::Types::MooseLike::Base qw/Str/;
  9         112737  
  9         1233  
10 9     9   110 use Test::More 0.96 import => [qw/subtest/];
  9         370  
  9         100  
11              
12             #--------------------------------------------------------------------------#
13             # attributes
14             #--------------------------------------------------------------------------#
15              
16             #pod =attr description
17             #pod
18             #pod A description for a subtest block wrapping all tests by the object. It is a
19             #pod 'lazy' attribute. Test classes may implement their own C<_build_description>
20             #pod method to create a description from object attributes. Otherwise, the default
21             #pod is "testing with CLASS".
22             #pod
23             #pod =cut
24              
25             has description => (
26             is => 'rw',
27             isa => Str,
28             lazy => 1,
29             builder => 1,
30             );
31              
32             sub _build_description {
33 7     7   6037 my $class = ref $_[0];
34 7         172 return "testing with $class";
35             }
36              
37             #--------------------------------------------------------------------------#
38             # class or object methods
39             #--------------------------------------------------------------------------#
40              
41             #pod =method run_tests
42             #pod
43             #pod # as a class method
44             #pod $class->run_tests();
45             #pod $class->run_tests($description);
46             #pod $class->run_tests($init_args);
47             #pod $class->run_tests($description $init_args);
48             #pod
49             #pod # as an object method
50             #pod $self->run_tests();
51             #pod $self->run_tests($description);
52             #pod
53             #pod If called as a class method, this creates a test object using an optional hash
54             #pod reference of initialization arguments.
55             #pod
56             #pod When called as an object method, or after an object has been generated, this
57             #pod method sets an optional description and runs tests. It will call the C
58             #pod method (triggering any method modifiers), will run all tests (triggering any
59             #pod method modifiers on C) and will call the C method
60             #pod (triggering any method modifiers).
61             #pod
62             #pod If a description is provided, it will override any initialized or generated
63             #pod C attribute.
64             #pod
65             #pod The setup, tests and teardown will be executed in a L subtest
66             #pod block.
67             #pod
68             #pod =cut
69              
70             sub run_tests {
71 12     12 1 9410 my $self = shift;
72             # get hashref from end of args
73             # if any args are left, it must be description
74 12         66 my ( $desc, $args );
75 12 100 100     102 $args = pop if @_ && ref $_[-1] eq 'HASH';
76 12         20 $desc = shift;
77              
78             # create an object if needed and possibly update description
79 12 100 100     155 $self = $self->new( $args || {} )
80             if !ref $self;
81 12 100       14301 $self->description($desc)
82             if defined $desc;
83              
84             # execute tests wrapped in a subtest
85             subtest $self->description => sub {
86 12     12   13174 $self->setup;
87 12         1513 $self->_do_tests;
88 12         261426 $self->teardown;
89 12         1145 };
90             }
91              
92             #--------------------------------------------------------------------------#
93             # private methods and stubs
94             #--------------------------------------------------------------------------#
95              
96             #pod =method setup
97             #pod
98             #pod This is an empty method used to anchor method modifiers. It should not
99             #pod be overridden by subclasses.
100             #pod
101             #pod =cut
102              
103 12     12 1 2862 sub setup { }
104              
105             #pod =method each_test
106             #pod
107             #pod This method wraps the code references set by the C function
108             #pod from L or L in a L subtest block.
109             #pod
110             #pod It may also be used to anchor modifiers that should run before or after
111             #pod each test block, though this can lead to brittle design as modifiers
112             #pod will globally affect every test block, including composed ones.
113             #pod
114             #pod =cut
115              
116             sub each_test {
117 14     14 1 4965 my ( $self, $code ) = @_;
118 14         62 $code->($self);
119             }
120              
121             #pod =method teardown
122             #pod
123             #pod This is an empty method used to anchor method modifiers. It should not
124             #pod be overridden by subclasses.
125             #pod
126             #pod =cut
127              
128 12     12 1 85 sub teardown { }
129              
130             # anchor for tests as method modifiers
131 12     12   373 sub _do_tests { }
132              
133             1;
134              
135              
136             # vim: ts=4 sts=4 sw=4 et:
137              
138             __END__