Debian Codegraph @

t/infl_defl/infl_transform.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5
6 # check that transform acts correctly as inflation, and
7 # sub deflation works ok.
8
9 {
10 package Test::Deflate2;
11 use HTML::FormHandler::Moose;
12 extends 'HTML::FormHandler';
13
14 has_field 'bullets' => ( type => 'Text',
15 apply => [ { transform => \&string_to_array } ],
16 deflation => \&array_to_string,
17 );
18 sub array_to_string {
19 my ( $value ) = @_;
20 my $string = '';
21 my $sep = '';
22 for ( @$value ) {
23 $string .= $sep . $_->{text};
24 $sep = ';';
25 }
26 return $string;
27 }
28 sub string_to_array {
29 my $value = shift;
30 return [ map { { text => $_ } } split(/\s*;\s*/, $value) ];
31 }
32 }
33
34 my $init_object = { bullets => [{ text => 'one'}, { text => 'two' }, { text => 'three'}] };
35 my $fif = { bullets => 'one;two;three' };
36 my $form = Test::Deflate2->new;
37 ok( $form, 'form built');
38 $form->process( init_object => $init_object, params => {} );
39 is_deeply( $form->fif, $fif, 'right fif' );
40 is_deeply( $form->value, $init_object, 'right value' );
41
42 $form->process( params => $fif );
43 is_deeply( $form->fif, $fif, 'right fif' );
44 is_deeply( $form->value, $init_object, 'right value' );
45
46 done_testing;