| 1 | package Test::Declare; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use base 'Exporter'; |
|---|
| 5 | |
|---|
| 6 | our $VERSION = '0.02'; |
|---|
| 7 | |
|---|
| 8 | my @test_more_exports; |
|---|
| 9 | my @test_more_method; |
|---|
| 10 | BEGIN { |
|---|
| 11 | @test_more_method = qw( |
|---|
| 12 | use_ok require_ok |
|---|
| 13 | eq_array eq_hash eq_set |
|---|
| 14 | can_ok |
|---|
| 15 | ); |
|---|
| 16 | @test_more_exports = (qw( |
|---|
| 17 | skip todo todo_skip |
|---|
| 18 | pass fail |
|---|
| 19 | plan |
|---|
| 20 | diag |
|---|
| 21 | BAIL_OUT |
|---|
| 22 | $TODO |
|---|
| 23 | ),@test_more_method); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | use Test::More import => \@test_more_exports; |
|---|
| 27 | use Test::Exception; |
|---|
| 28 | use Test::Warn; |
|---|
| 29 | use Test::Deep; |
|---|
| 30 | |
|---|
| 31 | my @test_wrappe_method = qw( |
|---|
| 32 | cmp_ok ok dies_ok throws_ok |
|---|
| 33 | is isnt is_deeply like unlike |
|---|
| 34 | isa_ok cmp_deeply re cmp_bag |
|---|
| 35 | prints_ok stderr_ok |
|---|
| 36 | warning_like warnings_like warning_is warnings_are |
|---|
| 37 | ); |
|---|
| 38 | |
|---|
| 39 | my @test_method = (@test_wrappe_method, @test_more_method); |
|---|
| 40 | |
|---|
| 41 | our @EXPORT = (@test_more_exports, @test_wrappe_method, qw/ |
|---|
| 42 | init cleanup run test describe blocks |
|---|
| 43 | /); |
|---|
| 44 | |
|---|
| 45 | my $test_block_name; |
|---|
| 46 | sub test ($$) { ## no critic |
|---|
| 47 | $test_block_name = shift; |
|---|
| 48 | shift->(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | { |
|---|
| 52 | no strict 'refs'; ## no critic |
|---|
| 53 | for my $sub (qw/init cleanup/) { |
|---|
| 54 | *{"Test\::Declare\::$sub"} = sub (&) { |
|---|
| 55 | shift->(); |
|---|
| 56 | }; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | sub run (&) { shift } ## no critic |
|---|
| 61 | |
|---|
| 62 | sub describe ($$) { ## no critic |
|---|
| 63 | shift; shift->(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | use PPI; |
|---|
| 67 | sub PPI::Document::find_test_blocks { |
|---|
| 68 | my $self = shift; |
|---|
| 69 | my $blocks = $self->find( |
|---|
| 70 | sub { |
|---|
| 71 | $_[1]->isa('PPI::Token::Word') |
|---|
| 72 | and |
|---|
| 73 | grep { $_[1]->{content} eq $_ } @test_method |
|---|
| 74 | } |
|---|
| 75 | )||[]; |
|---|
| 76 | return @$blocks |
|---|
| 77 | } |
|---|
| 78 | sub blocks { |
|---|
| 79 | my @caller = caller; |
|---|
| 80 | my $file = $caller[1]; |
|---|
| 81 | my $doc = PPI::Document->new($file) or die $!; |
|---|
| 82 | return scalar( $doc->find_test_blocks ); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | ## Test::More wrapper |
|---|
| 86 | { |
|---|
| 87 | no strict 'refs'; ## no critic |
|---|
| 88 | for my $sub (qw/is is_deeply like isa_ok isnt unlike/) { |
|---|
| 89 | *{"Test\::Declare\::$sub"} = sub ($$;$) { |
|---|
| 90 | my ($actual, $expected, $name) = @_; |
|---|
| 91 | my $test_more_code = "Test\::More"->can($sub); |
|---|
| 92 | goto $test_more_code, $actual, $expected, $name||$test_block_name; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | sub cmp_ok ($$$;$) { ## no critic |
|---|
| 99 | my ($actual, $operator, $expected, $name) = @_; |
|---|
| 100 | my $test_more_code = "Test\::More"->can('cmp_ok'); |
|---|
| 101 | goto $test_more_code, $actual, $operator, $expected, $name||$test_block_name; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | sub ok ($;$) { ## no critic |
|---|
| 105 | my ($test, $name) = @_; |
|---|
| 106 | my $test_more_code = "Test\::More"->can('ok'); |
|---|
| 107 | goto $test_more_code, $test, $name||$test_block_name; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | ## original method |
|---|
| 111 | use IO::Scalar; |
|---|
| 112 | sub prints_ok (&$;$) { ## no critic |
|---|
| 113 | my ($code, $expected, $name) = @_; |
|---|
| 114 | |
|---|
| 115 | tie *STDOUT, 'IO::Scalar', \my $stdout; |
|---|
| 116 | $code->(); |
|---|
| 117 | like($stdout, qr/$expected/, $name||$test_block_name); |
|---|
| 118 | untie *STDOUT; |
|---|
| 119 | } |
|---|
| 120 | sub stderr_ok (&$;$) { ## no critic |
|---|
| 121 | my ($code, $expected, $name) = @_; |
|---|
| 122 | |
|---|
| 123 | tie *STDERR, 'IO::Scalar', \my $stderr; |
|---|
| 124 | $code->(); |
|---|
| 125 | like($stderr, qr/$expected/, $name||$test_block_name); |
|---|
| 126 | untie *STDERR; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | 1; |
|---|
| 130 | |
|---|
| 131 | __END__ |
|---|
| 132 | =head1 NAME |
|---|
| 133 | |
|---|
| 134 | Test::Declare - declarative testing |
|---|
| 135 | |
|---|
| 136 | =head1 SYNOPSIS |
|---|
| 137 | |
|---|
| 138 | use strict; |
|---|
| 139 | use warnings; |
|---|
| 140 | use Test::Declare; |
|---|
| 141 | plan tests => blocks; |
|---|
| 142 | |
|---|
| 143 | describe 'foo bar test' => run { |
|---|
| 144 | init { |
|---|
| 145 | # init.. |
|---|
| 146 | }; |
|---|
| 147 | test 'foo is bar?' => run { |
|---|
| 148 | is foo, bar; |
|---|
| 149 | }; |
|---|
| 150 | cleanup { |
|---|
| 151 | # cleanup.. |
|---|
| 152 | }; |
|---|
| 153 | }; |
|---|
| 154 | |
|---|
| 155 | =head1 DESCRIPTION |
|---|
| 156 | |
|---|
| 157 | Test::More and Test::Exception and Test::Deep wrapper module. |
|---|
| 158 | |
|---|
| 159 | =head1 METHOD |
|---|
| 160 | |
|---|
| 161 | =head2 describe |
|---|
| 162 | outline setting. |
|---|
| 163 | |
|---|
| 164 | =head2 blocks |
|---|
| 165 | get test block count. |
|---|
| 166 | |
|---|
| 167 | =head2 init |
|---|
| 168 | definition of init block. |
|---|
| 169 | |
|---|
| 170 | =head2 test |
|---|
| 171 | definition of test block. |
|---|
| 172 | |
|---|
| 173 | =head2 run |
|---|
| 174 | run test code. |
|---|
| 175 | |
|---|
| 176 | =head2 cleanup |
|---|
| 177 | definition of cleanup block. |
|---|
| 178 | |
|---|
| 179 | =head1 BUGS AND LIMITATIONS |
|---|
| 180 | |
|---|
| 181 | No bugs have been reported. |
|---|
| 182 | |
|---|
| 183 | =head1 SEE ALSO |
|---|
| 184 | |
|---|
| 185 | This test module's core modules |
|---|
| 186 | |
|---|
| 187 | L<Test::More> and L<Test::Exception> and L<Test::Deep> and L<PPI> |
|---|
| 188 | |
|---|
| 189 | Test::Declare's sample tests |
|---|
| 190 | |
|---|
| 191 | L<DBIx::Class::TableNames> 's 01_table_names.t |
|---|
| 192 | |
|---|
| 193 | L<DBIx::Class::ProxyTable> 's 01_proxy.t |
|---|
| 194 | |
|---|
| 195 | =head1 AUTHOR |
|---|
| 196 | |
|---|
| 197 | Atsushi Kobayashi C<< <nekokak __at__ gmail.com> >> |
|---|
| 198 | |
|---|
| 199 | =head1 LICENCE AND COPYRIGHT |
|---|
| 200 | |
|---|
| 201 | Copyright (c) 2008, Atsushi Kobayashi C<< <nekokak __at__ gmail.com> >>. All rights reserved. |
|---|
| 202 | |
|---|
| 203 | This module is free software; you can redistribute it and/or |
|---|
| 204 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 205 | |
|---|