| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use lib 't/lib'; |
| 6 |
|
| 7 |
use DateTime; |
| 8 |
use Scalar::Util qw(blessed); |
| 9 |
|
| 10 |
use HTML::FormHandler::I18N; |
| 11 |
$ENV{LANGUAGE_HANDLE} = 'en_en'; |
| 12 |
|
| 13 |
{ |
| 14 |
package My::Form; |
| 15 |
use HTML::FormHandler::Moose; |
| 16 |
extends 'HTML::FormHandler'; |
| 17 |
|
| 18 |
use Moose::Util::TypeConstraints; |
| 19 |
|
| 20 |
subtype 'MyStr' |
| 21 |
=> as 'Str' |
| 22 |
=> where { /^a/ }; |
| 23 |
|
| 24 |
subtype 'MyInt' |
| 25 |
=> as 'Int'; |
| 26 |
|
| 27 |
coerce 'MyInt' |
| 28 |
=> from 'MyStr' |
| 29 |
=> via { return $1 if /(\d+)/ }; |
| 30 |
|
| 31 |
type 'MyDateTime' |
| 32 |
=> message { 'This is not a correct date' } |
| 33 |
=> where { blessed $_[0] && $_[0]->isa( 'DateTime' ) }; |
| 34 |
coerce 'MyDateTime' |
| 35 |
=> from 'HashRef' |
| 36 |
=> via { DateTime->new( $_ ) }; |
| 37 |
|
| 38 |
has_field 'coderef_transform'=> ( |
| 39 |
apply => [{ transform => \&my_transform }] |
| 40 |
); |
| 41 |
has_field 'sprintf_filter' => ( |
| 42 |
apply => [ { transform => sub{ sprintf '<%.1g>', $_[0] } } ] |
| 43 |
); |
| 44 |
has_field 'regex_trim' => ( |
| 45 |
trim => { transform => sub { |
| 46 |
my $string = shift; |
| 47 |
$string =~ s/^\s+//; |
| 48 |
$string =~ s/\s+$//; |
| 49 |
return $string; |
| 50 |
}} |
| 51 |
); |
| 52 |
has_field 'date_time_error' => ( |
| 53 |
apply => [ { transform => sub{ DateTime->new( $_[0] ) }, |
| 54 |
message => 'Not a valid DateTime' } ], |
| 55 |
); |
| 56 |
has_field 'date_time' => ( |
| 57 |
type => 'Compound', |
| 58 |
apply => [ { transform => sub{ DateTime->new( $_[0] ) } } ], |
| 59 |
); |
| 60 |
has_field 'date_time.year' => ( type => 'Text', ); |
| 61 |
has_field 'date_time.month' => ( type => 'Text', ); |
| 62 |
has_field 'date_time.day' => ( type => 'Text', ); |
| 63 |
|
| 64 |
has_field 'coerce_error' => ( |
| 65 |
apply => [ 'MyInt' ] |
| 66 |
); |
| 67 |
has_field 'coerce_pass' => ( |
| 68 |
apply => [ 'MyInt' ] |
| 69 |
); |
| 70 |
|
| 71 |
has_field 'date_coercion_pass' => ( |
| 72 |
type => 'Compound', |
| 73 |
apply => [ 'MyDateTime' ], |
| 74 |
); |
| 75 |
has_field 'date_coercion_pass.year' => ( type => 'Text', ); |
| 76 |
has_field 'date_coercion_pass.month' => ( type => 'Text', ); |
| 77 |
has_field 'date_coercion_pass.day' => ( type => 'Text', ); |
| 78 |
|
| 79 |
has_field 'date_coercion_error' => ( |
| 80 |
type => 'Compound', |
| 81 |
apply => [ 'MyDateTime' ], |
| 82 |
); |
| 83 |
has_field 'date_coercion_error.year' => ( type => 'Text', ); |
| 84 |
has_field 'date_coercion_error.month' => ( type => 'Text', ); |
| 85 |
has_field 'date_coercion_error.day' => ( type => 'Text', ); |
| 86 |
|
| 87 |
has_field 'date_time_fif' => ( |
| 88 |
type => 'Compound', |
| 89 |
apply => [ { transform => sub{ DateTime->new( $_[0] ) } } ], |
| 90 |
inflation => sub { { year => 1000, month => 1, day => 1 } }, |
| 91 |
fif_from_value => 1, |
| 92 |
); |
| 93 |
has_field 'date_time_fif.year' => ( fif_from_value => 1 ); |
| 94 |
has_field 'date_time_fif.month'; |
| 95 |
has_field 'date_time_fif.day' => ( fif_from_value => 1 ); |
| 96 |
|
| 97 |
sub my_transform { |
| 98 |
$_[0] =~ s/testing/IT WORKED/g; |
| 99 |
return $_[0]; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
my $form = My::Form->new(); |
| 105 |
ok( $form, 'get form' ); |
| 106 |
|
| 107 |
my $params = { |
| 108 |
sprintf_filter => '100', |
| 109 |
regex_trim => " xxxy \n", |
| 110 |
coderef_transform => 'testing', |
| 111 |
date_time_error => 'aaa', |
| 112 |
'date_time.year' => 2009, |
| 113 |
'date_time.month' => 4, |
| 114 |
'date_time.day' => 16, |
| 115 |
coerce_error => 'b10', |
| 116 |
coerce_pass => 'a10', |
| 117 |
'date_coercion_pass.year' => 2009, |
| 118 |
'date_coercion_pass.month' => 4, |
| 119 |
'date_coercion_pass.day' => 16, |
| 120 |
'date_coercion_error.year' => 2009, |
| 121 |
'date_coercion_error.month' => 20, |
| 122 |
'date_coercion_error.day' => 16, |
| 123 |
'date_time_fif.year' => 2009, |
| 124 |
'date_time_fif.month' => 4, |
| 125 |
'date_time_fif.day' => 16, |
| 126 |
}; |
| 127 |
$form->process($params); |
| 128 |
|
| 129 |
like( $form->field('sprintf_filter')->value, qr/<1e\+0+2>/, 'sprintf filter' ); |
| 130 |
is( $form->field('regex_trim')->value, 'xxxy', 'regex trim' ); |
| 131 |
is( $form->field('coderef_transform')->value, 'IT WORKED', 'coderef transform' ); |
| 132 |
ok( $form->field('date_time_error')->has_errors, 'DateTime error catched' ); |
| 133 |
is( $form->field('date_time_error')->errors->[0], 'Not a valid DateTime', 'error message'); |
| 134 |
is( ref $form->field('date_time')->value, 'DateTime', 'DateTime object created' ); |
| 135 |
ok( $form->field('coerce_error')->has_errors, 'no suitable coercion - error' ); |
| 136 |
is( $form->field('coerce_pass')->value, 10, 'coercion filter' ); |
| 137 |
is( ref $form->field('date_coercion_pass')->value, 'DateTime', 'values coerced to DateTime object' ); |
| 138 |
ok( $form->field('date_coercion_error')->has_errors, 'DateTime coercion error' ); |
| 139 |
my $message = $form->field('date_coercion_error')->errors->[0]; |
| 140 |
is( $message, 'This is not a correct date', 'Error message for coercion' ); |
| 141 |
|
| 142 |
is( $form->field( 'date_time_fif.year' )->fif, 2009, 'fif for year' ); |
| 143 |
$params->{'date_time_fif.year'} = 2009; |
| 144 |
$params->{'date_time_fif.day'} = 16; |
| 145 |
is_deeply( $form->fif, $params, 'fif is correct' ); |
| 146 |
is( $form->value->{date_time_fif}->year, 2009, 'right value' ); |
| 147 |
|
| 148 |
done_testing; |