File: default.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 (57 lines) | stat: -rw-r--r-- 1,644 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
use strict;
use warnings;
use Test::More;

# tests that a 'default' hashref on a compound field works
{
    {
        package MyApp::Test::Compound;
        use HTML::FormHandler::Moose;
        extends 'HTML::FormHandler';

        has_field 'comp_foo' => ( type => 'Compound', default => { one => 1, two => 2, three => 3 } );
        has_field 'comp_foo.one';
        has_field 'comp_foo.two';
        has_field 'comp_foo.three';
    }

    my $form = MyApp::Test::Compound->new;
    ok( $form );
    $form->process;
    is( $form->field('comp_foo.one')->value, 1, 'value is one' );
    is_deeply( $form->field('comp_foo')->value, { one => 1, two => 2, three => 3 },
       'value for compound is correct' );
}


# tests that default object for a compound field works
# object provided by default_method
{
    {
        package MyApp::Foo;
        use Moose;
        has 'one' => ( is => 'ro', default => 1 );
        has 'two' => ( is => 'ro', default => 2 );
        has 'three' => ( is => 'ro', default => 3 );
    }
    {
        package MyApp::Test::Compound2;
        use HTML::FormHandler::Moose;
        extends 'HTML::FormHandler';

        has_field 'comp_foo' => ( type => 'Compound', default_method => \&default_comp_foo );
        has_field 'comp_foo.one';
        has_field 'comp_foo.two';
        has_field 'comp_foo.three';
        sub default_comp_foo {
            return MyApp::Foo->new;
        }
    }

    my $form = MyApp::Test::Compound2->new;
    ok( $form );
    is( $form->field('comp_foo.one')->value, 1, 'value is one' );
    is( ref $form->field('comp_foo')->item, 'MyApp::Foo', 'item saved' );
}

done_testing;