App-Templer
view release on metacpan or search on metacpan
t/test-templer-plugin-fileglob.t view on Meta::CPAN
#
# Create a temporary tree.
#
my $dir = tempdir( CLEANUP => 1 );
#
# Create a page.
#
open( my $handle, ">", $dir . "/input.skx" );
print $handle <<EOF;
Title: This is my page title.
Files: file_glob( foo/* );
----
This is my page content.
EOF
close($handle);
#
# Now create some files (one of which is a templer input page)
#
mkdir("$dir/foo");
createFile( $dir . "/foo/foo.txt" );
createFile( $dir . "/foo/ok.txt" );
createFile( $dir . "/foo/bar.txt" );
createFile( $dir . "/foo/bar" );
open( $handle, ">", $dir . "/foo/input.skx" );
print $handle <<EOF;
Title: An included page
Variable: Value
----
This is another page content.
EOF
close($handle);
#
# Create the page
#
my $page = Templer::Site::Page->new( file => $dir . "/input.skx" );
ok( $page, "We created a page object" );
isa_ok( $page, "Templer::Site::Page", "Which has the correct type" );
#
# Get the title to be sure
#
is( $page->field("title"),
"This is my page title.",
"The page has the correct title" );
#
# Get the data, after plugin-expansion
#
my %original = $page->fields();
my $ref = $factory->expand_variables( $site, $page, \%original );
my %updated = %$ref;
ok( %updated, "Fetching the fields of the page succeeded" );
ok( $updated{ 'files' }, "The fields contain a file reference" );
foreach my $obj ( @{ $updated{ 'files' } } )
{
my $file = $obj->{ 'file' };
ok( $obj->{ 'file' }, "The file reference has a name : $obj->{ 'file' }" );
ok( $obj->{ 'file' } =~ m{^foo/}, " file reference is sane" );
if ( $file eq "foo/foo.txt" )
{
is( $obj->{ 'contents' }, "something", " content is propagated" );
is( $obj->{ 'dirname' }, 'foo', " dirname is captured" );
is( $obj->{ 'basename' }, 'foo', " basename is captured" );
is( $obj->{ 'extension' }, 'txt', " extension is captured" );
}
elsif ( $file eq "foo/ok.txt" )
{
is( $obj->{ 'contents' }, "something", " content is propagated" );
is( $obj->{ 'dirname' }, 'foo', " dirname is captured" );
is( $obj->{ 'basename' }, 'ok', " basename is captured" );
is( $obj->{ 'extension' }, 'txt', " extension is captured" );
}
elsif ( $file eq "foo/bar.txt" )
{
is( $obj->{ 'contents' }, "something", " content is propagated" );
is( $obj->{ 'dirname' }, 'foo', " dirname is captured" );
is( $obj->{ 'basename' }, 'bar', " basename is captured" );
is( $obj->{ 'extension' }, 'txt', " extension is captured" );
}
elsif ( $file eq "foo/bar" )
{
is( $obj->{ 'contents' }, "something", " content is propagated" );
is( $obj->{ 'dirname' }, 'foo', " dirname is captured" );
is( $obj->{ 'basename' }, 'bar', " basename is captured" );
is( $obj->{ 'extension' }, undef, " extension is empty" );
}
elsif ( $file eq "foo/input.skx" )
{
is( $obj->{ 'variable' },
'Value', " input variables are propagated" );
is( $obj->{ 'dirname' }, 'foo', " dirname is captured" );
is( $obj->{ 'basename' }, 'input', " basename is captured" );
is( $obj->{ 'extension' }, 'skx', " extension is captured" );
}
}
is( scalar( @{ $updated{ 'files' } } ),
5, "We received the number of files we expected" );
#
# All done.
#
#
# Create a file, given a name.
#
sub createFile
{
my ($filename) = (@_);
ok( !-e $filename, "The file we're creating is not present" );
open( my $handle, ">", $filename );
print $handle "something";
close($handle);
ok( -e $filename, "We created a temporary file" );
}
( run in 0.592 second using v1.01-cache-2.11-cpan-39bf76dae61 )