From 28cf6bf29b4d0e88f8fcd2ded52d9bca03149df9 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 19 Oct 2012 11:26:58 -0500 Subject: add test for loading just the syntax on its own --- t/lib/Test/Handlebars.pm | 5 +++- t/mustache-spec-syntax-only.t | 63 +++++++++++++++++++++++++++++++++++++++++++ t/mustache-spec.t | 17 ++++++------ 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 t/mustache-spec-syntax-only.t (limited to 't') diff --git a/t/lib/Test/Handlebars.pm b/t/lib/Test/Handlebars.pm index ececc41..c0f0da3 100644 --- a/t/lib/Test/Handlebars.pm +++ b/t/lib/Test/Handlebars.pm @@ -36,8 +36,11 @@ sub _render_ok { my ($template, $env, $expected, $desc) = @_; $opts->{cache} = 0; + my $create = delete $opts->{__create} || sub { + Text::Handlebars->new(%{ $_[0] }); + }; - my $tx = Text::Handlebars->new(%$opts); + my $tx = $create->($opts); my $exception = exception { local $Test::Builder::Level = $Test::Builder::Level + 5; diff --git a/t/mustache-spec-syntax-only.t b/t/mustache-spec-syntax-only.t new file mode 100644 index 0000000..d636e6e --- /dev/null +++ b/t/mustache-spec-syntax-only.t @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use lib 't/lib'; +use Test::More; +use Test::Handlebars; + +use Test::Requires 'JSON', 'Path::Class'; + +for my $file (dir('t', 'mustache-spec', 'specs')->children) { + next unless $file =~ /\.json$/; + my $tests = decode_json($file->slurp); + note("running " . $file->basename . " tests"); + for my $test (@{ $tests->{tests} }) { + local $TODO = "unimplemented" + if $file->basename eq 'partials.json' + && $test->{name} =~ /standalone/i + && $test->{name} !~ /line endings/i; + + my $opts = { + suffix => '.mustache', + path => [ + map { +{ "$_.mustache" => $test->{partials}{$_} } } + keys %{ $test->{partials} } + ], + __create => sub { + Text::Xslate->new( + syntax => 'Handlebars', + compiler => 'Text::Handlebars::Compiler', # XXX + %{ $_[0] }, + ); + }, + }; + render_ok( + $opts, + $test->{template}, + fix_data($test->{data}), + $test->{expected}, + "$test->{name}: $test->{desc}" + ); + } +} + +sub fix_data { + my ($data) = @_; + + if (ref($data) eq 'HASH') { + if ($data->{__tag__} && $data->{__tag__} eq 'code') { + return eval $data->{perl}; + } + else { + return { map { $_ => fix_data($data->{$_}) } keys %$data }; + } + } + elsif (ref($data) eq 'ARRAY') { + return [ map { fix_data($_) } @$data ]; + } + else { + return $data; + } +} + +done_testing; diff --git a/t/mustache-spec.t b/t/mustache-spec.t index ae69af5..abf51b7 100644 --- a/t/mustache-spec.t +++ b/t/mustache-spec.t @@ -17,16 +17,15 @@ for my $file (dir('t', 'mustache-spec', 'specs')->children) { && $test->{name} =~ /standalone/i && $test->{name} !~ /line endings/i; + my $opts = { + suffix => '.mustache', + path => [ + map { +{ "$_.mustache" => $test->{partials}{$_} } } + keys %{ $test->{partials} } + ], + }; render_ok( - ($test->{partials} - ? ({ - suffix => '.mustache', - path => [ - map { +{ "$_.mustache" => $test->{partials}{$_} } } - keys %{ $test->{partials} } - ] - }) - : ()), + $opts, $test->{template}, fix_data($test->{data}), $test->{expected}, -- cgit v1.2.3