summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-10-02 15:11:59 -0500
committerJesse Luehrs <doy@tozt.net>2012-10-02 15:31:20 -0500
commitf8163f38482ebe4a48b08a3f49080821e62e383b (patch)
tree83398b2cd786e538487d13e4d50a547d91b0bbbf /t
parent5dc5000f75492b6f78d27fab3c56055d4534b5a7 (diff)
downloadtext-handlebars-f8163f38482ebe4a48b08a3f49080821e62e383b.tar.gz
text-handlebars-f8163f38482ebe4a48b08a3f49080821e62e383b.zip
refactor tests
Diffstat (limited to 't')
-rw-r--r--t/basic.t15
-rw-r--r--t/blocks.t65
-rw-r--r--t/expressions.t33
-rw-r--r--t/lib/Test/Handlebars.pm57
-rw-r--r--t/mustache-extra.t58
-rw-r--r--t/mustache.t36
-rw-r--r--t/safestring.t58
7 files changed, 148 insertions, 174 deletions
diff --git a/t/basic.t b/t/basic.t
index 28d54c1..361864d 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -1,18 +1,15 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use lib 't/lib';
use Test::More;
+use Test::Handlebars;
-use Text::Handlebars;
-
-my $tx = Text::Handlebars->new;
-
-is(
- $tx->render_string(
- 'Hello, {{dialect}} world!',
- { dialect => 'Handlebars' },
- ),
+render_ok(
+ 'Hello, {{dialect}} world!',
+ { dialect => 'Handlebars' },
'Hello, Handlebars world!',
+ "basic test"
);
done_testing;
diff --git a/t/blocks.t b/t/blocks.t
index bbc22b9..7cc68fe 100644
--- a/t/blocks.t
+++ b/t/blocks.t
@@ -1,57 +1,50 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use lib 't/lib';
use Test::More;
+use Test::Handlebars;
-use Text::Handlebars;
-
-my $tx = Text::Handlebars->new;
-
-is(
- $tx->render_string(
- 'This is {{#shown}}shown{{/shown}}',
- { shown => 1 },
- ),
+render_ok(
+ 'This is {{#shown}}shown{{/shown}}',
+ { shown => 1 },
'This is shown',
+ "true block variable"
);
-is(
- $tx->render_string(
- 'This is {{#shown}}shown{{/shown}}',
- { shown => 0 },
- ),
+render_ok(
+ 'This is {{#shown}}shown{{/shown}}',
+ { shown => 0 },
'This is ',
+ "false block variable"
);
-is(
- $tx->render_string(
- 'This is {{#shown}}shown{{/shown}}',
- { shown => [({}) x 3] },
- ),
+render_ok(
+ 'This is {{#shown}}shown{{/shown}}',
+ { shown => [({}) x 3] },
'This is shownshownshown',
+ "array block variable"
);
-is(
- $tx->render_string(
- 'This is {{#shown}}{{content}}{{/shown}}',
- { shown => { content => 'SHOWN' } },
- ),
+render_ok(
+ 'This is {{#shown}}{{content}}{{/shown}}',
+ { shown => { content => 'SHOWN' } },
'This is SHOWN',
+ "nested hash block variable"
);
-is(
- $tx->render_string(
- 'This is {{#shown}}{{content}}{{/shown}}',
- {
- shown => [
- { content => '3' },
- { content => '2' },
- { content => '1' },
- { content => 'Shown' },
- ],
- },
- ),
+render_ok(
+ 'This is {{#shown}}{{content}}{{/shown}}',
+ {
+ shown => [
+ { content => '3' },
+ { content => '2' },
+ { content => '1' },
+ { content => 'Shown' },
+ ],
+ },
'This is 321Shown',
+ "nested array of hashes block variable"
);
done_testing;
diff --git a/t/expressions.t b/t/expressions.t
index 730196f..0a3ebf3 100644
--- a/t/expressions.t
+++ b/t/expressions.t
@@ -1,34 +1,29 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use lib 't/lib';
use Test::More;
+use Test::Handlebars;
-use Text::Handlebars;
-
-my $tx = Text::Handlebars->new;
-
-is(
- $tx->render_string(
- '<h1>{{title}}</h1>',
- { title => 'Xslate rocks' },
- ),
+render_ok(
+ '<h1>{{title}}</h1>',
+ { title => 'Xslate rocks' },
'<h1>Xslate rocks</h1>',
+ "basic variables"
);
-is(
- $tx->render_string(
- '<h1>{{article.title}}</h1>',
- { article => { title => 'Hash references rock' } },
- ),
+render_ok(
+ '<h1>{{article.title}}</h1>',
+ { article => { title => 'Hash references rock' } },
'<h1>Hash references rock</h1>',
+ ". separator"
);
-is(
- $tx->render_string(
- '<h1>{{article/title}}</h1>',
- { article => { title => 'Deprecated syntax does not' } },
- ),
+render_ok(
+ '<h1>{{article/title}}</h1>',
+ { article => { title => 'Deprecated syntax does not' } },
'<h1>Deprecated syntax does not</h1>',
+ "/ separator"
);
done_testing;
diff --git a/t/lib/Test/Handlebars.pm b/t/lib/Test/Handlebars.pm
new file mode 100644
index 0000000..25b22d9
--- /dev/null
+++ b/t/lib/Test/Handlebars.pm
@@ -0,0 +1,57 @@
+package Test::Handlebars;
+use strict;
+use warnings;
+
+use Test::Builder;
+use Test::Fatal;
+use Text::Handlebars;
+
+use Sub::Exporter -setup => {
+ exports => [
+ qw(render_ok render_file_ok)
+ ],
+ groups => {
+ default => [
+ qw(render_ok render_file_ok)
+ ],
+ },
+};
+
+my $Test = Test::Builder->new;
+
+sub render_ok {
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ return _render_ok('render_string', @_);
+}
+
+sub render_file_ok {
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ return _render_ok('render', @_);
+}
+
+sub _render_ok {
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ my $render_method = shift;
+ my $opts = ref($_[0]) && ref($_[0]) eq 'HASH' ? shift : {};
+ my ($template, $env, $expected, $desc) = @_;
+
+ my $tx = Text::Handlebars->new(%$opts);
+
+ my $exception = exception {
+ local $Test::Builder::Level = $Test::Builder::Level + 5;
+ $Test->is_eq($tx->$render_method($template, $env), $expected, $desc);
+ };
+ $Test->ok(0, "$desc (threw an exception)") if $exception;
+ {
+ no strict 'refs';
+ local ${ caller(1) . '::TODO' } = undef unless $exception;
+ use strict;
+ $Test->is_eq(
+ $exception,
+ undef,
+ "no exceptions for $desc"
+ );
+ }
+}
+
+1;
diff --git a/t/mustache-extra.t b/t/mustache-extra.t
deleted file mode 100644
index ac28498..0000000
--- a/t/mustache-extra.t
+++ /dev/null
@@ -1,58 +0,0 @@
-#!/usr/bin/env perl
-use strict;
-use warnings;
-use Test::More;
-use Test::Fatal;
-
-use Text::Handlebars;
-
-render_ok(
- <<'TEMPLATE',
-* {{name}}
-* {{age}}
-* {{company}}
-* {{& company}}
-TEMPLATE
- {
- name => 'Chris',
- company => '<b>GitHub</b>',
- },
- <<'RENDERED',
-* Chris
-*
-* &lt;b&gt;GitHub&lt;/b&gt;
-* <b>GitHub</b>
-RENDERED
- "& for make_raw"
-);
-
-sub render_ok {
- local $Test::Builder::Level = $Test::Builder::Level + 1;
- return _render_ok('render_string', @_);
-}
-
-sub render_file_ok {
- local $Test::Builder::Level = $Test::Builder::Level + 1;
- return _render_ok('render', @_);
-}
-
-sub _render_ok {
- my $render_method = shift;
- my $opts = ref($_[0]) && ref($_[0]) eq 'HASH' ? shift : {};
- my ($template, $env, $expected, $desc) = @_;
- local $Test::Builder::Level = $Test::Builder::Level + 1;
-
- my $tx = Text::Handlebars->new(%$opts);
-
- my $exception = exception {
- is($tx->$render_method($template, $env), $expected, $desc);
- };
- fail("$desc (threw an exception)") if $exception;
- is(
- $exception,
- undef,
- "no exceptions for $desc"
- );
-}
-
-done_testing;
diff --git a/t/mustache.t b/t/mustache.t
index e12f4ef..d1b98d2 100644
--- a/t/mustache.t
+++ b/t/mustache.t
@@ -1,10 +1,9 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use lib 't/lib';
use Test::More;
-use Test::Fatal;
-
-use Text::Handlebars;
+use Test::Handlebars;
# from the mustache(5) man page
# http://mustache.github.com/mustache.5.html
@@ -200,35 +199,4 @@ RENDERED
);
}
-sub render_ok {
- local $Test::Builder::Level = $Test::Builder::Level + 1;
- return _render_ok('render_string', @_);
-}
-
-sub render_file_ok {
- local $Test::Builder::Level = $Test::Builder::Level + 1;
- return _render_ok('render', @_);
-}
-
-sub _render_ok {
- my $render_method = shift;
- my $opts = ref($_[0]) && ref($_[0]) eq 'HASH' ? shift : {};
- my ($template, $env, $expected, $desc) = @_;
- local $Test::Builder::Level = $Test::Builder::Level + 1;
-
- my $tx = Text::Handlebars->new(%$opts);
-
- my $exception = exception {
- local $Test::Builder::Level = $Test::Builder::Level + 5;
- is($tx->$render_method($template, $env), $expected, $desc);
- };
- fail("$desc (threw an exception)") if $exception;
- local $TODO = undef unless $exception;
- is(
- $exception,
- undef,
- "no exceptions for $desc"
- );
-}
-
done_testing;
diff --git a/t/safestring.t b/t/safestring.t
index 27d9ac1..29ec4ec 100644
--- a/t/safestring.t
+++ b/t/safestring.t
@@ -1,35 +1,57 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use lib 't/lib';
use Test::More;
+use Test::Handlebars;
-use Text::Handlebars;
use Text::Xslate 'mark_raw';
-my $tx = Text::Handlebars->new;
-
-is(
- $tx->render_string(
- '<h1>{{title}}</h1><p>{{{body}}}</p>',
- { title => 'My New Post', body => 'This is my first post!' },
- ),
+render_ok(
+ '<h1>{{title}}</h1><p>{{{body}}}</p>',
+ { title => 'My New Post', body => 'This is my first post!' },
'<h1>My New Post</h1><p>This is my first post!</p>',
+ "raw body",
);
-is(
- $tx->render_string(
- '<h1>{{title}}</h1><p>{{{body}}}</p>',
- { title => 'All About <p> Tags', body => '<i>This is a post about &lt;p&gt; tags</i>' },
- ),
+render_ok(
+ '<h1>{{title}}</h1><p>{{{body}}}</p>',
+ {
+ title => 'All About <p> Tags',
+ body => '<i>This is a post about &lt;p&gt; tags</i>'
+ },
'<h1>All About &lt;p&gt; Tags</h1><p><i>This is a post about &lt;p&gt; tags</i></p>',
+ "raw body with html"
);
-is(
- $tx->render_string(
- '<h1>{{title}}</h1><p>{{{body}}}</p>',
- { title => mark_raw('All About &lt;p&gt; Tags'), body => '<i>This is a post about &lt;p&gt; tags</i>' },
- ),
+render_ok(
+ '<h1>{{title}}</h1><p>{{{body}}}</p>',
+ {
+ title => mark_raw('All About &lt;p&gt; Tags'),
+ body => '<i>This is a post about &lt;p&gt; tags</i>'
+ },
'<h1>All About &lt;p&gt; Tags</h1><p><i>This is a post about &lt;p&gt; tags</i></p>',
+ "raw title with manual mark_raw() call"
+);
+
+render_ok(
+ <<'TEMPLATE',
+* {{name}}
+* {{age}}
+* {{company}}
+* {{& company}}
+TEMPLATE
+ {
+ name => 'Chris',
+ company => '<b>GitHub</b>',
+ },
+ <<'RENDERED',
+* Chris
+*
+* &lt;b&gt;GitHub&lt;/b&gt;
+* <b>GitHub</b>
+RENDERED
+ "mark_raw via &"
);
done_testing;