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.
This commit is contained in:
parent
a3bd26cdd0
commit
2967681f6d
1 changed files with 83 additions and 21 deletions
|
|
@ -16,6 +16,7 @@ use WebGUI::Test;
|
||||||
use WebGUI::Macro;
|
use WebGUI::Macro;
|
||||||
use WebGUI::Session;
|
use WebGUI::Session;
|
||||||
use WebGUI::Macro_Config;
|
use WebGUI::Macro_Config;
|
||||||
|
use HTML::TokeParser;
|
||||||
use Data::Dumper;
|
use Data::Dumper;
|
||||||
|
|
||||||
use Test::More; # increment this value for each test you create
|
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');
|
Macro_Config::insert_macro($session, 'H', 'H_homeLink');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my ($versionTag, $template) = addTemplate();
|
||||||
|
|
||||||
my $homeAsset = WebGUI::Asset->getDefault($session);
|
my $homeAsset = WebGUI::Asset->getDefault($session);
|
||||||
|
|
||||||
my $i18n = WebGUI::International->new($session,'Macro_H_homeLink');
|
my $i18n = WebGUI::International->new($session,'Macro_H_homeLink');
|
||||||
|
|
||||||
my @testSets = (
|
my @testSets = (
|
||||||
{
|
{
|
||||||
macroText => q!^H("%s");!,
|
macroText => q!^H("%s");!,
|
||||||
label => q!linkonly!,
|
label => q!linkonly!,
|
||||||
format => q!!,
|
template => q!!,
|
||||||
output => $homeAsset->getUrl(),
|
output => $homeAsset->getUrl(),
|
||||||
comment => 'linkonly argument',
|
comment => 'linkonly argument',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
macroText => q!^H();!,
|
macroText => q!^H();!,
|
||||||
label => q!!,
|
label => $i18n->get(47),
|
||||||
format => q!!,
|
template => q!!,
|
||||||
output => sprintf(q!<a class="homeLink" href="%s">%s</a>!, $homeAsset->getUrl(), $i18n->get(47)),
|
url => $homeAsset->getUrl(),
|
||||||
comment => 'default macro call',
|
output => \&simpleHTMLParser,
|
||||||
|
comment => 'default macro call',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
macroText => q!^H("%s");!,
|
macroText => q!^H("%s");!,
|
||||||
label => q!Hi, want to go home?!,
|
label => q!Hi, want to go home?!,
|
||||||
format => q!!,
|
template => q!!,
|
||||||
output => sprintf(q!<a class="homeLink" href="%s">%s</a>!, $homeAsset->getUrl(), 'Hi, want to go home?'),
|
url => $homeAsset->getUrl(),
|
||||||
comment => 'default macro call',
|
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;
|
plan tests => $numTests;
|
||||||
|
|
||||||
foreach my $testSet (@testSets) {
|
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);
|
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: {
|
sub addTemplate {
|
||||||
local $TODO = "Tests to make later";
|
$session->user({userId=>3});
|
||||||
ok(0, 'Check label override');
|
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=<tmpl_var homeLink.url>\nLABEL=<tmpl_var homeLink.text>",
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue