summaryrefslogtreecommitdiffstats
path: root/t/mustache-spec.t
blob: ae69af59f43552276dec1b23fb9ae97dc1c57e50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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;

        render_ok(
            ($test->{partials}
                ? ({
                       suffix => '.mustache',
                       path   => [
                           map { +{ "$_.mustache" => $test->{partials}{$_} } }
                               keys %{ $test->{partials} }
                       ]
                   })
                : ()),
            $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;