NAME

Test::Mini - base assertions for Test::Mini

SYNOPSIS

use Test::Mini::Assertions;

assert($day_of_week eq 'Friday', 'Test should only be run on Friday!');

DESCRIPTION

This module provides a number of assertion functions, which are imported into your namespace when you use the module.

All of these functions take an optional final argument, $msg, which will be used as the text of the assertion, if specified.

EXPORTED FUNCTIONS

assert($test, $msg)

Asserts that $test is truthy, and throws a Test::Mini::Exception::Assert if that assertion fails. For example:

assert 1;
assert 'true', 'Truth should shine clear';

assert_block($block, $msg)

Deprecated, as this function offers little advantage over the assert() function, described above.

Asserts that the given code reference returns a truthy value. For example:

assert_block { 'true' };

assert_block \&some_sub, 'expected better from &some_sub';

assert_can($obj, $method, $msg)

Verifies that the given $obj is capable of responding to the given $method name.

Examples:

assert_can $date, 'day_of_week';

assert_can $time, 'seconds', '$time cannot respond to #seconds';

This function is aliased as function assert_responds_to().

assert_contains($collection, $obj, $msg)

Verifies that the given $collection contains the given $obj as a member.

Examples:

assert_contains [qw/ 1 2 3 /], 2;
assert_contains { a => 'b' }, 'a';  # 'b' also contained
assert_contains 'expectorate', 'xp';
assert_contains Collection->new(1, 2, 3), 2;  # if Collection->contains(2)

The first argument, $collection, can be an array, a hash, a string, or an object that provides a contains method.

This function is aliased as assert_includes().

assert_defined($obj, $msg)

Validates that the given $obj is defined.

Example:

assert_defined $value;

This function is aliased as refute_undef().

assert_dies($sub, $error, $msg)

Tests that the supplied code block dies, and fails if it succeeds. If $error is provided, the error message in $@ must contain it.

Examples:

assert_dies { die 'LAGHLAGHLAGHL' };
assert_dies { die 'Failure on line 27 in Foo.pm' } 'line 27';

assert_empty($collection, $msg)

Verifies the emptiness of a collection.

Examples:

assert_empty [];
assert_empty {};
assert_empty '';
assert_empty Collection->new();  # if Collection->new()->is_empty()

assert_equal($actual, $expected, $msg)

Checks two given arguments for equality. The first argument, $actual, is the value being tested (eg has been calculated by code under test), and the second argument gives the expected value.

Examples:

assert_equal 3.000, 3;
assert_equal lc('FOO'), 'foo';
assert_equal [qw/ 1 2 3 /], [ 1, 2, 3 ];
assert_equal { a => 'eh' }, { a => 'eh' };

# if $expected->equals(Class->new())
assert_equal Class->new(), $expected;

This function is also aliased as assert_eq().

assert_in_delta($actual, $expected, $delta, $msg)

Checks that the difference between $actual and $expected is less than $delta.

Examples:

assert_in_delta 1.001, 1;
assert_in_delta 104, 100, 5;

assert_in_epsilon($actual, $expected, $epsilon, $msg)

Checks that the difference between $actual and $expected is less than a given fraction of the smaller of the two numbers.

Examples:

assert_in_epsilon 22.0 / 7.0, Math::Trig::pi;
assert_in_epsilon 220, 200, 0.10;

If $epsilon isn't given, it defaults to 0.001.

assert_instance_of($object, $type, $msg)

Validates that the given $object is an instance of $type.

Examples:

my $object = MyApp::Person->new();
assert_instance_of $object, 'MyApp::Person';

assert_is_a($obj, $type, $msg)

Validates that $obj inherits from $type.

Examples:

assert_is_a 'Employee', 'Employee';
assert_is_a Employee->new(), 'Employee';
assert_is_a 'Employee', 'Person'; # assuming Employee->isa('Person')
assert_is_a Employee->new(), 'Person';

This function is also available as assert_isa().

assert_match($string, $pattern, $msg)

Validates that the given $string matches the given $pattern.

Examples:

assert_match 'Four score and seven years ago...', qr/score/;

assert_undef($obj, $msg)

Validates that the given $obj is undefined.

Examples:

assert_undef $value;  # if not defined $value

Also available as refute_defined().

flunk($msg)

Causes the current test to exit immediately with a failing status.

refute($test, $msg)

Asserts that $test is falsey, and throws a Test::Mini::Exception::Assert if that assertion fails.

Examples:

refute 0;
refute undef, 'Deny the untruths';

refute_block($block, $msg)

Deprecated: This assertion offers little advantage over the base refute(). This will be removed in v2.0.0.

Asserts that the given code reference returns a falsey value.

Examples:

refute_block { '' };
refute_block \&some_sub, 'expected worse from &some_sub';

refute_can($obj, $method, $msg)

Verifies that the given $obj is not capable of responding to the given $method name.

Examples:

refute_can $date, 'to_time';
refute_can $time, 'day', '$time cannot respond to #day';

Also available as refute_responds_to().

refute_contains($collection, $obj, $msg)

Verifies that the given $collection does not contain the given $obj as a member.

Examples:

refute_contains [qw/ 1 2 3 /], 5;
refute_contains { a => 'b' }, 'x';
refute_contains 'expectorate', 'spec';
refute_contains Collection->new(1, 2, 3), 5;  # unless Collection->contains(5)

The $collection can be a hash ref, an array ref, a string, or an instance of a class that provides a contains() method.

refute_empty($collection, $msg)

Verifies the non-emptiness of a collection.

Examples:

refute_empty [ 1 ];
refute_empty { a => 1 };
refute_empty 'full';
refute_empty Collection->new();  # unless Collection->new()->is_empty()

See the description for refute_contains() above for what $collection can be.

refute_equal($actual, $unexpected, $msg)

Checks two given arguments for inequality.

Examples:

refute_equal 3.001, 3;
refute_equal lc('FOOL'), 'foo';
refute_equal [qw/ 1 23 /], [ 1, 2, 3 ];
refute_equal { a => 'ae' }, { a => 'eh' };
refute_equal Class->new(), $expected;  # unless $expected->equals(Class->new())

Also available as refute_eq().

refute_in_delta($actual, $expected, $delta, $msg)

Checks that the difference between $actual and $expected is greater than $delta.

Examples:

refute_in_delta 1.002, 1;
refute_in_delta 106, 100, 5;

refute_in_epsilon($actual, $expected, $epsilon, $msg)

Checks that the difference between $actual and $expected is greater than a given fraction of the smaller of the two numbers.

Examples:

refute_in_epsilon 21.0 / 7.0, Math::Trig::pi;
refute_in_epsilon 220, 200, 0.20

refute_match($string, $pattern, $msg)

Validates that the given $string does not match the given $pattern.

Examples:

refute_match 'Four score and seven years ago...', qr/score/;

skip($msg)

Allows the current test to be bypassed with an indeterminate status.

SEE ALSO

Test::Mini

REPOSITORY

https://github.com/pvande/Test-Mini

AUTHOR

Pieter van de Bruggen <pvande@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by Pieter van de Bruggen <pvande@cpan.org>

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.