PageRenderTime 48ms CodeModel.GetById 28ms app.highlight 18ms RepoModel.GetById 0ms app.codeStats 0ms

/t/defaults.t

http://github.com/gshank/html-formhandler
text | 169 lines | 141 code | 28 blank | 0 comment | 0 complexity | 674047c5eea899df4241d91100d63792 MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More;
  4use lib 't/lib';
  5
  6{
  7    package Test::Defaults;
  8    use HTML::FormHandler::Moose;
  9    extends 'HTML::FormHandler';
 10
 11    has_field 'foo' => ( default => 'default_foo' );
 12    has_field 'bar' => ( default => '' );
 13    has_field 'bax' => ( default => 'default_bax' );
 14}
 15
 16my $form = Test::Defaults->new;
 17my $cmp_fif = {
 18    foo => 'default_foo',
 19    bar => '',
 20    bax => 'default_bax',
 21};
 22is_deeply( $form->fif, $cmp_fif, 'fif has right defaults' );
 23$form->process( params => {} );
 24is_deeply( $form->fif, $cmp_fif, 'fif has right defaults' );
 25
 26my $init_obj = { foo => '', bar => 'testing', bax => '' };
 27$form->process( init_object => $init_obj, params => {} );
 28is_deeply( $form->fif, { foo => '', bar => 'testing', bax => '' }, 'object overrides defaults');
 29
 30{
 31    package Test::DefaultsX;
 32    use HTML::FormHandler::Moose;
 33    extends 'HTML::FormHandler';
 34
 35    has_field 'foo' => ( default => 'default_foo', default_over_obj => 'default_foo' );
 36    has_field 'bar' => ( default => '', default_over_obj => '' );
 37    has_field 'bax' => ( default => 'default_bax', default_over_obj => 'default_bax' );
 38}
 39$form = Test::DefaultsX->new;
 40$form->process( init_object => $init_obj, params => {} );
 41is_deeply( $form->fif, $cmp_fif, 'fif uses defaults overriding object' );
 42
 43{
 44   package My::Form;
 45   use HTML::FormHandler::Moose;
 46   extends 'HTML::FormHandler';
 47
 48   has '+name' => ( default => 'testform_' );
 49   has_field 'optname' => ( temp => 'First' );
 50   has_field 'reqname' => ( required => 1 );
 51   has_field 'somename';
 52}
 53
 54
 55$form = My::Form->new( init_object => {reqname => 'Starting Perl',
 56                                       optname => 'Over Again' } );
 57ok( $form, 'non-db form created OK');
 58is( $form->field('optname')->value, 'Over Again', 'get right value from form');
 59$form->process({});
 60ok( !$form->validated, 'form validated' );
 61is( $form->field('reqname')->fif, 'Starting Perl',
 62                      'get right fif with init_object');
 63
 64{
 65   package My::Form;
 66   use HTML::FormHandler::Moose;
 67   extends 'HTML::FormHandler';
 68
 69   has '+name' => ( default => 'initform_' );
 70   has_field 'foo';
 71   has_field 'bar';
 72   has_field 'baz';
 73   has_field 'bax' => ( default => 'default_bax' );
 74   has '+init_object' => ( default => sub { { foo => 'initfoo' } } );
 75   sub default_bar { 'init_value_bar' }
 76   sub init_value_baz { 'init_value_baz' }
 77}
 78
 79$form = My::Form->new;
 80ok( $form->field('foo')->value, 'initfoo' );
 81ok( $form->field('bar')->value, 'init_value_bar' );
 82ok( $form->field('baz')->value, 'init_value_baz' );
 83ok( $form->field('bax')->value, 'default_bax' );
 84
 85{
 86    package Mock::Object;
 87    use Moose;
 88    has 'foo' => ( is => 'rw' );
 89    has 'bar' => ( is => 'rw' );
 90    has 'baz' => ( is => 'rw' );
 91}
 92{
 93    package Test::Object;
 94    use HTML::FormHandler::Moose;
 95    extends 'HTML::FormHandler';
 96    with 'HTML::FormHandler::Model::Object';
 97
 98    sub BUILD {
 99        my $self = shift;
100        my $var = 'test';
101    }
102    has_field 'foo';
103    has_field 'bar';
104    has_field 'baz';
105    has_field 'bax' => ( default => 'bax_from_default' );
106    has_field 'zero' => ( type => 'PosInteger', default => 0 );
107    has_field 'foo_list' => ( type => 'Multiple', default => [1,3],
108       options => [{ value => 1, label => 'One'},
109                   { value => 2, label => 'Two'},
110                   { value => 3, label => 'Three'},
111                  ]
112    );
113
114    sub init_object {
115        my $self = shift;
116        return { bar => 'initbar' };
117    }
118
119}
120
121my $obj = Mock::Object->new( foo => 'myfoo', bar => 'mybar', baz => 'mybaz' );
122
123$form = Test::Object->new;
124$form->process( item => $obj, item_id => 1, params => {} );
125is( $form->field('foo')->value, 'myfoo', 'field value from item');
126is( $form->field('bar')->value, 'mybar', 'field value from item');
127is( $form->field('bax')->value, 'bax_from_default', 'non-item field value from default' );
128is( $form->field('zero')->value, 0, 'zero default works');
129is_deeply( $form->field('foo_list')->value, [1,3], 'multiple default works' );
130
131{
132    package Test::Form;
133    use HTML::FormHandler::Moose;
134    extends 'HTML::FormHandler';
135
136    has 'quuz' => ( is => 'ro', default => 'some_quux' );
137    has_field 'foo';
138    has_field 'bar';
139
140    sub init_object {
141        my $self = shift;
142        return { foo => $self->quuz, bar => 'bar!' };
143    }
144}
145$form = Test::Form->new;
146is( $form->field('foo')->value, 'some_quux', 'field initialized by init_object' );
147
148
149{
150    package Mock::Object;
151    use Moose;
152    has 'meow' => ( is => 'rw' );
153}
154{
155    package Test::Object;
156    use HTML::FormHandler::Moose;
157    extends 'HTML::FormHandler';
158    with 'HTML::FormHandler::Model::Object';
159    has_field 'meow' => ( default => 'this_should_get_overridden' );
160
161}
162
163$obj = Mock::Object->new( meow => 'the_real_meow' );
164
165$form = Test::Object->new;
166$form->process( item => $obj, item_id => 1, params => {} );
167is( $form->field('meow')->value, 'the_real_meow', 'defaults should not override actual item values');
168
169done_testing;