[Html-widget] HTML::Widget::Constraint::Range wherein limit is zero
Steven Kuo
attn.steven.kuo at gmail.com
Thu Oct 19 23:35:08 GMT 2006
It's should be possible to set a constraint
with H::W::C::Range wherein the
minimum or maximum is zero, right?
But in order to register this constraint
it seems I have to use a "zero-but-true"
specification. It's not clear from the documentation
that a plain zero (0) won't work.
Here's a test that I inserted into the
test suite and ran with 'prove':
$ cat t/constraint_range_added.t
use strict;
use warnings;
use Test::More tests => 4;
use HTML::Widget;
use lib 't/lib';
use HTMLWidget::TestLib;
my $w1 = HTML::Widget->new;
my $w2 = HTML::Widget->new;
$w1->element( 'Textfield', 'foo' );
$w1->constraint( 'Range', 'foo' )->min("0e0"); # zero-but-true
$w2->element( 'Textfield', 'bar' );
$w2->constraint( 'Range', 'bar' )->min(0);
#
{
my $query = HTMLWidget::TestLib->mock_query(
{
foo => -1,
bar => -1,
}
);
my $f1 = $w1->process($query);
my $f2 = $w2->process($query);
is( $f1->param('foo'), undef, 'undef returned for invalid' );
ok( $f1->errors, 'errors expected: -1 is not >= 0' );
is( $f2->param('bar'), undef, 'undef returned for invalid' );
ok( $f2->errors, 'errors expected: -1 is not >= 0' );
}
$ prove -Iblib/lib -v t/constraint_range_added.t
t/constraint_range_added....1..4
ok 1 - undef returned for invalid
ok 2 - errors expected: -1 is not >= 0
# Failed test 'undef returned for invalid'
# in t/constraint_range_added.t at line 34.
not ok 3 - undef returned for invalid
# got: '-1'
# expected: undef
# Failed test 'errors expected: -1 is not >= 0'
# in t/constraint_range_added.t at line 35.
not ok 4 - errors expected: -1 is not >= 0
# Looks like you failed 2 tests of 4.
dubious
Test returned status 2 (wstat 512, 0x200)
DIED. FAILED tests 3-4
Failed 2/4 tests, 50.00% okay
Failed Test Stat Wstat Total Fail List of Failed
-------------------------------------------------------------------------------
t/constraint_range_added.t 2 512 4 2 3-4
Failed 1/1 test scripts. 2/4 subtests failed.
Files=1, Tests=4, 1 wallclock secs ( 0.28 cusr + 0.07 csys = 0.35 CPU)
Failed 1/1 test programs. 2/4 subtests failed.
The underlying reason is that the 'validate' subroutine
in H::W::C::Range only checks if the limits ( $minimum
and $maximum) are *logically true* (lines 53 and 56):
42 sub validate {
43 my ( $self, $value ) = @_;
44
45 return 1 if !defined $value || $value eq '';
46
47 my $minimum = $self->minimum;
48 my $maximum = $self->maximum;
49 my $failed = 0;
50
51 $failed++ if !looks_like_number($value);
52
53 if ( !$failed && $minimum ) {
54 $failed++ unless ( $value >= $minimum );
55 }
56 if ( !$failed && $maximum ) {
57 $failed++ unless ( $value <= $maximum );
58 }
59 return !$failed;
60 }
0, while it's (or should be) a valid limit, is none-the-less
logically (i.e., in boolean context) false. Hence, I'm finding
myself to use "0e0" instead.
Comments?
More information about the Html-widget
mailing list