NAME
Test::Arrow - Object-Oriented testing library
SYNOPSIS
use Test::Arrow;
my $arr = Test::Arrow->new;
$arr->ok(1);
$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;
# `unlike` shows where a place could have matched if it's failed
$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
$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;
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
fail
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->expect('FOO')->got(uc 'foo')->is;
is_num
isnt_num
Similar to is_num
and isnt_num
compare values with ==
and !=
.
$arr->expect(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->expect(qr/b/)->got('abc')->like;
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/);
BAIL OUT
BAIL_OUT($why)
Terminates tests.
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
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.
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::Kantan - A behavior-driven development framework
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.