summaryrefslogtreecommitdiffstats
path: root/t/helpers.t
blob: 51ab9ac51be8c863f936f68cfeaf2553ee1e668f (plain) (blame)
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
#!/usr/bin/env perl
use strict;
use warnings;
use lib 't/lib';
use Test::More;
use Test::Handlebars;

use Text::Xslate 'mark_raw';

render_ok(
    {
        helpers => {
            link => sub {
                my ($context, $object) = @_;
                return mark_raw(
                    "<a href='" . $object->{url} . "'>"
                  . $object->{text}
                  . "</a>"
                );
            },
        },
    },
    <<'TEMPLATE',
{{{link story}}}
TEMPLATE
    {
        story => {
            url  => 'http://example.com/',
            text => "<h1>It's an example!</h1>",
        },
    },
    <<'RENDERED',
<a href='http://example.com/'><h1>It's an example!</h1></a>
RENDERED
    "basic helpers"
);

render_ok(
    {
        helpers => {
            link => sub {
                my ($context, $text, $url) = @_;
                return mark_raw(
                    "<a href='" . $url . "'>" . $text . "</a>"
                );
            },
        },
    },
    <<'TEMPLATE',
{{{link "See more..." story.url}}}
TEMPLATE
    {
        story => {
            url  => 'http://example.com/',
        },
    },
    <<'RENDERED',
<a href='http://example.com/'>See more...</a>
RENDERED
    "helpers with literal args"
);

{ local $TODO = "unimplemented";
render_ok(
    {
        helpers => {
            link => sub {
                my ($context, $text, $options) = @_;

                my @attrs;
                for my $key (sort keys %$options) {
                    push @attrs, $key . '="' . $options->{$key} . '"';
                }

                return mark_raw(
                    "<a " . join(' ', @attrs) . ">" . $text . "</a>"
                );
            },
        },
    },
    <<'TEMPLATE',
{{{link "See more..." href=story.url class="story"}}}
TEMPLATE
    {
        story => {
            url  => 'http://example.com/',
        },
    },
    <<'RENDERED',
<a class="story" href="http://example.com/">See more...</a>
RENDERED
    "helpers with literal args"
);
}

done_testing;