Debian Codegraph @

t/structured.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5
6 my $struct = {
7 username => 'Joe Blow',
8 occupation => 'Programmer',
9 tags => ['Perl', 'programming', 'Moose' ],
10 employer => {
11 name => 'TechTronix',
12 country => 'Utopia',
13 },
14 options => {
15 flags => {
16 opt_in => 1,
17 email => 0,
18 },
19 cc_cards => [
20 {
21 type => 'Visa',
22 number => '4248999900001010',
23 },
24 {
25 type => 'MasterCard',
26 number => '4335992034971010',
27 },
28 ],
29 },
30 addresses => [
31 {
32 street => 'First Street',
33 city => 'Prime City',
34 country => 'Utopia',
35 id => 0,
36 },
37 {
38 street => 'Second Street',
39 city => 'Secondary City',
40 country => 'Graustark',
41 id => 1,
42 },
43 {
44 street => 'Third Street',
45 city => 'Tertiary City',
46 country => 'Atlantis',
47 id => 2,
48 }
49 ]
50 };
51
52
53 {
54 package Structured::Form;
55 use HTML::FormHandler::Moose;
56 extends 'HTML::FormHandler';
57
58 has_field 'username';
59 has_field 'occupation';
60 has_field 'tags' => ( type => 'Repeatable' );
61 has_field 'tags.contains' => ( type => 'Text' );
62 has_field 'employer' => ( type => 'Compound' );
63 has_field 'employer.name';
64 has_field 'employer.country';
65 has_field 'options' => ( type => 'Compound' );
66 has_field 'options.flags' => ( type => 'Compound' );
67 has_field 'options.flags.opt_in' => ( type => 'Boolean' );
68 has_field 'options.flags.email' => ( type => 'Boolean' );
69 has_field 'options.cc_cards' => ( type => 'Repeatable' );
70 has_field 'options.cc_cards.type';
71 has_field 'options.cc_cards.number';
72 has_field 'addresses' => ( type => 'Repeatable' );
73 has_field 'addresses.street';
74 has_field 'addresses.city';
75 has_field 'addresses.country';
76 has_field 'addresses.id';
77
78 }
79
80 #===========
81 # test structured params
82 my $form = Structured::Form->new;
83 ok( $form, 'form created' );
84 $form->process( params => $struct );
85 is( $form->result->num_results, 6, 'correct number of results' );
86 ok( $form->validated, 'form validated');
87 is_deeply( $form->field('tags')->value, ['Perl', 'programming', 'Moose' ],
88 'list field tags has right values' );
89 is( $form->field('addresses.0.city')->value, 'Prime City', 'get address field OK' );
90 is( $form->field('options.flags.opt_in')->value, 1, 'get opt_in flag');
91 #============
92 # test structured init_object/item
93 my $form2 = Structured::Form->new;
94 ok( $form2, 'form created' );
95 $form2->process( init_object => $struct, params => {} );
96 is( $form2->result->num_results, 6, 'correct number of results' );
97 ok( !$form2->validated, 'form validated');
98 is_deeply( $form2->field('employer')->item, { name => 'TechTronix', country => 'Utopia', }, 'has item');
99 is_deeply( $form2->field('addresses')->item, $struct->{addresses}, 'item for repeatable' );
100 #=============
101
102 my $fif = {
103 'addresses.0.city' => 'Prime City',
104 'addresses.0.country' => 'Utopia',
105 'addresses.0.id' => 0,
106 'addresses.0.street' => 'First Street',
107 'addresses.1.city' => 'Secondary City',
108 'addresses.1.country' => 'Graustark',
109 'addresses.1.id' => 1,
110 'addresses.1.street' => 'Second Street',
111 'addresses.2.city' => 'Tertiary City',
112 'addresses.2.country' => 'Atlantis',
113 'addresses.2.id' => 2,
114 'addresses.2.street' => 'Third Street',
115 'employer.country' => 'Utopia',
116 'employer.name' => 'TechTronix',
117 'occupation' => 'Programmer',
118 'options.cc_cards.0.number' => '4248999900001010',
119 'options.cc_cards.0.type' => 'Visa',
120 'options.cc_cards.1.number' => '4335992034971010',
121 'options.cc_cards.1.type' => 'MasterCard',
122 'options.flags.email' => 0,
123 'options.flags.opt_in' => 1,
124 'tags.0' => 'Perl',
125 'tags.1' => 'programming',
126 'tags.2' => 'Moose',
127 'username' => 'Joe Blow'
128 };
129
130 #=========
131 is_deeply( $form->fif, $fif, 'fif is correct' );
132 $form->process( $fif );
133 ok( $form->validated, 'form processed from fif' );
134 is_deeply( $form->values, $struct, 'values round-tripped from fif');
135
136 #=========
137 # works with item and params
138 $form2->process( item => $struct, params => $fif );
139 ok( $form2->validated, 'form processed from fif' );
140 is( $form2->result->num_results, 6, 'correct number of results' );
141 is_deeply( $form2->field('employer')->item, { name => 'TechTronix', country => 'Utopia', }, 'has item');
142 is_deeply( $form2->field('addresses')->item, $struct->{addresses}, 'item for repeatable' );
143
144 done_testing;