File: delegate.t

package info (click to toggle)
libcgi-application-plugin-autorunmode-perl 0.18-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 910; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 3,244 bytes parent folder | download | duplicates (5)
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
#!perl -T
#########################


use Test::More tests => 10;
BEGIN { use_ok('CGI::Application::Plugin::AutoRunmode') };

#########################
# Test CGI::App class
{ 
	package MyTestApp;
	use base 'CGI::Application';
	use CGI::Application::Plugin::AutoRunmode 
		qw [ cgiapp_prerun];
		
	sub setup{
		my $self = shift;
		$self->param(
			'::Plugin::AutoRunmode::delegate' => 'MyTestDelegate'
		);
	}
}

# Test delegate
{
	package MyTestDelegate;
	
	 sub mode1  {
	 	my ($self, $delegate) = @_;
		die "expected CGI::App instance as first parameter" unless $self->isa('CGI::Application');
		die "expected delegate class or instance as second parameter" unless $delegate;
		'called mode1';
	 }
}

$ENV{CGI_APP_RETURN_ONLY} = 1;
$ENV{REQUEST_METHOD} = 'GET';
$ENV{QUERY_STRING} = 'rm=mode1';

use CGI;
my $q = new CGI;
{
	my $testname = "call delegate runmode (class)";
	
	my $app = new MyTestApp(QUERY=>$q);
	my $t = $app->run;
	ok ($t =~ /called mode1/, $testname);
}

{
	my $testname = "call delegate runmode (object)";
	
	my $app = new MyTestApp(QUERY=>$q);
	$app->param("::Plugin::AutoRunmode::delegate"
		=> bless {}, 'MyTestDelegate');
	my $t = $app->run;
	ok ($t =~ /called mode1/, $testname);
}

{
	my $testname = "try to call a not-runmode";
	$q->param(rm => 'can');
	my $app = new MyTestApp(QUERY=>$q);
	eval{ 
		my $t = $app->run;
		};
	ok ($@ =~ /No such run.mode/, $testname);
}
	
# delegate subclass
{ 
	package MyTestSubDelegate;
	@MyTestSubDelegate::ISA = qw[MyTestDelegate ];
	
	 sub mode2  {
	 	'called mode2';
	 }
	 
	 sub mode3{
	 	my ($app, $delegate) = @_;
		'called mode3 '.$delegate->{hey};
	}
}


{	
	my $testname = "runmode from a superclass";
	$q->param(rm => 'mode1');
	my $app = new MyTestApp(QUERY=>$q);
	$app->param("::Plugin::AutoRunmode::delegate"
		=> 'MyTestSubDelegate');
	my $t = $app->run;
	ok ($t =~ /called mode1/, $testname);
}

{	
	my $testname = "runmode from a subclass";
	$q->param(rm => 'mode2');
	my $app = new MyTestApp(QUERY=>$q);
	$app->param("::Plugin::AutoRunmode::delegate"
		=> 'MyTestSubDelegate');
	my $t = $app->run;
	ok ($t =~ /called mode2/, $testname);
}

{	
	my $testname = "security check - calling packaged runmode";
	$q->param(rm => 'MyTestApp::setup');
	my $app = new MyTestApp(QUERY=>$q);
	eval{ my $t = $app->run; };
	ok ($@ =~ /^No such/, $testname);
}

{	
	my $testname = "stateful delegate";
	$q->param(rm => 'mode3');
	my $app = new MyTestApp(QUERY=>$q);
	$app->param("::Plugin::AutoRunmode::delegate"
		=> bless {hey => 'aaa'}, 'MyTestSubDelegate');
	my $t = $app->run;
	ok ($t =~ /called mode3 aaa/, $testname);
}

# delegate chain

{	
	my $testname = "delegate chain: call first one";
	$q->param(rm => 'mode1');
	my $app = new MyTestApp(QUERY=>$q);
	$app->param("::Plugin::AutoRunmode::delegate"
		=> [
			 'MyTestDelegate',
		   	bless {hey => 'bbb'}, 'MyTestSubDelegate'
		   ]);
	my $t = $app->run;
	ok ($t =~ /called mode1/, $testname);
}

{	
	my $testname = "delegate chain: call second one";
	$q->param(rm => 'mode3');
	my $app = new MyTestApp(QUERY=>$q);
	$app->param("::Plugin::AutoRunmode::delegate"
		=> [
			 'MyTestDelegate',
		   	bless {hey => 'bbb'}, 'MyTestSubDelegate'
		   ]);
	my $t = $app->run;
	ok ($t =~ /called mode3 bbb/, $testname);
}