File: 04basic.t

package info (click to toggle)
libhtml-formfu-perl 0.09007-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,184 kB
  • sloc: perl: 13,186; makefile: 9; sql: 5
file content (111 lines) | stat: -rw-r--r-- 2,592 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use strict;
use warnings;
use YAML::XS qw( LoadFile );

use Test::More tests => 7;

use HTML::FormFu;

my $form = HTML::FormFu->new(
    { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

$form->action('/foo/bar')->id('form')->auto_id('%n');

my $fs = $form->element('Fieldset')->legend('Jimi');

$fs->element('Text')->name('age')->label('Age')->comment('x')
    ->constraints( [ 'Integer', 'Required', ] );

$fs->element('Text')->name('name')->label('Name');
$fs->element('Hidden')->name('ok')->value('OK');

$form->constraints( {
        type => 'Required',
        name => 'name',
    } );

$form->filter('HTMLEscape');

# hash-ref

my $alt_hash = {
    action   => '/foo/bar',
    id       => 'form',
    auto_id  => '%n',
    elements => [ {
            type     => 'Fieldset',
            legend   => 'Jimi',
            elements => [ {
                    type        => 'Text',
                    name        => 'age',
                    label       => 'Age',
                    comment     => 'x',
                    constraints => [ 'Integer', 'Required', ],
                },
                { type => 'Text',   name => 'name', label => 'Name', },
                { type => 'Hidden', name => 'ok',   value => 'OK', },
            ],
        }
    ],
    constraints => {
        type => 'Required',
        name => 'name',
    },
    filters => ['HTMLEscape'],
};

# hash-ref from yaml

my $yml_hash = LoadFile('t/04basic.yml');

# compare hash-refs

is_deeply( $yml_hash, $alt_hash );

# xhtml output

my $alt_form = HTML::FormFu->new($alt_hash);
$alt_form->tt_args( { INCLUDE_PATH => 'share/templates/tt/xhtml' } );

my $yml_form = HTML::FormFu->new(
    { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );
$yml_form->load_config_file('t/04basic.yml');

my $xhtml = <<EOF;
<form action="/foo/bar" id="form" method="post">
<fieldset>
<legend>Jimi</legend>
<div class="text comment label">
<label for="age">Age</label>
<input name="age" type="text" id="age" />
<span class="comment">
x
</span>
</div>
<div class="text label">
<label for="name">Name</label>
<input name="name" type="text" id="name" />
</div>
<input name="ok" type="hidden" value="OK" id="ok" />
</fieldset>
</form>
EOF

is( "$form",     $xhtml );
is( "$alt_form", $xhtml );
is( "$yml_form", $xhtml );

# With mocked basic query
{
    $form->process( {
            age  => 'a',
            name => 'sri',
        } );

    ok( $form->valid('name'), 'name is valid' );
    ok( !$form->valid('age'), 'age is not valid' );

    my $errors = $form->get_errors;

    is( @$errors, 1 );
}