importing.t 3.62 KB
Newer Older
1 2 3
use warnings;
use strict;

4
##############################################################################
5 6 7 8 9 10
package Wifty::UI::imported_pkg;
use base qw/Template::Declare/;
use Template::Declare::Tags;

template 'imported' => sub {
    my $self = shift;
11
    div { outs_raw "Invocant: '$self'" };
12 13
};

14
##############################################################################
15 16 17 18
package Wifty::UI::imported_subclass_pkg;
use base qw/Wifty::UI::imported_pkg/;
use Template::Declare::Tags;

19
##############################################################################
20 21 22 23 24
package Wifty::UI;
use base qw/Template::Declare/;
use Template::Declare::Tags;

template simple => sub {
25
    print '# ', ref +shift, $/;
26 27 28
    html {
        head {};
        body { show 'private-content'; };
29
    }
30 31 32 33 34

};

private template 'private-content' => sub {
    my $self = shift;
35
    with ( id => 'body' ), div {
36 37 38 39 40 41 42
        outs( 'This is my content from' . $self );
    };
};

import_templates Wifty::UI::imported_pkg under '/imported_pkg';
import_templates Wifty::UI::imported_subclass_pkg under '/imported_subclass_pkg';

43 44 45 46 47 48 49 50
##############################################################################
package Wifty::OtherUI;
use base qw/Template::Declare/;
use Template::Declare::Tags;
import_templates Wifty::UI::imported_pkg under '/other_pkg';
import_templates Wifty::UI::imported_subclass_pkg under '/other_subclass';

##############################################################################
51 52
package main;
use Template::Declare::Tags;
53
Template::Declare->init( dispatch_to => ['Wifty::UI'] );
54

55 56
use Test::More tests => 18;
#use Test::More 'no_plan';
57 58
require "t/utils.pl";

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
# Visibility.
ok( Wifty::UI::imported_pkg->has_template('imported'),
    'Original template should be visible in its own class' );
ok( Wifty::UI::imported_subclass_pkg->has_template('imported'),
    'And be visible in a subclass');
ok( !Template::Declare->has_template('imported'),
    'But it should not be visible in Template::Declare');
ok( !Wifty::UI->has_template('imported'),
    'Nor in the packge it was imported into' );

ok( Template::Declare->has_template('imported_pkg/imported'),
    'But it should be visible in its imported path' );
ok( Template::Declare->has_template('imported_subclass_pkg/imported'),
    'And it should be visible when imported from a subclass' );

ok( !Template::Declare->has_template('other_pkg/imported'),
    'The imported template should not be visible when imported into non-root package' );

# Translate the path to where it was imported.
TODO: {
    local $TODO = 'path_for is confused', 1;
    is(
        Wifty::UI::imported_subclass_pkg->path_for('imported'),
        '/imported_subclass_pkg/imported',
        'The path for the imported template should be correct'
    );
}
is(
    Wifty::UI::imported_subclass_pkg->path_for('imported'),
    '/other_subclass/imported',
    'The imported template path should be correct for the last package it was imported into'
);
is( Wifty::UI->path_for('simple'), '/simple', 'Simple template should be in the root path' );
92 93

{
94 95 96
    ok my $simple = ( show('imported_pkg/imported') ), 'Should get output for imported template';
    like( $simple, qr'Invocant:', 'Its output should be correct' );
    like( $simple, qr{'Wifty::UI'}, '$self is correct in template block' );
97 98 99
    ok_lint($simple);
}
{
100 101
    ok my $simple = ( show('imported_subclass_pkg/imported') ),
        'Should get output from imported template from subclass';
102 103
    like(
        $simple,
104
        qr'Invocant:',
105 106 107 108
        "We got the imported version in the subclass"
    );
    like(
        $simple,
109
        qr{'Wifty::UI'},
110 111 112 113 114
        '$self is correct in template block'
    );
    ok_lint($simple);
}
1;