From 1ac514982f9b458ace1c121fee692bd006b6d752 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 27 Mar 2006 00:29:03 +0000 Subject: [PATCH] modify validation regexp for Float. Add Float test Add new Integer test --- lib/WebGUI/Form/Float.pm | 2 +- t/Form/Float.t | 97 ++++++++++++++++++++++++++++++++++++++++ t/Form/Integer.t | 5 ++- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 t/Form/Float.t diff --git a/lib/WebGUI/Form/Float.pm b/lib/WebGUI/Form/Float.pm index 345435cc5..3fc9a87e4 100644 --- a/lib/WebGUI/Form/Float.pm +++ b/lib/WebGUI/Form/Float.pm @@ -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; diff --git a/t/Form/Float.t b/t/Form/Float.t new file mode 100644 index 000000000..6959790c0 --- /dev/null +++ b/t/Form/Float.t @@ -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, diff --git a/t/Form/Integer.t b/t/Form/Integer.t index 17bb95c8f..456ac23e8 100644 --- a/t/Form/Integer.t +++ b/t/Form/Integer.t @@ -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,