From 8f48b142b49caf1d1e49b2124a27433fecbf5cfa Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 4 Oct 2012 18:21:47 -0500 Subject: add tests for non-block helpers --- t/helpers-examples.t | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ t/helpers.t | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 t/helpers-examples.t create mode 100644 t/helpers.t (limited to 't') 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', +
+

By {{fullName author}}

+
{{body}}
+ +

Comments

+ + {{#each comments}} +

By {{fullName author}}

+
{{body}}
+ {{/each}} +
+TEMPLATE + { + author => { firstName => "Alan", lastName => "Johnson" }, + body => "I Love Handlebars", + comments => [ + { + author => { firstName => "Yehuda", lastName => "Katz" }, + body => "Me too!" + }, + ], + }, + <<'RENDERED', +
+

By Alan Johnson

+
I Love Handlebars
+ +

Comments

+ +

By Yehuda Katz

+
Me too!
+
+RENDERED + "example" +); + +render_ok( + { + function => { + agree_button => sub { + my ($context) = @_; + return mark_raw( + "" + ); + }, + }, + }, + <<'TEMPLATE', + +TEMPLATE + { + items => [ + { name => "Handlebars", emotion => "love" }, + { name => "Mustache", emotion => "enjoy" }, + { name => "Ember", emotion => "want to learn" }, + ], + }, + <<'RENDERED', + +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( + "" + . $object->{text} + . "" + ); + }, + }, + }, + <<'TEMPLATE', +{{{link story}}} +TEMPLATE + { + story => { + url => 'http://example.com/', + text => "

It's an example!

", + }, + }, + <<'RENDERED', +

It's an example!

+RENDERED + "basic helpers" +); + +render_ok( + { + function => { + link => sub { + my ($context, $text, $url) = @_; + return mark_raw( + "" . $text . "" + ); + }, + }, + }, + <<'TEMPLATE', +{{{link "See more..." story.url}}} +TEMPLATE + { + story => { + url => 'http://example.com/', + }, + }, + <<'RENDERED', +See more... +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( + "" . $text . "" + ); + }, + }, + }, + <<'TEMPLATE', +{{{link "See more..." href=story.url class="story"}}} +TEMPLATE + { + story => { + url => 'http://example.com/', + }, + }, + <<'RENDERED', +See more... +RENDERED + "helpers with literal args" +); +} + +done_testing; -- cgit v1.2.3