forked from tokuhirom/mobirc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.pm
113 lines (95 loc) · 2.58 KB
/
Utils.pm
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
package t::Utils;
use strict;
use warnings;
use lib 'extlib';
use HTTP::Engine;
use HTTP::Request;
use App::Mobirc;
use App::Mobirc::Web::Handler;
use App::Mobirc::Web::Template;
use HTTP::Session;
use HTTP::Session::Store::OnMemory;
use HTTP::Session::State::Null;
use App::Mobirc::Util;
use Cwd;
sub import {
my $pkg = caller(0);
my $class = shift;
strict->import;
warnings->import;
{
no strict 'refs';
for my $meth (qw/test_he test_he_filter create_global_context global_context server describe keyword_channel test_channel test_view/) {
*{"${pkg}::${meth}"} = *{"${class}::${meth}"};
}
}
}
sub create_global_context {
App::Mobirc->new(
config => {
httpd => { lines => 40 },
global => {
keywords => [qw/foo/], stopwords => [qw/foo31/],
assets_dir => File::Spec->catfile(Cwd::cwd(), 'assets'),
}
}
);
}
sub test_he {
my ($req, $cb) = @_;
HTTP::Engine->new(
interface => {
module => 'Test',
request_handler => sub {
my $req = shift;
local $App::Mobirc::Web::Handler::CONTEXT = App::Mobirc::Web::Context->new(
req => $req,
session => HTTP::Session->new(
store => HTTP::Session::Store::OnMemory->new(),
state => HTTP::Session::State::Null->new(),
request => $req,
),
);
$cb->($req),
}
}
)->run( $req );
}
sub test_he_filter(&) {
my $cb = shift;
global_context();
test_he( HTTP::Request->new('GET', '/', ['User-Agent' => 'MYPC']), sub {
my $req = shift;
$cb->($req);
return HTTP::Engine::Response->new( status => 200 );
});
}
sub server () {
global_context->server
}
{
no warnings 'redefine';
*App::Mobirc::Web::Template::Run::irc_nick = sub () { 'tokuhirom' };
*App::Mobirc::Model::Message::irc_nick = sub () { 'tokuhirom' };
*App::Mobirc::Util::irc_nick = sub () { 'tokuhirom' };
}
sub keyword_channel () { server->get_channel(U "*keyword*") }
sub test_channel () { server->get_channel(U '#test') }
sub describe ($&) {
my ($name, $code) = @_;
$code->();
keyword_channel->clear_unread();
}
sub test_view {
my ($path, @args) = @_;
my $res;
test_he_filter {
$res = global_context->mt->render_file(
$path,
@args,
)->as_string;
};
$res;
}
create_global_context();
1;