File: 03-args.t

package info (click to toggle)
libtext-micromason-perl 2.13-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 624 kB
  • ctags: 180
  • sloc: perl: 3,222; makefile: 23
file content (107 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download | duplicates (4)
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
105
106
107
#!/usr/bin/perl -w

use strict;
use Test::More tests => 23;

use_ok 'Text::MicroMason';
ok my $m = Text::MicroMason->new();

######################################################################

{
    my $scr_hello = "Hello <% shift(@_) %>!";
    my $res_hello = "Hello World!";

    is $m->execute( text => $scr_hello, 'World' ), $res_hello;
    is $m->compile( text => $scr_hello)->( 'World' ), $res_hello;
}

######################################################################

{
    my $scr_bold = '<b><% $ARGS{label} %></b>';
    is $m->execute( text => $scr_bold, label=>'Foo'), '<b>Foo</b>';
    is $m->compile( text => $scr_bold)->(label=>'Foo'), '<b>Foo</b>';
}

######################################################################

SIMPLE_ARGS: {
    my $scr_bold = '<%args>$label</%args><b><% $label %></b>';
    is $m->execute( text => $scr_bold, label=>'Foo'), '<b>Foo</b>';
    is $m->compile( text => $scr_bold)->(label=>'Foo'), '<b>Foo</b>';
    is eval { $m->execute( text => $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 $m->execute( text => $scr_hello, name => 'World', hour => 13), $res_hello;
    is $m->compile( text => $scr_hello)->(name => 'World', hour => 13), $res_hello;
    like $m->execute( text => $scr_hello, name => 'World'), qr/Good (afternoon|morning), World!/;
    is eval { $m->execute( text => $scr_hello, hour => 13); 1 }, undef;
    is eval { $m->execute( text => $scr_hello); 1 }, undef;
}

######################################################################

ARGS_BLOCK_WITH_DEFAULT_LIST: {
    my $scr_count = <<'ENDSCRIPT';
<%args>
 @data => ()
</%args>
Count: <% scalar @data %>
ENDSCRIPT

    my $res_count_0 = "Count: 0\n";
    my $res_count_1 = "Count: 1\n";
    my $res_count_2 = "Count: 2\n";

    is $m->execute( text => $scr_count ), $res_count_0;
    is $m->execute( text => $scr_count, data => [] ), $res_count_0;
    is $m->execute( text => $scr_count, data => [ 1 ] ), $res_count_1;
    is $m->execute( text => $scr_count, data => [ 1 .. 2 ] ), $res_count_2;
}

######################################################################

ARGS_BLOCK_WITH_DEFAULT_LIST: {
    my $scr_count = <<'ENDSCRIPT';
<%args>
 @data => ( 1 )
</%args>
Count: <% scalar @data %>
ENDSCRIPT

    my $res_count_0 = "Count: 0\n";
    my $res_count_1 = "Count: 1\n";
    my $res_count_2 = "Count: 2\n";

    is $m->execute( text => $scr_count ), $res_count_1;
    is $m->execute( text => $scr_count, data => [] ), $res_count_0;
    is $m->execute( text => $scr_count, data => [ 1 ] ), $res_count_1;
    is $m->execute( text => $scr_count, data => [ 1 .. 2 ] ), $res_count_2;
}

######################################################################