use Test::More tests => 23; BEGIN { use_ok('Template::Trivial') }; ######################### my $tmpl; ## ## setup tests ## undef $tmpl; $tmpl = new Template::Trivial; ok( $tmpl->strict ); undef $tmpl; $tmpl = new Template::Trivial(strict => 0); is( $tmpl->strict, 0 ); $tmpl->strict(1); ok( $tmpl->strict ); undef $tmpl; $tmpl = new Template::Trivial; is( $tmpl->templates, '' ); undef $tmpl; $tmpl = new Template::Trivial(templates => '/var/tmp/foo'); is( $tmpl->templates, '/var/tmp/foo' ); undef $tmpl; $tmpl = new Template::Trivial; ## test assign_from_file (plain file) open FOO, ">test.foo" or die "Could not open test.foo: $!\n"; print FOO "Foo contents\n"; close FOO; $tmpl->assign_from_file(FOO => 'test.foo'); is($tmpl->to_string('FOO'), "Foo contents\n"); unlink "test.foo"; ## test assign_from_file (device) if( -e '/dev/null' ) { $tmpl->assign_from_file(FOO => '/dev/null'); ## this won't work on Windows } else { $tmpl->assign(FOO => ''); } is( $tmpl->to_string('FOO'), '' ); ## test variable assignment $tmpl->assign(FOO => 'foo'); is( $tmpl->to_string('FOO'), 'foo' ); ## test multiple variable assignment $tmpl->assign(BAR => 'bar', BAZ => 'baz'); is( $tmpl->to_string('BAR'), 'bar'); is( $tmpl->to_string('BAZ'), 'baz'); ## test append methods $tmpl->assign('.FOO' => 'ster'); is( $tmpl->to_string('FOO'), 'fooster' ); $tmpl->append(FOO => ' just'); is( $tmpl->to_string('FOO'), 'fooster just' ); ## test template definition $tmpl->strict(0); ## turn off stricture $tmpl->define(bar => 'bar.tmpl'); is($tmpl->to_string('bar'), ''); $tmpl->strict(1); ## should warn under strict print STDERR "A warning should appear here: "; $tmpl->define(bar => 'bar.tmpl'); print STDERR "A warning should appear here: "; is($tmpl->to_string('bar'), undef ); ## create bar.tmpl open FILE, ">bar.tmpl" or die "Could not write bar.tmpl: $!\n"; print FILE "This is bar {BARF} so there\n"; close FILE; ## empty variable test $tmpl->define_from_string( something => q!HI{BLANK}THERE!, blank => '' ); $tmpl->parse(BLANK => 'blank'); $tmpl->parse(SOMETHING => 'something'); is( $tmpl->to_string('SOMETHING'), 'HITHERE' ); ## FIXME: false variable test (0) ## undefined variable test $tmpl->define(bar => 'bar.tmpl'); print STDERR "A warning should appear here: "; $tmpl->parse(BAR => 'bar'); is($tmpl->to_string('BAR'), "This is bar {BARF} so there\n" ); ## try again $tmpl->assign(BARF => "barfus"); $tmpl->parse(BAR => 'bar'); is($tmpl->to_string('BAR'), "This is bar barfus so there\n"); ## test parse append $tmpl->assign(BARF => "sufrab"); $tmpl->parse('.BAR' => 'bar'); is($tmpl->to_string('BAR'), <<_BARF_); This is bar barfus so there This is bar sufrab so there _BARF_ unlink "bar.tmpl"; ## complete test my %files = ( main => 'main.tmpl', table => 'table.tmpl', ); my %content = ( main => <<_MAIN_, {TITLE} {TABLE} _MAIN_ table => ,<<_TABLE_ ); {ROWS}
_TABLE_ for my $file ( keys %files ) { open FILE, ">$files{$file}" or die "Could not create '$files{$file}': $!\n"; print FILE $content{$file}; close FILE; } undef $tmpl; $tmpl = new Template::Trivial; $tmpl->define( %files ); ## do title now $tmpl->define_from_string( title => "{TITLE}" ); $tmpl->assign( TITLE => "This is the title" ); $tmpl->parse( TITLE => 'title' ); is( $tmpl->to_string('TITLE'), "This is the title" ); open FOO, ">title.foo" or die "Could not open title.foo: $!\n"; print FOO "Foo: {TITLE}"; close FOO; ## override $tmpl->define( title => "title.foo" ); $tmpl->assign( TITLE => "This is the second title" ); $tmpl->parse( TITLE => 'title' ); is( $tmpl->to_string('TITLE'), "Foo: This is the second title" ); ## override $tmpl->define_from_string( title => "{TITLE}" ); $tmpl->assign( TITLE => "This is the title" ); $tmpl->parse( TITLE => 'title' ); is( $tmpl->to_string('TITLE'), "This is the title" ); ## define ROWS variable $tmpl->assign(ROWS => qq!namegecos\n!); my %chars = ( fred => "Fred Flintstone", barney => "Barney Rubble", wilma => "Wilma Flintstone", betty => "Betty Rubble" ); for my $char ( sort keys %chars ) { $tmpl->assign('.ROWS' => qq!$char$chars{$char}\n! ); } $tmpl->parse( TABLE => 'table' ); $tmpl->parse( MAIN => 'main' ); is( $tmpl->to_string('MAIN'), <<_MAIN_ ); This is the title
namegecos
barneyBarney Rubble
bettyBetty Rubble
fredFred Flintstone
wilmaWilma Flintstone
_MAIN_ ## ## cleanup tests ## END { unlink "bar.tmpl" if -e "bar.tmpl"; unlink "test.foo" if -e "test.foo"; unlink "title.foo" if -e "title.foo"; unlink values %files; }