File Coverage

blib/lib/App/RoboBot/Test/Mock.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package App::RoboBot::Test::Mock;
2             $App::RoboBot::Test::Mock::VERSION = '4.003';
3 4     4   194589 use strict;
  4         7  
  4         106  
4 4     4   18 use warnings;
  4         4  
  4         85  
5              
6 4     4   1423 use App::RoboBot;
  0            
  0            
7              
8             use App::RoboBot::Channel;
9             use App::RoboBot::Message;
10             use App::RoboBot::NetworkFactory;
11             use App::RoboBot::Nick;
12              
13             use Exporter::Easy (
14             'EXPORT' => [ 'mock_all' ],
15             'OK' => [ 'mock_bot', 'mock_channel', 'mock_message', 'mock_network' ],
16             );
17              
18             our %config = (
19             global => {
20             nick => 'robobot',
21             },
22             database => {
23             cache_connections => 0,
24             cache_statements => 0,
25             primary => {
26             driver => 'Pg',
27             database => ($ENV{'R_DBNAME'} // 'robobot_test'),
28             host => ($ENV{'R_DBHOST'} // 'localhost' ),
29             port => ($ENV{'R_DBPORT'} // '5432'),
30             user => ($ENV{'R_DBUSER'} // 'robobot'),
31             schemas => ["robobot","public"],
32             },
33             },
34             network => {
35             "test-net" => {
36             type => "irc",
37             enabled => 0,
38             host => "localhost",
39             port => "6667",
40             channel => ["test-channel"],
41             },
42             },
43             );
44              
45             sub envset_message {
46             my $msg = "R_DBTEST not set.";
47             my @vars = grep { ! exists $ENV{'R_DB'.$_} } qw( HOST PORT NAME USER );
48             $msg .= ' (Use R_DB{' . join(',', @vars) . '} to override connection info.)' if @vars > 0;
49             return $msg;
50             }
51              
52             sub mock_all {
53             my ($text) = @_;
54              
55             die "must provide message text to use for mock message" unless defined $text && $text =~ m{\w+};
56              
57             my $bot = mock_bot();
58             die "could not mock bot object" unless defined $bot && ref($bot) eq 'App::RoboBot';
59              
60             my $net = mock_network($bot);
61             die "could not mock network object" unless defined $net && ref($net) =~ m{^App::RoboBot::Network};
62              
63             my $chn = mock_channel($bot, $net);
64             die "could not mock channel object" unless defined $chn && ref($chn) eq 'App::RoboBot::Channel';
65              
66             my $msg = mock_message($bot, $chn, $text);
67             die "could not mock message object" unless defined $msg && ref($msg) eq 'App::RoboBot::Message';
68              
69             return ($bot, $net, $chn, $msg);
70             }
71              
72             sub mock_bot {
73             my $bot = App::RoboBot->new( raw_config => \%config );
74             my $cfg = App::RoboBot::Config->new( bot => $bot, config => \%config );
75              
76             die "could not mock bot" unless defined $bot;
77              
78             return $bot;
79             }
80              
81             sub mock_channel {
82             my ($bot, $network) = @_;
83              
84             die "must provide network for channel mockup"
85             unless defined $network && ref($network) =~ m{^App::RoboBot::Network};
86              
87             my $channel = App::RoboBot::Channel->new(
88             network => $network,
89             name => $config{'network'}{'test-net'}{'channel'}[0],
90             config => $bot->config,
91             );
92              
93             die "could not mock channel" unless defined $channel;
94              
95             return $channel;
96             }
97              
98             sub mock_message {
99             my ($bot, $channel, $text) = @_;
100              
101             my $sender = App::RoboBot::Nick->new(
102             name => 'mockuser',
103             config => $bot->config,
104             );
105              
106             my $message = App::RoboBot::Message->new(
107             bot => $bot,
108             network => $channel->network,
109             channel => $channel,
110             sender => $sender,
111             raw => $text,
112             );
113              
114             return $message;
115             }
116              
117             sub mock_network {
118             my ($bot) = @_;
119              
120             my $factory = App::RoboBot::NetworkFactory->new(
121             bot => $bot,
122             config => $bot->config,
123             nick => $config{'global'}{'nick'},
124             );
125              
126             my $network = $factory->create(
127             'test-net',
128             $config{'network'}{'test-net'},
129             );
130              
131             return $network;
132             }
133              
134             1;