File: spec_test.t

package info (click to toggle)
libtext-hogan-perl 2.01-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 240 kB
  • sloc: perl: 826; sh: 4; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download | duplicates (3)
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
58
59
60
61
62
63
64
65
66
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use YAML;
use Path::Tiny;
use Try::Tiny;
use Data::Visitor::Callback;

use Text::Hogan::Compiler;

my $data_fixer = Data::Visitor::Callback->new(
    hash => sub {
        my ($self, $rh) = @_;

        # Handle lambdas in the ~lambdas.yml spec file
        for my $k (keys %$rh) {
            if ($k eq 'perl') {
                $_ = eval $rh->{'perl'};
                return;
            }
        }

        # Handle true/false values
        for my $v (values %$rh) {
            if ($v eq 'true') { $v = 1 }
            elsif ($v eq 'false') { $v = 0 }
            elsif (ref($v) eq 'HASH') { $self->visit($v) }
        }
    }
);

my @spec_files = path("t", "specs")->children(qr/[.]yml$/);

for my $file (sort @spec_files) {
    my $yaml = $file->slurp_utf8;

    my $specs = YAML::Load($yaml);

    note "----- $file ", ("-" x (70-length($file)));

    for my $test (@{ $specs->{tests} }) {
        #
        # Handle true/false values
        # Handle lambdas in the ~lambdas.yml spec file
        #
        $data_fixer->visit($test->{data});

        my $parser = Text::Hogan::Compiler->new();

        my $rendered;
        try {
            $rendered = $parser->compile($test->{template})->render($test->{data}, $test->{partials});
        };
        is(
            $rendered,
            $test->{expected},
            "$test->{name} - $test->{desc}"
        );
    }
}

done_testing();