File: infl_transform.t

package info (click to toggle)
libhtml-formhandler-perl 0.40057-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,320 kB
  • ctags: 685
  • sloc: perl: 8,849; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use strict;
use warnings;
use Test::More;
use Test::Exception;

# check that transform acts correctly as inflation, and
# sub deflation works ok.

{
    package Test::Deflate2;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler';

    has_field 'bullets' => ( type => 'Text',
        apply => [ { transform => \&string_to_array } ],
        deflation => \&array_to_string,
    );
    sub array_to_string {
       my ( $value ) = @_;
       my $string = '';
       my $sep = '';
       for ( @$value ) {
           $string .= $sep . $_->{text};
           $sep = ';';
       }
       return $string;
    }
    sub string_to_array {
        my $value = shift;
        return [ map { { text => $_ } } split(/\s*;\s*/, $value) ];
    }
}

my $init_object = { bullets => [{ text => 'one'}, { text => 'two' }, { text => 'three'}] };
my $fif = { bullets => 'one;two;three' };
my $form = Test::Deflate2->new;
ok( $form, 'form built');
$form->process( init_object => $init_object, params => {} );
is_deeply( $form->fif, $fif, 'right fif' );
is_deeply( $form->value, $init_object, 'right value' );

$form->process( params => $fif );
is_deeply( $form->fif, $fif, 'right fif' );
is_deeply( $form->value, $init_object, 'right value' );

done_testing;