| 1 | package Mason::t::Request; | ||
| 2 | $Mason::t::Request::VERSION = '2.23'; | ||
| 3 | 1 | ▶ | use Test::Class::Most parent => 'Mason::Test::Class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | use Log::Any::Test; | ||
| 5 | use Log::Any qw($log); | ||
| 6 | |||
| 7 | sub _get_current_comp_class { | ||
| 8 | my $m = shift; | ||
| 9 | return $m->current_comp_class; | ||
| 10 | } | ||
| 11 | |||
| 12 | sub test_add_cleanup : Tests { | ||
| 13 | my $self = shift; | ||
| 14 | my $foo = 1; | ||
| 15 | $self->test_comp( | ||
| 16 | src => ' | ||
| 17 | % my $ref = $.args->{ref}; | ||
| 18 | % $m->add_cleanup(sub { $$ref++ }); | ||
| 19 | foo = <% $$ref %> | ||
| 20 | ', | ||
| 21 | args => { ref => \$foo }, | ||
| 22 | expect => 'foo = 1' | ||
| 23 | ); | ||
| 24 | is( $foo, 2, "foo now 2" ); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub test_capture : Tests { | ||
| 28 | my $self = shift; | ||
| 29 | $self->run_test_in_comp( | ||
| 30 | test => sub { | ||
| 31 | my $comp = shift; | ||
| 32 | my $m = $comp->m; | ||
| 33 | is( $m->capture( sub { print "abcde" } ), 'abcde' ); | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub test_comp_exists : Tests { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | $self->add_comp( path => '/comp_exists/one.mc', src => 'hi' ); | ||
| 42 | $self->test_comp( | ||
| 43 | path => '/comp_exists/two.mc', | ||
| 44 | src => ' | ||
| 45 | % foreach my $path (qw(/comp_exists/one.mc /comp_exists/two.mc /comp_exists/three.mc one.mc two.mc three.mc)) { | ||
| 46 | <% $path %>: <% $m->comp_exists($path) ? "yes" : "no" %> | ||
| 47 | % } | ||
| 48 | ', | ||
| 49 | expect => ' | ||
| 50 | /comp_exists/one.mc: yes | ||
| 51 | /comp_exists/two.mc: yes | ||
| 52 | /comp_exists/three.mc: no | ||
| 53 | one.mc: yes | ||
| 54 | two.mc: yes | ||
| 55 | three.mc: no | ||
| 56 | ', | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub test_current_comp_class : Tests { | ||
| 61 | shift->test_comp( | ||
| 62 | path => '/current_comp_class.mc', | ||
| 63 | src => '<% ' . __PACKAGE__ . '::_get_current_comp_class($m)->cmeta->path %>', | ||
| 64 | expect => '/current_comp_class.mc' | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub test_id : Tests { | ||
| 69 | my $self = shift; | ||
| 70 | $self->setup_dirs; | ||
| 71 | $self->add_comp( path => '/id.mc', src => 'id=<% $m->id %>' ); | ||
| 72 | my ($id1) = ( $self->interp->run('/id')->output =~ /id=(\d+)/ ); | ||
| 73 | my ($id2) = ( $self->interp->run('/id')->output =~ /id=(\d+)/ ); | ||
| 74 | ok( $id1 != $id2 ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub test_log : Tests { | ||
| 78 | my $self = shift; | ||
| 79 | $self->add_comp( path => '/log/one.mc', src => '% $m->log->info("message one")' ); | ||
| 80 | $self->run_test_in_comp( | ||
| 81 | path => '/log.mc', | ||
| 82 | test => sub { | ||
| 83 | my $comp = shift; | ||
| 84 | my $m = $comp->m; | ||
| 85 | $m->comp('/log/one.mc'); | ||
| 86 | $log->contains_ok("message one"); | ||
| 87 | }, | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub test_notes : Tests { | ||
| 92 | my $self = shift; | ||
| 93 | $self->add_comp( | ||
| 94 | path => '/show', | ||
| 95 | src => ' | ||
| 96 | <% $m->notes("foo") %> | ||
| 97 | % $m->notes("foo", 3); | ||
| 98 | ', | ||
| 99 | ); | ||
| 100 | $self->test_comp( | ||
| 101 | src => ' | ||
| 102 | % $m->notes("foo", 2); | ||
| 103 | <% $m->notes("foo") %> | ||
| 104 | <& /show &> | ||
| 105 | <% $m->notes("foo") %> | ||
| 106 | ', | ||
| 107 | expect => "2\n\n2\n\n3\n", | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub test_page : Tests { | ||
| 112 | my $self = shift; | ||
| 113 | $self->add_comp( path => '/page/other.mi', src => '<% $m->page->cmeta->path %>' ); | ||
| 114 | $self->test_comp( | ||
| 115 | path => '/page/first.mc', | ||
| 116 | src => '<% $m->page->cmeta->path %>; <& other.mi &>', | ||
| 117 | expect => '/page/first.mc; /page/first.mc' | ||
| 118 | ); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub test_result_data : Tests { | ||
| 122 | my $self = shift; | ||
| 123 | $self->test_comp( | ||
| 124 | src => '% $m->result->data->{color} = "red"', | ||
| 125 | expect_data => { color => "red" } | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub test_scomp : Tests { | ||
| 130 | my $self = shift; | ||
| 131 | $self->add_comp( path => '/str', src => 'abcde' ); | ||
| 132 | $self->run_test_in_comp( | ||
| 133 | test => sub { | ||
| 134 | my $comp = shift; | ||
| 135 | my $m = $comp->m; | ||
| 136 | is( $m->scomp('/str'), 'abcde' ); | ||
| 137 | is( $m->capture( sub { $m->scomp('/str') } ), '' ); | ||
| 138 | } | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub test_go : Tests { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | my ( $buf, $result ); | ||
| 146 | |||
| 147 | reset_id(); | ||
| 148 | $self->add_comp( | ||
| 149 | path => '/subreq/other.mc', | ||
| 150 | src => ' | ||
| 151 | id=<% $m->id %> | ||
| 152 | <% $m->page->cmeta->path %> | ||
| 153 | <% $m->request_path %> | ||
| 154 | args: <% Mason::Util::dump_one_line($m->request_args) %> | ||
| 155 | ', | ||
| 156 | ); | ||
| 157 | $self->test_comp( | ||
| 158 | path => '/subreq/go.mc', | ||
| 159 | src => ' | ||
| 160 | This should not get printed. | ||
| 161 | <%perl>$m->go("/subreq/other", foo => 5);</%perl>', | ||
| 162 | expect => ' | ||
| 163 | id=1 | ||
| 164 | /subreq/other.mc | ||
| 165 | /subreq/other | ||
| 166 | args: {foo => 5} | ||
| 167 | ', | ||
| 168 | ); | ||
| 169 | reset_id(); | ||
| 170 | $self->test_comp( | ||
| 171 | path => '/subreq/go_with_req_params.mc', | ||
| 172 | src => ' | ||
| 173 | This should not get printed. | ||
| 174 | <%perl>my $buf; $m->go({out_method => \$buf}, "/subreq/other", foo => 5)</%perl>', | ||
| 175 | expect => '', | ||
| 176 | ); | ||
| 177 | |||
| 178 | reset_id(); | ||
| 179 | $result = $self->interp->run( { out_method => \$buf }, '/subreq/go' ); | ||
| 180 | is( $result->output, '', 'no output' ); | ||
| 181 | is( | ||
| 182 | $buf, ' | ||
| 183 | id=1 | ||
| 184 | /subreq/other.mc | ||
| 185 | /subreq/other | ||
| 186 | args: {foo => 5} | ||
| 187 | ', 'output in buf' | ||
| 188 | ); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub test_visit : Tests { | ||
| 192 | my $self = shift; | ||
| 193 | |||
| 194 | my ( $buf, $result ); | ||
| 195 | |||
| 196 | reset_id(); | ||
| 197 | $self->add_comp( | ||
| 198 | path => '/subreq/other.mc', | ||
| 199 | src => ' | ||
| 200 | id=<% $m->id %> | ||
| 201 | <% $m->page->cmeta->path %> | ||
| 202 | <% $m->request_path %> | ||
| 203 | args: <% Mason::Util::dump_one_line($m->request_args) %> | ||
| 204 | ', | ||
| 205 | ); | ||
| 206 | $self->test_comp( | ||
| 207 | path => '/subreq/visit.mc', | ||
| 208 | src => ' | ||
| 209 | begin | ||
| 210 | id=<% $m->id %> | ||
| 211 | <%perl>$m->visit("/subreq/other", foo => 5);</%perl> | ||
| 212 | id=<% $m->id %> | ||
| 213 | end | ||
| 214 | ', | ||
| 215 | expect => ' | ||
| 216 | begin | ||
| 217 | id=0 | ||
| 218 | id=1 | ||
| 219 | /subreq/other.mc | ||
| 220 | /subreq/other | ||
| 221 | args: {foo => 5} | ||
| 222 | id=0 | ||
| 223 | end | ||
| 224 | ', | ||
| 225 | ); | ||
| 226 | |||
| 227 | reset_id(); | ||
| 228 | $self->test_comp( | ||
| 229 | path => '/subreq/visit_with_req_params.mc', | ||
| 230 | src => ' | ||
| 231 | begin | ||
| 232 | id=<% $m->id %> | ||
| 233 | <%perl>my $buf; $m->visit({out_method => \$buf}, "/subreq/other", foo => 5); print uc($buf);</%perl> | ||
| 234 | id=<% $m->id %> | ||
| 235 | end | ||
| 236 | ', | ||
| 237 | expect => ' | ||
| 238 | begin | ||
| 239 | id=0 | ||
| 240 | ID=1 | ||
| 241 | /SUBREQ/OTHER.MC | ||
| 242 | /SUBREQ/OTHER | ||
| 243 | ARGS: {FOO => 5} | ||
| 244 | id=0 | ||
| 245 | end | ||
| 246 | ', | ||
| 247 | ); | ||
| 248 | |||
| 249 | reset_id(); | ||
| 250 | $result = $self->interp->run( { out_method => \$buf }, '/subreq/visit' ); | ||
| 251 | is( $result->output, '', 'no output' ); | ||
| 252 | is( | ||
| 253 | $buf, ' | ||
| 254 | begin | ||
| 255 | id=0 | ||
| 256 | id=1 | ||
| 257 | /subreq/other.mc | ||
| 258 | /subreq/other | ||
| 259 | args: {foo => 5} | ||
| 260 | id=0 | ||
| 261 | end | ||
| 262 | ', 'visit with initial out_method' | ||
| 263 | ); | ||
| 264 | } | ||
| 265 | |||
| 266 | sub reset_id { | ||
| 267 | Mason::Request->_reset_next_id; | ||
| 268 | } | ||
| 269 | |||
| 270 | 1; | ||