From 2967681f6d8a69ffa02b3311a000e0aca581682b Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 6 Jul 2006 22:04:51 +0000 Subject: [PATCH] Updated macro test to instantiate a test template using the new version control/tag API and then use HTML and text parsers to read the output to see if the templates were executed correctly. --- t/Macro/H_homeLink.t | 104 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/t/Macro/H_homeLink.t b/t/Macro/H_homeLink.t index 65e93e1ae..7872d59f6 100644 --- a/t/Macro/H_homeLink.t +++ b/t/Macro/H_homeLink.t @@ -16,6 +16,7 @@ use WebGUI::Test; use WebGUI::Macro; use WebGUI::Session; use WebGUI::Macro_Config; +use HTML::TokeParser; use Data::Dumper; use Test::More; # increment this value for each test you create @@ -26,45 +27,106 @@ unless ($session->config->get('macros')->{'H_homeLink'}) { Macro_Config::insert_macro($session, 'H', 'H_homeLink'); } +my ($versionTag, $template) = addTemplate(); + my $homeAsset = WebGUI::Asset->getDefault($session); my $i18n = WebGUI::International->new($session,'Macro_H_homeLink'); my @testSets = ( { - macroText => q!^H("%s");!, - label => q!linkonly!, - format => q!!, - output => $homeAsset->getUrl(), - comment => 'linkonly argument', + macroText => q!^H("%s");!, + label => q!linkonly!, + template => q!!, + output => $homeAsset->getUrl(), + comment => 'linkonly argument', }, { - macroText => q!^H();!, - label => q!!, - format => q!!, - output => sprintf(q!%s!, $homeAsset->getUrl(), $i18n->get(47)), - comment => 'default macro call', + macroText => q!^H();!, + label => $i18n->get(47), + template => q!!, + url => $homeAsset->getUrl(), + output => \&simpleHTMLParser, + comment => 'default macro call', }, { - macroText => q!^H("%s");!, - label => q!Hi, want to go home?!, - format => q!!, - output => sprintf(q!%s!, $homeAsset->getUrl(), 'Hi, want to go home?'), - comment => 'default macro call', + macroText => q!^H("%s");!, + label => q!Hi, want to go home?!, + template => q!!, + url => $homeAsset->getUrl(), + output => \&simpleHTMLParser, + comment => 'custom label', + }, + { + macroText => q!^H("%s","%s");!, + label => q!Custom label!, + template => q!H_homeLink-test!, + url => $homeAsset->getUrl(), + output => \&simpleTextParser, + comment => 'custom template', }, ); -my $numTests = scalar @testSets + 1; +my $numTests = 0; +foreach my $testSet (@testSets) { + $numTests += 1 + (ref $testSet->{output} eq 'CODE'); +} plan tests => $numTests; foreach my $testSet (@testSets) { - my $output = sprintf $testSet->{macroText}, $testSet->{label}, $testSet->{format}; + my $output = sprintf $testSet->{macroText}, $testSet->{label}, $testSet->{template}; WebGUI::Macro::process($session, \$output); - is($output, $testSet->{output}, $testSet->{comment}); + if (ref $testSet->{output} eq 'CODE') { + my ($url, $label) = $testSet->{output}->($output); + is($label, $testSet->{label}, $testSet->{comment}.", label"); + is($url, $testSet->{url}, $testSet->{comment}.", url"); + } + else { + is($output, $testSet->{output}, $testSet->{comment}); + } } -TODO: { - local $TODO = "Tests to make later"; - ok(0, 'Check label override'); +sub addTemplate { + $session->user({userId=>3}); + my $importNode = WebGUI::Asset->getImportNode($session); + my $versionTag = WebGUI::VersionTag->getWorking($session); + $versionTag->set({name=>"H_homeLink test"}); + my $properties = { + title => 'H_homeLink test template', + className => 'WebGUI::Asset::Template', + url => 'H_homeLink-test', + namespace => 'Macro/H_homeLink', + template => "HREF=\nLABEL=", + id => 'testTemplateH_HomeLink' + }; + my $template = $importNode->addChild($properties, $properties->{id}); + $versionTag->commit; + return ($versionTag, $template); +} + +sub simpleHTMLParser { + my ($text) = @_; + my $p = HTML::TokeParser->new(\$text); + + my $token = $p->get_tag("a"); + my $url = $token->[1]{href} || "-"; + my $label = $p->get_trimmed_text("/a"); + + return ($url, $label); +} + +sub simpleTextParser { + my ($text) = @_; + + my ($url) = $text =~ /^HREF=(.+)$/m; + my ($label) = $text =~ /^LABEL=(.+)$/m; + + return ($url, $label); +} + +END { + if (defined $versionTag and ref $versionTag eq 'WebGUI::VersionTag') { + $versionTag->rollback; + } }