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

NAME

Test::Arrow - Object-Oriented testing library

SYNOPSIS

    use Test::Arrow;

    my $arr = Test::Arrow->new;

    $arr->got(1)->ok;

    $arr->expect(uc 'foo')->to_be('FOO');

    $arr->name('Test Name')
        ->expected('FOO')
        ->got(uc 'foo')
        ->is;

    $arr->expected(6)
        ->got(2 * 3)
        ->is_num;

    $arr->expected(qr/^ab/)
        ->got('abc')
        ->like;

    $arr->warnings(sub { warn 'Bar' })->catch(qr/^Ba/);
    $arr->throw(sub { die 'Baz' })->catch(qr/^Ba/);

DESCRIPTION

The opposite DSL.

MOTIVATION

Test::Arrow is a testing helper as object-oriented operation. Perl5 has a lot of testing libraries. These libraries have nice DSL ways. However, sometimes we hope the Object as similar to ORM. It may slightly sound strange. But it'd be better to clarify operations and it's easy to understand what/how it is. Although there are so many arrows.

IMPORT OPTIONS

no_strict / no_warnings

By default, Test::Arrow imports strict and warnings pragma automatically. If you don't want it, then you should pass 'no_strict' or 'no_warnings' option on use.

    use Test::Arrow; # Just use Test::Arrow, automatically turn on 'strict' and 'warnings'

Turn off 'strict' and 'warnings';

    use Test::Arrow qw/no_strict no_warnings/;

binary

By default, Test::Arrow sets utf8 pragma globally to avoid warnings such as "Wide charactors". If you don't want it, then you should pass 'binary' option on use.

    use Test::Arrow qw/binary/; # utf8 pragma off

METHODS

new

The constructor.

    my $arr = Test::Arrow->new;
no_x

If you set no_x option the ture value, then the x method doesn't show any message.

plan

If you set plan option with hash, then the plan method, it's same as Test::More's one, it will be called in constructor.

    my $arr = Test::Arrow->new(
        plan => {
            tests => 2,
        }
    );

    $arr->ok(1);
    $arr->is(1, 1);

If you want to skip all tests,

    my $arr = Test::Arrow->new(
        plan => {
            skip_all => 'Reason',
        }
    );

Test::More has the import option for test plan, but Test::Arrow doesn't. Below code doesn't work as your intent.

    use Test::Arrow plan => 12;

It should be in constructor option or should be called as straightforward method/function.

    $arr->plan(skip_all => 'Reason');

SETTERS

expected($expected)

The setter of expected value. $expected will be compared with $got

expect($expected)

The alias of expected method.

got($got)

The setter of got value. $got will be compared with $expected

name($test_name)

The setter of the test name. If you ommit to set the test name, then it's automatically set.

Note that the test name automatically set by Test::Name::FromLine.

If you write one test as multiple lines like below,

    L5:  $arr->expected('FOO')
    L6:      ->got(uc 'foo')
    L7:      ->is;

then the output of test will be like below

    ok 1 - L5: $arr->expected('FOO')

You might expect the test name like below, however, it's actually being like above.

    ok 1 - L7:     ->is;

The test name is taken from the first line of each test.

TEST EXECUTERS

pass($test_name)

fail($test_name)

Just pass or fail

ok

    $arr->got($true)->ok;

More easy,

    $arr->ok($true);

is

isnt

Similar to is and isnt compare values with eq and ne.

    $arr->expected('FOO')->got(uc 'foo')->is;

is_num

isnt_num

Similar to is_num and isnt_num compare values with == and !=.

    $arr->expected(6)->got( 2 * 3 )->is_num;

to_be($got)

The $got will be compare with expected value.

    $arr->expect(uc 'foo')->to_be('FOO');

like

unlike

like matches $got value against the $expected regex.

    $arr->expected(qr/b/)->got('abc')->like;

Works exactly as like, only it checks if $got does not match the expected pattern.

unlike shows where a place could have matched if it's failed like below.

    $arr->name('Unlike Fail example')
        ->expected(qr/b/)
        ->got('abc')
        ->unlike;
    #   Failed test 'Unlike Fail example'
    #   at t/unlike.t line 12.
    #                   'abc'
    #           matches '(?^:b)'
    #           matched at line: 1, offset: 2

can_ok($class, @methods)

Checks to make sure the $class or $object can do these @methods (works with functions, too).

    Test::Arrow->can_ok($class, @methods);
    Test::Arrow->can_ok($object, @methods);

isa_ok

    $arr->got($got_object)->expected($class)->isa_ok;

Checks to see if the given $got_object->isa($class). Also checks to make sure the object was defined in the first place.

It works on references, too:

    $arr->got($array_ref)->expected('ARRAY')->isa_ok;

EXCEPTION TEST

throw_ok($code_ref)

It makes sure that $code_ref gets an exception.

    $arr->throw_ok(sub { die 'oops' });

throw($code_ref)

catch($regex)

The throw method invokes $code_ref, and if it's certenly thrown an exception, then an exception message will be set as $got and the $regex in catch method will be evaluated to $got.

    $arr->throw(sub { die 'Baz' })->catch(qr/^Ba/);

Above test is equivalent to below

    $arr->throw(sub { die 'Baz' })->expected(qr/^Ba/)->like;

Actually, you can execute a test even only throw method

    $arr->throw(sub { die 'Baz' }, qr/^Ba/);

warnings_ok($code_ref)

It makes sure that $code_ref gets warnings.

    $arr->warnings_ok(sub { warn 'heads up' });

There are aliases of warnings_ok method: warning_ok, warn_ok.

warnings($code_ref)

warnings method is called like below:

    $arr->warnings(sub { warn 'heads up' })->catch(qr/^heads/);

warning is an alias of warnings.

BAIL OUT

BAIL_OUT($why)

Terminates tests.

CONDITIONAL TESTS

skip

In order to skip tests like below.

    SKIP: {
        $arr->skip($why, $how_many) if $condition;

        ...normal testing code goes here...
    }

Test::Arrow doesn't have todo_skip.

UTILITIES

You can call below utilities methods even without an instance.

diag

Output message to STDERR

    $arr->diag('some messages');
    Test::Arrow->diag('some message');

note

Output message to STDOUT

    $arr->note('some messages');
    Test::Arrow->note('some message');

explain

If you call explain method without args, then explain method outputs object info (expected, got and name) as hash.

    $arr->name('foo')->expected('BAR')->got(uc 'bar')->explain->is;
    # {
    #   'expected' => 'BAR',
    #   'got' => 'BAR',
    #   'name' => 'foo'
    # }
    ok 1 - foo

If you call explain method with arg, then explain method just dumps it.

    $arr->expected('BAR')->got(uc 'bar')->explain({ baz => 123 })->is;
    # {
    #   'baz' => 123
    # }
    ok 1 - foo

x($ref)

If you call x method, then the current values (name, expected and got) are dumped with arg.

    $arr->name('x test')->expected('BAR')->got(uc 'bar')->x({ foo => 123 })->is;
    # {
    #   'foo' => 123
    # }
    # {
    #   'expected' => 'BAR',
    #   'got' => 'BAR',
    #   'name' => 'x test'
    # }

done_testing

Declare of done testing.

    $arr->done_testing($number_of_tests_run);
    Test::Arrow->done_testing;

Note that you must never put done_testing inside an END { ... } block.

done

Alias of done_testing

CONSTANTS

PASS = 1

FAIL = 0

REPOSITORY

Test::Arrow is hosted on github: http://github.com/bayashi/Test-Arrow

I appreciate any feedback :D

AUTHOR

Dai Okabayashi <bayashi@cpan.org>

SEE ALSO

Test::More

Test::Kantan - A behavior-driven development framework

Test::Builder

Test::Name::FromLine

LICENSE

Test::Arrow is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0. (Note that, unlike the Artistic License 1.0, version 2.0 is GPL compatible by itself, hence there is no benefit to having an Artistic 2.0 / GPL disjunction.) See the file LICENSE for details.