modify validation regexp for Float.

Add Float test
Add new Integer test
This commit is contained in:
Colin Kuskie 2006-03-27 00:29:03 +00:00
parent 123886d4a9
commit 1ac514982f
3 changed files with 102 additions and 2 deletions

View file

@ -100,7 +100,7 @@ Returns the integer from the form post, or returns 0.0 if the post result is inv
sub getValueFromPost {
my $self = shift;
my $value = $self->session->form->param($self->get("name"));
if ($value =~ /^[\d\-\.]+$/) {
if ($value =~ /^-?[\d\.]+$/) {
return $value;
}
return 0.0;

97
t/Form/Float.t Normal file
View file

@ -0,0 +1,97 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2006 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/../lib";
use WebGUI::Test;
use WebGUI::Form;
use WebGUI::Form::Float;
use WebGUI::Session;
use Tie::IxHash;
use HTML::Form;
use Test::MockObject;
#The goal of this test is to verify that Float form elements work
use Test::More; # increment this value for each test you create
my $session = WebGUI::Test->session;
# put your tests here
my %testBlock;
tie %testBlock, 'Tie::IxHash';
%testBlock = (
TestInteger => [ '-1.23456', 1, 'valid, negative float'],
TestInteger2 => [ '.23456', 1, 'valid, no integer part'],
TestInteger3 => [ '123456789.', 1, 'valid, no fractional part'],
TestInteger4 => [ '-.123456', 1, 'valid, negative, no integer part'],
TestInteger5 => [ '+123.456', 0, 'invalid, no explicit plus sign'],
TestInteger6 => [ '123456', 1, 'WRONG, no decimal point'],
TestInteger7 => [ '......', 1, 'WRONG, no digits'],
TestInteger8 => [ '.123-456', 0, 'invalid, embedded minus sign'],
);
my $numTests = 6 + scalar keys %testBlock;
diag("Planning on running $numTests tests\n");
plan tests => $numTests;
my ($header, $footer) = (WebGUI::Form::formHeader($session), WebGUI::Form::formFooter($session));
my $html = join "\n",
$header,
WebGUI::Form::Float->new($session, {
name => 'TestFloat',
value => '12.3456',
})->toHtml,
$footer;
my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
##Test Form Generation
is(scalar @forms, 1, '1 form was parsed');
my @inputs = $forms[0]->inputs;
is(scalar @inputs, 1, 'The form has 1 input');
#Basic tests
my $input = $inputs[0];
is($input->name, 'TestFloat', 'Checking input name');
is($input->type, 'text', 'Checking input type');
is($input->value, '12.3456', 'Checking default value');
is($input->disabled, undef, 'Disabled param not sent to form');
##Test Form Output parsing
my $request = Test::MockObject->new;
$request->mock('body',
sub {
my ($self, $value) = @_;
return $testBlock{$value}->[0] if (exists $testBlock{$value});
return;
}
);
$session->{_request} = $request;
foreach my $key (keys %testBlock) {
my ($testValue, $passes, $comment) = @{ $testBlock{$key} };
my $value = $session->form->get($key, 'float');
is($value, ($passes ? $testValue : 0.0), $comment);
}
##Integer Forms have no special value preprocessing,

View file

@ -27,7 +27,7 @@ my $session = WebGUI::Test->session;
# put your tests here
my $numTests = 9;
my $numTests = 10;
diag("Planning on running $numTests tests\n");
@ -70,6 +70,7 @@ $request->mock('body',
return '-123456' if ($value eq 'TestInteger');
return "+123456" if ($value eq 'TestInteger2');
return "123-456" if ($value eq 'TestInteger3');
return "123.456" if ($value eq 'TestInteger4');
return;
}
);
@ -81,5 +82,7 @@ $value = $session->form->get('TestInteger2', 'integer');
is($value, 0, 'checking form rejects explicitly postive integer');
$value = $session->form->get('TestInteger3', 'integer');
is($value, 0, 'checking form rejects non-sense integer');
$value = $session->form->get('TestInteger4', 'integer');
is($value, 0, 'checking form rejects non-sense integer');
##Integer Forms have no special value preprocessing,