The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MojoX::CustomTemplateFileParser - Parses a custom Mojo template file format

SYNOPSIS

  use MojoX::CustomTemplateFileParser;

  my $content = MojoX::CustomTemplateFileParser->new(path => '/path/to/file.mojo')->parse->flatten;

  print $content;

STATUS

Unstable.

DESCRIPTION

MojoX::CustomTemplateFileParser parses files containing Mojo::Templates mixed with the expected rendering.

The parsing creates a data structure that also can be dumped into a string ready to be put in a Test::More file.

Its purpose is to facilitate development of tag helpers.

Options

path

The only argument given to the constructor is the path to the file that should be parsed.

Methods

$self->parse

Parses the file given in path. After parsing the structure is available in $self->structure.

$self->flatten

Returns a string that is suitable to put in a Test::More test file.

Example

Given a file (metacpan-1.mojo) that looks like this:

    # Code here

    ==test==
    --t--
        %= link_to 'MetaCPAN', 'http://www.metacpan.org/'
    --t--
    --e--
        <a href="http://www.metacpan.org/">MetaCPAN</a>
    --e--

    ==test==
    --t--
        %= text_field username => placeholder => 'Enter name'
    --t--
    --e--
        <input name="username" placeholder="Enter name" type="text" />
    --e--

    ==no test==
    --t--
        %= text_field username => placeholder => 'Not tested'
    --t--
    --e--
        <input name="username" placeholder="Not tested" type="text" />
    --e--

(Note the no test on the third test.)

Running $self->parse will fill $self->structure with:

    {
        head_lines => ['',
                       '# Code here',
                       '',
                       ''
                      ],
        tests => [
                    {
                        test_number => 1,
                        test_name => 'metacpan_1_1',
                        test_start_line => 4,
                        lines_before => [''],
                        lines_template => [ "%= link_to 'MetaCPAN', 'http://www.metacpan.org/" ],
                        lines_between => [''],
                        lines_expected => [ '<a href="http://www.metacpan.org/">MetaCPAN</a>' ],
                        lines_after => ['',''],
                    },
                    {
                       test_number => 2,
                       test_name => 'metacpan_1_2',
                       test_start_line => 12,
                       lines_before => [''],
                       lines_template => [ "%= text_field username => placeholder => 'Enter name'"" ],
                       lines_between => [''],
                       lines_expected => ['<input name="username" placeholder="Enter name" type="text" /> '],
                       lines_after => [],
                    }
                ],
        };

And $self->flatten returns:

    # Code here

    my $expected_1 = qq{ <a href="http://www.metacpan.org/">MetaCPAN</a> };

    get '/metacpan_1_1' => 'metacpan_1_1';

    $test->get_ok('/metacpan_1_1')->status_is(200)->trimmed_content_is($expected_1, 'Matched trimmed content in metacpan-1.mojo, line 4');

    my $expected_2 = qq{ <input name="username" placeholder="Enter name" type="text" /> };

    get '/metacpan_1_2' => 'metacpan_1_2';

    $test->get_ok('/metacpan_1_2')->status_is(200)->trimmed_content_is($expected_2, 'Matched trimmed content in metacpan-1.mojo, line 12');

    done_testing();

    __DATA__

    @@ metacpan_1_1.html.ep

    %= link_to 'MetaCPAN', 'http://www.metacpan.org/'

    @@ metacpan_1_2.html.ep

    %= text_field username => placeholder => 'Enter name'

And then all that remains is putting in a header. See Dist::Zilla::Plugin::Test::CreateFromMojoTemplates.

AUTHOR

Erik Carlsson <info@code301.com>

COPYRIGHT

Copyright 2014- Erik Carlsson

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO