Debian Codegraph @

t/repeatable/defaults.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 # tests that a default method works for a repeatable
6
7 {
8 package MyApp::Form::Rep;
9 use HTML::FormHandler::Moose;
10 extends 'HTML::FormHandler';
11
12 has '+name' => ( default => 'testform' );
13 has_field 'foo';
14 has_field 'my_array' => ( type => 'Repeatable' );
15 has_field 'my_array.one';
16 has_field 'my_array.two' => ( type => 'Select' );
17 has_field 'my_array.three';
18
19 sub default_my_array {
20 my $self = shift;
21 return (
22 { one => 'abc1', two => 2, three => 'abc3' },
23 { one => 'def1', two => 3, three => 'def3' },
24 { one => 'ghi1', two => 1, three => 'ghi3' }
25 );
26 }
27 sub options_my_array_two {
28 return (
29 1 => 'one',
30 2 => 'two',
31 3 => 'three',
32 );
33 }
34 # this won't take effect; the 'my_array' default has precedence
35 # would be used for 'num_extra'
36 sub default_my_array_three { 'default_three' }
37 }
38
39 my $form = MyApp::Form::Rep->new;
40 ok( $form );
41 $form->process;
42 is( $form->field('my_array')->num_fields, 3, 'right number of fields' );
43 my $fif = $form->fif;
44 my $exp_fif = {
45 'foo' => '',
46 'my_array.0.one' => 'abc1',
47 'my_array.0.three' => 'abc3',
48 'my_array.0.two' => 2,
49 'my_array.1.one' => 'def1',
50 'my_array.1.three' => 'def3',
51 'my_array.1.two' => 3,
52 'my_array.2.one' => 'ghi1',
53 'my_array.2.three' => 'ghi3',
54 'my_array.2.two' => 1,
55 };
56 is_deeply( $fif, $exp_fif, 'fif is correct' );
57 $fif->{foo} = 'foo_submitted';
58 $form->process( params => $fif );
59 my $values = $form->values;
60 my $exp_values = {
61 foo => 'foo_submitted',
62 my_array => [
63 { one => 'abc1', two => 2, three => 'abc3' },
64 { one => 'def1', two => 3, three => 'def3' },
65 { one => 'ghi1', two => 1, three => 'ghi3' }
66 ]
67 };
68 is_deeply( $values, $exp_values, 'got expected values' );
69
70
71 done_testing;