1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 22;
use_ok 'Text::MicroMason', qw( compile execute );
######################################################################
SIMPLE_ARGS: {
my $scr_bold = '<%args>$label</%args><b><% $label %></b>';
is execute($scr_bold, label=>'Foo'), '<b>Foo</b>';
is compile($scr_bold)->(label=>'Foo'), '<b>Foo</b>';
is eval { execute($scr_bold); 1 }, undef;
ok $@;
}
######################################################################
ARGS_BLOCK_WITH_DEFAULT: {
my $scr_hello = <<'ENDSCRIPT';
<%args>
$name
$hour => (localtime)[2]
</%args>
% if ( $name eq 'Dave' ) {
I'm sorry <% $name %>, I'm afraid I can't do that right now.
% } else {
<%perl>
my $greeting = ( $hour > 11 ) ? 'afternoon' : 'morning';
</%perl>
Good <% $greeting %>, <% $name %>!
% }
ENDSCRIPT
my $res_hello = <<'ENDSCRIPT';
Good afternoon, World!
ENDSCRIPT
is execute($scr_hello, name => 'World', hour => 13), $res_hello;
is compile($scr_hello)->(name => 'World', hour => 13), $res_hello;
like execute($scr_hello, name => 'World'), qr/Good (afternoon|morning), World!/;
is eval { execute($scr_hello, hour => 13); 1 }, undef;
ok $@;
is eval { execute($scr_hello); 1 }, undef;
ok $@;
}
######################################################################
SIMPLE_INIT_BLOCK: {
my $scr_hello = <<'ENDSCRIPT';
I'm sorry <% $name %>, I'm afraid I can't do that right now.
<%init>
my $name = 'Dave';
</%init>
ENDSCRIPT
my $res_hello = <<'ENDSCRIPT';
I'm sorry Dave, I'm afraid I can't do that right now.
ENDSCRIPT
is execute($scr_hello), $res_hello;
is compile($scr_hello)->(), $res_hello;
}
######################################################################
SIMPLE_ONCE_BLOCK: {
my $scr_hello = <<'ENDSCRIPT';
I'm sorry <% $name %>, I'm afraid I can't do that right now.
<%once>
my $name = 'Dave';
</%once>
ENDSCRIPT
my $res_hello = <<'ENDSCRIPT';
I'm sorry Dave, I'm afraid I can't do that right now.
ENDSCRIPT
is execute($scr_hello), $res_hello;
is compile($scr_hello)->(), $res_hello;
}
######################################################################
ONCE_AND_INIT_BLOCKS: {
my $scr_count = <<'ENDSCRIPT';
The count is now <% $count %>.
<%once>
my $count = 100;
</%once>
<%init>
$count ++;
</%init>
ENDSCRIPT
is execute($scr_count), "The count is now 101.\n";
is compile($scr_count)->(), "The count is now 101.\n";
ok my $sub_count = compile($scr_count);
is $sub_count->(), "The count is now 101.\n";
is $sub_count->(), "The count is now 102.\n";
is $sub_count->(), "The count is now 103.\n";
}
|