-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_funcs.t
65 lines (48 loc) · 1.56 KB
/
07_funcs.t
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
#!perl -w
use strict;
use Test::More;
use Text::ClearSilver;
use Carp ();
foreach (1 .. 2) {
note $_;
my $tcs = Text::ClearSilver->new();
$tcs->register_function( lc => sub { lc $_[0] }, 1);
$tcs->register_function( uc => sub { uc $_[0] }, 1);
$tcs->register_function( add => sub{ $_[0] + $_[1] }, 2);
$tcs->register_function( take_node => sub{
return $_[0]->obj_child->obj_value;
});
$tcs->register_function( croak => \&Carp::croak );
my $out = '';
$tcs->process(\'<?cs var:lc(foo) ?> <?cs var:uc(foo) ?>', { foo => '<FoO>' }, \$out);
is $out, '<foo> <FOO>', "register_function";
$out = '';
$tcs->process(\'<?cs var:add(#42, #2) ?>', {}, \$out);
is $out, 44;
$out = '';
$tcs->process(\'<?cs var:take_node(Foo) ?>', { Foo => { bar => 42 } }, \$out);
is $out, 42, 'take HDF node';
$out = '';
$tcs->process(\'<?cs var:lc(add(#10, #20)) ?>', {}, \$out);
is $out, 30, 'f(g(x))';
$out = '';
$tcs->process(\'<?cs var:lc(lc("FOO")) ?>', {}, \$out);
is $out, "foo", 'f(g(x))';
eval {
$out = '';
$tcs->process(\'<?cs var:croak(foo) ?>', { foo => "bar" }, \$out);
};
like $@, qr/\b bar \b/xms, "die in callback";
is $out, '';
eval {
$tcs->register_function(_ => sub {});
$tcs->process(\'', {}, \$out);
};
is $@, '', "_() is not registered";
eval {
$tcs->register_function(len => sub{});
$tcs->process(\'', {}, \$out);
};
like $@, qr/\b DuplicateError \b/xms, 'Cannot redefine builtins';
}
done_testing;