File: 011_macro.t

package info (click to toggle)
libtext-xslate-perl 3.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,108 kB
  • sloc: perl: 19,756; ansic: 214; pascal: 182; makefile: 9; cs: 8
file content (148 lines) | stat: -rw-r--r-- 2,163 bytes parent folder | download | duplicates (5)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!perl -w

use strict;
use Test::More;

use Text::Xslate;

my $tx = Text::Xslate->new(
    verbose      => 2,
);

my @set = (
    [<<'T', {lang => 'Xslate'}, <<'X', 'empty block'],
    A
    : block foo -> { }
    B
T
    A
    B
X

    [<<'T', {lang => 'Xslate'}, <<'X', 'template with a block'],
    A
    : block foo -> {
        Hello, <: $lang :> world!
    : }
    B
T
    A
        Hello, Xslate world!
    B
X

    [<<'T', {lang => 'Xslate'}, <<'X'],
    A
    : block foo -> {
        <em>Hello, <: $lang :> world!</em>
    : }
    B
T
    A
        <em>Hello, Xslate world!</em>
    B
X

    [<<'T', {}, <<'X', 'template with bocks'],
    A
    : block foo -> {
        FOO
    : }
    B
    : block bar -> {
        BAR
    : }
    C
T
    A
        FOO
    B
        BAR
    C
X

    [<<'T', {}, <<'X', 'simplest macro'],
: macro foo -> {
    FOO
: }
: foo()
T
    FOO
X

    [<<'T', {}, <<'X'],
: macro foo -> {
    <em>FOO</em>
: }
: foo()
T
    <em>FOO</em>
X

    [<<'T', {x => "foo"}, <<'X', 'with an arg'],
: macro foo -> ($x) {
    FOO(<:$x:>)
: }
: foo(42)
T
    FOO(42)
X

    [<<'T', {}, <<'X', 'macro with args'],
: macro add -> $x, $y {
    [<: ($x + $y) :>]
: }
:add(10, 20) # 30
:add(11, 22) # 33
:add(15, 25) # 40
T
    [30]
    [33]
    [40]
X

    [<<'T', { VERSION => '1.012' }, <<'X', 'returns string'],
: macro signeture -> {
    This is foo version <:= $VERSION :>
: }
: "*" ~ signeture()
T
*    This is foo version 1.012
X
);

foreach my $d(@set) {
    my($in, $vars, $out, $msg) = @$d;
    is $tx->render_string($in, $vars), $out, $msg
        for 1 .. 2;
}

my $warn = '';
$tx = Text::Xslate->new(
    warn_handler => sub{ $warn .= "@_" },
);
my $out = eval {
    $tx->render_string(<<'T', {});
    : macro foo -> $arg {
        Hello <:= $arg :>, world!
    : }
    : foo()
T
};
is $out, '';
like $warn, qr/Wrong number of arguments for foo/, 'too few arguments';
is $@, '';

$out = eval {
    $tx->render_string(<<'T', {});
    : macro foo -> $arg {
        Hello <:= $arg :>, world!
    : }
    : foo(1, 2)
T
};
is $out, '';
like $warn, qr/Wrong number of arguments for foo/, 'too many arguments';
is $@, '';

done_testing;