From d678d91d7a48f8fa01aec2f0bbf86c97a214468d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 4 Oct 2012 17:22:27 -0500 Subject: add block helper tests --- t/helpers.t | 352 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 341 insertions(+), 11 deletions(-) (limited to 't') diff --git a/t/helpers.t b/t/helpers.t index 439f191..3cc418d 100644 --- a/t/helpers.t +++ b/t/helpers.t @@ -14,12 +14,128 @@ render_ok( }, }, }, - '

{{title}}

{{#noop}}{{body}}{{/noop}}

', + <<'TEMPLATE', +
+

{{title}}

+
+ {{#noop}}{{body}}{{/noop}} +
+
+TEMPLATE { title => 'A', body => 'the first letter' }, - '

A

the first letter

', + <<'RENDERED', +
+

A

+
+ the first letter +
+
+RENDERED "noop helper" ); +render_ok( + { + helpers => { + with => sub { + my ($context, $new_context, $options) = @_; + return $options->{fn}->($new_context); + }, + }, + }, + <<'TEMPLATE', +
+

{{title}}

+ {{#with story}} +
{{{intro}}}
+
{{{body}}}
+ {{/with}} +
+TEMPLATE + { + title => 'First Post', + story => { + intro => 'Before the jump', + body => 'After the jump', + }, + }, + <<'RENDERED', +
+

First Post

+
Before the jump
+
After the jump
+
+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', +
+

{{title}}

+ {{#with story}} +
{{{intro}}}
+
{{{body}}}
+ {{/with}} +
+
+ {{#each comments}} +
+

{{subject}}

+ {{{body}}} +
+ {{/each}} +
+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', +
+

First Post

+
Before the jump
+
After the jump
+
+
+
+

Subject A

+ Body A +
+
+

Subject B

+ Body B +
+
+RENDERED + "each helper" +); + render_ok( { helpers => { @@ -31,18 +147,232 @@ render_ok( $out .= "
  • " . $options->{fn}->($item) . "
  • "; } - return $out . ""; + return $out . "\n"; }, }, }, - '{{#list people}}{{firstName}} {{lastName}}{{/list}}', - { people => [ - { firstName => 'Jesse', lastName => 'Luehrs' }, - { firstName => 'Shawn', lastName => 'Moore' }, - { firstName => 'Stevan', lastName => 'Little' }, - ] }, - '', - "helpers with arguments" + <<'TEMPLATE', +{{#list nav}} + {{title}} +{{/list}} +TEMPLATE + { + nav => [ + { + url => 'http://www.yehudakatz.com', + title => 'Katz Got Your Tongue', + }, + { + url => 'http://www.sproutcore.com/block', + title => 'SproutCore Blog', + }, + ], + }, + <<'RENDERED', + +RENDERED + "list helper" +); + +render_ok( + { + helpers => { + if => sub { + my ($context, $conditional, $options) = @_; + if ($conditional) { + return $options->{fn}->($context); + } + return ''; + }, + }, + }, + <<'TEMPLATE', +{{#if isActive}} + Active +{{/if}} +TEMPLATE + { + isActive => 1, + }, + <<'RENDERED', + Active +RENDERED + "if helper (true)" +); + +render_ok( + { + helpers => { + if => sub { + my ($context, $conditional, $options) = @_; + if ($conditional) { + return $options->{fn}->($context); + } + return ''; + }, + }, + }, + <<'TEMPLATE', +{{#if isActive}} + 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}} + Active +{{else}} + Inactive +{{/if}} +TEMPLATE + { + isActive => 1, + }, + <<'RENDERED', + 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}} + Active +{{else}} + Inactive +{{/if}} +TEMPLATE + { + isActive => 0, + }, + <<'RENDERED', + 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 ""; + }, + }, + }, + <<'TEMPLATE', +{{list nav id="nav-bar" class="top"}} + {{title}} +{{/list}} +TEMPLATE + { + nav => [ + { + url => 'http://www.yehudakatz.com', + title => 'Katz Got Your Tongue', + }, + { + url => 'http://www.sproutcore.com/block', + title => 'SproutCore Blog', + }, + ], + }, + <<'RENDERED', + +RENDERED + "helper arguments" +); + +render_ok( + { + helpers => { + list => sub { + my ($context, $items, $options) = @_; + + my $out = ""; + return $out; + }, + }, + }, + <<'TEMPLATE', +{{#list array}} + {{@index}}. {{title}} +{{/list}} +TEMPLATE + { + array => [ + "Foo", + "Bar", + "Baz", + ], + }, + <<'RENDERED', + +RENDERED + "helper private variables" ); +} done_testing; -- cgit v1.2.3-54-g00ecf