File: 12protocol-hints.t

package info (click to toggle)
libprotocol-irc-perl 0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 2,122; makefile: 2
file content (232 lines) | stat: -rw-r--r-- 8,198 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/perl

use v5.14;
use warnings;

use Test::More;

my $CRLF = "\x0d\x0a"; # because \r\n isn't portable

my @messages;

package TestIRC {
   use base qw( Protocol::IRC );

   sub new { return bless [], shift }

   my %isupport = (
      CHANTYPES   => "#&",
         channame_re => qr/^[#&]/,
      PREFIX      => "(ohv)@%+",
         prefix_modes => 'ohv',
         prefix_flags => '@%+',
         prefixflag_re => qr/^[@%+]/,
         prefix_map_m2f => { 'o' => '@', 'h' => '%', 'v' => '+' },
         prefix_map_f2m => { '@' => 'o', '%' => 'h', '+' => 'v' },
   );

   sub isupport { return $isupport{$_[1]} }

   sub nick { return "MyNick" }

   sub on_message
   {
      my $self = shift;
      my ( $command, $message, $hints ) = @_;
      # Only care about real events, not synthesized ones
      return 0 if $hints->{synthesized};
      # Ignore numerics
      return 0 if $command =~ m/^\d\d\d$/;

      push @messages, [ $command, $message, $hints ];
      return $command ne "NOTICE";
   }
}

my $irc = TestIRC->new;
sub write_irc
{
   my $line = $_[0];
   $irc->on_read( $line );
   length $line == 0 or die '$irc failed to read all of the line';
}

# 001
{
   write_irc( ':irc.example.com 001 MyNick :Welcome to IRC MyNick!me@your.host' . $CRLF );

   my $m = shift @messages;

   ok( defined $m, '$m defined after server reply' );

   my ( $command, $msg, $hints ) = @$m;

   is( $command, "RPL_WELCOME", '$command' );

   isa_ok( $msg, "Protocol::IRC::Message", '$msg isa Protocol::IRC::Message' );

   is( $msg->command, "001",             '$msg->command for 001' );
   is( $msg->prefix,  "irc.example.com", '$msg->prefix for 001' );
   is_deeply( [ $msg->args ], [ "MyNick", "Welcome to IRC MyNick!me\@your.host" ], '$msg->args for 001' );

   is_deeply( $hints,
              { prefix_nick        => undef,
                prefix_nick_folded => undef,
                prefix_user        => undef,
                prefix_host        => "irc.example.com",
                prefix_name        => "irc.example.com",
                prefix_name_folded => "irc.example.com",
                text               => "Welcome to IRC MyNick!me\@your.host",
                handled            => 1 },
              '$hints for 001' );
}

# PRIVMSG
{
   write_irc( ':Someone!theiruser@their.host PRIVMSG MyNick :Their message here' . $CRLF );

   my ( $command, $msg, $hints ) = @{ shift @messages };

   is( $msg->command, "PRIVMSG",                      '$msg->command for PRIVMSG' );
   is( $msg->prefix,  'Someone!theiruser@their.host', '$msg->prefix for PRIVMSG' );

   is_deeply( $hints,
              { prefix_nick        => "Someone",
                prefix_nick_folded => "someone",
                prefix_user        => "theiruser",
                prefix_host        => "their.host",
                prefix_name        => "Someone",
                prefix_name_folded => "someone",
                prefix_is_me       => '',
                targets            => "MyNick",
                text               => "Their message here",
                handled            => 1 },
              '$hints for PRIVMSG' );

   write_irc( ':MyNick!me@your.host PRIVMSG MyNick :Hello to me' . $CRLF );

   ( $command, $msg, $hints ) = @{ shift @messages };

   is( $msg->command, "PRIVMSG",             '$msg->command for PRIVMSG to self' );
   is( $msg->prefix,  'MyNick!me@your.host', '$msg->prefix for PRIVMSG to self' );

   is_deeply( $hints,
              { prefix_nick        => "MyNick",
                prefix_nick_folded => "mynick",
                prefix_user        => "me",
                prefix_host        => "your.host",
                prefix_name        => "MyNick",
                prefix_name_folded => "mynick",
                prefix_is_me       => 1,
                targets            => "MyNick",
                text               => "Hello to me",
                handled            => 1 },
              '$hints for PRIVMSG to self' );
}

# TOPIC
{
   write_irc( ':Someone!theiruser@their.host TOPIC #channel :Message of the day' . $CRLF );

   my ( $command, $msg, $hints ) = @{ shift @messages };

   is( $msg->command, "TOPIC",                        '$msg->command for TOPIC' );
   is( $msg->prefix,  'Someone!theiruser@their.host', '$msg->prefix for TOPIC' );

   is_deeply( $hints,
              { prefix_nick        => "Someone",
                prefix_nick_folded => "someone",
                prefix_user        => "theiruser",
                prefix_host        => "their.host",
                prefix_name        => "Someone",
                prefix_name_folded => "someone",
                prefix_is_me       => '',
                target_name        => "#channel",
                target_name_folded => "#channel",
                target_is_me       => '',
                target_type        => "channel",
                text               => "Message of the day",
                handled            => 1 },
              '$hints for TOPIC' );
}

# NOTICE
{
   write_irc( ':Someone!theiruser@their.host NOTICE #channel :Please ignore me' . $CRLF );

   my ( $command, $msg, $hints ) = @{ shift @messages };

   is( $msg->command, "NOTICE",                       '$msg->command for NOTICE' );
   is( $msg->prefix,  'Someone!theiruser@their.host', '$msg->prefix for NOTICE' );

   is_deeply( $hints,
              { prefix_nick        => "Someone",
                prefix_nick_folded => "someone",
                prefix_user        => "theiruser",
                prefix_host        => "their.host",
                prefix_name        => "Someone",
                prefix_name_folded => "someone",
                prefix_is_me       => '',
                targets            => "#channel",
                text               => "Please ignore me",
                handled            => 0 },
              '$hints for NOTICE' );

   write_irc( ':Someone!theiruser@their.host NICK NewName' . $CRLF );

   ( $command, $msg, $hints ) = @{ shift @messages };

   is( $msg->command, "NICK",                         '$msg->command for NICK' );
   is( $msg->prefix,  'Someone!theiruser@their.host', '$msg->prefix for NICK' );

   is_deeply( $hints,
              { prefix_nick        => "Someone",
                prefix_nick_folded => "someone",
                prefix_user        => "theiruser",
                prefix_host        => "their.host",
                prefix_name        => "Someone",
                prefix_name_folded => "someone",
                prefix_is_me       => '',
                old_nick           => "Someone",
                old_nick_folded    => "someone",
                old_is_me          => '',
                new_nick           => "NewName",
                new_nick_folded    => "newname",
                new_is_me          => '',
                handled            => 1 },
              '$hints for NICK' );
}

# KICK
{
   write_irc( ':Someone!theiruser@their.host KICK #a-channel MyNick :Go away' . $CRLF );

   my ( $command, $msg, $hints ) = @{ shift @messages };

   is( $msg->command, "KICK",                         '$msg->command for KICK' );
   is( $msg->prefix,  'Someone!theiruser@their.host', '$msg->prefix for KICK' );

   is_deeply( $hints,
              { prefix_nick        => "Someone",
                prefix_nick_folded => "someone",
                prefix_user        => "theiruser",
                prefix_host        => "their.host",
                prefix_name        => "Someone",
                prefix_name_folded => "someone",
                prefix_is_me       => '',
                target_name        => "#a-channel",
                target_name_folded => "#a-channel",
                target_is_me       => '',
                target_type        => "channel",
                kicker_nick        => "Someone",
                kicker_nick_folded => "someone",
                kicker_is_me       => '',
                kicked_nick        => "MyNick",
                kicked_nick_folded => "mynick",
                kicked_is_me       => 1,
                text               => "Go away",
                handled            => 1 },
             '$hints for KICK' );
}

done_testing;