| 1 | use 5.006; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Generic::Assertions; | ||
| 6 | |||
| 7 | our $VERSION = '0.001001'; | ||
| 8 | |||
| 9 | # ABSTRACT: A Generic Assertion checking class | ||
| 10 | |||
| 11 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 12 | |||
| 13 | use Carp qw( croak carp ); | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ( $class, @args ) = @_; | ||
| 17 | if ( @args % 2 == 1 and not ref $args[0] ) { | ||
| 18 | croak '->new() expects even number of arguments or a hash reference, got ' . scalar @args . ' argument(s)'; | ||
| 19 | } | ||
| 20 | my $hash; | ||
| 21 | if ( ref $args[0] ) { | ||
| 22 | $hash = { args => $args[0] }; | ||
| 23 | } | ||
| 24 | else { | ||
| 25 | $hash = { args => {@args} }; | ||
| 26 | } | ||
| 27 | my $self = bless $hash, $class; | ||
| 28 | $self->BUILD; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | sub BUILD { | ||
| 37 | my ($self) = @_; | ||
| 38 | my $tests = $self->_tests; | ||
| 39 | for my $test ( keys %{$tests} ) { | ||
| 40 | croak 'test ' . $test . ' must be a CodeRef' if not 'CODE' eq ref $tests->{$test}; | ||
| 41 | } | ||
| 42 | my $handlers = $self->_handlers; | ||
| 43 | for my $handler ( keys %{$handlers} ) { | ||
| 44 | croak 'handler ' . $handler . ' must be a CodeRef' if not 'CODE' eq ref $handlers->{$handler}; | ||
| 45 | } | ||
| 46 | croak 'input_transformer must be a CodeRef' if not 'CODE' eq ref $self->_input_transformer; | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _args { | ||
| 51 | my ($self) = @_; | ||
| 52 | return $self->{args} if exists $self->{args}; | ||
| 53 | return ( $self->{args} = {} ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _tests { | ||
| 57 | my ( $self, ) = @_; | ||
| 58 | return $self->{tests} if exists $self->{tests}; | ||
| 59 | my %tests; | ||
| 60 | for my $key ( grep { !/\A-/msx } keys %{ $self->_args } ) { | ||
| 61 | $tests{$key} = $self->_args->{$key}; | ||
| 62 | } | ||
| 63 | return ( $self->{tests} = { %tests, %{ $self->_args->{'-tests'} || {} } } ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _handlers { | ||
| 67 | my ( $self, ) = @_; | ||
| 68 | return $self->{handlers} if exists $self->{handlers}; | ||
| 69 | return ( $self->{handlers} = { %{ $self->_handler_defaults }, %{ $self->_args->{'-handlers'} || {} } } ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _handler_defaults { | ||
| 73 | return { | ||
| 74 | test => sub { | ||
| 75 | my ($status) = @_; | ||
| 76 | return $status; | ||
| 77 | }, | ||
| 78 | log => sub { | ||
| 79 | my ( $status, $message, $name, @slurpy ) = @_; | ||
| 80 | carp sprintf 'Assertion < log %s > = %s : %s', $name, ( $status || '0' ), $message; | ||
| 81 | return $slurpy[0]; | ||
| 82 | }, | ||
| 83 | should => sub { | ||
| 84 | my ( $status, $message, $name, @slurpy ) = @_; | ||
| 85 | carp "Assertion < should $name > failed: $message" unless $status; | ||
| 86 | return $slurpy[0]; | ||
| 87 | }, | ||
| 88 | should_not => sub { | ||
| 89 | my ( $status, $message, $name, @slurpy ) = @_; | ||
| 90 | carp "Assertion < should_not $name > failed: $message" if $status; | ||
| 91 | return $slurpy[0]; | ||
| 92 | }, | ||
| 93 | must => sub { | ||
| 94 | my ( $status, $message, $name, @slurpy ) = @_; | ||
| 95 | croak "Assertion < must $name > failed: $message" unless $status; | ||
| 96 | return $slurpy[0]; | ||
| 97 | }, | ||
| 98 | must_not => sub { | ||
| 99 | my ( $status, $message, $name, @slurpy ) = @_; | ||
| 100 | croak "Assertion < must_not $name > failed: $message" if $status; | ||
| 101 | return $slurpy[0]; | ||
| 102 | }, | ||
| 103 | }; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _transform_input { | ||
| 107 | my ( $self, $name, @slurpy ) = @_; | ||
| 108 | return $self->_input_transformer->( $name, @slurpy ); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _input_transformer { | ||
| 112 | my ( $self, ) = @_; | ||
| 113 | return $self->{input_transformer} if exists $self->{input_transformer}; | ||
| 114 | if ( exists $self->_args->{'-input_transformer'} ) { | ||
| 115 | return ( $self->{input_transformer} = $self->_args->{'-input_transformer'} ); | ||
| 116 | } | ||
| 117 | return ( $self->{input_transformer} = $self->_input_transformer_default ); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _input_transformer_default { | ||
| 121 | return sub { shift; return @_ }; | ||
| 122 | } | ||
| 123 | |||
| 124 | # Dispatch the result of test name $test_name | ||
| 125 | sub _handle { ## no critic (Subroutines::ProhibitManyArgs) | ||
| 126 | my ( $self, $handler_name, $status_code, $message, $test_name, @slurpy ) = @_; | ||
| 127 | return $self->_handlers->{$handler_name}->( $status_code, $message, $test_name, @slurpy ); | ||
| 128 | } | ||
| 129 | |||
| 130 | # Perform $test_name and return its result | ||
| 131 | sub _test { | ||
| 132 | my ( $self, $test_name, @slurpy ) = @_; | ||
| 133 | my $tests = $self->_tests; | ||
| 134 | if ( not exists $tests->{$test_name} ) { | ||
| 135 | croak sprintf q[INVALID ASSERTION %s ( avail: %s )], $test_name, ( join q[,], keys %{$tests} ); | ||
| 136 | } | ||
| 137 | return $tests->{$test_name}->(@slurpy); | ||
| 138 | } | ||
| 139 | |||
| 140 | # Long form | ||
| 141 | # ->_assert( should => exist => path('./foo')) | ||
| 142 | # ->should( exist => path('./foo')) | ||
| 143 | sub _assert { | ||
| 144 | my ( $self, $handler_name, $test_name, @slurpy ) = @_; | ||
| 145 | my (@input) = $self->_transform_input( $test_name, @slurpy ); | ||
| 146 | my ( $status, $message ) = $self->_test( $test_name, @input ); | ||
| 147 | return $self->_handle( $handler_name, $status, $message, $test_name, @input ); | ||
| 148 | } | ||
| 149 | |||
| 150 | for my $handler (qw( should must should_not must_not test log )) { | ||
| 151 | my $code = sub { | ||
| 152 | my ( $self, $name, @slurpy ) = @_; | ||
| 153 | return $self->_assert( $handler, $name, @slurpy ); | ||
| 154 | }; | ||
| 155 | { | ||
| 156 | ## no critic (TestingAndDebugging::ProhibitNoStrict]) | ||
| 157 | no strict 'refs'; | ||
| 158 | *{ __PACKAGE__ . q[::] . $handler } = $code; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | __END__ |