Debian Codegraph @

t/form_setup/init_obj.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 {
6 package Test::Form;
7 use HTML::FormHandler::Moose;
8 extends 'HTML::FormHandler';
9
10 has '+use_init_obj_when_no_accessor_in_item' => ( default => 1 );
11 has_field 'foo';
12 has_field 'bar';
13 has_field 'max';
14 has_field 'my_comp' => ( type => 'Compound' );
15 has_field 'my_comp.one';
16 has_field 'my_comp.two';
17 }
18
19 my $form = Test::Form->new;
20 my $item = { foo => 'item_foo', bar => 'item_bar' };
21 my $init_obj = { max => 'init_obj_max' };
22
23 $form->process( item => $item, init_object => $init_obj, params => {} );
24 is( $form->field('foo')->fif, 'item_foo' );
25 is( $form->field('bar')->fif, 'item_bar' );
26 is( $form->field('max')->fif, 'init_obj_max', 'init_obj value pulled in' );
27
28 $init_obj = { my_comp => { one => 'init_obj_one', two => 'init_obj_two' } };
29 $form->process( item => $item, init_object => $init_obj, params => {} );
30 is( $form->field('foo')->fif, 'item_foo' );
31 is( $form->field('bar')->fif, 'item_bar' );
32 is( $form->field('max')->fif, '' );
33 is( $form->field('my_comp.one')->fif, 'init_obj_one', 'init_obj value pulled in for compound' );
34
35 $init_obj = { foo => 'init_obj_foo', bar => 'init_obj_bar', max => 'init_obj_max',
36 my_comp => { one => 'init_obj_one', two => 'init_obj_two' } };
37
38 $item = undef;
39 $form->process( item => $item, init_object => $init_obj, params => {} );
40 is( $form->field('foo')->fif, 'init_obj_foo' );
41 is( $form->field('bar')->fif, 'init_obj_bar' );
42 is( $form->field('max')->fif, 'init_obj_max' );
43 is( $form->field('my_comp.one')->fif, 'init_obj_one', 'init_obj value pulled in for compound' );
44
45 $item = { foo => 'item_foo', bar => 'item_bar' };
46 $form->process( item => $item, init_object => $init_obj, params => {} );
47 is( $form->field('foo')->fif, 'item_foo' );
48 is( $form->field('bar')->fif, 'item_bar' );
49 is( $form->field('max')->fif, 'init_obj_max' );
50 is( $form->field('my_comp.one')->fif, 'init_obj_one', 'init_obj value pulled in for compound' );
51
52 # test form reuse
53 $form->process( params => {} );
54 is( $form->field('foo')->fif, '' );
55 is( $form->field('bar')->fif, '' );
56 is( $form->field('max')->fif, '' );
57 is( $form->field('my_comp.one')->fif, '', "all fields empty on form reuse with init_object" );
58
59 # test params_to_values method
60 my $params = {
61 foo => 'one',
62 bar => 'two',
63 max => 'three',
64 'my_comp.one' => 'xxx',
65 'my_comp.two' => 'yyy',
66 };
67 $init_obj = $form->params_to_values($params);
68 $form->process( init_object => $init_obj, params => {} );
69 is_deeply( $form->fif, $params, 'fif is the same as params' );
70 is_deeply( $form->value, $init_obj, 'value is the same as init_obj');
71
72 done_testing;