diff options
-rw-r--r-- | t/helpers-examples.t | 97 | ||||
-rw-r--r-- | t/helpers.t | 96 |
2 files changed, 193 insertions, 0 deletions
diff --git a/t/helpers-examples.t b/t/helpers-examples.t new file mode 100644 index 0000000..73c053a --- /dev/null +++ b/t/helpers-examples.t @@ -0,0 +1,97 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use lib 't/lib'; +use Test::More; +use Test::Handlebars; + +use Text::Xslate 'mark_raw'; + +{ local $TODO = "unimplemented"; +render_ok( + { + function => { + fullName => sub { + my ($context, $person) = @_; + return $person->{firstName} . ' ' . $person->{lastName}; + }, + }, + }, + <<'TEMPLATE', +<div class="post"> + <h1>By {{fullName author}}</h1> + <div class="body">{{body}}</div> + + <h1>Comments</h1> + + {{#each comments}} + <h2>By {{fullName author}}</h2> + <div class="body">{{body}}</div> + {{/each}} +</div> +TEMPLATE + { + author => { firstName => "Alan", lastName => "Johnson" }, + body => "I Love Handlebars", + comments => [ + { + author => { firstName => "Yehuda", lastName => "Katz" }, + body => "Me too!" + }, + ], + }, + <<'RENDERED', +<div class="post"> + <h1>By Alan Johnson</h1> + <div class="body">I Love Handlebars</div> + + <h1>Comments</h1> + + <h2>By Yehuda Katz</h2> + <div class="body">Me too!</div> +</div> +RENDERED + "example" +); + +render_ok( + { + function => { + agree_button => sub { + my ($context) = @_; + return mark_raw( + "<button>I agree. I " + . $context->{emotion} + . ' ' + . $context->{name} + . "</button>" + ); + }, + }, + }, + <<'TEMPLATE', +<ul> + {{#each items}} + <li>{{agree_button}}</li> + {{/each}} +</ul> +TEMPLATE + { + items => [ + { name => "Handlebars", emotion => "love" }, + { name => "Mustache", emotion => "enjoy" }, + { name => "Ember", emotion => "want to learn" }, + ], + }, + <<'RENDERED', +<ul> + <li><button>I agree. I love Handlebars</button></li> + <li><button>I agree. I enjoy Mustache</button></li> + <li><button>I agree. I want to learn Ember</button></li> +</ul> +RENDERED + "example" +); +} + +done_testing; diff --git a/t/helpers.t b/t/helpers.t new file mode 100644 index 0000000..52b7e09 --- /dev/null +++ b/t/helpers.t @@ -0,0 +1,96 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use lib 't/lib'; +use Test::More; +use Test::Handlebars; + +use Text::Xslate 'mark_raw'; + +{ local $TODO = "unimplemented"; +render_ok( + { + function => { + 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( + { + function => { + 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" +); + +render_ok( + { + function => { + 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; |