Data-TreeValidator

 view release on metacpan or  search on metacpan

t/Tests/Data/TreeValidator/Constraints.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Test::Routine;
use Test::Routine::Util;
use MooseX::Types::Moose qw/Num HashRef/;

use Data::TreeValidator::Constraints qw( required length options type );

test '"required" constraint' => sub {
    my $constraint = required;
    ok(!exception { $constraint->('Some input') },
        'non-empty string passes required constraint');

    ok(exception { $constraint->(undef) },
        'constraint does not allow undef input');

    ok(exception { $constraint->('') },
        'constraint does not allow empty string input');

    ok(!exception { $constraint->('0') },
        'constraint does allow string "0"');
};

test '"length" constraint' => sub {
  my $constraint = length(min => 4, max => 7);

  ok(exception { $constraint->(undef) },
     'constraint does not allow undef input');
  
  ok(exception { $constraint->('') },
     'constraint does not allow empty string input');

  ok(exception { $constraint->('abc') },
     '3-char string is not between 4 and 7 chars');

  ok(exception { $constraint->('abcdefghi') },
     '9-char string is not between 4 and 7 chars');

  ok(!exception { $constraint->('abcde') },
     '5-char string is between 4 and 7 chars');
};
  

test 'options constraint' => sub {
  my $constraint = options(qw(a b c d));

  ok(exception { $constraint->(undef) },
     'constraint does not allow undef input');
  
  ok(exception { $constraint->('') },
     'constraint does not allow empty string input');

  ok(exception { $constraint->('moose') },
     'moose is not a valid option');

  ok(exception { $constraint->(5) },
     '5 is not a valid option');

  ok(!exception { $constraint->('a') },
     'a is a valid option');

  ok(exception { $constraint->('ab') },
     'ab is not a valid option');
};

test 'type constraint (Num)' => sub {
  my $constraint = type(Num);

  ok(exception { $constraint->(undef) },
     'constraint does not allow undef input');



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