Test::Arrow - Object-Oriented testing library
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/); done;
The function t is exported as a shortcut for constructor. It initializes an instance for each.
t
use Test::Arrow; t->got(1)->ok; t->expect(uc 'foo')->to_be('FOO'); done;
The opposite DSL.
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.
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.
Test::Arrow
strict
warnings
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/;
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
The constructor.
my $arr = Test::Arrow->new;
If you set no_x option the ture value, then the x method doesn't show any message.
no_x
x
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.
plan
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');
The function t will be exported. It's initializer to get instance as shortcut.
my $arr = Test::Arrow; $arr->got(1)->ok;
Above test is same as below.
t->got(1)->ok;
The function t can get arguments as same as new.
new
The setter of expected value. $expected will be compared with $got
The alias of expected method.
expected
The setter of got value. $got will be compared with $expected
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.
Just pass or fail
$arr->got($true)->ok;
More easy,
$arr->ok($true);
Similar to is and isnt compare values with eq and ne.
is
isnt
eq
ne
$arr->expected('FOO')->got(uc 'foo')->is;
Similar to is_num and isnt_num compare values with == and !=.
is_num
isnt_num
==
!=
$arr->expected(6)->got( 2 * 3 )->is_num;
The $got will be compare with expected value.
$arr->expect(uc 'foo')->to_be('FOO');
like matches $got value against the $expected regex.
like
$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.
unlike
$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
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);
$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.
$got_object->isa($class)
It works on references, too:
$arr->got($array_ref)->expected('ARRAY')->isa_ok;
$arr->got($ref1)->expected($ref2)->is_deeply;
Compare references, it does a deep comparison walking each data structure to see if they are equivalent.
This is_deeply is mostly same as Test::More's one. You can use Test::Deep more in-depth functionality along these lines. Also Test::Deep::Matcher is more better to use with.
is_deeply
It makes sure that $code_ref gets an exception.
$arr->throw_ok(sub { die 'oops' });
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.
throw
catch
$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/);
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_ok
warning_ok
warn_ok
warnings method is called like below:
$arr->warnings(sub { warn 'heads up' })->catch(qr/^heads/);
warning is an alias of warnings.
warning
Terminates tests.
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.
todo_skip
You can call below utilities methods even without an instance.
Output message to STDERR
$arr->diag('some messages'); Test::Arrow->diag('some message');
Output message to STDOUT
$arr->note('some messages'); Test::Arrow->note('some message');
If you call explain method without args, then explain method outputs object info (expected, got and name) as hash.
explain
$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
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' # }
Declare of done testing. Test::Arrow exports done into your test script. You can call done as function.
done
$arr->ok(1); done();
Same as done. But done_testing is NOT exported. You should call done_testing as class method or instance method.
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.
END { ... }
Test::Arrow is hosted on github: http://github.com/bayashi/Test-Arrow
I appreciate any feedback :D
Dai Okabayashi <bayashi@cpan.org>
Test::More
Test::Kantan - A behavior-driven development framework
Test::Builder
Test::Name::FromLine
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.
To install Test::Arrow, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Arrow
CPAN shell
perl -MCPAN -e shell install Test::Arrow
For more information on module installation, please visit the detailed CPAN module installation guide.