File: build_id.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 (82 lines) | stat: -rw-r--r-- 2,314 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
70
71
72
73
74
75
76
77
78
79
80
81
82
use strict;
use warnings;
use Test::More;

use HTML::FormHandler::Field;

# This is an example of the different ways to change an attribute of a field.
# It uses the 'id' field for demonstration purposes - most of these methods
# can also be used against other attributes.

{
    package Test::IDRole;
    use Moose::Role;

    sub build_id {
        my $self = shift;
        return "meth_role." . $self->html_name;
    };
}

# can't use a simple method role in field_traits because
# it's applied against the base field class which contains
# the 'build_id' method. a method in a role won't override
# a method in a class itself. It *will* override a method in
# a superclass, so this kind of role can be applied against
# the field subclasses
# a method modifier can be used to override a field class
# method
{
    package Test::IDRoleMM;
    use Moose::Role;

    around 'build_id' => sub {
        my $orig = shift;
        my $self = shift;
        return "mm_role." . $self->html_name;
    };
}

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


    # function with lexical variable
    my $name = 'test_form';
    my $create_id = sub {
        my $field_name = shift;
        return "$name.$field_name";
    };

    sub BUILD {
        my $self = shift;
        $self->field('tue')->id('my_build.tue');
    }

    has "+name" => ( default => $name );
    has_field 'foo' => ( id => 'form.foo' );
    has_field 'bar' => ( id => &my_id );
    has_field 'dux' => ( traits => ['Test::IDRole'] );
    has_field 'pax';
    has_field 'mon' => ( id => &$create_id('mon') );
    has_field 'tue';

    sub my_id { 'my_form.bar' }

}

HTML::FormHandler::Field->apply_traits('Test::IDRoleMM');

#my $form = Test::Form->new( field_traits => ['Test::IDRole'] );
my $form = Test::Form->new;
ok( $form, 'form built' );
is( $form->field('foo')->id, 'form.foo', 'id attribute works' );
is( $form->field('bar')->id, 'my_form.bar', 'id function works' );
is( $form->field('dux')->id, 'meth_role.dux', 'build_id role works' );
is( $form->field('pax')->id, 'mm_role.pax', 'role with meth modifier around build_id works' );
is( $form->field('mon')->id, 'test_form.mon', 'build_id function with var' );
is( $form->field('tue')->id, 'my_build.tue', 'set id in BUILD' );

done_testing;