Test-Simpler
view release on metacpan or search on metacpan
lib/Test/Simpler.pm view on Meta::CPAN
package Test::Simpler;
use warnings;
use strict;
use autodie;
use 5.014;
our $VERSION = '0.000008';
use PadWalker qw< peek_my peek_our >;
use Data::Dump qw< dump >;
use List::Util qw< max >;
use base 'Test::Builder::Module';
# Export the module's interface...
our @EXPORT = ( 'ok' );
our @EXPORT_OK = ();
our %EXPORT_TAGS = ();
sub ok($;$) {
my $outcome = shift;
my $desc = @_ ? "@_" : undef;
# Grab the upscope variables...
my %value_for = ( %{peek_our(1)}, %{peek_my(1)} );
# Cache for source code...
state %source;
# Where were we called???
my ($package, $file, $line) = caller;
# Grab the source...
if (!exists $source{$file}) {
open my $fh, '<', $file;
$source{$file} = do { local $/; readline $fh };
}
my $source = $source{$file};
my $remove_lines = $line - 1;
$source =~ s{ \A (?: \N*\n ){$remove_lines} }{}xms;
# Extract code from source...
use PPI;
my $doc = PPI::Document->new(\$source);
# Extract statement from code...
my @target;
STATEMENT:
for my $statement (@{ $doc->find('PPI::Statement') }) {
my @token = $statement->children;
next STATEMENT if $token[0]->content ne 'ok';
@target = @token[1..$#token]; # don't need the 'ok'
last STATEMENT;
}
# Did we find the statement?
die "Can't understand arguments to ok()" if !@target;
# Flatten to a list of relevant tokens...
SKIPPED:
while (1) {
# Remove whitespaces...
if ($target[0]->isa('PPI::Token::Whitespace')) {
shift @target;
}
# Step into lists...
elsif ($target[0]->isa('PPI::Structure::List')) {
@target = $target[0]->children;
}
lib/Test/Simpler.pm view on Meta::CPAN
ok 4 - $result ~~ $expected[0]
not ok 5 - $result !~ $expected[0]
# Failed test at demo/ts_ok-er.pl line 18
# $result
# isn't !~
# $expected[0]
# Because:
# $result --> 1
# $expected[0] --> 1
#
not ok 6 - $result > double($hash{'b b'})
# Failed test at demo/ts_ok-er.pl line 19
# $result
# isn't >
# twice($hash{'half'})
# Because:
# $result --> 1
# $hash{'half'} --> 2
#
# Looks like you failed 4 tests of 6.
=head1 INTERFACE
The module's API is identical to Test::Simple. See that module's
documentation for details.
=head1 DIAGNOSTICS
=over
=item C<< Can't understand arguments to ok() >>
The module was unable to parse the arguments you passed to C<ok()>. Or,
more precisely, PPI was not able to. That must be some freaky arcane
Perl expression you used there! Maybe try a simpler test condition?
=back
=head1 CONFIGURATION AND ENVIRONMENT
Test::Simpler requires no configuration files or environment variables.
=head1 DEPENDENCIES
Requires:
=over
=item PPI
...to parse the arguments of C<ok()>
=item Test::Builder::Module
...to produce TAP reports and to emulate the Test::Simple interface.
=item PadWalker
...to track variable values
=item Data::Dump
...to print variable values
=back
(Which means it's only simpler on the outside ;-)
=head1 INCOMPATIBILITIES
None reported.
=head1 BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to
C<bug-test-simpler@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 AUTHOR
Damian Conway C<< <DCONWAY@CPAN.org> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2012, Damian Conway C<< <DCONWAY@CPAN.org> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
( run in 0.361 second using v1.01-cache-2.11-cpan-0d8aa00de5b )