File: element_defaults.t

package info (click to toggle)
libhtml-formfu-perl 0.07002-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,928 kB
  • ctags: 989
  • sloc: perl: 12,036; makefile: 9; sql: 5
file content (62 lines) | stat: -rw-r--r-- 1,764 bytes parent folder | download
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
use strict;
use warnings;

use Test::More tests => 9;

use HTML::FormFu;
use Clone ();

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

_call_element_defaults( $form, {
        Password => { render_value => 1, },
        Block    => { attributes   => { class => 'block' }, },
        Text     => {
                     attributes => { class => 'custom' },
                     constraint => [
                        {
                            type  => 'Regex',
                            regex => qr/\w/,
                        }
                    ],
        },
    } );

# take a deep copy of element_defaults, so we can check they've not been butchered, later
my $element_defaults = Clone::clone( _call_element_defaults( $form ) );

$form->populate( {
        elements => [
            { type => 'Password', name => 'foo' },
            { type => 'Text',     name => 'bar' },
            {   type     => 'Block',
                elements => [ { type => 'Text', name => 'baz' }, ],
            },
        ],
    } );

is( $form->get_field('foo')->render_value, 1 );

like( $form->get_field('bar'), qr/name="bar" [^>]* class="custom"/x );

is( $form->get_field('bar')->get_constraint->type, 'Regex' );

like( $form->get_element( { type => 'Block' } ), qr/div [^>]* class="block"/x );

like( $form->get_element( { type => 'Block' } )->get_field('baz'),
    qr/name="baz" .* class="custom"/x );

is_deeply( $element_defaults, _call_element_defaults( $form ) );

sub _call_element_defaults {
    my $form = shift;

    my $warnings;
    local $SIG{ __WARN__ } = sub { $warnings++ };

    my $result = $form->element_defaults( @_ );

    ok( $warnings, 'a deprecation warning was thrown' );

    return $result;
}