1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
use 5.008001;
use strictures;
package Test::Roo::Role;
# ABSTRACT: Composable role for Test::Roo
our $VERSION = '1.002'; # VERSION
use Test::Roo (); # no imports!
use Sub::Install;
sub import {
my ( $class, @args ) = @_;
my $caller = caller;
Sub::Install::install_sub(
{ into => $caller, code => 'test', from => 'Test::Roo' } );
strictures->import; # do this for Moo, since we load Moo in eval
eval qq{
package $caller;
use Moo::Role;
};
if (@args) {
eval qq{ package $caller; use Test::More \@args };
}
else {
eval qq{ package $caller; use Test::More };
}
die $@ if $@;
}
1;
# vim: ts=4 sts=4 sw=4 et:
__END__
=pod
=encoding utf-8
=head1 NAME
Test::Roo::Role - Composable role for Test::Roo
=head1 VERSION
version 1.002
=head1 SYNOPSIS
A testing role:
# t/lib/MyTestRole.pm
package MyTestRole;
use Test::Roo::Role; # loads Moo::Role and Test::More
requires 'class';
test 'object creation' => sub {
my $self = shift;
require_ok( $self->class );
my $obj = new_ok( $self->class );
};
1;
=head1 DESCRIPTION
This module defines test behaviors as a L<Moo::Role>.
=for Pod::Coverage method_names_here
=head1 USAGE
Importing L<Test::Roo::Role> also loads L<Moo::Role> (which gives you
L<strictures> with fatal warnings and other goodies).
Importing also loads L<Test::More>. Any import arguments are passed through to
Test::More's C<import> method.
=head2 Creating and requiring fixtures
You can create fixtures with normal Moo syntax. You can even make them lazy
if you want and require the composing class to provide the builder:
has fixture => (
is => 'lazy'
);
requires '_build_fixture';
Because this is a L<Moo::Role>, you can require any method you like, not
just builders.
See L<Moo::Role> and L<Role::Tiny> for everything you can do with roles.
=head2 Setup and teardown
You can add method modifiers around the C<setup> and C<teardown> methods and
these will be run before tests begin and after tests finish (respectively).
before setup => sub { ... };
after teardown => sub { ... };
You can also add method modifiers around C<each_test>, which will be
run before and after B<every> individual test. You could use these to
prepare or reset a fixture.
has fixture => ( is => 'lazy, clearer => 1, predicate => 1 );
after each_test => sub { shift->clear_fixture };
Roles may also modify C<setup>, C<teardown>, and C<each_test>, so the order
that modifiers will be called will depend on when roles are composed. Be
careful with C<each_test>, though, because the global effect may make
composition more fragile.
You can call test functions in modifiers. For example, you could
confirm that something has been set up or cleaned up.
before each_test => sub { ok( ! shift->has_fixture ) };
=head1 EXPORTED FUNCTIONS
Loading L<Test::Roo::Role> exports a single subroutine into the calling package
to declare tests.
=head2 test
test $label => sub { ... };
The C<test> function adds a subtest. The code reference will be called with
the test object as its only argument.
Tests are run in the order declared, so the order of tests from roles will
depend on when they are composed relative to other test declarations.
=head1 AUTHOR
David Golden <dagolden@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|