Skip to content

Instantly share code, notes, and snippets.

@aereal
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/e3bd73d5018784fefd64 to your computer and use it in GitHub Desktop.
Save aereal/e3bd73d5018784fefd64 to your computer and use it in GitHub Desktop.
package t::constraints;
use strict;
use warnings;
use FormValidator::Lite::Constraint;
use Test::More;
rule VALUES => sub {
diag explain +{
'$_' => +{
ref => ref($_) // '(not ref)',
object => $_,
},
};
1;
};
package main;
use strict;
use warnings;
use utf8;
use Encode ();
use FormValidator::Lite;
use HTTP::Message::PSGI;
use HTTP::Request::Common;
use Plack::Request;
use Test::More;
FormValidator::Lite->load_constraints(qw( +t::constraints ));
sub create_request {
my ($method, @rest) = @_;
my $req = HTTP::Request::Common->can(uc($method // ''))->(@rest);
my $env = HTTP::Message::PSGI::req_to_psgi($req);
return Plack::Request->new($env);
}
sub create_validator {
my (@content) = @_;
my $req = create_request(
POST => '/',
Content => [ map { Encode::encode_utf8($_) } @content ],
);
return FormValidator::Lite->new($req);
}
subtest VALUES => sub {
subtest 'scalar' => sub {
my $validator = create_validator(
v => '1',
);
my $result = $validator->check(
v => [qw( VALUES )],
);
ok $result->is_valid;
};
subtest 'multiple' => sub {
my $validator = create_validator(
v => ['1'],
);
my $result = $validator->check(
v => [qw( VALUES )],
);
ok $result->is_valid;
};
};
done_testing;
[12:11:16] t/validator.t ..
# {
# '$_' => {
# 'object' => '1',
# 'ref' => ''
# }
# }
ok 1
1..1
ok 1 - scalar
# {
# '$_' => {
# 'object' => 'ARRAY(0x7fe465062890)',
# 'ref' => ''
# }
# }
ok 1
1..1
ok 2 - multiple
1..2
ok 1 - VALUES
1..1
ok 82 ms
[12:11:16]
All tests successful.
Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.07 cusr 0.01 csys = 0.11 CPU)
Result: PASS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment