~ubuntu-branches/ubuntu/trusty/libtest-roo-perl/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Test/Roo/Class.pm

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2014-01-14 19:25:11 UTC
  • Revision ID: package-import@ubuntu.com-20140114192511-9ycl4dc1zgamfnek
Tags: upstream-1.002
ImportĀ upstreamĀ versionĀ 1.002

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use 5.008001;
 
2
use strictures;
 
3
 
 
4
package Test::Roo::Class;
 
5
# ABSTRACT: Base class for Test::Roo test classes
 
6
our $VERSION = '1.002'; # VERSION
 
7
 
 
8
use Moo;
 
9
use MooX::Types::MooseLike::Base qw/Str/;
 
10
use Test::More 0.96 import => [qw/subtest/];
 
11
 
 
12
#--------------------------------------------------------------------------#
 
13
# attributes
 
14
#--------------------------------------------------------------------------#
 
15
 
 
16
 
 
17
has description => (
 
18
    is      => 'rw',
 
19
    isa     => Str,
 
20
    lazy    => 1,
 
21
    builder => 1,
 
22
);
 
23
 
 
24
sub _build_description {
 
25
    my $class = ref $_[0];
 
26
    return "testing with $class";
 
27
}
 
28
 
 
29
#--------------------------------------------------------------------------#
 
30
# class or object methods
 
31
#--------------------------------------------------------------------------#
 
32
 
 
33
 
 
34
sub run_tests {
 
35
    my $self = shift;
 
36
    # get hashref from end of args
 
37
    # if any args are left, it must be description
 
38
    my ( $desc, $args );
 
39
    $args = pop if @_ && ref $_[-1] eq 'HASH';
 
40
    $desc = shift;
 
41
 
 
42
    # create an object if needed and possibly update description
 
43
    $self = $self->new( $args || {} )
 
44
      if !ref $self;
 
45
    $self->description($desc)
 
46
      if defined $desc;
 
47
 
 
48
    # execute tests wrapped in a subtest
 
49
    subtest $self->description => sub {
 
50
        $self->setup;
 
51
        $self->_do_tests;
 
52
        $self->teardown;
 
53
    };
 
54
}
 
55
 
 
56
#--------------------------------------------------------------------------#
 
57
# private methods and stubs
 
58
#--------------------------------------------------------------------------#
 
59
 
 
60
 
 
61
sub setup { }
 
62
 
 
63
 
 
64
sub each_test {
 
65
    my ( $self, $code ) = @_;
 
66
    $code->($self);
 
67
}
 
68
 
 
69
 
 
70
sub teardown { }
 
71
 
 
72
# anchor for tests as method modifiers
 
73
sub _do_tests { }
 
74
 
 
75
1;
 
76
 
 
77
 
 
78
# vim: ts=4 sts=4 sw=4 et:
 
79
 
 
80
__END__
 
81
 
 
82
=pod
 
83
 
 
84
=encoding utf-8
 
85
 
 
86
=head1 NAME
 
87
 
 
88
Test::Roo::Class - Base class for Test::Roo test classes
 
89
 
 
90
=head1 VERSION
 
91
 
 
92
version 1.002
 
93
 
 
94
=head1 DESCRIPTION
 
95
 
 
96
This module is the base class for L<Test::Roo> test classes.  It provides
 
97
methods to run tests and anchor modifiers.  Generally, you should not extend
 
98
this class yourself, but use L<Test::Roo> to do so instead.
 
99
 
 
100
=head1 ATTRIBUTES
 
101
 
 
102
=head2 description
 
103
 
 
104
A description for a subtest block wrapping all tests by the object.  It is a
 
105
'lazy' attribute.  Test classes may implement their own C<_build_description>
 
106
method to create a description from object attributes.  Otherwise, the default
 
107
is "testing with CLASS".
 
108
 
 
109
=head1 METHODS
 
110
 
 
111
=head2 run_tests
 
112
 
 
113
    # as a class method
 
114
    $class->run_tests();
 
115
    $class->run_tests($description);
 
116
    $class->run_tests($init_args);
 
117
    $class->run_tests($description $init_args);
 
118
 
 
119
    # as an object method
 
120
    $self->run_tests();
 
121
    $self->run_tests($description);
 
122
 
 
123
If called as a class method, this creates a test object using an optional hash
 
124
reference of initialization arguments.
 
125
 
 
126
When called as an object method, or after an object has been generated, this
 
127
method sets an optional description and runs tests.  It will call the C<setup>
 
128
method (triggering any method modifiers), will run all tests (triggering any
 
129
method modifiers on C<each_test>) and will call the C<teardown> method
 
130
(triggering any method modifiers).
 
131
 
 
132
If a description is provided, it will override any initialized or generated
 
133
C<description> attribute.
 
134
 
 
135
The setup, tests and teardown will be executed in a L<Test::More> subtest
 
136
block.
 
137
 
 
138
=head2 setup
 
139
 
 
140
This is an empty method used to anchor method modifiers.  It should not
 
141
be overridden by subclasses.
 
142
 
 
143
=head2 each_test
 
144
 
 
145
This method wraps the code references set by the C<test> function
 
146
from L<Test::Roo> or L<Test::Roo::Role> in a L<Test::More> subtest block.
 
147
 
 
148
It may also be used to anchor modifiers that should run before or after
 
149
each test block, though this can lead to brittle design as modifiers
 
150
will globally affect every test block, including composed ones.
 
151
 
 
152
=head2 teardown
 
153
 
 
154
This is an empty method used to anchor method modifiers.  It should not
 
155
be overridden by subclasses.
 
156
 
 
157
=for Pod::Coverage each_test setup teardown
 
158
 
 
159
=head1 AUTHOR
 
160
 
 
161
David Golden <dagolden@cpan.org>
 
162
 
 
163
=head1 COPYRIGHT AND LICENSE
 
164
 
 
165
This software is Copyright (c) 2013 by David Golden.
 
166
 
 
167
This is free software, licensed under:
 
168
 
 
169
  The Apache License, Version 2.0, January 2004
 
170
 
 
171
=cut