Assert-Conditional

 view release on metacpan or  search on metacpan

t/asserts.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;
use utf8;
use charnames qw(:full);

#use Carp::Always;

use Cwd qw(abs_path);
use FindBin qw($Bin);

$| = 1;

use lib abs_path("$Bin/../lib");

use Config qw(%Config); # because it's tied
use Math::BigInt; # for overloads
use Env qw($PATH @PATH); # because they're tied

use Test::More;
use Test2::Tools::Exception qw(dies lives);

use IO::File;
use IO::Handle;
use Hash::Util qw(lock_keys unlock_keys);

use Assert::Conditional qw(:all -if 1);
use Assert::Conditional::Utils qw(:list);

my($junk, @junk) ;

my $undef;
my $zero      = 0;
my $true      = 1;
my $one       = 1;
my $two       = 2;
my $minus_two = -2;
my $float     = 3.14;
my $string    = "string";
my @empty     = ();
my %empty;
my @stuff     = ("a" .. "z");
my @ten       = (1 .. 10);
my $class     = "IO::File";
my $obj       = $class->new();

my @primary_colors    = qw(red green blue);
my @all_colors        = (@primary_colors, qw(orange yellow cyan violet));
my @not_colors        = qw(black white pink purple);
my %primary_color     = map { $_ => 1 } @primary_colors;
my $primary_color_ref = \%primary_color;

my %locked_hash       =  %primary_color;
my $locked_hashref    = \%locked_hash;
lock_keys %locked_hash;
my %unlocked_hash     =  %primary_color;
my $unlocked_hashref  = \%unlocked_hash;

my %hash_of_hashes = (
    LOCKED  => $locked_hashref,
    UNLOCKED => $unlocked_hashref,
);

my $hashref_of_hashes        =  \%hash_of_hashes;
my $ref_of_hashref_of_hashes = \\%hash_of_hashes;

my $bignum = Math::BigInt->new("1000");

my $tied_object = do { 
    no warnings "once"; 
    tie *Tied_FH, "TieOut";
};

sub  void_context     {  assert_void_context()     }
sub  nonvoid_context  {  assert_nonvoid_context()  }
sub  list_context     {  assert_list_context()     }
sub  nonlist_context  {  assert_nonlist_context()  }
sub  scalar_context   {  assert_scalar_context()   }

my @good_tests = (

    q{assert_defined(1) },
    q{assert_defined_value(1) },
    q{assert_undefined(undef) },
    q{assert_defined("string") },
    q{assert_defined_value("string") },
    q{assert_defined_variable($true) },

    q{@junk = list_context() },
    q{$junk = nonlist_context() },
    q{@junk = nonvoid_context() },
    q{nonlist_context(); 0      },
    q{void_context(); 1      },
    q{$junk = nonvoid_context() },
    q{$junk = scalar_context() },
    q{@junk = list_context() },

    q{assert_false(!1) },
    q{assert_false(0) },
    q{assert_false(q()) },
    q{assert_false(undef) },
    q{assert_true('0 but true') },
    q{assert_true(1) },

    q{assert_is($one, $one)},
    q{assert_is($float, $float)},
    q{assert_is(@stuff, @stuff)},
    q{assert_isnt($one, $two)},

    q{assert_zero($zero)},
    q{assert_zero(+0)},
    q{assert_zero("-0")},
    q{assert_zero(0.0)},

    q{assert_numeric($one)},
    q{assert_numeric($float)},
    q{assert_numeric($minus_two)},
    q{assert_nonnumeric("stuff")},
    q{assert_negative($minus_two)},
    q{assert_positive($two)},
    q{assert_nonnegative($zero)},

t/asserts.t  view on Meta::CPAN


    q{ assert_tied_array(@PATH) },
    q{ assert_tied_arrayref(\\@PATH) },

    q{ assert_tied_hash(%Config) },
    q{ assert_tied_hashref(\\%Config) },

    q{ assert_tied_glob(*Tied_FH) },
    q{ assert_tied_globref(\\*Tied_FH) },

    q{assert_untied($0)},
    q{assert_untied(@ARGV)},
    q{assert_untied(%ENV)},
    q{assert_untied(*STDIN)},

    q{assert_untied_referent(\\$0)},
    q{assert_untied_referent(\\@ARGV)},
    q{assert_untied_referent(\\%ENV)},
    q{assert_untied_referent(\\*STDIN)},

    q{assert_untied_scalar($0)},
    q{assert_untied_array(@ARGV)},
    q{assert_untied_hash(%ENV)},
    q{assert_untied_glob(*STDIN)},

    q{assert_untied_scalarref(\\$0)},
    q{assert_untied_arrayref(\\@ARGV)},
    q{assert_untied_hashref(\\%ENV)},
    q{assert_untied_globref(\\*STDIN)},

    q{assert_object_overloads($bignum)},
    q{assert_object_overloads($bignum, qw[+ ++ - += * **])},

    q{assert_object_nummifies($bignum)},
    q{assert_object_stringifies($bignum)},
    q{assert_object_boolifies($bignum)},

    q{assert_object_stringifies($tied_object)},

    q[assert_happy_code { time() > 1 }],
    q[assert_unhappy_code { time() > time()+6 }],  

    q{assert_open_handle(*STDIN)},
    q{assert_open_handle(*DATA)},
    q{assert_open_handle(\*DATA)},
    q{assert_open_handle(*DATA{IO})},
    q{assert_directory("/")},


);

my $hu_version = Hash::Util->VERSION;

my %is_exported = map { $_ => 1 } (
    @Assert::Conditional::EXPORT,
    @Assert::Conditional::EXPORT_OK,
);

cmp_ok scalar keys %is_exported, ">", 50, "we exported at least 50 functions";

my @lock_assertions = qw(assert_locked assert_unlocked);
my $lock_tests = commify_and @lock_assertions;

if ($hu_version < 0.15) {
    diag "Omitting tests for $lock_tests because Hash::Util version is only v$hu_version but we need v0.15";

    for my $subname (@lock_assertions) { 
        is($is_exported{$subname}, undef, "$subname is not exported under $hu_version");
    }
}
else {
    diag "Including assert tests for $lock_tests because Hash::Util version is v$hu_version and we need only v0.15";

    for my $subname (@lock_assertions) { 
        is $is_exported{$subname}, 1, "$subname is exported under $hu_version";
    }

    push @good_tests, (
        q{assert_locked(%locked_hash)},
        q{assert_locked($locked_hashref)},
        q{assert_locked($hash_of_hashes{LOCKED})},
        q{assert_locked($hashref_of_hashes->{LOCKED})},
        q{assert_locked($$ref_of_hashref_of_hashes->{LOCKED})},

        q{assert_unlocked(%unlocked_hash)},
        q{assert_unlocked($unlocked_hashref)},
        q{assert_unlocked($$ref_of_hashref_of_hashes)},
        q{assert_unlocked($hash_of_hashes{UNLOCKED})},
        q{assert_unlocked($hashref_of_hashes->{UNLOCKED})},
        q{assert_unlocked($$ref_of_hashref_of_hashes->{UNLOCKED})},
    );

} 

my @bad_tests = (
    q{assert(1)},
    q{assert_ainta()},
    q{assert_alnum()},
    q{assert_alphabetic()},
    q{assert_anyref()},
    q{assert_argc_max()},
    q{assert_argc_min()},
    q{assert_argc_minmax()},
    q{assert_array_length()},
    q{assert_array_length_max()},
    q{assert_array_length_min()},
    q{assert_array_length_minmax()},
    q{assert_array_nonempty()},
    q{assert_arrayref()},
    q{assert_arrayref_nonempty()},
    q{assert_arrayref_nonempty([])},
    q{assert_ascii()},
    q{assert_ascii_ident()},
    q{assert_astral()},
    q{assert_blank()},
    q{assert_bmp()},
    q{assert_box_number()},
    q{assert_bytes()},
    q{assert_can()},
    q{assert_cant()},
    q{assert_coderef()},
    q{assert_defined()},
    q{assert_defined_value()},
    q{assert_defined_variable()},
    q{assert_digits()},
    q{assert_directory()},
    q{assert_does()},
    q{assert_doesnt()},
    q{assert_empty()},
    q{assert_eq()},
    q{assert_eq_letters()},
    q{assert_even_number()},
    q{assert_false()},
    q{assert_fractional()},
    q{assert_fractional($one)},
    q{assert_full_perl_ident()},
    q{assert_globref()},
    q{assert_happy_code()},
    q{assert_hash_keys()},
    q{assert_hash_nonempty()},
    q{assert_hashref()},
    q{assert_hashref_keys()},
    q{assert_hashref_nonempty()},
    q{assert_hex_number()},
    q{assert_in_list()},
    q{assert_in_numeric_range()},
    q{assert_integer()},
    q{assert_ioref()},
    q{assert_is()},
    q{assert_isa()},

t/asserts.t  view on Meta::CPAN

    q{assert_hash_keys(%empty, qw<HOME PATH USER>)},
    q{assert_hashref_keys(\%empty, qw<HOME PATH USER>)},

    q{assert_hash_keys_required(@junk, qw<yellow violet cyan>)},
    q{assert_hash_keys_required(%primary_color, @empty)},
    q{assert_hash_keys_required(%primary_color, qw<yellow violet cyan>)},
    q{assert_hash_keys_required(%primary_color, qw<pink purple>)},
    q{assert_hash_keys_allowed(%primary_color, qw<yellow violet cyan>)},
    q{assert_hash_keys_allowed(%primary_color, qw<pink purple>)},
    q{assert_hash_keys_allowed(%primary_color, qw<red>)},
    q{assert_hash_keys_allowed(%primary_color, @empty)},
    q{assert_hashref_keys_required(%primary_color, qw<yellow violet cyan>)},
    q{assert_hashref_keys_allowed(%primary_color, qw<yellow violet cyan>)},
    q{assert_hashref_keys_required(\%primary_color, qw<yellow violet cyan>)},
    q{assert_hashref_keys_allowed(\%primary_color, qw<yellow violet cyan>)},

    q{assert_hash_keys_required_and_allowed(%primary_color, [qw<red blue green>], ["red"])},
    q{assert_hash_keys_required_and_allowed(%primary_color, [qw<red blue green>], [])},
    q{assert_hash_keys_required_and_allowed(%primary_color, [], [])},
    q{assert_hashref_keys_required_and_allowed(\\%primary_color, [qw<red blue green>], ["red"])},
    q{assert_hashref_keys_required_and_allowed($primary_color_ref, [qw<red blue green>], ["red"])},
    q{assert_hash_keys_allowed_and_required(%primary_color, ["red"], [qw<red blue green>])},
    q{assert_hash_keys_allowed_and_required(%primary_color, [], [qw<red blue green>])},
    q{assert_hash_keys_allowed_and_required(%primary_color, [], [])},
    q{assert_hashref_keys_allowed_and_required(\\%primary_color,, ["red"] [qw<red blue green>])},
    q{assert_hashref_keys_allowed_and_required($primary_color_ref, ["red"], [qw<red blue green>])},

    q{assert_hashref_keys_required(\\%primary_color, qw<red pink green>)},
    q{assert_hashref_keys_allowed(\\%primary_color, qw<red pink green>)},
    q{assert_hashref_keys_required($primary_color_ref, qw<red pink green>)},
    q{assert_hashref_keys_allowed($primary_color_ref, qw<red pink green>)},

    q{assert_keys(%primary_color, qw<red pink green>)},
    q{assert_keys(%primary_color, @all_colors)},
    q{assert_keys(%primary_color, @not_colors)},
    q{assert_keys(%primary_color, @empty)},
    q{assert_keys($primary_color_ref, qw<red pink green>)},
    q{assert_keys($primary_color_ref, @all_colors)},
    q{assert_keys($primary_color_ref, @not_colors)},
    q{assert_keys($primary_color_ref, @empty)},

    q{assert_min_keys(%primary_color, qw<red pink green>)},
    q{assert_min_keys(%primary_color, @empty)},
    q{assert_min_keys(%primary_color, @all_colors)},
    q{assert_min_keys(%primary_color, @not_colors)},
    q{assert_min_keys($primary_color_ref, qw<red pink green>)},
    q{assert_min_keys($primary_color_ref, @all_colors)},
    q{assert_min_keys($primary_color_ref, @not_colors)},

    q{assert_max_keys(%primary_color, qw<orange yellow violet>)},
    q{assert_max_keys(%primary_color, @not_colors)},
    q{assert_max_keys(%primary_color, @empty)},
    q{assert_max_keys($primary_color_ref, qw<orange yellow violet>)},
    q{assert_max_keys($primary_color_ref, @not_colors)},

    q{assert_minmax_keys(%primary_color, @all_colors, @primary_color)},
    q{assert_minmax_keys($primary_color_ref, @all_colors, @primary_colors)},
    q{assert_minmax_keys(%primary_color, @empty, @empty)},
    q{assert_minmax_keys($primary_color_ref, @empty, @empty)},

    q{assert_unlocked(%locked_hash)},
    q{assert_unlocked($locked_hashref)},
    q{assert_unlocked($hash_of_hashes{LOCKED})},
    q{assert_unlocked($hashref_of_hashes->{LOCKED})},
    q{assert_unlocked($$ref_of_hashref_of_hashes->{LOCKED})},

    q{assert_locked(%unlocked_hash)},
    q{assert_locked($unlocked_hashref)},
    q{assert_locked($$ref_of_hashref_of_hashes)},
    q{assert_locked($hash_of_hashes{UNLOCKED})},
    q{assert_locked($hashref_of_hashes->{UNLOCKED})},
    q{assert_locked($$ref_of_hashref_of_hashes->{UNLOCKED})},

    q{assert_anyref( "string" )},
    q{assert_anyref( 0 )},
    q{assert_anyref( $0 )},
    q{assert_anyref( *0 )},
    q{assert_anyref( @ARGV )},
    q{assert_anyref( )},
    q{assert_anyref( %ENV )},
    q{assert_anyref( defined &lives  )},
    q{assert_anyref( time()  )},
    q{assert_anyref( localtime()  )},
    q{assert_anyref( *STDIN )},
    q{assert_anyref( @{ *ARGV{ARRAY} } )},
    q{assert_anyref( @{ *{$main::{ARGV}}{ARRAY} } )},

    q{assert_reftype( ARRAY   =>  \"string"                )},
    q{assert_reftype( ARRAY   =>  \0                       )},
    q{assert_reftype( ARRAY   =>  \$0                      )},
    q{assert_reftype( ARRAY   =>  *0{SCALAR}               )},
    q{assert_reftype( REFREF  =>  \\\$0                    )},
    q{assert_reftype( SCALAR  =>  \@ARGV                   )},
    q{assert_reftype( ARRAY   =>  \\\\\@ARGV                 )},
    q{assert_reftype( STRING  =>  [ ]                      )},
    q{assert_reftype( CODE    =>  \%ENV                    )},
    q{assert_reftype( CODE    =>  {  }                     )},
    q{assert_reftype( ARRAY    =>  sub  {    }              )},
    q{assert_reftype( CODE    =>  time()               )},
    q{assert_reftype( IO  =>  \time()                  )},
    q{assert_reftype( HASH    =>  \*ENV                    )},
    q{assert_reftype( "IO::Handle"      =>  *STDIN{IO}               )},
    q{assert_reftype( GLOB   =>  *ARGV{ARRAY}             )},
    q{assert_reftype( HASH   =>  *{$main::{ARGV}}{ARRAY}  )},

    q{assert_hashref(  \"string"    )},
    q{assert_hashref(  \0           )},
    q{assert_hashref(  \$0          )},
    q{assert_hashref(  *0{SCALAR}   )},
    q{assert_scalarref(     \\\$0        )},
    q{assert_arrayref(   \\\\\\@ARGV       )},
    q{assert_hashref(   \@ARGV       )},
    q{assert_hashref(   [ ]          )},
    q{assert_arrayref(    \%ENV        )},
    q{assert_arrayref(    { }          )},
    q{assert_hashref(    sub {    }   )},
    q{assert_hashref(    \&lives   )},
    q{assert_hashref(  \time()      )},
    q{assert_refref(    \*ENV        )},
    q{assert_scalarref(      *STDIN{IO}   )},
    q{assert_globref(   *ARGV{ARRAY}             )},
    q{assert_hashref(   *{$main::{ARGV}}{ARRAY}  )},

    q{assert_unblessed_ref(      *STDIN{IO}   )},
    q{assert_unblessed_ref(      *Tied_FH   )},
    q{assert_unblessed_ref(      $bignum   )},
    q{assert_unblessed_ref(      $tied_object   )},

    q{assert_known_package("IO::Fil")},
    q{assert_known_package(IO::Fil::)},
    q{assert_object($class)},
    q{assert_object([])},



( run in 0.703 second using v1.01-cache-2.11-cpan-39bf76dae61 )