| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
# tests that a 'default' hashref on a compound field works |
| 6 |
{ |
| 7 |
{ |
| 8 |
package MyApp::Test::Compound; |
| 9 |
use HTML::FormHandler::Moose; |
| 10 |
extends 'HTML::FormHandler'; |
| 11 |
|
| 12 |
has_field 'comp_foo' => ( type => 'Compound', default => { one => 1, two => 2, three => 3 } ); |
| 13 |
has_field 'comp_foo.one'; |
| 14 |
has_field 'comp_foo.two'; |
| 15 |
has_field 'comp_foo.three'; |
| 16 |
} |
| 17 |
|
| 18 |
my $form = MyApp::Test::Compound->new; |
| 19 |
ok( $form ); |
| 20 |
$form->process; |
| 21 |
is( $form->field('comp_foo.one')->value, 1, 'value is one' ); |
| 22 |
is_deeply( $form->field('comp_foo')->value, { one => 1, two => 2, three => 3 }, |
| 23 |
'value for compound is correct' ); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
# tests that default object for a compound field works |
| 28 |
# object provided by default_method |
| 29 |
{ |
| 30 |
{ |
| 31 |
package MyApp::Foo; |
| 32 |
use Moose; |
| 33 |
has 'one' => ( is => 'ro', default => 1 ); |
| 34 |
has 'two' => ( is => 'ro', default => 2 ); |
| 35 |
has 'three' => ( is => 'ro', default => 3 ); |
| 36 |
} |
| 37 |
{ |
| 38 |
package MyApp::Test::Compound2; |
| 39 |
use HTML::FormHandler::Moose; |
| 40 |
extends 'HTML::FormHandler'; |
| 41 |
|
| 42 |
has_field 'comp_foo' => ( type => 'Compound', default_method => \&default_comp_foo ); |
| 43 |
has_field 'comp_foo.one'; |
| 44 |
has_field 'comp_foo.two'; |
| 45 |
has_field 'comp_foo.three'; |
| 46 |
sub default_comp_foo { |
| 47 |
return MyApp::Foo->new; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
my $form = MyApp::Test::Compound2->new; |
| 52 |
ok( $form ); |
| 53 |
is( $form->field('comp_foo.one')->value, 1, 'value is one' ); |
| 54 |
is( ref $form->field('comp_foo')->item, 'MyApp::Foo', 'item saved' ); |
| 55 |
} |
| 56 |
|
| 57 |
done_testing; |