| 1 | package Mason::t::Autobase; | ||
| 2 | $Mason::t::Autobase::VERSION = '2.23'; | ||
| 3 | 1 | ▶ | use Test::Class::Most parent => 'Mason::Test::Class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | sub test_autobase : Tests { | ||
| 6 | my $self = shift; | ||
| 7 | my $interp = $self->interp; | ||
| 8 | |||
| 9 | my $check_parent = sub { | ||
| 10 | my ( $path, $parent ) = @_; | ||
| 11 | |||
| 12 | my $base_comp_class = $interp->load($path) | ||
| 13 | or die "could not load '$path'"; | ||
| 14 | my $parent_comp_class = ( $parent =~ /\// ) ? $interp->load($parent) : $parent; | ||
| 15 | cmp_deeply( [ $base_comp_class->meta->superclasses ], | ||
| 16 | [$parent_comp_class], "parent of $path is $parent" ); | ||
| 17 | }; | ||
| 18 | |||
| 19 | my $add = sub { | ||
| 20 | my ( $path, $extends ) = @_; | ||
| 21 | |||
| 22 | $self->add_comp( | ||
| 23 | path => $path, | ||
| 24 | src => ( $extends ? "<%flags>\nextends => $extends\n</%flags>" : " " ) | ||
| 25 | ); | ||
| 26 | }; | ||
| 27 | |||
| 28 | my $remove = sub { | ||
| 29 | my ($path) = @_; | ||
| 30 | |||
| 31 | $self->remove_comp( path => $path, ); | ||
| 32 | }; | ||
| 33 | |||
| 34 | # Add components with no autobases, make sure they inherit from | ||
| 35 | # Mason::Component | ||
| 36 | # | ||
| 37 | $add->('/comp.mc'); | ||
| 38 | $add->('/foo/comp.mc'); | ||
| 39 | $add->('/foo/bar/comp.mc'); | ||
| 40 | $add->('/foo/bar/baz/comp.mc'); | ||
| 41 | |||
| 42 | my $base_class = $self->interp->component_class; | ||
| 43 | |||
| 44 | $check_parent->( '/comp.mc', $base_class ); | ||
| 45 | $check_parent->( '/foo/comp.mc', $base_class ); | ||
| 46 | $check_parent->( '/foo/bar/comp.mc', $base_class ); | ||
| 47 | $check_parent->( '/foo/bar/baz/comp.mc', $base_class ); | ||
| 48 | |||
| 49 | # Add autobases, test the parents of the components and autobases | ||
| 50 | # | ||
| 51 | $add->('/Base.mc'); | ||
| 52 | $add->('/foo/Base.mc'); | ||
| 53 | $add->('/foo/bar/baz/Base.mc'); | ||
| 54 | $self->interp->_flush_load_cache(); | ||
| 55 | |||
| 56 | $check_parent->( '/Base.mc', $base_class ); | ||
| 57 | $check_parent->( '/foo/Base.mc', '/Base.mc' ); | ||
| 58 | $check_parent->( '/foo/bar/baz/Base.mc', '/foo/Base.mc' ); | ||
| 59 | $check_parent->( '/comp.mc', '/Base.mc' ); | ||
| 60 | |||
| 61 | $check_parent->( '/foo/comp.mc', '/foo/Base.mc' ); | ||
| 62 | $check_parent->( '/foo/bar/comp.mc', '/foo/Base.mc' ); | ||
| 63 | $check_parent->( '/foo/bar/baz/comp.mc', '/foo/bar/baz/Base.mc' ); | ||
| 64 | |||
| 65 | $add->( '/foo/bar/baz/none.mc', "undef" ); | ||
| 66 | $check_parent->( '/foo/bar/baz/none.mc', $base_class ); | ||
| 67 | |||
| 68 | $add->( '/foo/bar/baz/top.mc', "'/Base.mc'" ); | ||
| 69 | $check_parent->( '/foo/bar/baz/top.mc', '/Base.mc' ); | ||
| 70 | |||
| 71 | $add->( '/foo/bar/baz/top2.mc', "'../../Base.mc'" ); | ||
| 72 | $check_parent->( '/foo/bar/baz/top2.mc', '/foo/Base.mc' ); | ||
| 73 | |||
| 74 | # Multiple autobases same directory | ||
| 75 | $add->('/Base.mp'); | ||
| 76 | $add->('/foo/Base.mp'); | ||
| 77 | $self->interp->_flush_load_cache(); | ||
| 78 | $check_parent->( '/Base.mp', $base_class ); | ||
| 79 | $check_parent->( '/Base.mc', '/Base.mp' ); | ||
| 80 | $check_parent->( '/foo/Base.mp', '/Base.mc' ); | ||
| 81 | $check_parent->( '/foo/Base.mc', '/foo/Base.mp' ); | ||
| 82 | $check_parent->( '/foo/comp.mc', '/foo/Base.mc' ); | ||
| 83 | |||
| 84 | # Remove most autobases, test parents again | ||
| 85 | # | ||
| 86 | $remove->('/Base.mp'); | ||
| 87 | $remove->('/Base.mc'); | ||
| 88 | $remove->('/foo/Base.mp'); | ||
| 89 | $remove->('/foo/Base.mc'); | ||
| 90 | $self->interp->_flush_load_cache(); | ||
| 91 | |||
| 92 | $check_parent->( '/comp.mc', $base_class ); | ||
| 93 | $check_parent->( '/foo/comp.mc', $base_class ); | ||
| 94 | $check_parent->( '/foo/bar/comp.mc', $base_class ); | ||
| 95 | $check_parent->( '/foo/bar/baz/comp.mc', '/foo/bar/baz/Base.mc' ); | ||
| 96 | $check_parent->( '/foo/bar/baz/Base.mc', $base_class ); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub test_cycles : Tests { | ||
| 100 | my $self = shift; | ||
| 101 | |||
| 102 | # An inheritance cycle | ||
| 103 | # | ||
| 104 | $self->add_comp( | ||
| 105 | path => '/cycle/Base.mc', | ||
| 106 | src => "<%flags>\nextends => '/cycle/c/index.mc'\n</%flags>\n", | ||
| 107 | ); | ||
| 108 | $self->test_comp( | ||
| 109 | path => '/cycle/c/index.mc', | ||
| 110 | src => "ok", | ||
| 111 | expect_error => qr/inheritance cycle/, | ||
| 112 | ); | ||
| 113 | |||
| 114 | # This isn't a cycle but a bug that tried to preload default parent was causing | ||
| 115 | # it to infinite loop | ||
| 116 | # | ||
| 117 | $self->add_comp( | ||
| 118 | path => '/pseudo/Base.mc', | ||
| 119 | src => "<%flags>\nextends => '/pseudo/c/index.mc'\n</%flags>\n", | ||
| 120 | ); | ||
| 121 | $self->test_comp( | ||
| 122 | path => '/pseudo/c/index.mc', | ||
| 123 | src => "<%flags>\nextends => undef\n</%flags>\nok", | ||
| 124 | expect => 'ok', | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub test_wrapping : Tests { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | $self->add_comp( | ||
| 132 | path => '/wrap/Base.mc', | ||
| 133 | src => ' | ||
| 134 | <%augment wrap> | ||
| 135 | <html> | ||
| 136 | % inner(); | ||
| 137 | </html> | ||
| 138 | </%augment> | ||
| 139 | ' | ||
| 140 | ); | ||
| 141 | $self->add_comp( | ||
| 142 | path => '/wrap/subdir/Base.mc', | ||
| 143 | src => ' | ||
| 144 | |||
| 145 | <%method hello> | ||
| 146 | Hello world | ||
| 147 | </%method> | ||
| 148 | |||
| 149 | ' | ||
| 150 | ); | ||
| 151 | $self->add_comp( | ||
| 152 | path => '/wrap/subdir/subdir2/Base.mc', | ||
| 153 | src => ' | ||
| 154 | <%augment wrap> | ||
| 155 | <body> | ||
| 156 | % inner(); | ||
| 157 | </body> | ||
| 158 | </%augment> | ||
| 159 | ' | ||
| 160 | ); | ||
| 161 | $self->test_comp( | ||
| 162 | path => '/wrap/subdir/subdir2/wrap_me.mc', | ||
| 163 | src => '<% $self->hello %>', | ||
| 164 | expect => ' | ||
| 165 | <html> | ||
| 166 | |||
| 167 | <body> | ||
| 168 | |||
| 169 | Hello world | ||
| 170 | </body> | ||
| 171 | </html> | ||
| 172 | ' | ||
| 173 | ); | ||
| 174 | $self->test_comp( | ||
| 175 | path => '/wrap/subdir/subdir2/dont_wrap_me.mc', | ||
| 176 | src => ' | ||
| 177 | <%class>method wrap { $.main() }</%class> | ||
| 178 | <% $self->hello() %> | ||
| 179 | ', | ||
| 180 | expect => 'Hello world' | ||
| 181 | ); | ||
| 182 | $self->test_comp( | ||
| 183 | path => '/wrap/subdir/subdir2/dont_wrap_me_either.mc', | ||
| 184 | src => ' | ||
| 185 | <%class>CLASS->no_wrap;</%class> | ||
| 186 | <% $self->hello() %> | ||
| 187 | ', | ||
| 188 | expect => 'Hello world' | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | |||
| 192 | # not yet implemented | ||
| 193 | sub _test_no_main_in_autobase { | ||
| 194 | my $self = shift; | ||
| 195 | |||
| 196 | $self->test_comp( | ||
| 197 | path => '/wrap/Base.mc', | ||
| 198 | src => ' | ||
| 199 | <body> | ||
| 200 | % inner(); | ||
| 201 | </body> | ||
| 202 | ', | ||
| 203 | expect_error => qr/content found in main body of autobase/, | ||
| 204 | ); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub test_recompute_inherit : Tests { | ||
| 208 | my $self = shift; | ||
| 209 | my $interp = $self->interp; | ||
| 210 | |||
| 211 | # Test that /comp.mc class can be recomputed without garbage collection issues. | ||
| 212 | # | ||
| 213 | my $remove = sub { | ||
| 214 | my ($path) = @_; | ||
| 215 | |||
| 216 | $self->remove_comp( path => $path, ); | ||
| 217 | }; | ||
| 218 | |||
| 219 | $self->add_comp( path => '/comp.mc', src => ' ' ); | ||
| 220 | $self->interp->load('/comp.mc'); | ||
| 221 | $self->add_comp( path => '/Base.mc', src => ' ' ); | ||
| 222 | $self->interp->_flush_load_cache(); | ||
| 223 | $self->interp->load('/comp.mc'); | ||
| 224 | ok(1); | ||
| 225 | |||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; | ||