#!/usr/bin/perl -w

use strict;

use File::Spec;
use HTML::Mason::Tests;

my $tests = make_tests();
$tests->run;

sub make_tests
{
    my $group = HTML::Mason::Tests->new( name => 'misc',
					 description => 'autohandler and dhandler functionality' );


#------------------------------------------------------------

    $group->add_support( path => '/autohandler_test/autohandler',
			 component => <<'EOF',
<& header &>
Autohandler comp: <% $m->fetch_next->title %>
% my $buf;
% $m->call_next(b=>$a*2);
<& footer &>

<%args>
$a=>5
</%args>
EOF
		       );


#------------------------------------------------------------

    $group->add_support( path => '/autohandler_test/header',
			 component => <<'EOF',
<body bgcolor=<% $bgcolor %>>
<h2>The Site</h2>

<%args>
$bgcolor=>'white'
</%args>
EOF
		       );


#------------------------------------------------------------

    $group->add_support( path => '/autohandler_test/footer',
			 component => <<'EOF',
<hr>
Copyright 1999 Schmoopie Inc.

EOF
		       );


#------------------------------------------------------------

    $group->add_test( name => 'autohandler',
		      path => '/autohandler_test/hello',
		      call_path => '/autohandler_test/hello',
		      description => 'autohandler test',
		      component => <<'EOF',
Hello World!
The answer is <% $b %>.
<%args>
$b
</%args>



EOF
		      expect => <<'EOF',
<body bgcolor=white>
<h2>The Site</h2>


Autohandler comp: /misc/autohandler_test/hello
Hello World!
The answer is 10.



<hr>
Copyright 1999 Schmoopie Inc.



EOF
		    );



#------------------------------------------------------------

    $group->add_support( path => '/dhandler_test/dhandler',
			 component => <<'EOF',
dhandler = <% $m->current_comp->title %>
dhandler arg = <% $m->dhandler_arg %>
EOF
		       );


#------------------------------------------------------------

    $group->add_support( path => '/dhandler_test/subdir/dhandler',
			 component => <<'EOF',
% $m->decline if $m->dhandler_arg eq 'leaf3';
% $m->decline if $m->dhandler_arg eq 'slashes';
% $m->decline if $m->dhandler_arg eq 'buffers';
dhandler = <% $m->current_comp->title %>
dhandler arg = <% $m->dhandler_arg %>
EOF
		       );


#------------------------------------------------------------

    $group->add_support( path => '/dhandler_test/subdir/autohandler',
			 component => <<'EOF',
Header
<% $m->call_next %>
EOF
		       );


#------------------------------------------------------------

    $group->add_support( path => '/dhandler_test/bar/dhandler',
			 component => <<'EOF',
dhandler = <% $m->current_comp->title %>
dhandler arg = <% $m->dhandler_arg %>
EOF
		       );

#------------------------------------------------------------

    $group->add_support( path => '/dhandler_test/buff/dhandler',
			 component => <<'EOF',
Buffer stack size: <% scalar $m->buffer_stack %>
EOF
		       );


#------------------------------------------------------------

    $group->add_test( name => 'dhandler1',
		      description => 'tests dhandler against nonexistent comp',
		      call_path => '/dhandler_test/foo/bar',
		      skip_component => 1,
		      expect => <<'EOF',
dhandler = /misc/dhandler_test/dhandler
dhandler arg = foo/bar

EOF
		    );


#------------------------------------------------------------

    $group->add_test( name => 'dhandler2',
		      description => 'real comp to make sure the real comp is invoked, not the dhandler',
		      path => '/dhandler_test/subdir/leaf',
		      call_path => '/dhandler_test/subdir/leaf',
		      component => <<'EOF',
I'm leaf
EOF
		      expect => <<'EOF',
Header
I'm leaf

EOF
		    );


#------------------------------------------------------------

    $group->add_test( name => 'dhandler3',
		      description => 'real comp declines the request to make sure the dhandler is invoked',
		      path => '/dhandler_test/subdir/leaf2',
		      call_path => '/dhandler_test/subdir/leaf2',
		      component => <<'EOF',
% $m->decline;
I'm leaf2

EOF
		      expect => <<'EOF',
Header
dhandler = /misc/dhandler_test/subdir/dhandler
dhandler arg = leaf2

EOF
		    );


#------------------------------------------------------------

    $group->add_test( name => 'dhandler4',
		      description => 'declines twice to make sure higher level dhandler is called',
		      path => '/dhandler_test/subdir/leaf3',
		      call_path => '/dhandler_test/subdir/leaf3',
		      component => <<'EOF',
% $m->decline;
I'm leaf3
EOF
		      expect => <<'EOF',
dhandler = /misc/dhandler_test/dhandler
dhandler arg = subdir/leaf3

EOF
		    );

#------------------------------------------------------------

    $group->add_test( name => 'dhandler5',
		      description => 'decline with doubled slash (//) in URL path',
		      path => '/dhandler_test/subdir/slashes',
		      call_path => '//dhandler_test//subdir//slashes',
		      component => <<'EOF',
% $m->decline;
I have many slashes!
EOF
		      expect => <<'EOF',
dhandler = /misc/dhandler_test/dhandler
dhandler arg = subdir/slashes

EOF
		    );

#------------------------------------------------------------

    $group->add_test( name => 'dhandler6',
		      description => 'test that a dhandler more than one directory up is found',
		      call_path => '/dhandler_test/bar/baz/quux/not_here',
		      skip_component => 1,
		      expect => <<'EOF',
dhandler = /misc/dhandler_test/bar/dhandler
dhandler arg = baz/quux/not_here

EOF
		    );

#------------------------------------------------------------

    $group->add_test( name => 'dhandler7',
		      description => 'test that $m->decline does not leak buffer objects',
		      call_path => '/dhandler_test/buff/buffers',
		      component => <<'EOF',
% $m->decline;
I'm buffers
EOF
		      expect => <<'EOF',
Buffer stack size: 2
EOF
		    );

#------------------------------------------------------------

    $group->add_test( name => 'accessor_validate',
		      description => 'test accessor parameter validation',
		      component => <<'EOF',
% $m->interp->code_cache_max_size([1]);
EOF
		      expect_error => qr/Parameter #1 to .*? was an 'arrayref'/,
		    );

#------------------------------------------------------------

    $group->add_test( name => 'contained_accessor_validate',
		      description => 'test contained accessor parameter validation',
		      component => <<'EOF',
% $m->interp->autoflush([1]);
EOF
		      expect_error => qr/Parameter #1 to .*? was an 'arrayref'/,
		    );

#------------------------------------------------------------

    # this is sneaky and bad!
    my $updir = File::Spec->updir;
    $group->add_support( path => "$updir/dhandler",
			 component => <<'EOF',
top-level dhandler
EOF
		       );

#------------------------------------------------------------

    $group->add_support( path => '/dhandler',
			 component => <<'EOF',
% $m->decline;
EOF
		       );

#------------------------------------------------------------

    $group->add_test( name => 'top_level_dhandler',
		      description => 'make sure dhandler at /dhandler is called correctly after decline from lower-level dhandler',
                      path      => '/notused',
                      call_path => '/nonexistent',
		      component => <<'EOF',
not ever used
EOF
		      expect => <<'EOF',
top-level dhandler
EOF
		    );

#------------------------------------------------------------

    return $group;
}
