File: init_obj.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 (72 lines) | stat: -rw-r--r-- 2,616 bytes parent folder | download | duplicates (4)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use strict;
use warnings;
use Test::More;

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

    has '+use_init_obj_when_no_accessor_in_item' => ( default => 1 );
    has_field 'foo';
    has_field 'bar';
    has_field 'max';
    has_field 'my_comp' => ( type => 'Compound' );
    has_field 'my_comp.one';
    has_field 'my_comp.two';
}

my $form = Test::Form->new;
my $item = { foo => 'item_foo', bar => 'item_bar' };
my $init_obj = { max => 'init_obj_max' };

$form->process( item => $item, init_object => $init_obj, params => {} );
is( $form->field('foo')->fif, 'item_foo' );
is( $form->field('bar')->fif, 'item_bar' );
is( $form->field('max')->fif, 'init_obj_max', 'init_obj value pulled in' );

$init_obj = { my_comp => { one => 'init_obj_one', two => 'init_obj_two' } };
$form->process( item => $item, init_object => $init_obj, params => {} );
is( $form->field('foo')->fif, 'item_foo' );
is( $form->field('bar')->fif, 'item_bar' );
is( $form->field('max')->fif, '' );
is( $form->field('my_comp.one')->fif, 'init_obj_one', 'init_obj value pulled in for compound' );

$init_obj = { foo => 'init_obj_foo', bar => 'init_obj_bar', max => 'init_obj_max',
    my_comp => { one => 'init_obj_one', two => 'init_obj_two' } };

$item = undef;
$form->process( item => $item, init_object => $init_obj, params => {} );
is( $form->field('foo')->fif, 'init_obj_foo' );
is( $form->field('bar')->fif, 'init_obj_bar' );
is( $form->field('max')->fif, 'init_obj_max' );
is( $form->field('my_comp.one')->fif, 'init_obj_one', 'init_obj value pulled in for compound' );

$item = { foo => 'item_foo', bar => 'item_bar' };
$form->process( item => $item, init_object => $init_obj, params => {} );
is( $form->field('foo')->fif, 'item_foo' );
is( $form->field('bar')->fif, 'item_bar' );
is( $form->field('max')->fif, 'init_obj_max' );
is( $form->field('my_comp.one')->fif, 'init_obj_one', 'init_obj value pulled in for compound' );

# test form reuse
$form->process( params => {} );
is( $form->field('foo')->fif, '' );
is( $form->field('bar')->fif, '' );
is( $form->field('max')->fif, '' );
is( $form->field('my_comp.one')->fif, '', "all fields empty on form reuse with init_object" );

# test params_to_values method
my $params = {
   foo => 'one',
   bar => 'two',
   max => 'three',
   'my_comp.one' => 'xxx',
   'my_comp.two' => 'yyy',
};
$init_obj = $form->params_to_values($params);
$form->process( init_object => $init_obj, params => {} );
is_deeply( $form->fif, $params, 'fif is the same as params' );
is_deeply( $form->value, $init_obj, 'value is the same as init_obj');

done_testing;