added splitTag test cases

This commit is contained in:
David Delikat 2009-01-26 03:56:02 +00:00
parent 13ac9dc064
commit d24079f2ca
3 changed files with 138 additions and 6 deletions

View file

@ -1,4 +1,5 @@
7.6.10
- added test for splitTag function recently added to HTML module
- fixed #9455: fixed synopsis to pick out html paragraphs, fixed FeedPP fields to scalar when blank
- fixed: With autocommit and no comments on, making a shortcut of an asset takes you to that asset's view.
- fixed #9506: Choose billing address

View file

@ -37,7 +37,7 @@ A package for manipulating and massaging HTML.
$html = WebGUI::HTML::html2text($html);
$html = WebGUI::HTML::makeAbsolute($session, $html);
$html = WebGUI::HTML::processReplacements($session, $html);
$html = WebGUI::HTML::splitTag([$tag,]$html[,$count]); # defaults to ( 'p', $html, 0 )
$html = WebGUI::HTML::splitTag([$tag,]$html[,$count]); # defaults to ( 'p', $html, 1 )
=head1 METHODS
@ -414,7 +414,7 @@ The block of HTML text that will be disected
=head3 count
How many items do we want? defaults to 1; returns 1 non-blank item
How many items do we want? defaults to 1; returns 1 non-blank item; -1 returns all items
=cut
@ -423,15 +423,13 @@ sub splitTag {
my $tag = shift;
my $html = shift;
my $count = shift || 1;
if( not defined $html or $html =~ /^(\d+)$/ ) {
if( not defined $html or $html =~ /^(-?\d+)$/ ) {
$count = $html if $1;
$html = $tag;
$tag = 'p'; # the default tag is 'p' -- grabs a paragraph
}
my @result;
$html =~ s/\ //g; # get rid of all non-breaking spaces
my $p = HTML::TokeParser->new(\$html);
while (my $token = $p->get_tag($tag)) {
@ -441,7 +439,8 @@ sub splitTag {
last if @result == $count; # if we have a full count then quit
}
return @result;
return @result if wantarray;
return $result[0];
}
1;

132
t/splitTag.t Normal file
View file

@ -0,0 +1,132 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2008 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::HTML;
use WebGUI::Session;
use Test::More; # increment this value for each test you create
my $session = WebGUI::Test->session;
# the style param below refers to how the callis made. the function has two optional params
# by testing with each style we can test the default for thsee params.
# also array/scalar return type are tested.
# call type = 1: all three params; return array
# call type = 2: only tag and text; return scalar
# call type = 3: only text and count; return array
# call type = 4: only the text; return scalar
my @testArray = (
{
inputText => q!<p>First Paragraph</p> <p>Second Paragraph</p>!,
output => q!First Paragraph!,
style => 1,
tag => 'p',
count => 1,
comment => 'get one paragraph, no defaults',
},
{
inputText => q!<p>First Paragraph</p> <p>Second Paragraph</p>!,
output => q!First Paragraph!,
style => 4,
tag => 'p',
count => 1,
comment => 'get one paragraph, all defaults',
},
{
inputText => q!<p>First Paragraph</p> <p>Second Paragraph</p>!,
output => q!First Paragraph;Second Paragraph!,
style => 1,
tag => 'p',
count => -1,
comment => 'get all paragraphs, no defaults',
},
{
inputText => q!<p>First Paragraph</p> <p>Second Paragraph</p>!,
output => q!First Paragraph;Second Paragraph!,
style => 3,
tag => 'p',
count => -1,
comment => 'get all paragraphs, default tag',
},
{
inputText => q!<p> </p><p>First nonblank Paragraph</p> <p>Second Paragraph</p>!,
output => q!First nonblank Paragraph!,
style => 4,
tag => 'p',
count => -1,
comment => 'get nonblank paragraph, all defaults',
},
{
inputText => q!<p> &nbsp;</p><p>First nonblank Paragraph</p> <p>Second Paragraph</p>!,
output => q!First nonblank Paragraph!,
style => 4,
tag => 'p',
count => -1,
comment => 'get nonblank(nbsp) paragraph, all defaults',
},
{
inputText => q!<li>First List Item</li> <li>Second List Item</li> <li>Third List Item</li>!,
output => q!First List Item;Second List Item;Third List Item!,
style => 1,
tag => 'li',
count => -1,
comment => 'get all list items, no defaults',
},
{
inputText => q!<li>First List Item</li> <li>Second List Item</li> <li>Third List Item</li>!,
output => q!First List Item;Second List Item!,
style => 1,
tag => 'li',
count => 2,
comment => 'get first 2 list items, no defaults',
},
{
inputText => q!<tr><td>First Item</td> <td>Second Item</td></tr>!,
output => q!First Item Second Item!,
style => 1,
tag => 'tr',
count => -1,
comment => 'get table row ignore table data',
},
{
inputText => q!<tr><td>First Item</td> <td>Second Item</td></tr>!,
output => q!First Item;Second Item!,
style => 1,
tag => 'td',
count => -1,
comment => 'get table data ignore table row',
},
);
my $numTests = scalar @testArray;
plan tests => $numTests;
my $output;
foreach my $testSet (@testArray) {
if( $testSet->{style} == 1 ) {
$output = join ';',WebGUI::HTML::splitTag($testSet->{tag},$testSet->{inputText}, $testSet->{count});
} elsif( $testSet->{style} == 2 ) {
$output = WebGUI::HTML::splitTag($testSet->{tag},$testSet->{inputText});
} elsif( $testSet->{style} == 3 ) {
$output = join ';', WebGUI::HTML::splitTag($testSet->{inputText}, $testSet->{count});
} elsif( $testSet->{style} == 4 ) {
$output = WebGUI::HTML::splitTag($testSet->{inputText});
}
is($output, $testSet->{output}, $testSet->{comment});
}