File: compound.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 (69 lines) | stat: -rw-r--r-- 1,829 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use strict;
use warnings;
use Test::More;


my $struct = {
   username => 'Joe Blow',
   occupation => 'Programmer',
   tags => ['Perl', 'programming', 'Moose' ],
   employer => {
      name => 'TechTronix',
      country => 'Utopia',
   },
};


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

   has_field 'username';
   has_field 'occupation';
   has_field 'tags' => ( type => 'Repeatable' );
   has_field 'tags.contains' => ( type => 'Text' );
   has_field 'employer' => ( type => 'Compound' );
   has_field 'employer.name';
   has_field 'employer.country';

}

my $form = Structured::Form->new;
ok( $form, 'form created' );
$form->process( params => $struct );
ok( $form->validated, 'form validated');

is_deeply( $form->field('tags')->value, ['Perl', 'programming', 'Moose' ],
   'list field tags has right values' );
my $result = $form->result;
is( $result->num_results, 4, 'right number of results');

my $first_tag = $result->field('tags.0');
is( ref $first_tag, 'HTML::FormHandler::Field::Result', 'get result object');
is( $first_tag->value, 'Perl', 'result has right value' );
is( $first_tag->parent, $result->field('tags'), 'correct parent for ');

my $employer = $result->field('employer');
my $employer_name = $result->field('employer.name');
is( $employer_name->parent, $employer, 'correct parent for compound field');

my $fif = {
   'employer.country' => 'Utopia',
   'employer.name' => 'TechTronix',
   'occupation' => 'Programmer',
   'tags.0' => 'Perl',
   'tags.1' => 'programming',
   'tags.2' => 'Moose',
   'username' => 'Joe Blow'
};

is_deeply( $form->fif, $fif, 'fif is correct' );

$result = $form->run( $fif );

ok( $result->validated, 'form processed from fif' );

is_deeply( $result->value, $struct, 'values round-tripped from fif');

done_testing;