Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ainame/p5-test-more-hooks
Test::More::Hooks provide before/after hooks for each subtest test cases.
https://github.com/ainame/p5-test-more-hooks
Last synced: about 2 months ago
JSON representation
Test::More::Hooks provide before/after hooks for each subtest test cases.
- Host: GitHub
- URL: https://github.com/ainame/p5-test-more-hooks
- Owner: ainame
- License: other
- Created: 2013-04-28T03:02:21.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-06-08T06:13:30.000Z (over 11 years ago)
- Last Synced: 2024-06-21T15:22:31.099Z (7 months ago)
- Language: Perl
- Size: 203 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
package Test::More::Hooks;
use 5.008005;
use strict;
use warnings;our $VERSION = "0.12";
use Carp qw(croak);
use Test::Builder::Module;
our @ISA = qw(Test::Builder::Module);
our @EXPORT = qw(subtest before after);# $ LEVEL is controlled by the local variable.
our $LEVEL = 0;
my $BEFORE = {};
my $AFTER = {};BEGIN {
croak "Test::More::Hooks must be loaded after Test::More."
unless exists $INC{'Test/More.pm'};
}sub subtest {
my ($name, $subtests) = @_;
$BEFORE->{$LEVEL}->() if 'CODE' eq ref $BEFORE->{$LEVEL};my $result;
{
local $LEVEL = $LEVEL + 1;
my $tb = Test::More::Hooks->builder;
$result = $tb->subtest(@_);
_clean_hooks();
};$AFTER->{$LEVEL}->() if 'CODE' eq ref $AFTER->{$LEVEL};
return $result;
}sub before (&) {
my $block = shift;
$BEFORE->{$LEVEL} = $block;
}sub after (&) {
my $block = shift;
$AFTER->{$LEVEL} = $block;
}sub level {
return $LEVEL;
}sub _clean_hooks {
$BEFORE->{$LEVEL} = undef;
$AFTER->{$LEVEL} = undef;
}1;
__END__=encoding utf-8
=head1 NAME
Test::More::Hooks - It provides before/after hooks of subtest.
=head1 SYNOPSIS
use Test::More;
use Test::More::Hooks;subtest "some context" => sub {
my $subject;
before { $subject = Foo->new; };
after { undef $subject; };subtest "given some argument" => sub {
my $actual = $subject->foo(1,2,3);
is $actual, 10;
};subtest "given other argument" => sub {
my $actual = $subject->foo(4,5,6);
is $actual, 20;
};
};=head1 DESCRIPTION
Test::More::Hooks is simply testing module. This provides only before/after hooks
for Test::More::subtest based test cases.=head1 FUNCTIONS
=head2 before BLOCK
Test::More::Hooks export this function by default.
If you given BLOCK for 'before' function, Test::More::Hooks would register this BLOCK with the 'before stack'.
Then, it is executed before the process continue to each subtest blocks of same level variable scope.=head2 after BLOCK
Test::More::Hooks export this function by default.
If you given BLOCK for 'after' function, Test::More::Hooks would register this BLOCK with the 'after stack'.
Then, it is executed after the process go out each subtest blocks of same level variable scope.=head1 LICENSE
Copyright (C) ainame.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.=head1 AUTHOR
ainame [email protected]
=cut