summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-10-04 17:22:27 -0500
committerJesse Luehrs <doy@tozt.net>2012-10-04 17:22:27 -0500
commitd678d91d7a48f8fa01aec2f0bbf86c97a214468d (patch)
treeda02fec938a131fffc56ef9e6d895621c6133c1e /t
parent2d0480d6ff96b3a89400fc6db137a875ebd095ee (diff)
downloadtext-handlebars-d678d91d7a48f8fa01aec2f0bbf86c97a214468d.tar.gz
text-handlebars-d678d91d7a48f8fa01aec2f0bbf86c97a214468d.zip
add block helper tests
Diffstat (limited to 't')
-rw-r--r--t/helpers.t352
1 files changed, 341 insertions, 11 deletions
diff --git a/t/helpers.t b/t/helpers.t
index 439f191..3cc418d 100644
--- a/t/helpers.t
+++ b/t/helpers.t
@@ -14,15 +14,131 @@ render_ok(
},
},
},
- '<h1>{{title}}</h1><p>{{#noop}}{{body}}{{/noop}}</p>',
+ <<'TEMPLATE',
+<div class="entry">
+ <h1>{{title}}</h1>
+ <div class="body">
+ {{#noop}}{{body}}{{/noop}}
+ </div>
+</div>
+TEMPLATE
{ title => 'A', body => 'the first letter' },
- '<h1>A</h1><p>the first letter</p>',
+ <<'RENDERED',
+<div class="entry">
+ <h1>A</h1>
+ <div class="body">
+ the first letter
+ </div>
+</div>
+RENDERED
"noop helper"
);
render_ok(
{
helpers => {
+ with => sub {
+ my ($context, $new_context, $options) = @_;
+ return $options->{fn}->($new_context);
+ },
+ },
+ },
+ <<'TEMPLATE',
+<div class="entry">
+ <h1>{{title}}</h1>
+ {{#with story}}
+ <div class="intro">{{{intro}}}</div>
+ <div class="body">{{{body}}}</div>
+ {{/with}}
+</div>
+TEMPLATE
+ {
+ title => 'First Post',
+ story => {
+ intro => 'Before the jump',
+ body => 'After the jump',
+ },
+ },
+ <<'RENDERED',
+<div class="entry">
+ <h1>First Post</h1>
+ <div class="intro">Before the jump</div>
+ <div class="body">After the jump</div>
+</div>
+RENDERED
+ "with helper"
+);
+
+render_ok(
+ {
+ helpers => {
+ with => sub {
+ my ($context, $new_context, $options) = @_;
+ return $options->{fn}->($new_context);
+ },
+ each => sub {
+ my ($context, $list, $options) = @_;
+
+ my $ret = '';
+ for my $new_context (@$list) {
+ $ret .= $options->{fn}->($new_context);
+ }
+
+ return $ret;
+ },
+ },
+ },
+ <<'TEMPLATE',
+<div class="entry">
+ <h1>{{title}}</h1>
+ {{#with story}}
+ <div class="intro">{{{intro}}}</div>
+ <div class="body">{{{body}}}</div>
+ {{/with}}
+</div>
+<div class="comments">
+ {{#each comments}}
+ <div class="comment">
+ <h2>{{subject}}</h2>
+ {{{body}}}
+ </div>
+ {{/each}}
+</div>
+TEMPLATE
+ {
+ title => 'First Post',
+ story => {
+ intro => 'Before the jump',
+ body => 'After the jump',
+ },
+ comments => [
+ { subject => "Subject A", body => "Body A" },
+ { subject => "Subject B", body => "Body B" },
+ ],
+ },
+ <<'RENDERED',
+<div class="entry">
+ <h1>First Post</h1>
+ <div class="intro">Before the jump</div>
+ <div class="body">After the jump</div>
+</div>
+<div class="comments">
+ <div class="comment">
+ <h2>Subject A</h2>
+ Body A
+ </div>
+ <div class="comment">
+ <h2>Subject B</h2>
+ Body B
+ </div>
+</div>
+RENDERED
+ "each helper"
+);
+
+render_ok(
+ {
+ helpers => {
list => sub {
my ($context, $items, $options) = @_;
my $out = "<ul>";
@@ -31,18 +147,232 @@ render_ok(
$out .= "<li>" . $options->{fn}->($item) . "</li>";
}
- return $out . "</ul>";
+ return $out . "</ul>\n";
},
},
},
- '{{#list people}}{{firstName}} {{lastName}}{{/list}}',
- { people => [
- { firstName => 'Jesse', lastName => 'Luehrs' },
- { firstName => 'Shawn', lastName => 'Moore' },
- { firstName => 'Stevan', lastName => 'Little' },
- ] },
- '<ul><li>Jesse Luehrs</li><li>Shawn Moore</li><li>Stevan Little</li></ul>',
- "helpers with arguments"
+ <<'TEMPLATE',
+{{#list nav}}
+ <a href="{{url}}">{{title}}</a>
+{{/list}}
+TEMPLATE
+ {
+ nav => [
+ {
+ url => 'http://www.yehudakatz.com',
+ title => 'Katz Got Your Tongue',
+ },
+ {
+ url => 'http://www.sproutcore.com/block',
+ title => 'SproutCore Blog',
+ },
+ ],
+ },
+ <<'RENDERED',
+<ul><li> <a href="http://www.yehudakatz.com">Katz Got Your Tongue</a>
+</li><li> <a href="http://www.sproutcore.com/block">SproutCore Blog</a>
+</li></ul>
+RENDERED
+ "list helper"
+);
+
+render_ok(
+ {
+ helpers => {
+ if => sub {
+ my ($context, $conditional, $options) = @_;
+ if ($conditional) {
+ return $options->{fn}->($context);
+ }
+ return '';
+ },
+ },
+ },
+ <<'TEMPLATE',
+{{#if isActive}}
+ <img src="star.gif" alt="Active">
+{{/if}}
+TEMPLATE
+ {
+ isActive => 1,
+ },
+ <<'RENDERED',
+ <img src="star.gif" alt="Active">
+RENDERED
+ "if helper (true)"
+);
+
+render_ok(
+ {
+ helpers => {
+ if => sub {
+ my ($context, $conditional, $options) = @_;
+ if ($conditional) {
+ return $options->{fn}->($context);
+ }
+ return '';
+ },
+ },
+ },
+ <<'TEMPLATE',
+{{#if isActive}}
+ <img src="star.gif" alt="Active">
+{{/if}}
+TEMPLATE
+ {
+ isActive => 0,
+ },
+ <<'RENDERED',
+RENDERED
+ "if helper (false)"
+);
+
+{ local $TODO = "unimplemented"; local $SIG{__WARN__} = sub { };
+render_ok(
+ {
+ helpers => {
+ if => sub {
+ my ($context, $conditional, $options) = @_;
+ if ($conditional) {
+ return $options->{fn}->($context);
+ }
+ else {
+ return $options->{inverse}->($context);
+ }
+ },
+ },
+ },
+ <<'TEMPLATE',
+{{#if isActive}}
+ <img src="star.gif" alt="Active">
+{{else}}
+ <img src="cry.gif" alt="Inactive">
+{{/if}}
+TEMPLATE
+ {
+ isActive => 1,
+ },
+ <<'RENDERED',
+ <img src="star.gif" alt="Active">
+RENDERED
+ "if/else helper (true)"
+);
+
+render_ok(
+ {
+ helpers => {
+ if => sub {
+ my ($context, $conditional, $options) = @_;
+ if ($conditional) {
+ return $options->{fn}->($context);
+ }
+ else {
+ return $options->{inverse}->($context);
+ }
+ },
+ },
+ },
+ <<'TEMPLATE',
+{{#if isActive}}
+ <img src="star.gif" alt="Active">
+{{else}}
+ <img src="cry.gif" alt="Inactive">
+{{/if}}
+TEMPLATE
+ {
+ isActive => 0,
+ },
+ <<'RENDERED',
+ <img src="cry.gif" alt="Inactive">
+RENDERED
+ "if/else helper (false)"
+);
+
+render_ok(
+ {
+ helpers => {
+ list => sub {
+ my ($context, $items, $options) = @_;
+
+ my $attrs = join ' ', map { $_ => $options->{hash}{$_} }
+ sort keys %{ $options->{hash} };
+
+ return "<ul $attrs>"
+ . join("\n", map {
+ "<li>" . $options->{fn}->($_) . "</li>"
+ } @$items)
+ . "</ul>";
+ },
+ },
+ },
+ <<'TEMPLATE',
+{{list nav id="nav-bar" class="top"}}
+ <a href="{{url}}">{{title}}</a>
+{{/list}}
+TEMPLATE
+ {
+ nav => [
+ {
+ url => 'http://www.yehudakatz.com',
+ title => 'Katz Got Your Tongue',
+ },
+ {
+ url => 'http://www.sproutcore.com/block',
+ title => 'SproutCore Blog',
+ },
+ ],
+ },
+ <<'RENDERED',
+<ul class="top" id="nav-bar"><li> <a href="http://www.yehudakatz.com">Katz Got Your Tongue</a>
+</li>
+ <a href="http://www.sproutcore.com/block">SproutCore Blog</a>
+</li></ul>
+RENDERED
+ "helper arguments"
+);
+
+render_ok(
+ {
+ helpers => {
+ list => sub {
+ my ($context, $items, $options) = @_;
+
+ my $out = "<ul>";
+ for my $item (@$items) {
+ my $data;
+ if ($options->{data}) {
+ $data = $options->{create_frame}->($options->{data});
+ }
+ $out .= "<li>"
+ . $options->{fn}->($item, {data => $data})
+ . "</li>";
+ }
+
+ $out .= "</ul>";
+ return $out;
+ },
+ },
+ },
+ <<'TEMPLATE',
+{{#list array}}
+ {{@index}}. {{title}}
+{{/list}}
+TEMPLATE
+ {
+ array => [
+ "Foo",
+ "Bar",
+ "Baz",
+ ],
+ },
+ <<'RENDERED',
+<ul><li> 1. Foo
+</li><li> 2. Bar
+</li><li> 3. Baz
+</li></ul>
+RENDERED
+ "helper private variables"
);
+}
done_testing;