PageRenderTime 33ms CodeModel.GetById 24ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/t/render_filter.t

http://github.com/gshank/html-formhandler
text | 58 lines | 45 code | 13 blank | 0 comment | 0 complexity | 8655ffe6bd33f50ab06b3afdeca54053 MD5 | raw file
 1use strict;
 2use warnings;
 3use Test::More;
 4
 5use HTML::FormHandler::Field::Text;
 6
 7my $field = HTML::FormHandler::Field::Text->new( name => 'test' );
 8
 9my $string = '"J.Doe" <jdoe@gmail.com>';
10
11my $output = $field->html_filter($string);
12is( $output, '&quot;J.Doe&quot; &lt;jdoe@gmail.com&gt;', 'output ok' );
13
14{
15    package Test::RenderFilter;
16    use HTML::FormHandler::Moose;
17    extends 'HTML::FormHandler';
18
19    has_field 'foo';
20    has_field 'bar';
21
22    sub render_filter {
23        my $string = shift;
24        $string =~ s/my/MY/g;
25        return $string;
26    }
27}
28
29my $form = Test::RenderFilter->new;
30ok( $form, 'form builds' );
31
32$form->process( params => { foo => 'This is my test' } );
33like( $form->field('foo')->render, qr/MY/, 'rendering was filters' );
34
35{
36    package Test::FieldFilter;
37    use HTML::FormHandler::Moose;
38    extends 'HTML::FormHandler';
39
40    has_field 'foo' => ( render_filter => \&foo_render );
41    has_field 'bar' => ( render_filter => sub { shift } );
42    sub foo_render {
43        my $string = shift;
44        $string =~ s/yours/YOURS/g;
45        return $string;
46    }
47}
48
49$form = Test::FieldFilter->new;
50$form->process( params => { foo => "What's mine is yours", bar => '<what a hoot>' } );
51is( $form->field('bar')->render, '
52<div><label class="label" for="bar">Bar: </label><input type="text" name="bar" id="bar" value="<what a hoot>" /></div>
53', 'renders ok' );
54is( $form->field('foo')->render, '
55<div><label class="label" for="foo">Foo: </label><input type="text" name="foo" id="foo" value="What\'s mine is YOURS" /></div>
56', 'renders ok' );
57
58done_testing;