The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Magic - terse tests with useful error feedback

VERSION

Version 0.21

SYNOPSIS

    use Test::Magic tests => 9;

    test 'numbers',
      is 1 == 1,
      is 1 > 2; 

    test 'strings',
      is 'asdf' eq 'asdf',
      is 'asdf' gt 'asdf';

    test 'regex',
      is 'abcd' == qr/bc/,   # == is overloaded when rhs is a regex
      is 'abcd' ~~ q/bc/,    # ~~ can be used with a string rhs in perl 5.10+
      is 'badc' ~~ q/bc/;

    test 'data structures',
      is [1, 2, 3] == [1, 2, 3],   # also overloaded when rhs is a reference
      is {a => 1, b => 2} == {a => 1, b => 1};

results in the following output:

    1..9
    ok 1 - numbers 1
    not ok 2 - numbers 2
    #   Failed test 'numbers 2'
    #   at example.t line 3.
    #     '1'
    #         >
    #     '2'
    ok 3 - strings 1
    not ok 4 - strings 2
    #   Failed test 'strings 2'
    #   at example.t line 7.
    #     'asdf'
    #         gt
    #     'asdf'
    ok 5 - regex 1
    ok 6 - regex 2
    not ok 7 - regex 3
    #   Failed test 'regex 3'
    #   at example.t line 11.
    #                   'badc'
    #     doesn't match '(?-xism:bc)'
    ok 8 - data structures 1
    not ok 9 - data structures 2
    #   Failed test 'data structures 2'
    #   at example.t line 16.
    #     Structures begin differing at:
    #          $got->{b} = '2'
    #     $expected->{b} = '1'
    # Looks like you failed 4 tests of 9.

you get the output of Test::More's cmp_ok , like , or is_deeply with a more natural syntax, and the test's name is moved before the test and is numbered if you have more than one test.

EXPORT

test is isnt and everything from Test::More except is and isnt

SUBROUTINES

test NAME, LIST_OF_TESTS

test runs a list of tests. if there is one test, NAME is used unchanged. otherwise, each test is sequentially numbered ( NAME 1 , NAME 2 , ...)

is GOT OPERATOR EXPECTED

prepares a test for test . do not use parenthesis with is . if you must, it needs to be written (is 1 == 1) and never is(1 == 1)

isnt GOT OPERATOR EXPECTED

prepares a test for test that expects to fail. do not use parenthesis with isnt . if you must, it needs to be written (isnt 1 == 1) and never isnt(1 == 1)

NOTES

this module does not use source filtering. for those interested in how it does work, the code:

    test 'my test',
      is 1 == 1,
      is 1 == 2;

is parsed as follows:

    test( 'my test,
       (is(1) == 1),
       (is(1) == 2)
    );

the is function binds tightly to its argument, making the parenthesis unnecessary. it returns an overloaded object that then captures the comparison operator and the rhs argument. the overloading operation returns a code reference which expects to be passed its test name. the test function does just that. so ultimately, the code becomes something like this:

   Test::More::cmp_ok( 1, '==', 1, 'my test 1' );
   Test::More::cmp_ok( 1, '==', 2, 'my test 2' );

cmp_ok is used for most comparisons, like or unlike for regex, and is_deeply when the operator is == and the rhs (the expected value) is a reference.

if you need to do some setup before the test:

    test 'this test requires setup', do {
      my $obj = Package->new();
      ...
      is ref $obj eq 'Package',
      is $obj->value eq 'some value'
    };

AUTHOR

Eric Strom, <asg at cpan.org>

BUGS

Please report any bugs or feature requests to bug-test-magic at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Magic. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

this module uses Test::More internally

LICENSE AND COPYRIGHT

Copyright 2010 Eric Strom.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.