diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-10-02 15:11:59 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-10-02 15:31:20 -0500 |
commit | f8163f38482ebe4a48b08a3f49080821e62e383b (patch) | |
tree | 83398b2cd786e538487d13e4d50a547d91b0bbbf /t | |
parent | 5dc5000f75492b6f78d27fab3c56055d4534b5a7 (diff) | |
download | text-handlebars-f8163f38482ebe4a48b08a3f49080821e62e383b.tar.gz text-handlebars-f8163f38482ebe4a48b08a3f49080821e62e383b.zip |
refactor tests
Diffstat (limited to 't')
-rw-r--r-- | t/basic.t | 15 | ||||
-rw-r--r-- | t/blocks.t | 65 | ||||
-rw-r--r-- | t/expressions.t | 33 | ||||
-rw-r--r-- | t/lib/Test/Handlebars.pm | 57 | ||||
-rw-r--r-- | t/mustache-extra.t | 58 | ||||
-rw-r--r-- | t/mustache.t | 36 | ||||
-rw-r--r-- | t/safestring.t | 58 |
7 files changed, 148 insertions, 174 deletions
@@ -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; @@ -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 -* -* <b>GitHub</b> -* <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 <p> tags</i>' }, - ), +render_ok( + '<h1>{{title}}</h1><p>{{{body}}}</p>', + { + title => 'All About <p> Tags', + body => '<i>This is a post about <p> tags</i>' + }, '<h1>All About <p> Tags</h1><p><i>This is a post about <p> tags</i></p>', + "raw body with html" ); -is( - $tx->render_string( - '<h1>{{title}}</h1><p>{{{body}}}</p>', - { title => mark_raw('All About <p> Tags'), body => '<i>This is a post about <p> tags</i>' }, - ), +render_ok( + '<h1>{{title}}</h1><p>{{{body}}}</p>', + { + title => mark_raw('All About <p> Tags'), + body => '<i>This is a post about <p> tags</i>' + }, '<h1>All About <p> Tags</h1><p><i>This is a post about <p> 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 +* +* <b>GitHub</b> +* <b>GitHub</b> +RENDERED + "mark_raw via &" ); done_testing; |