|
| 1 | +#!/usr/bin/env perl |
| 2 | +# -*- mode: perl; coding: utf-8 -*- |
| 3 | +#---------------------------------------- |
| 4 | +use strict; |
| 5 | +use warnings qw(FATAL all NONFATAL misc); |
| 6 | +use FindBin; BEGIN { do "$FindBin::Bin/t_lib.pl" } |
| 7 | +#---------------------------------------- |
| 8 | + |
| 9 | +use Test::Kantan; |
| 10 | +use File::Temp qw/tempdir/; |
| 11 | + |
| 12 | +use Plack::Request; |
| 13 | +use Plack::Response; |
| 14 | +use HTTP::Request::Common; |
| 15 | +use HTTP::Message::PSGI; |
| 16 | + |
| 17 | +use YATT::t::t_preload; # To make Devel::Cover happy. |
| 18 | +use YATT::Lite::WebMVC0::SiteApp; |
| 19 | +use YATT::Lite::Util qw/combination/; |
| 20 | +use YATT::Lite::Util::File qw/mkfile/; |
| 21 | +use File::Path qw(make_path); |
| 22 | +use Cwd; |
| 23 | + |
| 24 | +use YATT::Lite::PSGIEnv; |
| 25 | + |
| 26 | +my $TEMPDIR = tempdir(CLEANUP => 1); |
| 27 | +my $CWD = cwd(); |
| 28 | +my $TESTNO = 0; |
| 29 | +my $CT = ["Content-Type", q{text/html; charset="utf-8"}]; |
| 30 | + |
| 31 | +my $TODO = $ENV{TEST_TODO}; |
| 32 | + |
| 33 | +#---------------------------------------- |
| 34 | + |
| 35 | +my $make_dirs = sub { |
| 36 | + my $app_root = "$TEMPDIR/t" . ++$TESTNO; |
| 37 | + my $html_dir = "$app_root/html"; |
| 38 | + |
| 39 | + make_path($html_dir); |
| 40 | + |
| 41 | + ($app_root, $html_dir); |
| 42 | +}; |
| 43 | + |
| 44 | +my $make_siteapp = sub { |
| 45 | + my ($app_root, $html_dir, @args) = @_; |
| 46 | + |
| 47 | + my $site = YATT::Lite::WebMVC0::SiteApp |
| 48 | + ->new(app_ns => "Test$TESTNO" |
| 49 | + , app_root => $app_root |
| 50 | + , doc_root => $html_dir |
| 51 | + , debug_cgen => $ENV{DEBUG} |
| 52 | + ); |
| 53 | + |
| 54 | + wantarray ? ($app_root, $html_dir, $site) : $site; |
| 55 | +}; |
| 56 | + |
| 57 | +my $with_or_without = sub {$_[0] ? "With" : "Without"}; |
| 58 | + |
| 59 | +#======================================== |
| 60 | + |
| 61 | +foreach my $has_index (1, 0) { |
| 62 | + |
| 63 | + describe $with_or_without->($has_index)." index.yatt", sub { |
| 64 | + |
| 65 | + describe "action.ydo", sub { |
| 66 | + my ($app_root, $html_dir, $site) = $make_siteapp->($make_dirs->()); |
| 67 | + |
| 68 | + MY->mkfile("$html_dir/index.yatt", <<'END') if $has_index; |
| 69 | +<h2>Hello</h2> |
| 70 | +
|
| 71 | +<!yatt:action "/foo"> |
| 72 | +# should not be called (hidden by foo.ydo) |
| 73 | +print $CON "action foo in index.yatt"; |
| 74 | +
|
| 75 | +<!yatt:action "/bar"> |
| 76 | +print $CON "action bar in index.yatt"; |
| 77 | +END |
| 78 | + |
| 79 | + MY->mkfile("$html_dir/foo.ydo", <<'END'); |
| 80 | +use strict; |
| 81 | +return sub { |
| 82 | + my ($this, $CON) = @_; |
| 83 | + print $CON "action in foo.ydo"; |
| 84 | +}; |
| 85 | +END |
| 86 | + |
| 87 | + describe "request /foo.ydo", sub { |
| 88 | + my Env $psgi = (GET "/foo.ydo")->to_psgi; |
| 89 | + |
| 90 | + it "should invoke action in foo.ydo", sub { |
| 91 | + expect($site->call($psgi))->to_be([200, $CT, ["action in foo.ydo"]]); |
| 92 | + }; |
| 93 | + }; |
| 94 | + |
| 95 | + # TODO: |
| 96 | + if ($TODO) { |
| 97 | + describe "request /foo", sub { |
| 98 | + my Env $psgi = (GET "/foo")->to_psgi; |
| 99 | + |
| 100 | + it "should invoke action in foo.ydo", sub { |
| 101 | + expect($site->call($psgi))->to_be([200, $CT, ["action in foo.ydo"]]); |
| 102 | + }; |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + if ($has_index) { |
| 107 | + describe "request /bar (for sanity check)", sub { |
| 108 | + my Env $psgi = (GET "/bar")->to_psgi; |
| 109 | + |
| 110 | + it "should invoke action bar in index.yatt", sub { |
| 111 | + expect($site->call($psgi))->to_be([200, $CT, ["action bar in index.yatt"]]); |
| 112 | + }; |
| 113 | + }; |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + describe "Action in .htyattrc.pl", sub { |
| 118 | + my ($app_root, $html_dir) = $make_dirs->(); |
| 119 | + |
| 120 | + make_path(my $tmpl_dir = "$app_root/ytmpl"); |
| 121 | + |
| 122 | + MY->mkfile("$html_dir/index.yatt", <<'END') if $has_index; |
| 123 | +<h2>Hello</h2> |
| 124 | +
|
| 125 | +<!yatt:page "/bar"> |
| 126 | +page bar in index.yatt |
| 127 | +END |
| 128 | + |
| 129 | + # .htyattrc.pl should be created BEFORE siteapp->new. |
| 130 | + MY->mkfile("$html_dir/.htyattrc.pl", <<'END'); |
| 131 | +use strict; |
| 132 | +
|
| 133 | +use YATT::Lite qw/Action/; |
| 134 | +
|
| 135 | +Action foo => sub { |
| 136 | + my ($this, $CON) = @_; |
| 137 | + print $CON "action foo in .htyattrc.pl"; |
| 138 | +}; |
| 139 | +
|
| 140 | +END |
| 141 | + |
| 142 | + my $site = $make_siteapp->($app_root, $html_dir, app_base => '@ytmpl'); |
| 143 | + |
| 144 | + if ($has_index or $TODO) { |
| 145 | + describe "request /?!foo=1", sub { |
| 146 | + my Env $psgi = (GET "/?!foo=1")->to_psgi; |
| 147 | + |
| 148 | + it "should invoke action foo in .htyattrc.pl", sub { |
| 149 | + expect($site->call($psgi))->to_be([200, $CT, ["action foo in .htyattrc.pl"]]); |
| 150 | + }; |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + if ($TODO) { |
| 155 | + describe "request /foo", sub { |
| 156 | + my Env $psgi = (GET "/foo")->to_psgi; |
| 157 | + |
| 158 | + it "should invoke action foo in .htyattrc.pl", sub { |
| 159 | + expect($site->call($psgi))->to_be([200, $CT, ["action foo in .htyattrc.pl"]]); |
| 160 | + }; |
| 161 | + }; |
| 162 | + } |
| 163 | + }; |
| 164 | + |
| 165 | + ($has_index or $TODO) |
| 166 | + and |
| 167 | + describe "site->mount_action(URL, subref)", sub { |
| 168 | + my ($app_root, $html_dir) = $make_dirs->(); |
| 169 | + |
| 170 | + make_path($html_dir); |
| 171 | + |
| 172 | + MY->mkfile("$html_dir/index.yatt", <<'END') if $has_index; |
| 173 | +<h2>Hello</h2> |
| 174 | +
|
| 175 | +<!yatt:page "/bar"> |
| 176 | +page bar in index.yatt |
| 177 | +END |
| 178 | + |
| 179 | + my $site = $make_siteapp->($app_root, $html_dir, app_base => '@ytmpl'); |
| 180 | + |
| 181 | + $site->mount_action( |
| 182 | + '/foo', |
| 183 | + sub { |
| 184 | + my ($this, $con) = @_; |
| 185 | + print $con "action foo from mount_action"; |
| 186 | + } |
| 187 | + ); |
| 188 | + |
| 189 | + if ($TODO) { |
| 190 | + describe "request /?!foo=1", sub { |
| 191 | + my Env $psgi = (GET "/?!foo=1")->to_psgi; |
| 192 | + |
| 193 | + it "should invoke action foo in .htyattrc.pl", sub { |
| 194 | + expect($site->call($psgi))->to_be([200, $CT, ["action foo from mount_action"]]); |
| 195 | + }; |
| 196 | + }; |
| 197 | + } |
| 198 | + |
| 199 | + describe "request /foo", sub { |
| 200 | + my Env $psgi = (GET "/foo")->to_psgi; |
| 201 | + |
| 202 | + it "should invoke action foo in .htyattrc.pl", sub { |
| 203 | + expect($site->call($psgi))->to_be([200, $CT, ["action foo from mount_action"]]); |
| 204 | + }; |
| 205 | + }; |
| 206 | + }; |
| 207 | + }; |
| 208 | +} |
| 209 | + |
| 210 | +#======================================== |
| 211 | +chdir($CWD); |
| 212 | + |
| 213 | +done_testing(); |
| 214 | + |
0 commit comments