File: Autobase.pm

package info (click to toggle)
libmason-perl 2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 748 kB
  • sloc: perl: 4,882; makefile: 7
file content (229 lines) | stat: -rw-r--r-- 5,961 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package Mason::t::Autobase;
$Mason::t::Autobase::VERSION = '2.24';
use Test::Class::Most parent => 'Mason::Test::Class';

sub test_autobase : Tests {
    my $self   = shift;
    my $interp = $self->interp;

    my $check_parent = sub {
        my ( $path, $parent ) = @_;

        my $base_comp_class = $interp->load($path)
          or die "could not load '$path'";
        my $parent_comp_class = ( $parent =~ /\// ) ? $interp->load($parent) : $parent;
        cmp_deeply( [ $base_comp_class->meta->superclasses ],
            [$parent_comp_class], "parent of $path is $parent" );
    };

    my $add = sub {
        my ( $path, $extends ) = @_;

        $self->add_comp(
            path => $path,
            src  => ( $extends ? "<%flags>\nextends => $extends\n</%flags>" : " " )
        );
    };

    my $remove = sub {
        my ($path) = @_;

        $self->remove_comp( path => $path, );
    };

    # Add components with no autobases, make sure they inherit from
    # Mason::Component
    #
    $add->('/comp.mc');
    $add->('/foo/comp.mc');
    $add->('/foo/bar/comp.mc');
    $add->('/foo/bar/baz/comp.mc');

    my $base_class = $self->interp->component_class;

    $check_parent->( '/comp.mc',             $base_class );
    $check_parent->( '/foo/comp.mc',         $base_class );
    $check_parent->( '/foo/bar/comp.mc',     $base_class );
    $check_parent->( '/foo/bar/baz/comp.mc', $base_class );

    # Add autobases, test the parents of the components and autobases
    #
    $add->('/Base.mc');
    $add->('/foo/Base.mc');
    $add->('/foo/bar/baz/Base.mc');
    $self->interp->_flush_load_cache();

    $check_parent->( '/Base.mc',             $base_class );
    $check_parent->( '/foo/Base.mc',         '/Base.mc' );
    $check_parent->( '/foo/bar/baz/Base.mc', '/foo/Base.mc' );
    $check_parent->( '/comp.mc',             '/Base.mc' );

    $check_parent->( '/foo/comp.mc',         '/foo/Base.mc' );
    $check_parent->( '/foo/bar/comp.mc',     '/foo/Base.mc' );
    $check_parent->( '/foo/bar/baz/comp.mc', '/foo/bar/baz/Base.mc' );

    $add->( '/foo/bar/baz/none.mc', "undef" );
    $check_parent->( '/foo/bar/baz/none.mc', $base_class );

    $add->( '/foo/bar/baz/top.mc', "'/Base.mc'" );
    $check_parent->( '/foo/bar/baz/top.mc', '/Base.mc' );

    $add->( '/foo/bar/baz/top2.mc', "'../../Base.mc'" );
    $check_parent->( '/foo/bar/baz/top2.mc', '/foo/Base.mc' );

    # Multiple autobases same directory
    $add->('/Base.mp');
    $add->('/foo/Base.mp');
    $self->interp->_flush_load_cache();
    $check_parent->( '/Base.mp',     $base_class );
    $check_parent->( '/Base.mc',     '/Base.mp' );
    $check_parent->( '/foo/Base.mp', '/Base.mc' );
    $check_parent->( '/foo/Base.mc', '/foo/Base.mp' );
    $check_parent->( '/foo/comp.mc', '/foo/Base.mc' );

    # Remove most autobases, test parents again
    #
    $remove->('/Base.mp');
    $remove->('/Base.mc');
    $remove->('/foo/Base.mp');
    $remove->('/foo/Base.mc');
    $self->interp->_flush_load_cache();

    $check_parent->( '/comp.mc',             $base_class );
    $check_parent->( '/foo/comp.mc',         $base_class );
    $check_parent->( '/foo/bar/comp.mc',     $base_class );
    $check_parent->( '/foo/bar/baz/comp.mc', '/foo/bar/baz/Base.mc' );
    $check_parent->( '/foo/bar/baz/Base.mc', $base_class );
}

sub test_cycles : Tests {
    my $self = shift;

    # An inheritance cycle
    #
    $self->add_comp(
        path => '/cycle/Base.mc',
        src  => "<%flags>\nextends => '/cycle/c/index.mc'\n</%flags>\n",
    );
    $self->test_comp(
        path         => '/cycle/c/index.mc',
        src          => "ok",
        expect_error => qr/inheritance cycle/,
    );

    # This isn't a cycle but a bug that tried to preload default parent was causing
    # it to infinite loop
    #
    $self->add_comp(
        path => '/pseudo/Base.mc',
        src  => "<%flags>\nextends => '/pseudo/c/index.mc'\n</%flags>\n",
    );
    $self->test_comp(
        path   => '/pseudo/c/index.mc',
        src    => "<%flags>\nextends => undef\n</%flags>\nok",
        expect => 'ok',
    );
}

sub test_wrapping : Tests {
    my $self = shift;

    $self->add_comp(
        path => '/wrap/Base.mc',
        src  => '
<%augment wrap>
<html>
% inner();
</html>
</%augment>
'
    );
    $self->add_comp(
        path => '/wrap/subdir/Base.mc',
        src  => '

<%method hello>
Hello world
</%method>

'
    );
    $self->add_comp(
        path => '/wrap/subdir/subdir2/Base.mc',
        src  => '
<%augment wrap>
<body>
% inner();
</body>
</%augment>
'
    );
    $self->test_comp(
        path   => '/wrap/subdir/subdir2/wrap_me.mc',
        src    => '<% $self->hello %>',
        expect => '
<html>

<body>

Hello world
</body>
</html>
'
    );
    $self->test_comp(
        path => '/wrap/subdir/subdir2/dont_wrap_me.mc',
        src  => '
<%class>method wrap { $.main() }</%class>
<% $self->hello() %>
',
        expect => 'Hello world'
    );
    $self->test_comp(
        path => '/wrap/subdir/subdir2/dont_wrap_me_either.mc',
        src  => '
<%class>CLASS->no_wrap;</%class>
<% $self->hello() %>
',
        expect => 'Hello world'
    );
}

# not yet implemented
sub _test_no_main_in_autobase {
    my $self = shift;

    $self->test_comp(
        path => '/wrap/Base.mc',
        src  => '
<body>
% inner();
</body>
',
        expect_error => qr/content found in main body of autobase/,
    );
}

sub test_recompute_inherit : Tests {
    my $self   = shift;
    my $interp = $self->interp;

    # Test that /comp.mc class can be recomputed without garbage collection issues.
    #
    my $remove = sub {
        my ($path) = @_;

        $self->remove_comp( path => $path, );
    };

    $self->add_comp( path => '/comp.mc', src => ' ' );
    $self->interp->load('/comp.mc');
    $self->add_comp( path => '/Base.mc', src => ' ' );
    $self->interp->_flush_load_cache();
    $self->interp->load('/comp.mc');
    ok(1);

    return;
}

1;