File Coverage

blib/lib/Test2/Roo/Class.pm
Criterion Covered Total %
statement 31 31 100.0
branch 6 6 100.0
condition 5 5 100.0
subroutine 12 12 100.0
pod 4 4 100.0
total 58 58 100.0


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