Skip to content

Commit f4a5a7f

Browse files
committed
fix bug: empty array should not die
1 parent c5cf7ea commit f4a5a7f

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

lib/HTML/FormBuilder/Select.pm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ our $VERSION = '0.05';
66

77
use Carp;
88
use Moo;
9+
use Data::Dumper;
10+
911
use namespace::clean;
1012

1113
=head1 NAME
@@ -63,8 +65,12 @@ has name => (
6365
has options => (
6466
is => 'rw',
6567
isa => sub {
66-
die "$_[0] is not ArrayRef[HashRef[Any]]"
67-
unless (ref($_[0]) eq 'ARRAY' && ref($_[0][0]) eq 'HASH');
68+
my $arg = $_[0];
69+
my $msg = Dumper($arg) . " is not ArrayRef[HashRef[Any]]";
70+
die $msg if !ref($arg) || ref($arg) ne 'ARRAY';
71+
for my $elem (@$arg) {
72+
die $msg if (!ref($elem) || ref($elem) ne 'HASH');
73+
}
6874
},
6975
);
7076

t/formbuilder/select.t

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
use HTML::FormBuilder::Select;
3-
use Test::More tests => 24;
3+
use Test::More tests => 26;
4+
use Test::Exception;
45
use strict;
56

67
my $basicselect = HTML::FormBuilder::Select->new(
@@ -70,3 +71,59 @@ unlike $widget_html, qr/value="1" SELECTED/, 'widget_select shows correct value'
7071
like $widget_html, qr/ id="basic"/, 'html shows correct id';
7172
like $widget_html, qr/ name="Basic"/, 'html shows correct name';
7273

74+
subtest "empty option can create select option", sub {
75+
my $select;
76+
lives_ok(
77+
sub {
78+
$select = HTML::FormBuilder::Select->new(
79+
id => 'basic',
80+
name => 'Basic',
81+
options => [],
82+
);
83+
84+
},
85+
'create empty select list ok'
86+
);
87+
};
88+
89+
subtest "check options data type", sub {
90+
throws_ok(
91+
sub {
92+
HTML::FormBuilder::Select->new(
93+
id => 'basic',
94+
name => 'Basic',
95+
options => ["hello",],
96+
);
97+
},
98+
qr/is not ArrayRef\[HashRef\[Any\]\]/,
99+
"string is not correct"
100+
);
101+
102+
throws_ok(
103+
sub {
104+
HTML::FormBuilder::Select->new(
105+
id => 'basic',
106+
name => 'Basic',
107+
options => "hello",
108+
);
109+
},
110+
qr/is not ArrayRef\[HashRef\[Any\]\]/,
111+
"string is not correct"
112+
);
113+
throws_ok(
114+
sub {
115+
HTML::FormBuilder::Select->new(
116+
id => 'basic',
117+
name => 'Basic',
118+
options => [{
119+
value => 1,
120+
text => 'one',
121+
},
122+
"hello",
123+
]);
124+
},
125+
qr/is not ArrayRef\[HashRef\[Any\]\]/,
126+
"string is not correct"
127+
);
128+
129+
};

0 commit comments

Comments
 (0)