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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
use strict;
use warnings;
use File::Basename;
use HTML::Mason::Tests;
my $outside_comp_root_test_file;
my $tests = make_tests();
$tests->run;
sub make_tests
{
my $group = HTML::Mason::Tests->tests_class->new( name => 'comp-calls',
description => 'Component call syntax' );
$outside_comp_root_test_file = dirname($group->comp_root) . "/.outside_comp";
#------------------------------------------------------------
$group->add_support( path => '/support/amper_test',
component => <<'EOF',
amper_test.<p>
% if (%ARGS) {
Arguments:<p>
% foreach my $key (sort keys %ARGS) {
<b><% $key %></b>: <% $ARGS{$key} %><br>
% }
% }
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'ampersand',
description => 'tests all variations of component call path syntax',
component => <<'EOF',
<&support/amper_test&>
<& support/amper_test &>
<& support/amper_test, &>
<& support/amper_test
&>
<&
support/amper_test &>
<&
support/amper_test
&>
EOF
expect => <<'EOF',
amper_test.<p>
amper_test.<p>
amper_test.<p>
amper_test.<p>
amper_test.<p>
amper_test.<p>
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'ampersand_with_args',
description => 'tests variations of component calls with arguments',
component => <<'EOF',
<& /comp-calls/support/amper_test, message=>'Hello World!' &>
<& support/amper_test, message=>'Hello World!',
to=>'Joe' &>
<& "support/amper_test" &>
% my $dir = "support";
% my %args = (a=>17, b=>32);
<& $dir . "/amper_test", %args &>
EOF
expect => <<'EOF',
amper_test.<p>
Arguments:<p>
<b>message</b>: Hello World!<br>
amper_test.<p>
Arguments:<p>
<b>message</b>: Hello World!<br>
<b>to</b>: Joe<br>
amper_test.<p>
amper_test.<p>
Arguments:<p>
<b>a</b>: 17<br>
<b>b</b>: 32<br>
EOF
);
#------------------------------------------------------------
$group->add_support( path => '/support/funny_-+=@~~~._name',
component => <<'EOF',
foo is <% $ARGS{foo} %>
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'ampersand_with_funny_name',
description => 'component with non-alphabetic characters',
component => <<'EOF',
<& support/funny_-+=@~~~._name, foo => 5 &>
EOF
expect => <<'EOF',
foo is 5
EOF
);
#------------------------------------------------------------
# This only tests for paths passed through Request::fetch_comp,
# not Interp::load. Not sure how zealously we want to
# canonicalize.
#
$group->add_test( name => 'canonicalize_paths',
description => 'test that various paths are canonicalized to the same component',
component => <<'EOF',
<%perl>
my $path1 = '///comp-calls/support//amper_test';
my $comp1 = $m->fetch_comp($path1)
or die "could not fetch comp1";
my $path2 = './support/./././amper_test';
my $comp2 = $m->fetch_comp($path2)
or die "could not fetch comp2";
my $path3 = './support/../support/../support/././amper_test';
my $comp3 = $m->fetch_comp($path3)
or die "could not fetch comp3";
unless ($comp1 == $comp2 && $comp2 == $comp3) {
die sprintf
(
"different component objects for same canonical path:\n %s (%s -> %s)\n %s (%s -> %s)\n %s (%s -> %s)",
$comp1, $path1, $comp1->path,
$comp2, $path2, $comp2->path,
$comp3, $path3, $comp3->path,
);
}
$m->comp($comp1);
$m->comp($comp2);
$m->comp($comp3);
</%perl>
EOF
expect => <<'EOF',
amper_test.<p>
amper_test.<p>
amper_test.<p>
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'fetch_comp_no_arg',
description => 'fetch_comp with blank or undefined argument returns undef',
component => <<'EOF',
fetch_comp(undef) = <% defined($m->fetch_comp(undef)) ? 'defined' : 'undefined' %>
fetch_comp("") = <% defined($m->fetch_comp("")) ? 'defined' : 'undefined' %>
EOF
expect => <<'EOF',
fetch_comp(undef) = undefined
fetch_comp("") = undefined
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'outside_comp_root_prepare',
description => 'test that file exists in dist/t for next two tests',
pre_code => sub { local *F; open(F, ">$outside_comp_root_test_file"); print F "hi"; },
component => "test file '$outside_comp_root_test_file' <% -e '$outside_comp_root_test_file' ? 'exists' : 'does not exist' %>",
expect => "test file '$outside_comp_root_test_file' exists",
);
#------------------------------------------------------------
$group->add_test( name => 'outside_comp_root_absolute',
description => 'cannot call components outside comp root with absolute path',
component => <<'EOF',
<& /../.outside_comp &>
EOF
expect_error => qr{could not find component for path '/../.outside_comp'},
);
#------------------------------------------------------------
$group->add_test( name => 'outside_comp_root_relative',
description => 'cannot call components outside comp root with relative path',
component => <<'EOF',
<& ../../.outside_comp &>
EOF
expect_error => qr{could not find component for path '../../.outside_comp'},
);
#------------------------------------------------------------
# put /../ in add_support path to put component right under comp root
$group->add_support( path => '/../outside_comp_root_from_top',
component => <<'EOF',
<& ../.outside_comp &>
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'outside_comp_root_relative_from_top',
description => 'cannot call components outside comp root with relative path from component at top of root',
component => <<'EOF',
<& /outside_comp_root_from_top &>
EOF
expect_error => qr{could not find component for path '../.outside_comp'},
);
#------------------------------------------------------------
$group->add_test( name => 'parent_designator_with_no_parent',
description => 'using PARENT from component with no parent',
component => <<'EOF',
<%flags>
inherit=>undef
</%flags>
<& PARENT:foo &>
EOF
expect_error => qr/PARENT designator used from component with no parent/,
);
#------------------------------------------------------------
$group->add_test( name => 'no_such_method',
description => 'calling nonexistent method on existing component',
component => <<'EOF',
<& support/amper_test:bar &>
EOF
expect_error => qr/no such method 'bar' for component/,
);
#------------------------------------------------------------
$group->add_test( name => 'fetch_comp_no_errors',
description => 'fetch_comp should not throw any errors',
component => <<'EOF',
% foreach my $path (qw(foo support/amper_test:bar PARENT)) {
<% $m->fetch_comp($path) ? 'defined' : 'undefined' %>
% }
EOF
expect => <<'EOF',
undefined
undefined
undefined
EOF
);
#------------------------------------------------------------
$group->add_support( path => '/support/methods',
component => <<'EOF',
<%method foo></%method>
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'comp_exists',
description => 'test comp_exists with various types of paths',
component => <<'EOF',
<%perl>
my @paths = qw(
support/methods
support/methods:foo
support/methods:bar
.foo
.bar
SELF
SELF:foo
PARENT
PARENT:foo
REQUEST
REQUEST:foo
);
</%perl>
<%def .foo></%def>
% foreach my $path (@paths) {
<% $path %>: <% $m->comp_exists($path) %>
% }
EOF
expect => <<'EOF',
support/methods: 1
support/methods:foo: 1
support/methods:bar: 0
.foo: 1
.bar: 0
SELF: 1
SELF:foo: 0
PARENT: 0
PARENT:foo: 0
REQUEST: 1
REQUEST:foo: 0
EOF
);
#------------------------------------------------------------
$group->add_test( name => 'comp_exists_no_arg',
description => 'comp_exists with blank or undefined argument returns 0',
component => <<'EOF',
comp_exists(undef) = <% $m->comp_exists(undef) %>
comp_exists("") = <% $m->comp_exists("") %>
EOF
expect => <<'EOF',
comp_exists(undef) = 0
comp_exists("") = 0
EOF
);
return $group;
}
|