From d4880b300ecf9444acb772ee502c49a2311c80f5 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sat, 25 Mar 2006 17:41:39 +0000 Subject: [PATCH] Add tests for Password and Email. Updated comments appropriately. --- t/Form/Email.t | 84 ++++++++++++++++++++++++++++++++++++++++ t/Form/Password.t | 97 +++++++++++++++++++++++++++++++++++++++++++++++ t/Form/Text.t | 5 +-- t/Form/Textarea.t | 5 +-- 4 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 t/Form/Email.t create mode 100644 t/Form/Password.t diff --git a/t/Form/Email.t b/t/Form/Email.t new file mode 100644 index 000000000..ce83eda78 --- /dev/null +++ b/t/Form/Email.t @@ -0,0 +1,84 @@ +#------------------------------------------------------------------- +# 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::Email; +use WebGUI::Session; +use HTML::Form; +use Test::MockObject; + +#The goal of this test is to verify that Email form elements work. +#The Email form accepts and validates an email address. + +use Test::More; # increment this value for each test you create + +my $session = WebGUI::Test->session; + +# put your tests here + +my $numTests = 8; + +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::Email->new($session, { + name => 'TestEmail', + value => 'me@nowhere.com', + })->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, 'TestEmail', 'Checking input name'); +is($input->type, 'text', 'Checking input type'); +is($input->value, 'me@nowhere.com', '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) = @_; + my @return = (); + return 'me@nowhere.com' if ($value eq 'TestEmail'); + return "what do you want?" if ($value eq 'TestEmail2'); + return; + } +); +$session->{_request} = $request; + +my $value = $session->form->get('TestEmail', 'Email'); +is($value, 'me@nowhere.com', 'checking existent form value'); +$value = $session->form->get('TestEmail2', 'Email'); +is($value, undef, 'checking form postprocessing for bad email address'); + +##Email form does no preprocessing of its input + diff --git a/t/Form/Password.t b/t/Form/Password.t new file mode 100644 index 000000000..d9bf10483 --- /dev/null +++ b/t/Form/Password.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::Password; +use WebGUI::Session; +use HTML::Form; +use Test::MockObject; + +#The goal of this test is to verify that Password form elements work + +use Test::More; # increment this value for each test you create + +my $session = WebGUI::Test->session; + +# put your tests here + +my $numTests = 10; + +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::Password->new($session, { + name => 'TestText', + value => 'Some text in here', + })->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, 'TestText', 'Checking input name'); +is($input->type, 'password', 'Checking input type'); +is($input->value, 'Some text in here', '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) = @_; + my @return = (); + return 'some user value' if ($value eq 'TestText'); + return "some user value\nwith\r\nwhitespace" if ($value eq 'TestText2'); + return; + } +); +$session->{_request} = $request; + +my $value = $session->form->get('TestText', 'Password'); +is($value, 'some user value', 'checking existent form value'); +$value = $session->form->get('TestText2', 'Password'); +is($value, "some user value\nwith\r\nwhitespace", 'checking form postprocessing for whitespace'); + +##Form value preprocessing +##Note that HTML::Form will unencode the text for you. + +$html = join "\n", + $header, + WebGUI::Form::Password->new($session, { + name => 'preTestText', + value => q!Some & text in " here!, + })->toHtml, + $footer; + +@forms = HTML::Form->parse($html, 'http://www.webgui.org'); +@inputs = $forms[0]->inputs; +$input = $inputs[0]; +is($input->name, 'preTestText', 'Checking input name'); +is($input->value, 'Some & text in " here', 'Checking default value'); diff --git a/t/Form/Text.t b/t/Form/Text.t index 7087e60ad..1e6bd5b8a 100644 --- a/t/Form/Text.t +++ b/t/Form/Text.t @@ -19,10 +19,7 @@ use WebGUI::Session; use HTML::Form; use Test::MockObject; -#The goal of this test is to verify that Text form elements -#work, via HTMLForm. HTML::Form is actuall parsing the HTML -#looking for stuff inside form tags, which is why we have to use -#HTMLForm. +#The goal of this test is to verify that Text form elements work use Test::More; # increment this value for each test you create diff --git a/t/Form/Textarea.t b/t/Form/Textarea.t index b00df50ca..c1678e642 100644 --- a/t/Form/Textarea.t +++ b/t/Form/Textarea.t @@ -19,10 +19,7 @@ use WebGUI::Session; use HTML::Form; use Test::MockObject; -#The goal of this test is to verify that Text form elements -#work, via HTMLForm. HTML::Form is actuall parsing the HTML -#looking for stuff inside form tags, which is why we have to use -#HTMLForm. +#The goal of this test is to verify that Textarea form elements work use Test::More; # increment this value for each test you create